Skip to content

Commit 30dea51

Browse files
committed
add tests
Signed-off-by: wind57 <[email protected]>
1 parent c0096ce commit 30dea51

File tree

11 files changed

+314
-5
lines changed

11 files changed

+314
-5
lines changed

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/test_for_1715/AbstractTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByLabel;
2323
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByName;
24+
import org.springframework.test.context.ActiveProfiles;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
2627

2728
/**
2829
* @author wind57
2930
*/
31+
@ActiveProfiles({ "k8s" })
3032
abstract class AbstractTests {
3133

3234
@Autowired

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/test_for_1715/BootstrapTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
package org.springframework.cloud.kubernetes.client.config.applications.test_for_1715;
1818

1919
import org.springframework.boot.test.context.SpringBootTest;
20-
import org.springframework.test.context.ActiveProfiles;
2120

2221
/**
2322
* Stubs for this test are in
2423
* {@link org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub}
2524
*
2625
* @author wind57
2726
*/
28-
@ActiveProfiles({ "k8s" })
2927
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
3028
properties = { "spring.cloud.bootstrap.name=fix-1715", "fix.1715.enabled=true",
3129
"spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true",

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/test_for_1715/ConfigDataTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626

2727
import org.springframework.boot.test.context.SpringBootTest;
2828
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
29-
import org.springframework.test.context.ActiveProfiles;
3029

3130
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
3231
import static org.mockito.Mockito.mockStatic;
3332
import static org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub.stubData;
3433

35-
@ActiveProfiles({ "k8s" })
3634
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
3735
properties = { "spring.cloud.application.name=fix-1715", "fix.1715.enabled=true",
3836
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:,classpath:./fix-1715.yaml" })

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/bootstrap/stubs/FixFor1715ConfigurationStub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void stubData() {
8282
.withNamespace("spring-k8s")
8383
.withLabels(Map.of("color", "blue"))
8484
.build())
85-
.addToData(Map.of("aByLabel", "aByLabel"))
85+
.addToData(Map.of("aByLabelK8s", "aByLabelK8s"))
8686
.build();
8787

8888
// the actual stub for CoreV1Api calls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715;
18+
19+
import java.util.Map;
20+
21+
import io.fabric8.kubernetes.api.model.ConfigMap;
22+
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
23+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
24+
import io.fabric8.kubernetes.client.Config;
25+
import io.fabric8.kubernetes.client.KubernetesClient;
26+
import org.junit.jupiter.api.Test;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties.APropertySourceByLabel;
31+
import org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties.APropertySourceByName;
32+
import org.springframework.test.context.ActiveProfiles;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* @author wind57
38+
*/
39+
@ActiveProfiles({ "k8s" })
40+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
41+
properties = { "spring.application.name=fix-1715", "spring.cloud.kubernetes.reload.enabled=false",
42+
"spring.main.cloud-platform=KUBERNETES" })
43+
abstract class AbstractTests {
44+
45+
private static KubernetesClient mockClient;
46+
47+
@Autowired
48+
private APropertySourceByName aByName;
49+
50+
@Autowired
51+
private APropertySourceByLabel aByLabel;
52+
53+
/**
54+
* this one is simply read by name
55+
*/
56+
@Test
57+
void testAByName() {
58+
assertThat(aByName.aByName()).isEqualTo("aByName");
59+
}
60+
61+
/**
62+
* this one is read by name + profile
63+
*/
64+
@Test
65+
void testAByNameAndProfile() {
66+
assertThat(aByName.aByNameK8s()).isEqualTo("aByNameK8s");
67+
}
68+
69+
/**
70+
* this one is simply read by name
71+
*/
72+
@Test
73+
void testAByLabel() {
74+
assertThat(aByLabel.aByLabel()).isEqualTo("aByLabel");
75+
}
76+
77+
/**
78+
* This one is not read at all. This proves that includeProfileSpecificSources is not
79+
* relevant for labels based searches. Notice that we do read from: 'a-by-name-k8s',
80+
* but not from 'a-by-label-k8s'.
81+
*/
82+
@Test
83+
void testAByLabelAndProfile() {
84+
assertThat(aByLabel.aByLabelAndProfile()).isNull();
85+
}
86+
87+
static void setUpBeforeClass(KubernetesClient mockClient) {
88+
89+
// Configure the kubernetes master url to point to the mock server
90+
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
91+
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
92+
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
93+
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
94+
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "spring-k8s");
95+
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
96+
97+
ConfigMap aByName = new ConfigMapBuilder()
98+
.withMetadata(new ObjectMetaBuilder().withName("a-by-name").withNamespace("spring-k8s").build())
99+
.addToData(Map.of("aByName", "aByName"))
100+
.build();
101+
createConfigmap(aByName);
102+
103+
ConfigMap aByNameAndProfile = new ConfigMapBuilder()
104+
.withMetadata(new ObjectMetaBuilder().withName("a-by-name-k8s").withNamespace("spring-k8s").build())
105+
.addToData(Map.of("aByNameK8s", "aByNameK8s"))
106+
.build();
107+
createConfigmap(aByNameAndProfile);
108+
109+
ConfigMap aByLabel = new ConfigMapBuilder()
110+
.withMetadata(new ObjectMetaBuilder().withName("a-by-label")
111+
.withLabels(Map.of("color", "blue"))
112+
.withNamespace("spring-k8s")
113+
.build())
114+
.addToData(Map.of("aByLabel", "aByLabel"))
115+
.build();
116+
createConfigmap(aByLabel);
117+
118+
ConfigMap aByLabelAndProfile = new ConfigMapBuilder()
119+
.withMetadata(new ObjectMetaBuilder().withName("a-by-label-k8s")
120+
.withLabels(Map.of("color", "blue"))
121+
.withNamespace("spring-k8s")
122+
.build())
123+
.addToData(Map.of("aByLabel", "aByLabel"))
124+
.build();
125+
createConfigmap(aByLabelAndProfile);
126+
127+
}
128+
129+
static void createConfigmap(ConfigMap configMap) {
130+
mockClient.configMaps().resource(configMap).create();
131+
}
132+
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715;
18+
19+
import io.fabric8.kubernetes.client.KubernetesClient;
20+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
21+
import org.junit.jupiter.api.BeforeAll;
22+
23+
import org.springframework.test.context.TestPropertySource;
24+
25+
/**
26+
* @author wind57
27+
*/
28+
@TestPropertySource(properties = { "spring.cloud.bootstrap.enabled=true", "spring.cloud.bootstrap.name=fix-1715" })
29+
@EnableKubernetesMockClient(crud = true, https = false)
30+
public class BootstrapTests extends AbstractTests {
31+
32+
private static KubernetesClient mockClient;
33+
34+
@BeforeAll
35+
static void setUpBeforeClass() {
36+
setUpBeforeClass(mockClient);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715;
18+
19+
import io.fabric8.kubernetes.client.KubernetesClient;
20+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
21+
import org.junit.jupiter.api.BeforeAll;
22+
23+
import org.springframework.test.context.TestPropertySource;
24+
25+
@TestPropertySource(properties = "spring.config.import=kubernetes:,classpath:./fix-1715.yaml")
26+
@EnableKubernetesMockClient(crud = true, https = false)
27+
class ConfigDataTests extends AbstractTests {
28+
29+
private static KubernetesClient mockClient;
30+
31+
@BeforeAll
32+
static void setUpBeforeClass() {
33+
setUpBeforeClass(mockClient);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
22+
import org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties.APropertySourceByLabel;
23+
import org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties.APropertySourceByName;
24+
25+
@SpringBootApplication
26+
@EnableConfigurationProperties({ APropertySourceByName.class, APropertySourceByLabel.class })
27+
public class Fix1715App {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(Fix1715App.class, args);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* @author wind57
23+
*/
24+
@ConfigurationProperties("a-by-label")
25+
public record APropertySourceByLabel(String aByLabel, String aByLabelAndProfile) {
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.fix_for_1715.properties;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* @author wind57
23+
*/
24+
@ConfigurationProperties("a-by-name")
25+
public record APropertySourceByName(String aByName, String aByNameK8s) {
26+
}

0 commit comments

Comments
 (0)