|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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.client.discovery.it; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | + |
| 22 | +import io.fabric8.kubernetes.api.model.Service; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.fabric8.kubernetes.client.utils.Serialization; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Nested; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.test.context.SpringBootTest; |
| 32 | +import org.springframework.boot.test.context.TestConfiguration; |
| 33 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 34 | +import org.springframework.cloud.kubernetes.fabric8.client.discovery.Fabric8DiscoveryApp; |
| 35 | +import org.springframework.cloud.kubernetes.integration.tests.commons.Images; |
| 36 | +import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.Primary; |
| 39 | +import org.springframework.test.context.TestPropertySource; |
| 40 | + |
| 41 | +import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.assertAllServices; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author wind57 |
| 45 | + */ |
| 46 | +class Fabric8DiscoveryAllServicesIT extends Fabric8DiscoveryBase { |
| 47 | + |
| 48 | + @Nested |
| 49 | + @TestPropertySource(properties = { "spring.cloud.kubernetes.discovery.include-external-name-services=true" }) |
| 50 | + class NonBootstrap { |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private DiscoveryClient discoveryClient; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void beforeEach() { |
| 57 | + Images.loadBusybox(K3S); |
| 58 | + util.busybox(NAMESPACE, Phase.CREATE); |
| 59 | + externalNameServices(Phase.CREATE); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterEach |
| 63 | + void afterEach() { |
| 64 | + util.busybox(NAMESPACE, Phase.DELETE); |
| 65 | + externalNameServices(Phase.DELETE); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * <pre> |
| 70 | + * - there are 3 services : 'busybox-service', 'kubernetes', 'external-name-service' |
| 71 | + * - all of them are found |
| 72 | + * </pre> |
| 73 | + */ |
| 74 | + @Test |
| 75 | + void test() { |
| 76 | + assertAllServices(discoveryClient); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Nested |
| 81 | + @TestPropertySource(properties = { "spring.cloud.kubernetes.discovery.include-external-name-services=true", |
| 82 | + "spring.cloud.bootstrap.enabled=true"}) |
| 83 | + class Bootstrap { |
| 84 | + |
| 85 | + @Autowired |
| 86 | + private DiscoveryClient discoveryClient; |
| 87 | + |
| 88 | + @BeforeEach |
| 89 | + void beforeEach() { |
| 90 | + Images.loadBusybox(K3S); |
| 91 | + util.busybox(NAMESPACE, Phase.CREATE); |
| 92 | + externalNameServices(Phase.CREATE); |
| 93 | + } |
| 94 | + |
| 95 | + @AfterEach |
| 96 | + void afterEach() { |
| 97 | + util.busybox(NAMESPACE, Phase.DELETE); |
| 98 | + externalNameServices(Phase.DELETE); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * <pre> |
| 103 | + * - there are 3 services : 'busybox-service', 'kubernetes', 'external-name-service' |
| 104 | + * - all of them are found |
| 105 | + * </pre> |
| 106 | + */ |
| 107 | + @Test |
| 108 | + void test() { |
| 109 | + assertAllServices(discoveryClient); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void externalNameServices(Phase phase) { |
| 114 | + try (InputStream externalNameServiceStream = util.inputStream("external-name-service.yaml")) { |
| 115 | + Service externalServiceName = Serialization.unmarshal(externalNameServiceStream, Service.class); |
| 116 | + if (phase == Phase.CREATE) { |
| 117 | + util.createAndWait(NAMESPACE, null, null, externalServiceName, null, true); |
| 118 | + } |
| 119 | + else { |
| 120 | + util.deleteAndWait(NAMESPACE, null, externalServiceName, null); |
| 121 | + } |
| 122 | + } |
| 123 | + catch (IOException e) { |
| 124 | + throw new RuntimeException(e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments