Skip to content

Commit 1289b88

Browse files
authored
Merge pull request #2030 from wind57/fix-1715-drop-profiles-support-add-targeted-tests
add tests for 1715
2 parents 9ed11aa + 30dea51 commit 1289b88

File tree

16 files changed

+681
-0
lines changed

16 files changed

+681
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.client.config.applications.test_for_1715;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByLabel;
23+
import org.springframework.cloud.kubernetes.client.config.applications.test_for_1715.properties.APropertySourceByName;
24+
import org.springframework.test.context.ActiveProfiles;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* @author wind57
30+
*/
31+
@ActiveProfiles({ "k8s" })
32+
abstract class AbstractTests {
33+
34+
@Autowired
35+
private APropertySourceByName aByName;
36+
37+
@Autowired
38+
private APropertySourceByLabel aByLabel;
39+
40+
/**
41+
* this one is simply read by name
42+
*/
43+
@Test
44+
void testAByName() {
45+
assertThat(aByName.aByName()).isEqualTo("aByName");
46+
}
47+
48+
/**
49+
* this one is read by name + profile
50+
*/
51+
@Test
52+
void testAByNameAndProfile() {
53+
assertThat(aByName.aByNameK8s()).isEqualTo("aByNameK8s");
54+
}
55+
56+
/**
57+
* this one is simply read by name
58+
*/
59+
@Test
60+
void testAByLabel() {
61+
assertThat(aByLabel.aByLabel()).isEqualTo("aByLabel");
62+
}
63+
64+
/**
65+
* This one is not read at all. This proves that includeProfileSpecificSources is not
66+
* relevant for labels based searches. Notice that we do read from: 'a-by-name-k8s',
67+
* but not from 'a-by-label-k8s'.
68+
*/
69+
@Test
70+
void testAByLabelAndProfile() {
71+
assertThat(aByLabel.aByLabelAndProfile()).isNull();
72+
}
73+
74+
}
Lines changed: 33 additions & 0 deletions
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.client.config.applications.test_for_1715;
18+
19+
import org.springframework.boot.test.context.SpringBootTest;
20+
21+
/**
22+
* Stubs for this test are in
23+
* {@link org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub}
24+
*
25+
* @author wind57
26+
*/
27+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
28+
properties = { "spring.cloud.bootstrap.name=fix-1715", "fix.1715.enabled=true",
29+
"spring.main.cloud-platform=KUBERNETES", "spring.cloud.bootstrap.enabled=true",
30+
"spring.cloud.kubernetes.client.namespace=spring-k8s" })
31+
public class BootstrapTests extends AbstractTests {
32+
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.client.config.applications.test_for_1715;
18+
19+
import com.github.tomakehurst.wiremock.WireMockServer;
20+
import com.github.tomakehurst.wiremock.client.WireMock;
21+
import io.kubernetes.client.util.ClientBuilder;
22+
import org.junit.jupiter.api.AfterAll;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.mockito.MockedStatic;
25+
import org.mockito.Mockito;
26+
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
29+
30+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
31+
import static org.mockito.Mockito.mockStatic;
32+
import static org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub.stubData;
33+
34+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Fix1715App.class,
35+
properties = { "spring.cloud.application.name=fix-1715", "fix.1715.enabled=true",
36+
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:,classpath:./fix-1715.yaml" })
37+
class ConfigDataTests extends AbstractTests {
38+
39+
private static MockedStatic<KubernetesClientUtils> clientUtilsMock;
40+
41+
@BeforeAll
42+
static void wireMock() {
43+
WireMockServer server = new WireMockServer(options().dynamicPort());
44+
server.start();
45+
WireMock.configureFor("localhost", server.port());
46+
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
47+
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
48+
.thenReturn(new ClientBuilder().setBasePath(server.baseUrl()).build());
49+
clientUtilsMock
50+
.when(() -> KubernetesClientUtils.getApplicationNamespace(Mockito.any(), Mockito.any(), Mockito.any()))
51+
.thenReturn("spring-k8s");
52+
stubData();
53+
}
54+
55+
@AfterAll
56+
static void teardown() {
57+
clientUtilsMock.close();
58+
}
59+
60+
}
Lines changed: 33 additions & 0 deletions
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.client.config.applications.test_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.client.config.applications.test_for_1715.properties.APropertySourceByLabel;
23+
import org.springframework.cloud.kubernetes.client.config.applications.test_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+
}
Lines changed: 27 additions & 0 deletions
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.client.config.applications.test_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+
}
Lines changed: 26 additions & 0 deletions
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.client.config.applications.test_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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.client.config.bootstrap.stubs;
18+
19+
import java.util.Map;
20+
21+
import com.github.tomakehurst.wiremock.WireMockServer;
22+
import com.github.tomakehurst.wiremock.client.WireMock;
23+
import io.kubernetes.client.openapi.ApiClient;
24+
import io.kubernetes.client.openapi.JSON;
25+
import io.kubernetes.client.openapi.models.V1ConfigMap;
26+
import io.kubernetes.client.openapi.models.V1ConfigMapBuilder;
27+
import io.kubernetes.client.openapi.models.V1ConfigMapList;
28+
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
29+
import io.kubernetes.client.util.ClientBuilder;
30+
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.core.annotation.Order;
35+
36+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
37+
38+
@Order(0)
39+
@Configuration
40+
@ConditionalOnProperty("fix.1715.enabled")
41+
public class FixFor1715ConfigurationStub {
42+
43+
@Bean
44+
public WireMockServer wireMock() {
45+
WireMockServer server = new WireMockServer(options().dynamicPort());
46+
server.start();
47+
WireMock.configureFor("localhost", server.port());
48+
return server;
49+
}
50+
51+
@Bean
52+
public ApiClient apiClient(WireMockServer wireMockServer) {
53+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
54+
io.kubernetes.client.openapi.Configuration.setDefaultApiClient(apiClient);
55+
apiClient.setDebugging(true);
56+
stubData();
57+
return apiClient;
58+
}
59+
60+
public static void stubData() {
61+
62+
V1ConfigMap aByName = new V1ConfigMapBuilder()
63+
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-name").withNamespace("spring-k8s").build())
64+
.addToData(Map.of("aByName", "aByName"))
65+
.build();
66+
67+
V1ConfigMap aByNameAndProfile = new V1ConfigMapBuilder()
68+
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-name-k8s").withNamespace("spring-k8s").build())
69+
.addToData(Map.of("aByNameK8s", "aByNameK8s"))
70+
.build();
71+
72+
V1ConfigMap aByLabel = new V1ConfigMapBuilder()
73+
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-label")
74+
.withNamespace("spring-k8s")
75+
.withLabels(Map.of("color", "blue"))
76+
.build())
77+
.addToData(Map.of("aByLabel", "aByLabel"))
78+
.build();
79+
80+
V1ConfigMap aByLabelAndProfile = new V1ConfigMapBuilder()
81+
.withMetadata(new V1ObjectMetaBuilder().withName("a-by-label-k8s")
82+
.withNamespace("spring-k8s")
83+
.withLabels(Map.of("color", "blue"))
84+
.build())
85+
.addToData(Map.of("aByLabelK8s", "aByLabelK8s"))
86+
.build();
87+
88+
// the actual stub for CoreV1Api calls
89+
V1ConfigMapList configMapList = new V1ConfigMapList();
90+
configMapList.addItemsItem(aByName);
91+
configMapList.addItemsItem(aByNameAndProfile);
92+
configMapList.addItemsItem(aByLabel);
93+
configMapList.addItemsItem(aByLabelAndProfile);
94+
95+
WireMock.stubFor(WireMock.get("/api/v1/namespaces/spring-k8s/configmaps")
96+
.willReturn(WireMock.aResponse().withStatus(200).withBody(JSON.serialize(configMapList))));
97+
}
98+
99+
}

spring-cloud-kubernetes-client-config/src/test/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.IncludeProfil
1212
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.ConfigMapNameAsPrefixConfigurationStub, \
1313
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.SourcesOrderConfigurationStub, \
1414
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.BootstrapKubernetesClientSanitizeEnvEndpointStub, \
15+
org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.FixFor1715ConfigurationStub, \
1516
org.springframework.cloud.kubernetes.client.config.EnableRetryBootstrapConfiguration
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
application:
3+
name: fix-1715
4+
cloud:
5+
kubernetes:
6+
config:
7+
namespace: spring-k8s
8+
sources:
9+
10+
- name: a-by-name
11+
useNameAsPrefix: true
12+
includeProfileSpecificSources: true
13+
14+
- labels:
15+
color: blue
16+
useNameAsPrefix: true
17+
includeProfileSpecificSources: true

0 commit comments

Comments
 (0)