|
1 | 1 | package orchestrator |
| 2 | + |
| 3 | +import ( |
| 4 | + "astron-xmod-shim/internal/config" |
| 5 | + "astron-xmod-shim/internal/core/goal" |
| 6 | + "astron-xmod-shim/internal/core/shimlet" |
| 7 | + "astron-xmod-shim/internal/core/spec" |
| 8 | + "astron-xmod-shim/internal/core/typereg" |
| 9 | + "astron-xmod-shim/internal/core/workqueue" |
| 10 | + dto "astron-xmod-shim/internal/dto/deploy" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/mock" |
| 16 | +) |
| 17 | + |
| 18 | +// MockShimlet 是shimlet.Shimlet接口的简化mock实现 |
| 19 | +type MockShimlet struct { |
| 20 | + mock.Mock |
| 21 | +} |
| 22 | + |
| 23 | +func (m *MockShimlet) ID() string { |
| 24 | + args := m.Called() |
| 25 | + return args.String(0) |
| 26 | +} |
| 27 | + |
| 28 | +func (m *MockShimlet) Description() string { |
| 29 | + args := m.Called() |
| 30 | + return args.String(0) |
| 31 | +} |
| 32 | + |
| 33 | +func (m *MockShimlet) InitWithConfig(confPath string) error { |
| 34 | + args := m.Called(confPath) |
| 35 | + return args.Error(0) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *MockShimlet) Apply(spec *dto.RequirementSpec) error { |
| 39 | + args := m.Called(spec) |
| 40 | + return args.Error(0) |
| 41 | +} |
| 42 | + |
| 43 | +func (m *MockShimlet) Delete(resourceId string) error { |
| 44 | + args := m.Called(resourceId) |
| 45 | + return args.Error(0) |
| 46 | +} |
| 47 | + |
| 48 | +func (m *MockShimlet) Status(resourceId string) (*dto.RuntimeStatus, error) { |
| 49 | + args := m.Called(resourceId) |
| 50 | + status, _ := args.Get(0).(*dto.RuntimeStatus) |
| 51 | + return status, args.Error(1) |
| 52 | +} |
| 53 | + |
| 54 | +func (m *MockShimlet) ListDeployedServices() ([]string, error) { |
| 55 | + args := m.Called() |
| 56 | + services, _ := args.Get(0).([]string) |
| 57 | + return services, args.Error(1) |
| 58 | +} |
| 59 | + |
| 60 | +// 简化的测试用例 |
| 61 | +func TestOrchestrator(t *testing.T) { |
| 62 | + // 创建临时配置文件 |
| 63 | + configContent := ` |
| 64 | +current-shimlet: "k8s" |
| 65 | +shimlets: |
| 66 | + k8s: |
| 67 | + config-path: "/conf/k8s-shimlet.yaml" |
| 68 | +` |
| 69 | + tmpFile, err := os.CreateTemp("", "test-config-*.yaml") |
| 70 | + assert.NoError(t, err) |
| 71 | + defer os.Remove(tmpFile.Name()) |
| 72 | + |
| 73 | + _, err = tmpFile.WriteString(configContent) |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.NoError(t, tmpFile.Close()) |
| 76 | + |
| 77 | + // 设置配置文件路径 |
| 78 | + config.SetConfigPath(tmpFile.Name()) |
| 79 | + |
| 80 | + // 测试Provision方法 |
| 81 | + t.Run("Provision", func(t *testing.T) { |
| 82 | + // 创建测试需要的组件 |
| 83 | + mockQueue := workqueue.New() |
| 84 | + defer mockQueue.ShutDown() |
| 85 | + |
| 86 | + mockSpecStore := spec.NewMemoryStore() |
| 87 | + shimReg := typereg.New[shimlet.Shimlet]() |
| 88 | + |
| 89 | + // 创建并配置mock shimlet |
| 90 | + mockShim := new(MockShimlet) |
| 91 | + mockShim.On("ID").Return("k8s") |
| 92 | + mockShim.On("InitWithConfig", "/conf/k8s-shimlet.yaml").Return(nil) |
| 93 | + |
| 94 | + // 注册mock shimlet到注册中心 |
| 95 | + shimReg.AutoRegister(mockShim) |
| 96 | + |
| 97 | + // 创建orchestrator实例,使用固定的shimlet ID "k8s" |
| 98 | + orchestrator := NewOrchestratorWithShimletGetter( |
| 99 | + shimReg, |
| 100 | + map[string]*goal.GoalSet{}, |
| 101 | + mockQueue, |
| 102 | + mockSpecStore, |
| 103 | + func() string { |
| 104 | + return "k8s" |
| 105 | + }, |
| 106 | + ) |
| 107 | + |
| 108 | + serviceSpec := &dto.RequirementSpec{ |
| 109 | + ServiceId: "test-service", |
| 110 | + GoalSetName: "test-goalset", |
| 111 | + ResourceRequirements: &dto.ResourceRequirements{}, |
| 112 | + } |
| 113 | + |
| 114 | + err := orchestrator.Provision(serviceSpec) |
| 115 | + assert.NoError(t, err) |
| 116 | + |
| 117 | + // 验证规格被保存 |
| 118 | + savedSpec := mockSpecStore.Get("test-service") |
| 119 | + assert.NotNil(t, savedSpec) |
| 120 | + |
| 121 | + // 验证项目被添加到队列 |
| 122 | + assert.Equal(t, 1, mockQueue.Len()) |
| 123 | + |
| 124 | + // 清理队列 |
| 125 | + if mockQueue.Len() > 0 { |
| 126 | + _, done := mockQueue.Get() |
| 127 | + done() |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | +} |
0 commit comments