Skip to content

Commit

Permalink
Add additional ports to Cloud.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
xlight05 committed May 31, 2024
1 parent 49adfa3 commit a3687a8
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.ballerina.c2c.test.samples;

import io.ballerina.c2c.KubernetesConstants;
import io.ballerina.c2c.exceptions.KubernetesPluginException;
import io.ballerina.c2c.test.utils.DockerTestException;
import io.ballerina.c2c.test.utils.KubernetesTestUtils;
import io.ballerina.c2c.utils.KubernetesUtils;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.autoscaling.v2.HorizontalPodAutoscaler;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static io.ballerina.c2c.KubernetesConstants.DOCKER;
import static io.ballerina.c2c.KubernetesConstants.KUBERNETES;

/**
* Test cases for sample 1.
*/
public class AdditionalPortTest extends SampleTest {

private static final Path SOURCE_DIR_PATH = SAMPLE_DIR.resolve("additional_ports");
private static final Path DOCKER_TARGET_PATH = SOURCE_DIR_PATH.resolve("target").resolve(DOCKER).resolve("diff_ports");
private static final Path KUBERNETES_TARGET_PATH = SOURCE_DIR_PATH.resolve("target").resolve(KUBERNETES).resolve("diff_ports");
private static final String DOCKER_IMAGE = "xlight05/diff_port:latest";
private Deployment deployment;
private Service service;

@BeforeClass
public void compileSample() throws IOException, InterruptedException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH, "k8s"), 0);
File artifactYaml = KUBERNETES_TARGET_PATH.resolve("diff_ports.yaml").toFile();
Assert.assertTrue(artifactYaml.exists());
KubernetesClient client = new KubernetesClientBuilder().build();
List<HasMetadata> k8sItems = client.load(new FileInputStream(artifactYaml)).items();
for (HasMetadata data : k8sItems) {
switch (data.getKind()) {
case "Deployment":
deployment = (Deployment) data;
break;
case "Service":
service = (Service) data;
break;
}
}
}

@Test
public void validateDeployment() {
Assert.assertNotNull(deployment);
Assert.assertEquals(deployment.getMetadata().getName(), "diff-ports-deployment");
Assert.assertEquals(deployment.getSpec().getReplicas().intValue(), 1);
Assert.assertEquals(deployment.getMetadata().getLabels().get(KubernetesConstants
.KUBERNETES_SELECTOR_KEY), "diff_ports");
Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().size(), 1);
Container container = deployment.getSpec().getTemplate().getSpec().getContainers().get(0);
Assert.assertEquals(container.getImage(), DOCKER_IMAGE);
Assert.assertEquals(container.getPorts().size(), 1);
Assert.assertEquals(container.getEnv().size(), 0);
}

@Test
public void validateK8SService() {
Assert.assertNotNull(service);
Assert.assertEquals(service.getMetadata().getName(), "diff-ports-svc");
Assert.assertEquals(service.getMetadata().getLabels().get(KubernetesConstants
.KUBERNETES_SELECTOR_KEY), "diff_ports");
Assert.assertEquals(service.getSpec().getType(), KubernetesConstants.ServiceType.ClusterIP.name());
Assert.assertEquals(service.getSpec().getPorts().size(), 1);
Assert.assertEquals(service.getSpec().getPorts().get(0).getPort().intValue(), 9092);
}

@Test
public void validateDockerfile() throws IOException {
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile();
String dockerFileContent = new String(Files.readAllBytes(dockerFile.toPath()));
Assert.assertTrue(dockerFileContent.contains("EXPOSE 9092"));
Assert.assertTrue(dockerFileContent.contains("USER ballerina"));
Assert.assertTrue(dockerFile.exists());
}

@Test
public void validateDockerImage() throws DockerTestException, InterruptedException {
List<String> ports = KubernetesTestUtils.getExposedPorts(DOCKER_IMAGE);
Assert.assertEquals(ports.size(), 1);
Assert.assertEquals(ports.get(0), "9092/tcp");
}

@Test(groups = { "integration" })
public void deploySample() throws IOException, InterruptedException {
Assert.assertEquals(0, KubernetesTestUtils.loadImage(DOCKER_IMAGE));
Assert.assertEquals(0, KubernetesTestUtils.deployK8s(KUBERNETES_TARGET_PATH));
Assert.assertEquals(0, KubernetesTestUtils.deleteK8s(KUBERNETES_TARGET_PATH));
}

@AfterClass
public void cleanUp() throws KubernetesPluginException {
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,111 +1,119 @@
{
"position": {
"line": 4,
"character": 0
"position": {
"line": 4,
"character": 0
},
"source": "main/source/Cloud.toml",
"items": [
{
"label": "cloud.config.maps",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.maps]]"
},
"source": "main/source/Cloud.toml",
"items": [
{
"label": "cloud.config.maps",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.maps]]"
},
{
"label": "settings",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[settings]"
},
{
"label": "container.copy.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[container.copy.files]]"
},
{
"label": "cloud.deployment.probes.readiness",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[cloud.deployment.probes.readiness]"
},
{
"label": "cloud.config.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.files]]"
},
{
"label": "cloud.config.secrets",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.secrets]]"
},
{
"label": "repository",
"kind": "Snippet",
"detail": "String",
"sortText": "A",
"insertText": "repository=\"${1:}\"",
"insertTextFormat": "Snippet"
},
{
"label": "cloud.deployment.storage.volumes",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.deployment.storage.volumes]]"
},
{
"label": "graalvm.builder",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[graalvm.builder]"
},
{
"label": "entrypoint",
"kind": "Snippet",
"detail": "String",
"sortText": "A",
"insertText": "entrypoint=\"${1:}\"",
"insertTextFormat": "Snippet"
},
{
"label": "cloud.deployment.autoscaling",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[cloud.deployment.autoscaling]"
},
{
"label": "cloud.config.envs",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.envs]]"
},
{
"label": "cloud.secret.envs",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.secret.envs]]"
},
{
"label": "cloud.secret.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.secret.files]]"
}
]
{
"label": "settings",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[settings]"
},
{
"label": "container.copy.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[container.copy.files]]"
},
{
"label": "cloud.deployment.probes.readiness",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[cloud.deployment.probes.readiness]"
},
{
"label": "cloud.config.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.files]]"
},
{
"label": "cloud.config.secrets",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.secrets]]"
},
{
"label": "repository",
"kind": "Snippet",
"detail": "String",
"sortText": "A",
"insertText": "repository=\"${1:}\"",
"insertTextFormat": "Snippet"
},
{
"label": "cloud.secret.envs",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.secret.envs]]"
},
{
"label": "additionalPorts",
"kind": "Snippet",
"detail": "Array",
"sortText": "A",
"insertText": "additionalPorts=[${1:}]",
"insertTextFormat": "Snippet"
},
{
"label": "cloud.deployment.storage.volumes",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.deployment.storage.volumes]]"
},
{
"label": "cloud.secret.files",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.secret.files]]"
},
{
"label": "entrypoint",
"kind": "Snippet",
"detail": "String",
"sortText": "A",
"insertText": "entrypoint=\"${1:}\"",
"insertTextFormat": "Snippet"
},
{
"label": "graalvm.builder",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[graalvm.builder]"
},
{
"label": "cloud.deployment.autoscaling",
"kind": "Snippet",
"detail": "Table",
"sortText": "C",
"insertText": "[cloud.deployment.autoscaling]"
},
{
"label": "cloud.config.envs",
"kind": "Snippet",
"detail": "Table Array",
"sortText": "C",
"insertText": "[[cloud.config.envs]]"
}
]
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<class name="io.ballerina.c2c.test.samples.Sample10Test"/>
<class name="io.ballerina.c2c.test.samples.Sample11Test"/>
<class name="io.ballerina.c2c.test.samples.Sample12Test"/>
<class name="io.ballerina.c2c.test.samples.AdditionalPortTest"/>
<class name="io.ballerina.c2c.test.samples.NativeTest"/>
<class name="io.ballerina.c2c.test.samples.NativeArgsTest"/>
<class name="io.ballerina.c2c.test.samples.NativeBaseTest"/>
Expand Down
1 change: 1 addition & 0 deletions compiler-plugin-tests/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<class name="io.ballerina.c2c.test.samples.Sample10Test"/>
<class name="io.ballerina.c2c.test.samples.Sample11Test"/>
<class name="io.ballerina.c2c.test.samples.Sample12Test"/>
<class name="io.ballerina.c2c.test.samples.AdditionalPortTest"/>
<class name="io.ballerina.c2c.test.samples.NativeTest"/>
<class name="io.ballerina.c2c.test.samples.NativeArgsTest"/>
<class name="io.ballerina.c2c.test.samples.NativeBaseTest"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class KubernetesConstants {
public static final String MEMORY = "memory";
public static final String CPU = "cpu";
public static final String CHOREO = "choreo";
public static final String CONTAINER_IMAGE = "container.image";

/**
* Restart policy enum.
Expand Down
Loading

0 comments on commit a3687a8

Please sign in to comment.