@@ -7,53 +7,37 @@ import (
77
88 appconfig "github.com/replicatedhq/embedded-cluster/api/internal/managers/app/config"
99 "github.com/replicatedhq/embedded-cluster/api/internal/statemachine"
10- "github.com/replicatedhq/embedded-cluster/api/internal/store "
10+ states "github.com/replicatedhq/embedded-cluster/api/internal/states/install "
1111 "github.com/replicatedhq/embedded-cluster/api/types"
12- kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
13- "github.com/replicatedhq/kotskinds/multitype"
1412 "github.com/stretchr/testify/assert"
1513 "github.com/stretchr/testify/mock"
1614 "github.com/stretchr/testify/require"
15+ "github.com/stretchr/testify/suite"
1716)
1817
19- func TestInstallController_PatchAppConfigValues (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 .FromString ("default" ),
33- Value : multitype .FromString ("value" ),
34- },
35- },
36- },
37- },
38- },
39- }
18+ type AppInstallControllerTestSuite struct {
19+ suite.Suite
20+ InstallType string
21+ CreateStateMachine func (initialState statemachine.State ) statemachine.Interface
22+ }
4023
24+ func (s * AppInstallControllerTestSuite ) TestPatchAppConfigValues () {
4125 tests := []struct {
4226 name string
4327 values types.AppConfigValues
4428 currentState statemachine.State
4529 expectedState statemachine.State
46- setupMocks func (* appconfig.MockAppConfigManager , * store. MockStore )
30+ setupMocks func (* appconfig.MockAppConfigManager )
4731 expectedErr bool
4832 }{
4933 {
5034 name : "successful set app config values" ,
5135 values : types.AppConfigValues {
5236 "test-item" : types.AppConfigValue {Value : "new-item" },
5337 },
54- currentState : StateNew ,
55- expectedState : StateApplicationConfigured ,
56- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
38+ currentState : states . StateNew ,
39+ expectedState : states . StateApplicationConfigured ,
40+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
5741 mock .InOrder (
5842 am .On ("ValidateConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
5943 am .On ("PatchConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
@@ -66,9 +50,9 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
6650 values : types.AppConfigValues {
6751 "test-item" : types.AppConfigValue {Value : "new-item" },
6852 },
69- currentState : StateApplicationConfigurationFailed ,
70- expectedState : StateApplicationConfigured ,
71- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
53+ currentState : states . StateApplicationConfigurationFailed ,
54+ expectedState : states . StateApplicationConfigured ,
55+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
7256 mock .InOrder (
7357 am .On ("ValidateConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
7458 am .On ("PatchConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
@@ -81,9 +65,9 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
8165 values : types.AppConfigValues {
8266 "test-item" : types.AppConfigValue {Value : "new-item" },
8367 },
84- currentState : StateApplicationConfigured ,
85- expectedState : StateApplicationConfigured ,
86- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
68+ currentState : states . StateApplicationConfigured ,
69+ expectedState : states . StateApplicationConfigured ,
70+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
8771 mock .InOrder (
8872 am .On ("ValidateConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
8973 am .On ("PatchConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
@@ -96,9 +80,9 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
9680 values : types.AppConfigValues {
9781 "test-item" : types.AppConfigValue {Value : "invalid-value" },
9882 },
99- currentState : StateNew ,
100- expectedState : StateApplicationConfigurationFailed ,
101- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
83+ currentState : states . StateNew ,
84+ expectedState : states . StateApplicationConfigurationFailed ,
85+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
10286 mock .InOrder (
10387 am .On ("ValidateConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "invalid-value" }}).Return (errors .New ("validation error" )),
10488 )
@@ -110,9 +94,9 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
11094 values : types.AppConfigValues {
11195 "test-item" : types.AppConfigValue {Value : "new-item" },
11296 },
113- currentState : StateNew ,
114- expectedState : StateApplicationConfigurationFailed ,
115- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
97+ currentState : states . StateNew ,
98+ expectedState : states . StateApplicationConfigurationFailed ,
99+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
116100 mock .InOrder (
117101 am .On ("ValidateConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (nil ),
118102 am .On ("PatchConfigValues" , types.AppConfigValues {"test-item" : types.AppConfigValue {Value : "new-item" }}).Return (errors .New ("set config error" )),
@@ -125,31 +109,26 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
125109 values : types.AppConfigValues {
126110 "test-item" : types.AppConfigValue {Value : "new-item" },
127111 },
128- currentState : StateInfrastructureInstalling ,
129- expectedState : StateInfrastructureInstalling ,
130- setupMocks : func (am * appconfig.MockAppConfigManager , st * store. MockStore ) {
112+ currentState : states . StateInfrastructureInstalling ,
113+ expectedState : states . StateInfrastructureInstalling ,
114+ setupMocks : func (am * appconfig.MockAppConfigManager ) {
131115 },
132116 expectedErr : true ,
133117 },
134118 }
135119
136120 for _ , tt := range tests {
137- t .Run (tt .name , func (t * testing.T ) {
138- sm := NewStateMachine (WithCurrentState (tt .currentState ))
139-
140- mockAppConfigManager := & appconfig.MockAppConfigManager {}
141- mockStore := & store.MockStore {}
142-
143- tt .setupMocks (mockAppConfigManager , mockStore )
121+ s .T ().Run (tt .name , func (t * testing.T ) {
144122
123+ manager := & appconfig.MockAppConfigManager {}
124+ sm := s .CreateStateMachine (tt .currentState )
145125 controller , err := NewInstallController (
146126 WithStateMachine (sm ),
147- WithAppConfigManager (mockAppConfigManager ),
148- WithReleaseData (getTestReleaseData (& appConfig )),
149- WithStore (mockStore ),
127+ WithAppConfigManager (manager ),
150128 )
151- require .NoError (t , err )
129+ require .NoError (t , err , "failed to create install controller" )
152130
131+ tt .setupMocks (manager )
153132 err = controller .PatchAppConfigValues (t .Context (), tt .values )
154133
155134 if tt .expectedErr {
@@ -162,12 +141,8 @@ func TestInstallController_PatchAppConfigValues(t *testing.T) {
162141 return sm .CurrentState () == tt .expectedState
163142 }, time .Second , 100 * time .Millisecond , "state should be %s but is %s" , tt .expectedState , sm .CurrentState ())
164143 assert .False (t , sm .IsLockAcquired (), "state machine should not be locked after setting app config values" )
144+ manager .AssertExpectations (s .T ())
165145
166- mockAppConfigManager .AssertExpectations (t )
167- mockStore .LinuxInfraMockStore .AssertExpectations (t )
168- mockStore .LinuxInstallationMockStore .AssertExpectations (t )
169- mockStore .LinuxPreflightMockStore .AssertExpectations (t )
170- mockStore .AppConfigMockStore .AssertExpectations (t )
171146 })
172147 }
173148}
0 commit comments