27
27
import io .kubernetes .client .openapi .models .V1Secret ;
28
28
import io .kubernetes .client .openapi .models .V1SecretBuilder ;
29
29
import io .kubernetes .client .openapi .models .V1SecretList ;
30
- import io .kubernetes .client .openapi .models .V1SecretListBuilder ;
30
+ import org .junit .jupiter .api .AfterAll ;
31
+ import org .junit .jupiter .api .AfterEach ;
31
32
import org .junit .jupiter .api .BeforeAll ;
32
33
import org .junit .jupiter .api .Test ;
34
+ import org .mockito .Mockito ;
33
35
34
36
import org .springframework .cloud .config .environment .Environment ;
37
+ import org .springframework .cloud .kubernetes .client .config .KubernetesClientConfigMapsCache ;
38
+ import org .springframework .cloud .kubernetes .client .config .KubernetesClientSecretsCache ;
35
39
import org .springframework .cloud .kubernetes .commons .config .Constants ;
36
40
37
41
import static org .assertj .core .api .Assertions .assertThat ;
38
- import static org .mockito .ArgumentMatchers .eq ;
39
42
import static org .mockito .Mockito .mock ;
40
43
import static org .mockito .Mockito .when ;
41
44
47
50
*/
48
51
class KubernetesPropertySourceSupplierTests {
49
52
50
- private static final CoreV1Api coreApi = mock (CoreV1Api .class );
53
+ private static final CoreV1Api CORE_V1_API = mock (CoreV1Api .class );
51
54
52
55
private static final V1ConfigMapList CONFIGMAP_DEFAULT_LIST = new V1ConfigMapList ()
53
56
.addItemsItem (buildConfigMap ("gateway" , "default" ));
@@ -58,41 +61,51 @@ class KubernetesPropertySourceSupplierTests {
58
61
private static final V1ConfigMapList CONFIGMAP_TEAM_B_LIST = new V1ConfigMapList ()
59
62
.addItemsItem (buildConfigMap ("orders" , "team-b" ));
60
63
61
- private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretListBuilder ()
62
- .addToItems (buildSecret ("gateway" , "default" ))
63
- .build ();
64
+ private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretList ()
65
+ .addItemsItem (buildSecret ("gateway" , "default" ));
64
66
65
- private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretListBuilder ()
66
- .addToItems (buildSecret ("stores" , "team-a" ))
67
- .build ();
67
+ private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretList ()
68
+ .addItemsItem (buildSecret ("stores" , "team-a" ));
68
69
69
- private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretListBuilder ()
70
- .addToItems (buildSecret ("orders" , "team-b" ))
71
- .build ();
70
+ private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretList ()
71
+ .addItemsItem (buildSecret ("orders" , "team-b" ));
72
+
73
+ private static final KubernetesConfigServerProperties PROPERTIES = properties ();
72
74
73
75
@ BeforeAll
74
- public static void before () throws ApiException {
75
- when (coreApi .listNamespacedConfigMap (eq ( "default" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
76
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
76
+ static void beforeAll () throws ApiException {
77
+ when (CORE_V1_API .listNamespacedConfigMap ("default" , null , null , null , null , null , null , null , null , null , null ,
78
+ null ))
77
79
.thenReturn (CONFIGMAP_DEFAULT_LIST );
78
- when (coreApi .listNamespacedConfigMap (eq ( "team-a" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
79
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
80
+ when (CORE_V1_API .listNamespacedConfigMap ("team-a" , null , null , null , null , null , null , null , null , null , null ,
81
+ null ))
80
82
.thenReturn (CONFIGMAP_TEAM_A_LIST );
81
- when (coreApi .listNamespacedConfigMap (eq ( "team-b" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
82
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
83
+ when (CORE_V1_API .listNamespacedConfigMap ("team-b" , null , null , null , null , null , null , null , null , null , null ,
84
+ null ))
83
85
.thenReturn (CONFIGMAP_TEAM_B_LIST );
84
86
85
- when (coreApi .listNamespacedSecret (eq ( "default" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
86
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
87
+ when (CORE_V1_API .listNamespacedSecret ("default" , null , null , null , null , null , null , null , null , null , null ,
88
+ null ))
87
89
.thenReturn (SECRET_DEFAULT_LIST );
88
- when (coreApi .listNamespacedSecret (eq ( "team-a" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
89
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
90
+ when (CORE_V1_API .listNamespacedSecret ("team-a" , null , null , null , null , null , null , null , null , null , null ,
91
+ null ))
90
92
.thenReturn (SECRET_TEAM_A_LIST );
91
- when (coreApi .listNamespacedSecret (eq ( "team-b" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
92
- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
93
+ when (CORE_V1_API .listNamespacedSecret ("team-b" , null , null , null , null , null , null , null , null , null , null ,
94
+ null ))
93
95
.thenReturn (SECRET_TEAM_B_LIST );
94
96
}
95
97
98
+ @ AfterAll
99
+ static void afterAll () {
100
+ Mockito .reset (CORE_V1_API );
101
+ }
102
+
103
+ @ AfterEach
104
+ void afterEach () {
105
+ new KubernetesClientConfigMapsCache ().discardAll ();
106
+ new KubernetesClientSecretsCache ().discardAll ();
107
+ }
108
+
96
109
@ Test
97
110
void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded () {
98
111
KubernetesConfigServerProperties kubernetesConfigServerProperties = new KubernetesConfigServerProperties ();
@@ -101,8 +114,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
101
114
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
102
115
.configMapPropertySourceSupplier (kubernetesConfigServerProperties );
103
116
104
- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
105
- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
117
+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
118
+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
106
119
107
120
Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
108
121
assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (1 );
@@ -122,8 +135,8 @@ void whenExtraNamespacesAddedThenConfigMapsInCurrentNamespaceAreNotIncluded() {
122
135
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
123
136
.configMapPropertySourceSupplier (kubernetesConfigServerProperties );
124
137
125
- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
126
- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
138
+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
139
+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
127
140
128
141
Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
129
142
assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (0 );
@@ -143,8 +156,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllSecretsAreIncluded() {
143
156
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
144
157
.secretsPropertySourceSupplier (kubernetesConfigServerProperties );
145
158
146
- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
147
- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
159
+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
160
+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
148
161
149
162
Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
150
163
assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (1 );
@@ -164,8 +177,8 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
164
177
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
165
178
.secretsPropertySourceSupplier (kubernetesConfigServerProperties );
166
179
167
- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
168
- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
180
+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
181
+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
169
182
170
183
Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
171
184
assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (0 );
@@ -179,19 +192,25 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
179
192
180
193
private static V1ConfigMap buildConfigMap (String name , String namespace ) {
181
194
return new V1ConfigMapBuilder ()
182
- .withMetadata (
183
- new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).withResourceVersion ("1" ).build ())
184
- .addToData (Constants .APPLICATION_YAML , "dummy:\n property:\n string: \" " + name + "\" \n " )
195
+ .withMetadata (new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).build ())
196
+ .addToData (Constants .APPLICATION_PROPERTIES , """
197
+ dummy.property.string=%s
198
+ """ .formatted (name ))
185
199
.build ();
186
200
}
187
201
188
202
private static V1Secret buildSecret (String name , String namespace ) {
189
203
return new V1SecretBuilder ()
190
- .withMetadata (
191
- new V1ObjectMetaBuilder ().withName (name ).withResourceVersion ("0" ).withNamespace (namespace ).build ())
204
+ .withMetadata (new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).build ())
192
205
.addToData ("password" , "p455w0rd" .getBytes ())
193
206
.addToData ("username" , "user" .getBytes ())
194
207
.build ();
195
208
}
196
209
210
+ private static KubernetesConfigServerProperties properties () {
211
+ KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties ();
212
+ properties .setOrder (1 );
213
+ return properties ;
214
+ }
215
+
197
216
}
0 commit comments