Load images into container before running tests

This commit is contained in:
Ryan Baxter
2022-06-09 20:24:36 -04:00
parent 33652e01b4
commit e822e22393
15 changed files with 86 additions and 41 deletions

View File

@@ -81,7 +81,7 @@ class ConfigMapAndSecretIT {
static void setup() throws Exception {
K3S.start();
Commons.validateImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
Commons.loadImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
appsApi = new AppsV1Api();

View File

@@ -76,7 +76,7 @@ class LoadBalancerIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
appsApi = new AppsV1Api();
@@ -93,7 +93,7 @@ class LoadBalancerIT {
@BeforeEach
void setup() throws Exception {
k8SUtils.deployWiremock(NAMESPACE, false);
k8SUtils.deployWiremock(NAMESPACE, false, K3S);
}
@AfterEach

View File

@@ -80,7 +80,7 @@ class ReactiveDiscoveryClientIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(SPRING_CLOUD_K8S_REACTIVE_DISCOVERY_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_REACTIVE_DISCOVERY_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_REACTIVE_DISCOVERY_APP_NAME, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
appsApi = new AppsV1Api();
@@ -97,7 +97,7 @@ class ReactiveDiscoveryClientIT {
@BeforeEach
void setup() throws Exception {
k8SUtils.deployWiremock(NAMESPACE, false);
k8SUtils.deployWiremock(NAMESPACE, false, K3S);
}
@AfterEach

View File

@@ -78,7 +78,8 @@ class ActuatorRefreshIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
System.out.println(K3S.getKubeConfigYaml());
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
appsApi = new AppsV1Api();
@@ -95,7 +96,7 @@ class ActuatorRefreshIT {
@BeforeEach
void setup() throws Exception {
deployConfigWatcher();
k8SUtils.deployWiremock(NAMESPACE, true);
k8SUtils.deployWiremock(NAMESPACE, true, K3S);
}
@AfterEach
@@ -152,8 +153,8 @@ class ActuatorRefreshIT {
private V1Deployment getConfigWatcherDeployment() throws Exception {
V1Deployment deployment = (V1Deployment) K8SUtils.readYamlFromClasspath(
"config-watcher/spring-cloud-kubernetes-configuration-watcher-http-deployment.yaml");
String image = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage() + ":"
+ getPomVersion();
String image = K8SUtils.getImageFromDeployment(deployment) + ":"
+ getPomVersion();
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(image);
return deployment;
}

View File

@@ -72,6 +72,8 @@ class ActuatorRefreshKafkaIT {
private static final String ZOOKEEPER_DEPLOYMENT = "zookeeper";
private static final String KAFKA_IMAGE = "wurstmeister/kafka";
private static CoreV1Api api;
private static AppsV1Api appsApi;
@@ -87,10 +89,10 @@ class ActuatorRefreshKafkaIT {
K3S.start();
Commons.validateImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.validateImage(CONFIG_WATCHER_IT_IMAGE, K3S);
Commons.loadImage(CONFIG_WATCHER_IT_IMAGE, K3S);
Commons.loadSpringCloudKubernetesImage(CONFIG_WATCHER_IT_IMAGE, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
@@ -183,11 +185,19 @@ class ActuatorRefreshKafkaIT {
private void deployZookeeper() throws Exception {
api.createNamespacedService(NAMESPACE, getZookeeperService(), null, null, null);
appsApi.createNamespacedDeployment(NAMESPACE, getZookeeperDeployment(), null, null, null);
V1Deployment deployment = getZookeeperDeployment();
String[] image = K8SUtils.getImageFromDeployment(deployment).split(":");
Commons.pullImage(image[0], image[1], K3S);
Commons.loadImage(image[0], image[1], "zookeeper", K3S);
appsApi.createNamespacedDeployment(NAMESPACE, deployment, null, null, null);
}
private void deployKafka() throws Exception {
api.createNamespacedService(NAMESPACE, getKafkaService(), null, null, null);
V1Deployment deployment = getKafkaDeployment();
String[] image = K8SUtils.getImageFromDeployment(deployment).split(":");
Commons.pullImage(image[0], image[1], K3S);
Commons.loadImage(image[0], image[1], "kafka", K3S);
appsApi.createNamespacedDeployment(NAMESPACE, getKafkaDeployment(), null, null, null);
}
@@ -204,8 +214,7 @@ class ActuatorRefreshKafkaIT {
private V1Deployment getConfigWatcherDeployment() throws Exception {
V1Deployment deployment = (V1Deployment) K8SUtils.readYamlFromClasspath(
"app-watcher/spring-cloud-kubernetes-configuration-watcher-bus-kafka-deployment.yaml");
String image = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage() + ":"
+ getPomVersion();
String image = K8SUtils.getImageFromDeployment(deployment) + ":" + getPomVersion();
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(image);
return deployment;
}
@@ -213,8 +222,7 @@ class ActuatorRefreshKafkaIT {
private V1Deployment getItDeployment() throws Exception {
String urlString = "app/spring-cloud-kubernetes-configuration-watcher-it-bus-kafka-deployment.yaml";
V1Deployment deployment = (V1Deployment) K8SUtils.readYamlFromClasspath(urlString);
String image = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage() + ":"
+ getPomVersion();
String image = K8SUtils.getImageFromDeployment(deployment) + ":" + getPomVersion();
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(image);
return deployment;
}

View File

@@ -82,10 +82,10 @@ class ActuatorRefreshRabbitMQIT {
K3S.start();
Commons.validateImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_CONFIG_WATCHER_APP_NAME, K3S);
Commons.validateImage(CONFIG_WATCHER_IT_IMAGE, K3S);
Commons.loadImage(CONFIG_WATCHER_IT_IMAGE, K3S);
Commons.loadSpringCloudKubernetesImage(CONFIG_WATCHER_IT_IMAGE, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
@@ -186,14 +186,17 @@ class ActuatorRefreshRabbitMQIT {
private void deployRabbitMQ() throws Exception {
api.createNamespacedService(NAMESPACE, getRabbitMQService(), null, null, null);
String[] image = getRabbitMQReplicationController().getSpec().getTemplate().getSpec().getContainers().get(0)
.getImage().split(":");
Commons.pullImage(image[0], image[1], K3S);
Commons.loadImage(image[0], image[1], "rabbitmq", K3S);
api.createNamespacedReplicationController(NAMESPACE, getRabbitMQReplicationController(), null, null, null);
}
private V1Deployment getConfigWatcherDeployment() throws Exception {
V1Deployment deployment = (V1Deployment) K8SUtils.readYamlFromClasspath(
"app-watcher/spring-cloud-kubernetes-configuration-watcher-bus-amqp-deployment.yaml");
String image = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage() + ":"
+ getPomVersion();
String image = K8SUtils.getImageFromDeployment(deployment) + ":" + getPomVersion();
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(image);
return deployment;
}
@@ -201,8 +204,7 @@ class ActuatorRefreshRabbitMQIT {
private V1Deployment getItDeployment() throws Exception {
String urlString = "app-watcher/spring-cloud-kubernetes-configuration-watcher-it-bus-amqp-deployment.yaml";
V1Deployment deployment = (V1Deployment) K8SUtils.readYamlFromClasspath(urlString);
String image = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage() + ":"
+ getPomVersion();
String image = K8SUtils.getImageFromDeployment(deployment) + ":" + getPomVersion();
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setImage(image);
return deployment;
}

View File

@@ -73,7 +73,7 @@ class ActuatorEndpointIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
Commons.loadImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();
appsApi = new AppsV1Api();

View File

@@ -83,10 +83,10 @@ class DiscoveryClientIT {
K3S.start();
Commons.validateImage(DISCOVERY_SERVER_APP_NAME, K3S);
Commons.loadImage(DISCOVERY_SERVER_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(DISCOVERY_SERVER_APP_NAME, K3S);
Commons.validateImage(SPRING_CLOUD_K8S_DISCOVERY_CLIENT_APP_NAME, K3S);
Commons.loadImage(SPRING_CLOUD_K8S_DISCOVERY_CLIENT_APP_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_DISCOVERY_CLIENT_APP_NAME, K3S);
createApiClient(K3S.getKubeConfigYaml());
api = new CoreV1Api();

View File

@@ -65,7 +65,7 @@ class Fabric8ConfigMapIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml());
client = new DefaultKubernetesClient(config);
Fabric8Utils.setUp(client, NAMESPACE);

View File

@@ -72,7 +72,7 @@ class Fabric8DiscoveryIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml());
client = new DefaultKubernetesClient(config);

View File

@@ -75,7 +75,7 @@ public class Fabric8ClientLoadbalancerIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml());
client = new DefaultKubernetesClient(config);

View File

@@ -63,7 +63,7 @@ class SimpleCoreIT {
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
Config config = Config.fromKubeconfig(K3S.getKubeConfigYaml());
client = new DefaultKubernetesClient(config);

View File

@@ -54,7 +54,13 @@ class Fabric8IstioIT {
private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-istio-it";
private static final String LOCAL_ISTIO_BIN_PATH = "../../istio-cli/istio-1.13.3/bin";
private static final String ISTIO_PROXY = "istio/proxyv2";
private static final String ISTIO_PILOT = "istio/pilot";
private static final String ISTIO_VERSION = "1.13.3";
private static final String LOCAL_ISTIO_BIN_PATH = "../../istio-cli/istio-" + ISTIO_VERSION + "/bin";
private static final String CONTAINER_ISTIO_BIN_PATH = "/tmp/istio/istio-bin/bin/";
@@ -75,7 +81,12 @@ class Fabric8IstioIT {
K3S = Commons.container().withFileSystemBind(absolutePath, CONTAINER_ISTIO_BIN_PATH);
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
Commons.pullImage(ISTIO_PROXY, ISTIO_VERSION, K3S);
Commons.loadImage(ISTIO_PROXY, ISTIO_VERSION, "istioproxy", K3S);
Commons.pullImage(ISTIO_PILOT, ISTIO_VERSION, K3S);
Commons.loadImage(ISTIO_PILOT, ISTIO_VERSION, "istiopilot", K3S);
processExecResult(K3S.execInContainer("sh", "-c", "kubectl create namespace istio-test"));
processExecResult(

View File

@@ -71,17 +71,20 @@ public final class Commons {
return CONTAINER;
}
public static void loadImage(String image, K3sContainer container) throws Exception {
// save image
InputStream imageStream = container.getDockerClient().saveImageCmd("springcloud/" + image)
.withTag(getPomVersion()).exec();
public static void loadSpringCloudKubernetesImage(String project, K3sContainer container) throws Exception {
loadImage("springcloud/" + project, getPomVersion(), project, container);
}
Path imagePath = Paths.get(TEMP_FOLDER + "/" + image + ".tar");
public static void loadImage(String image, String tag, String tarName, K3sContainer container) throws Exception {
// save image
InputStream imageStream = container.getDockerClient().saveImageCmd(image).withTag(tag).exec();
Path imagePath = Paths.get(TEMP_FOLDER + "/" + tarName + ".tar");
Files.deleteIfExists(imagePath);
Files.copy(imageStream, imagePath);
// import image with ctr. this works because TEMP_FOLDER is mounted in the
// container
container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + image + ".tar");
container.execInContainer("ctr", "i", "import", TEMP_FOLDER + "/" + tarName + ".tar");
}
public static void cleanUp(String image, K3sContainer container) throws Exception {
@@ -105,6 +108,10 @@ public final class Commons {
+ "You need to build it first, and then run the test"));
}
public static void pullImage(String image, String tag, K3sContainer container) throws InterruptedException {
container.getDockerClient().pullImageCmd(image).withTag(tag).start().awaitCompletion();
}
/**
* A K3sContainer, but with fixed port mappings. This is needed because of the nature
* of some integration tests.

View File

@@ -58,6 +58,7 @@ import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Yaml;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testcontainers.k3s.K3sContainer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ReflectionUtils;
@@ -317,8 +318,8 @@ public class K8SUtils {
return (V1Role) K8SUtils.readYamlFromClasspath("setup/role.yaml");
}
public void deployWiremock(String namespace, boolean rootPath) throws Exception {
innerDeployWiremock(namespace, rootPath);
public void deployWiremock(String namespace, boolean rootPath, K3sContainer container) throws Exception {
innerDeployWiremock(namespace, rootPath, container);
// Check to make sure the wiremock deployment is ready
waitForDeployment(WIREMOCK_DEPLOYMENT_NAME, namespace);
@@ -346,11 +347,26 @@ public class K8SUtils {
*/
public void removeWiremockImage() throws Exception {
V1Deployment wiremockDeployment = getWiremockDeployment();
String wiremockImage = wiremockDeployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage();
String wiremockImage = getImageFromDeployment(wiremockDeployment);
Commons.cleanUpDownloadedImage(wiremockImage);
}
private void innerDeployWiremock(String namespace, boolean rootPath) throws Exception {
/**
* Gets the image from the deployment yaml. Assumes there is only one container
* defined in the deployment.
* @param deployment deployment yaml
* @return An array where the first item is the mage name and the second item is the
* tag
*/
public static String getImageFromDeployment(V1Deployment deployment) {
return deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getImage();
}
private void innerDeployWiremock(String namespace, boolean rootPath, K3sContainer container) throws Exception {
V1Deployment deployment = getWiremockDeployment();
String[] image = getImageFromDeployment(deployment).split(":");
Commons.pullImage(image[0], image[1], container);
Commons.loadImage(image[0], image[1], "wiremock", container);
appsApi.createNamespacedDeployment(namespace, getWiremockDeployment(), null, null, null);
api.createNamespacedService(namespace, getWiremockAppService(), null, null, null);