Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional ports to Cloud.toml #796

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "cloud"
version = "3.0.0"
version = "3.0.1"
repository = "https://github.com/ballerina-platform/module-ballerina-c2c"
license = ["Apache-2.0"]
keywords = ["cloud", "kubernetes", "docker", "k8s", "c2c"]
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "code2cloud"
class = "io.ballerina.c2c.C2CCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-3.0.0.jar"
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-3.0.1-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.9.0"
[[package]]
org = "ballerina"
name = "cloud"
version = "3.0.0"
version = "3.0.1"
modules = [
{org = "ballerina", packageName = "cloud", moduleName = "cloud"}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,14 @@ private Optional<ListenerInfo> getListenerInfo(String path, ExpressionNode expre
}
return Optional.of(new ListenerInfo(path, portNumber));
}
} else {
} else if (expression instanceof BasicLiteralNode) {
//on new http:Listener(9091)
int port = Integer.parseInt(((BasicLiteralNode) expression).literalToken().text());
return Optional.of(new ListenerInfo(path, port));
} else {
diagnostics.add(C2CDiagnosticCodes
.createDiagnostic(C2CDiagnosticCodes.FAILED_PORT_RETRIEVAL, expression.location()));
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2024, 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.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);
}
}
Loading
Loading