Skip to content

Commit e7da6bb

Browse files
committed
f
1 parent 4f3d3fe commit e7da6bb

File tree

4 files changed

+670
-9
lines changed

4 files changed

+670
-9
lines changed
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
package install
2+
3+
import (
4+
"errors"
5+
"testing"
6+
"time"
7+
8+
appconfig "github.com/replicatedhq/embedded-cluster/api/internal/managers/app/config"
9+
"github.com/replicatedhq/embedded-cluster/api/internal/statemachine"
10+
"github.com/replicatedhq/embedded-cluster/api/internal/store"
11+
"github.com/replicatedhq/embedded-cluster/pkg/release"
12+
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
13+
"github.com/replicatedhq/kotskinds/multitype"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/mock"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestInstallController_SetAppConfigValues(t *testing.T) {
20+
// Create an app config for testing
21+
appConfig := kotsv1beta1.Config{
22+
Spec: kotsv1beta1.ConfigSpec{
23+
Groups: []kotsv1beta1.ConfigGroup{
24+
{
25+
Name: "test-group",
26+
Title: "Test Group",
27+
Items: []kotsv1beta1.ConfigItem{
28+
{
29+
Name: "test-item",
30+
Type: "text",
31+
Title: "Test Item",
32+
Default: multitype.BoolOrString{StrVal: "default"},
33+
Value: multitype.BoolOrString{StrVal: "value"},
34+
},
35+
},
36+
},
37+
},
38+
},
39+
}
40+
41+
tests := []struct {
42+
name string
43+
values map[string]string
44+
currentState statemachine.State
45+
expectedState statemachine.State
46+
setupMocks func(*appconfig.MockAppConfigManager, *store.MockStore)
47+
expectedErr bool
48+
}{
49+
{
50+
name: "successful set app config values",
51+
values: map[string]string{
52+
"test-item": "new-value",
53+
},
54+
currentState: StateNew,
55+
expectedState: StateApplicationConfigured,
56+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
57+
mock.InOrder(
58+
am.On("ValidateConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
59+
am.On("SetConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
60+
)
61+
},
62+
expectedErr: false,
63+
},
64+
{
65+
name: "successful set app config values from application configuration failed state",
66+
values: map[string]string{
67+
"test-item": "new-value",
68+
},
69+
currentState: StateApplicationConfigurationFailed,
70+
expectedState: StateApplicationConfigured,
71+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
72+
mock.InOrder(
73+
am.On("ValidateConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
74+
am.On("SetConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
75+
)
76+
},
77+
expectedErr: false,
78+
},
79+
{
80+
name: "successful set app config values from application configured state",
81+
values: map[string]string{
82+
"test-item": "new-value",
83+
},
84+
currentState: StateApplicationConfigured,
85+
expectedState: StateApplicationConfigured,
86+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
87+
mock.InOrder(
88+
am.On("ValidateConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
89+
am.On("SetConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
90+
)
91+
},
92+
expectedErr: false,
93+
},
94+
{
95+
name: "validation error",
96+
values: map[string]string{
97+
"test-item": "invalid-value",
98+
},
99+
currentState: StateNew,
100+
expectedState: StateApplicationConfigurationFailed,
101+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
102+
mock.InOrder(
103+
am.On("ValidateConfigValues", appConfig, map[string]string{"test-item": "invalid-value"}).Return(errors.New("validation error")),
104+
)
105+
},
106+
expectedErr: true,
107+
},
108+
{
109+
name: "set config values error",
110+
values: map[string]string{
111+
"test-item": "new-value",
112+
},
113+
currentState: StateNew,
114+
expectedState: StateApplicationConfigurationFailed,
115+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
116+
mock.InOrder(
117+
am.On("ValidateConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(nil),
118+
am.On("SetConfigValues", appConfig, map[string]string{"test-item": "new-value"}).Return(errors.New("set config error")),
119+
)
120+
},
121+
expectedErr: true,
122+
},
123+
{
124+
name: "invalid state transition",
125+
values: map[string]string{
126+
"test-item": "new-value",
127+
},
128+
currentState: StateInfrastructureInstalling,
129+
expectedState: StateInfrastructureInstalling,
130+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
131+
},
132+
expectedErr: true,
133+
},
134+
{
135+
name: "app config not found",
136+
values: map[string]string{
137+
"test-item": "new-value",
138+
},
139+
currentState: StateNew,
140+
expectedState: StateNew,
141+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
142+
},
143+
expectedErr: true,
144+
},
145+
}
146+
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
sm := NewStateMachine(WithCurrentState(tt.currentState))
150+
151+
mockAppConfigManager := &appconfig.MockAppConfigManager{}
152+
mockStore := &store.MockStore{}
153+
154+
tt.setupMocks(mockAppConfigManager, mockStore)
155+
156+
// For the "app config not found" test case, pass nil as AppConfig
157+
var releaseData *release.ReleaseData
158+
if tt.name == "app config not found" {
159+
releaseData = getTestReleaseData(nil)
160+
} else {
161+
releaseData = getTestReleaseData(&appConfig)
162+
}
163+
164+
controller, err := NewInstallController(
165+
WithStateMachine(sm),
166+
WithAppConfigManager(mockAppConfigManager),
167+
WithReleaseData(releaseData),
168+
WithStore(mockStore),
169+
)
170+
require.NoError(t, err)
171+
172+
err = controller.SetAppConfigValues(t.Context(), tt.values)
173+
174+
if tt.expectedErr {
175+
assert.Error(t, err)
176+
} else {
177+
assert.NoError(t, err)
178+
}
179+
180+
assert.Eventually(t, func() bool {
181+
return sm.CurrentState() == tt.expectedState
182+
}, time.Second, 100*time.Millisecond, "state should be %s but is %s", tt.expectedState, sm.CurrentState())
183+
assert.False(t, sm.IsLockAcquired(), "state machine should not be locked after setting app config values")
184+
185+
mockAppConfigManager.AssertExpectations(t)
186+
mockStore.LinuxInfraMockStore.AssertExpectations(t)
187+
mockStore.LinuxInstallationMockStore.AssertExpectations(t)
188+
mockStore.LinuxPreflightMockStore.AssertExpectations(t)
189+
mockStore.AppConfigMockStore.AssertExpectations(t)
190+
})
191+
}
192+
}
193+
194+
func TestInstallController_GetAppConfigValues(t *testing.T) {
195+
tests := []struct {
196+
name string
197+
setupMocks func(*appconfig.MockAppConfigManager, *store.MockStore)
198+
expectedValues map[string]string
199+
expectedErr bool
200+
}{
201+
{
202+
name: "successful get app config values",
203+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
204+
expectedValues := map[string]string{
205+
"test-item": "test-value",
206+
"another-item": "another-value",
207+
}
208+
am.On("GetConfigValues").Return(expectedValues, nil)
209+
},
210+
expectedValues: map[string]string{
211+
"test-item": "test-value",
212+
"another-item": "another-value",
213+
},
214+
expectedErr: false,
215+
},
216+
{
217+
name: "get config values error",
218+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
219+
am.On("GetConfigValues").Return(nil, errors.New("get config values error"))
220+
},
221+
expectedValues: nil,
222+
expectedErr: true,
223+
},
224+
{
225+
name: "empty config values",
226+
setupMocks: func(am *appconfig.MockAppConfigManager, st *store.MockStore) {
227+
am.On("GetConfigValues").Return(map[string]string{}, nil)
228+
},
229+
expectedValues: map[string]string{},
230+
expectedErr: false,
231+
},
232+
}
233+
234+
for _, tt := range tests {
235+
t.Run(tt.name, func(t *testing.T) {
236+
mockAppConfigManager := &appconfig.MockAppConfigManager{}
237+
mockStore := &store.MockStore{}
238+
239+
tt.setupMocks(mockAppConfigManager, mockStore)
240+
241+
controller, err := NewInstallController(
242+
WithAppConfigManager(mockAppConfigManager),
243+
WithStore(mockStore),
244+
)
245+
require.NoError(t, err)
246+
247+
values, err := controller.GetAppConfigValues(t.Context())
248+
249+
if tt.expectedErr {
250+
assert.Error(t, err)
251+
assert.Nil(t, values)
252+
} else {
253+
assert.NoError(t, err)
254+
assert.Equal(t, tt.expectedValues, values)
255+
}
256+
257+
mockAppConfigManager.AssertExpectations(t)
258+
mockStore.LinuxInfraMockStore.AssertExpectations(t)
259+
mockStore.LinuxInstallationMockStore.AssertExpectations(t)
260+
mockStore.LinuxPreflightMockStore.AssertExpectations(t)
261+
mockStore.AppConfigMockStore.AssertExpectations(t)
262+
})
263+
}
264+
}

0 commit comments

Comments
 (0)