http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/445884b1/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationLiveTest.java ---------------------------------------------------------------------- diff --git a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationLiveTest.java b/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationLiveTest.java deleted file mode 100644 index 4fb416f..0000000 --- a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationLiveTest.java +++ /dev/null @@ -1,226 +0,0 @@ -package io.cloudsoft.amp.containerservice.kubernetes.location; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; - -import java.util.List; -import java.util.Map; - -import org.apache.brooklyn.api.location.MachineDetails; -import org.apache.brooklyn.api.location.OsDetails; -import org.apache.brooklyn.core.location.BasicMachineDetails; -import org.apache.brooklyn.core.location.LocationConfigKeys; -import org.apache.brooklyn.core.location.access.PortForwardManager; -import org.apache.brooklyn.core.location.access.PortForwardManagerLocationResolver; -import org.apache.brooklyn.core.test.BrooklynAppLiveTestSupport; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.test.Asserts; -import org.apache.brooklyn.util.collections.MutableMap; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.net.HostAndPort; - -import io.cloudsoft.amp.containerservice.kubernetes.location.machine.KubernetesMachineLocation; -import io.cloudsoft.amp.containerservice.kubernetes.location.machine.KubernetesSshMachineLocation; - -/** -/** - * Live tests for deploying simple containers. Particularly useful during dev, but not so useful - * after that (because assumes the existence of a kubernetes endpoint). It needs configured with - * something like: - * - * {@code -Dtest.amp.kubernetes.endpoint=http://10.104.2.206:8080}). - * - * The QA Framework is more important for that - hence these tests (trying to be) kept simple - * and focused. - */ -public class KubernetesLocationLiveTest extends BrooklynAppLiveTestSupport { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesLocationLiveTest.class); - - public static final String KUBERNETES_ENDPOINT = System.getProperty("test.amp.kubernetes.endpoint", ""); - public static final String IDENTITY = System.getProperty("test.amp.kubernetes.identity", ""); - public static final String CREDENTIAL = System.getProperty("test.amp.kubernetes.credential", ""); - - protected KubernetesLocation loc; - protected List<KubernetesMachineLocation> machines; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - machines = Lists.newCopyOnWriteArrayList(); - } - - // FIXME: Clear up properly: Test leaves deployment, replicas and pods behind if obtain fails. - @AfterMethod(alwaysRun=true) - @Override - public void tearDown() throws Exception { - for (KubernetesMachineLocation machine : machines) { - try { - loc.release(machine); - } catch (Exception e) { - LOG.error("Error releasing machine "+machine+" in location "+loc, e); - } - } - super.tearDown(); - } - - protected KubernetesLocation newKubernetesLocation(Map<String, ?> flags) throws Exception { - Map<String,?> allFlags = MutableMap.<String,Object>builder() - .put("identity", IDENTITY) - .put("credential", CREDENTIAL) - .put("endpoint", KUBERNETES_ENDPOINT) - .putAll(flags) - .build(); - return (KubernetesLocation) mgmt.getLocationRegistry().getLocationManaged("kubernetes", allFlags); - } - - @Test(groups={"Live"}) - public void testDefault() throws Exception { - // Default is "cloudsoft/centos:7" - runImage(ImmutableMap.<String, Object>of(), "centos", "7"); - } - - @Test(groups={"Live"}) - public void testMatchesCentos() throws Exception { - runImage(ImmutableMap.<String, Object>of(KubernetesLocationConfig.OS_FAMILY.getName(), "centos"), "centos", "7"); - } - - @Test(groups={"Live"}) - public void testMatchesCentos7() throws Exception { - ImmutableMap<String, Object> conf = ImmutableMap.<String, Object>of( - KubernetesLocationConfig.OS_FAMILY.getName(), "centos", - KubernetesLocationConfig.OS_VERSION_REGEX.getName(), "7.*"); - runImage(conf, "centos", "7"); - } - - @Test(groups={"Live"}) - public void testMatchesUbuntu() throws Exception { - runImage(ImmutableMap.<String, Object>of(KubernetesLocationConfig.OS_FAMILY.getName(), "ubuntu"), "ubuntu", "14.04"); - } - - @Test(groups={"Live"}) - public void testMatchesUbuntu16() throws Exception { - ImmutableMap<String, Object> conf = ImmutableMap.<String, Object>of( - KubernetesLocationConfig.OS_FAMILY.getName(), "ubuntu", - KubernetesLocationConfig.OS_VERSION_REGEX.getName(), "16.*"); - runImage(conf, "ubuntu", "16.04"); - } - - @Test(groups={"Live"}) - public void testCloudsoftCentos7() throws Exception { - runImage(ImmutableMap.of(KubernetesLocationConfig.IMAGE.getName(), "cloudsoft/centos:7"), "centos", "7"); - } - - @Test(groups={"Live"}) - public void testCloudsoftUbuntu14() throws Exception { - runImage(ImmutableMap.of(KubernetesLocationConfig.IMAGE.getName(), "cloudsoft/ubuntu:14.04"), "ubuntu", "14.04"); - } - - @Test(groups={"Live"}) - public void testCloudsoftUbuntu16() throws Exception { - runImage(ImmutableMap.of(KubernetesLocationConfig.IMAGE.getName(), "cloudsoft/ubuntu:16.04"), "ubuntu", "16.04"); - } - - @Test(groups={"Live"}) - public void testFailsForNonMatching() throws Exception { - ImmutableMap<String, Object> conf = ImmutableMap.<String, Object>of( - KubernetesLocationConfig.OS_FAMILY.getName(), "weirdOsFamiliy"); - try { - runImage(conf, null, null); - Asserts.shouldHaveFailedPreviously(); - } catch (Exception e) { - Asserts.expectedFailureContains(e, "No matching image found"); - } - } - - protected void runImage(Map<String, ?> config, String expectedOs, String expectedVersion) throws Exception { - loc = newKubernetesLocation(ImmutableMap.<String, Object>of()); - SshMachineLocation machine = newContainerMachine(loc, ImmutableMap.<String, Object>builder() - .putAll(config) - .put(LocationConfigKeys.CALLER_CONTEXT.getName(), app) - .build()); - - assertTrue(machine.isSshable(), "not sshable machine="+machine); - assertOsNameContains(machine, expectedOs, expectedVersion); - assertMachinePasswordSecure(machine); - } - - @Test(groups={"Live"}) - protected void testUsesSuppliedLoginPassword() throws Exception { - // Because defaulting to "cloudsoft/centos:7", it knows to set the loginUserPassword - // on container creation. - String password = "myCustomP4ssword"; - loc = newKubernetesLocation(ImmutableMap.<String, Object>of()); - SshMachineLocation machine = newContainerMachine(loc, ImmutableMap.<String, Object>builder() - .put(KubernetesLocationConfig.LOGIN_USER_PASSWORD.getName(), password) - .put(LocationConfigKeys.CALLER_CONTEXT.getName(), app) - .build()); - - assertTrue(machine.isSshable(), "not sshable machine="+machine); - assertEquals(machine.config().get(SshMachineLocation.PASSWORD), password); - } - - @Test(groups={"Live"}) - public void testOpenPorts() throws Exception { - List<Integer> inboundPorts = ImmutableList.of(22, 443, 8000, 8081); - loc = newKubernetesLocation(ImmutableMap.<String, Object>of()); - SshMachineLocation machine = newContainerMachine(loc, ImmutableMap.<String, Object>builder() - .put(KubernetesLocationConfig.IMAGE.getName(), "cloudsoft/centos:7") - .put(KubernetesLocationConfig.LOGIN_USER_PASSWORD.getName(), "p4ssw0rd") - .put(KubernetesLocationConfig.INBOUND_PORTS.getName(), inboundPorts) - .put(LocationConfigKeys.CALLER_CONTEXT.getName(), app) - .build()); - assertTrue(machine.isSshable()); - - String publicHostText = machine.getSshHostAndPort().getHostText(); - PortForwardManager pfm = (PortForwardManager) mgmt.getLocationRegistry().getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC); - for (int targetPort : inboundPorts) { - HostAndPort mappedPort = pfm.lookup(machine, targetPort); - assertNotNull(mappedPort, "no mapping for targetPort "+targetPort); - assertEquals(mappedPort.getHostText(), publicHostText); - assertTrue(mappedPort.hasPort(), "no port-part in "+mappedPort+" for targetPort "+targetPort); - } - } - - protected void assertOsNameContains(SshMachineLocation machine, String expectedNamePart, String expectedVersionPart) { - MachineDetails machineDetails = app.getExecutionContext() - .submit(BasicMachineDetails.taskForSshMachineLocation(machine)) - .getUnchecked(); - OsDetails osDetails = machineDetails.getOsDetails(); - String osName = osDetails.getName(); - String osVersion = osDetails.getVersion(); - assertTrue(osName != null && osName.toLowerCase().contains(expectedNamePart), "osDetails="+osDetails); - assertTrue(osVersion != null && osVersion.toLowerCase().contains(expectedVersionPart), "osDetails="+osDetails); - } - - protected SshMachineLocation newContainerMachine(KubernetesLocation loc, Map<?, ?> flags) throws Exception { - KubernetesMachineLocation result = loc.obtain(flags); - machines.add(result); - assertTrue(result instanceof KubernetesSshMachineLocation); - return (SshMachineLocation) result; - } - - protected void assertMachinePasswordSecure(SshMachineLocation machine) { - String password = machine.config().get(SshMachineLocation.PASSWORD); - assertTrue(password.length() > 10, "password="+password); - boolean hasUpper = false; - boolean hasLower = false; - boolean hasNonAlphabetic = false; - for (char c : password.toCharArray()) { - if (Character.isUpperCase(c)) hasUpper = true; - if (Character.isLowerCase(c)) hasLower = true; - if (!Character.isAlphabetic(c)) hasNonAlphabetic = true; - } - assertTrue(hasUpper && hasLower && hasNonAlphabetic, "password="+password); - } -}
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/445884b1/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationResolverTest.java b/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationResolverTest.java deleted file mode 100644 index acd366b..0000000 --- a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationResolverTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package io.cloudsoft.amp.containerservice.kubernetes.location; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import java.util.Map; - -import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.core.internal.BrooklynProperties; -import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -public class KubernetesLocationResolverTest extends BrooklynMgmtUnitTestSupport { - - private static final Logger LOG = LoggerFactory.getLogger(KubernetesLocationResolverTest.class); - - private BrooklynProperties brooklynProperties; - - @BeforeMethod(alwaysRun = true) - public void setUp() throws Exception { - super.setUp(); - brooklynProperties = mgmt.getBrooklynProperties(); - - brooklynProperties.put("brooklyn.location.kubernetes.identity", "kubernetes-id"); - brooklynProperties.put("brooklyn.location.kubernetes.credential", "kubernetes-cred"); - } - - @Test - public void testGivesCorrectLocationType() { - LocationSpec<?> spec = getLocationSpec("kubernetes"); - assertEquals(spec.getType(), KubernetesLocation.class); - - KubernetesLocation loc = resolve("kubernetes"); - assertTrue(loc instanceof KubernetesLocation, "loc="+loc); - } - - @Test - public void testParametersInSpecString() { - KubernetesLocation loc = resolve("kubernetes(endpoint=myMasterUrl)"); - assertEquals(loc.getConfig(KubernetesLocation.MASTER_URL), "myMasterUrl"); - } - - @Test - public void testTakesDotSeparateProperty() { - brooklynProperties.put("brooklyn.location.kubernetes.endpoint", "myMasterUrl"); - KubernetesLocation loc = resolve("kubernetes"); - assertEquals(loc.getConfig(KubernetesLocation.MASTER_URL), "myMasterUrl"); - } - - @Test - public void testPropertiesPrecedence() { - // prefer those in "spec" over everything else - brooklynProperties.put("brooklyn.location.named.mykubernetes", "kubernetes:(loginUser=\"loginUser-inSpec\")"); - - brooklynProperties.put("brooklyn.location.named.mykubernetes.loginUser", "loginUser-inNamed"); - brooklynProperties.put("brooklyn.location.kubernetes.loginUser", "loginUser-inDocker"); - - // prefer those in "named" over everything else - brooklynProperties.put("brooklyn.location.named.mykubernetes.privateKeyFile", "privateKeyFile-inNamed"); - brooklynProperties.put("brooklyn.location.kubernetes.privateKeyFile", "privateKeyFile-inDocker"); - - // prefer those in kubernetes-specific - brooklynProperties.put("brooklyn.location.kubernetes.publicKeyFile", "publicKeyFile-inDocker"); - - Map<String, Object> conf = resolve("named:mykubernetes").config().getBag().getAllConfig(); - - assertEquals(conf.get("loginUser"), "loginUser-inSpec"); - assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed"); - assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inDocker"); - } - - private LocationSpec<?> getLocationSpec(String spec) { - LOG.debug("Obtaining location spec '{}'", spec); - return mgmt.getLocationRegistry().getLocationSpec(spec).get(); - } - - private KubernetesLocation resolve(String spec) { - LOG.debug("Resolving location spec '{}'", spec); - return (KubernetesLocation) mgmt.getLocationRegistry().getLocationManaged(spec); - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/445884b1/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationYamlLiveTest.java ---------------------------------------------------------------------- diff --git a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationYamlLiveTest.java b/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationYamlLiveTest.java deleted file mode 100644 index 6c09b16..0000000 --- a/kubernetes-location/src/test/java/io/cloudsoft/amp/containerservice/kubernetes/location/KubernetesLocationYamlLiveTest.java +++ /dev/null @@ -1,518 +0,0 @@ -package io.cloudsoft.amp.containerservice.kubernetes.location; - -import static com.google.common.base.Predicates.and; -import static com.google.common.base.Predicates.equalTo; -import static com.google.common.base.Predicates.not; -import static com.google.common.base.Predicates.notNull; -import static io.cloudsoft.amp.containerservice.kubernetes.location.KubernetesLocationLiveTest.CREDENTIAL; -import static io.cloudsoft.amp.containerservice.kubernetes.location.KubernetesLocationLiveTest.IDENTITY; -import static io.cloudsoft.amp.containerservice.kubernetes.location.KubernetesLocationLiveTest.KUBERNETES_ENDPOINT; -import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEquals; -import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEqualsEventually; -import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEventually; -import static org.apache.brooklyn.core.entity.EntityAsserts.assertAttributeEventuallyNonNull; -import static org.apache.brooklyn.core.entity.EntityAsserts.assertEntityHealthy; -import static org.apache.brooklyn.test.Asserts.succeedsEventually; -import static org.apache.brooklyn.util.http.HttpAsserts.assertHttpStatusCodeEventuallyEquals; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; - -import java.util.List; -import java.util.Map; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.location.MachineLocation; -import org.apache.brooklyn.api.location.MachineProvisioningLocation; -import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest; -import org.apache.brooklyn.core.entity.Attributes; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.EntityPredicates; -import org.apache.brooklyn.core.location.Machines; -import org.apache.brooklyn.core.network.OnPublicNetworkEnricher; -import org.apache.brooklyn.core.sensor.Sensors; -import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; -import org.apache.brooklyn.entity.software.base.SoftwareProcess; -import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess; -import org.apache.brooklyn.entity.stock.BasicStartable; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.util.core.config.ConfigBag; -import org.apache.brooklyn.util.net.Networking; -import org.apache.brooklyn.util.text.Identifiers; -import org.apache.logging.log4j.util.Strings; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.net.HostAndPort; - -import io.cloudsoft.amp.containerservice.dockercontainer.DockerContainer; -import io.cloudsoft.amp.containerservice.kubernetes.entity.KubernetesPod; -import io.cloudsoft.amp.containerservice.kubernetes.entity.KubernetesResource; -import io.fabric8.kubernetes.api.model.Pod; -import io.fabric8.kubernetes.client.KubernetesClient; - -/** - * Live tests for deploying simple blueprints. Particularly useful during dev, but not so useful - * after that (because assumes the existence of a kubernetes endpoint). It needs configured with - * something like: - * - * {@code -Dtest.amp.kubernetes.endpoint=http://10.104.2.206:8080}). - * - * The QA Framework is more important for that - hence these tests (trying to be) kept simple - * and focused. - */ -public class KubernetesLocationYamlLiveTest extends AbstractYamlTest { - - protected KubernetesLocation loc; - protected List<MachineLocation> machines; - protected String locationYaml; - - @BeforeMethod(alwaysRun=true) - @Override - public void setUp() throws Exception { - super.setUp(); - - locationYaml = Joiner.on("\n").join( - "location:", - " kubernetes:", - " " + KubernetesLocationConfig.MASTER_URL.getName() + ": \"" + KUBERNETES_ENDPOINT + "\"", - " " + (Strings.isBlank(IDENTITY) ? "" : "identity: "+IDENTITY), - " " + (Strings.isBlank(CREDENTIAL) ? "" : "credential: "+CREDENTIAL)); - } - - @Test(groups={"Live"}) - public void testLoginPasswordOverride() throws Exception { - String customPassword = "myDifferentPassword"; - - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + EmptySoftwareProcess.class.getName(), - " brooklyn.config:", - " provisioning.properties:", - " " + KubernetesLocationConfig.LOGIN_USER_PASSWORD.getName() + ": " + customPassword); - - Entity app = createStartWaitAndLogApplication(yaml); - EmptySoftwareProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, EmptySoftwareProcess.class)); - - SshMachineLocation machine = Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).get(); - assertEquals(machine.config().get(SshMachineLocation.PASSWORD), customPassword); - assertTrue(machine.isSshable()); - } - - @Test(groups={"Live"}) - public void testNetcatServer() throws Exception { - // Runs as root user (hence not `sudo yum install ...`) - // breaks if shell.env uses attributeWhenReady, so not doing that - see testNetcatServerWithDslInShellEnv() - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + VanillaSoftwareProcess.class.getName(), - " brooklyn.parameters:", - " - name: netcat.port", - " type: port", - " default: 8081", - " brooklyn.config:", - " install.command: |", - " yum install -y nc", - " launch.command: |", - " echo $MESSAGE | nc -l $NETCAT_PORT &", - " echo $! > $PID_FILE", - " shell.env:", - " MESSAGE: mymessage", - " NETCAT_PORT: $brooklyn:attributeWhenReady(\"netcat.port\")", - " brooklyn.enrichers:", - " - type: " + OnPublicNetworkEnricher.class.getName(), - " brooklyn.config:", - " " + OnPublicNetworkEnricher.SENSORS.getName() + ":", - " - netcat.port"); - - Entity app = createStartWaitAndLogApplication(yaml); - VanillaSoftwareProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaSoftwareProcess.class)); - - String publicMapped = assertAttributeEventuallyNonNull(entity, Sensors.newStringSensor("netcat.endpoint.mapped.public")); - HostAndPort publicPort = HostAndPort.fromString(publicMapped); - - assertTrue(Networking.isReachable(publicPort), "publicPort="+publicPort); - } - - @Test(groups={"Live"}) - public void testInterContainerNetworking() throws Exception { - String message = "mymessage"; - int netcatPort = 8081; - - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + VanillaSoftwareProcess.class.getName(), - " name: server1", - " brooklyn.parameters:", - " - name: netcat.port", - " type: port", - " default: " + netcatPort, - " brooklyn.config:", - " install.command: |", - " yum install -y nc", - " launch.command: |", - " echo " + message + " | nc -l " + netcatPort + " > netcat.out &", - " echo $! > $PID_FILE", - " - type: " + VanillaSoftwareProcess.class.getName(), - " name: server2", - " brooklyn.config:", - " install.command: |", - " yum install -y nc", - " launch.command: true", - " checkRunning.command: true"); - - Entity app = createStartWaitAndLogApplication(yaml); - Entities.dumpInfo(app); - - Entity server1 = Iterables.find(Entities.descendantsAndSelf(app), EntityPredicates.displayNameEqualTo("server1")); - Entity server2 = Iterables.find(Entities.descendantsAndSelf(app), EntityPredicates.displayNameEqualTo("server2")); - - SshMachineLocation machine1 = Machines.findUniqueMachineLocation(server1.getLocations(), SshMachineLocation.class).get(); - SshMachineLocation machine2 = Machines.findUniqueMachineLocation(server2.getLocations(), SshMachineLocation.class).get(); - - String addr1 = server1.sensors().get(Attributes.SUBNET_ADDRESS); - String addr2 = server2.sensors().get(Attributes.SUBNET_ADDRESS); - - // Ping between containers - int result1 = machine1.execCommands("ping-server2", ImmutableList.of("ping -c 4 " + addr2)); - int result2 = machine2.execCommands("ping-server1", ImmutableList.of("ping -c 4 " + addr1)); - - // Reach netcat port from other container - int result3 = machine2.execCommands("nc-to-server1", ImmutableList.of( - "echo \"fromServer2\" | nc " + addr1 + " " + netcatPort + " > netcat.out", - "cat netcat.out", - "grep " + message + " netcat.out")); - - String errMsg = "result1="+result1+"; result2="+result2+"; result3="+result3; - assertEquals(result1, 0, errMsg); - assertEquals(result2, 0, errMsg); - assertEquals(result3, 0, errMsg); - } - - @Test(groups={"Live"}) - public void testTomcatPod() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesPod.class.getName(), - " brooklyn.config:", - " docker.container.imageName: tomcat", - " docker.container.inboundPorts: [ \"8080\" ]"); - - runTomcat(yaml, KubernetesPod.class); - } - - @Test(groups={"Live"}) - public void testTomcatPodExtras() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesPod.class.getName(), - " brooklyn.config:", - " docker.container.imageName: tomcat", - " docker.container.inboundPorts: [ \"8080\" ]", - " metadata:", - " extra: test"); - - KubernetesPod entity = runTomcat(yaml, KubernetesPod.class); - - String namespace = entity.sensors().get(KubernetesPod.KUBERNETES_NAMESPACE); - String podName = entity.sensors().get(KubernetesPod.KUBERNETES_POD); - KubernetesClient client = getClient(entity); - Pod pod = client.pods().inNamespace(namespace).withName(podName).get(); - Map<String, String> labels = pod.getMetadata().getLabels(); - assertTrue(labels.containsKey("extra")); - assertEquals(labels.get("extra"), "test"); - } - - @Test(groups={"Live"}) - public void testTomcatContainer() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + DockerContainer.class.getName(), - " brooklyn.config:", - " docker.container.imageName: tomcat", - " docker.container.inboundPorts: [ \"8080\" ]"); - - runTomcat(yaml, DockerContainer.class); - } - - /** - * Assumes that the container entity uses port 8080. - */ - protected <T extends Entity> T runTomcat(String yaml, Class<T> type) throws Exception { - Entity app = createStartWaitAndLogApplication(yaml); - T entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, type)); - - Entities.dumpInfo(app); - String publicMapped = assertAttributeEventuallyNonNull(entity, Sensors.newStringSensor("docker.port.8080.mapped.public")); - HostAndPort publicPort = HostAndPort.fromString(publicMapped); - - assertReachableEventually(publicPort); - assertHttpStatusCodeEventuallyEquals("http://"+publicPort.getHostText()+":"+publicPort.getPort(), 200); - - return entity; - } - - @Test(groups={"Live"}) - public void testWordpressInContainersWithStartableParent() throws Exception { - // TODO docker.container.inboundPorts doesn't accept list of ints - need to use quotes - String randomId = Identifiers.makeRandomLowercaseId(4); - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + BasicStartable.class.getName(), - " brooklyn.children:", - " - type: " + DockerContainer.class.getName(), - " id: wordpress-mysql", - " name: mysql", - " brooklyn.config:", - " docker.container.imageName: mysql:5.6", - " docker.container.inboundPorts:", - " - \"3306\"", - " docker.container.environment:", - " MYSQL_ROOT_PASSWORD: \"password\"", - " provisioning.properties:", - " deployment: wordpress-mysql-" + randomId, - " - type: " + DockerContainer.class.getName(), - " id: wordpress", - " name: wordpress", - " brooklyn.config:", - " docker.container.imageName: wordpress:4-apache", - " docker.container.inboundPorts:", - " - \"80\"", - " docker.container.environment:", - " WORDPRESS_DB_HOST: \"wordpress-mysql-" + randomId + "\"", - " WORDPRESS_DB_PASSWORD: \"password\"", - " provisioning.properties:", - " deployment: wordpress-" + randomId); - - runWordpress(yaml, randomId); - } - - @Test(groups={"Live"}) - public void testWordpressInPodsWithStartableParent() throws Exception { - // TODO docker.container.inboundPorts doesn't accept list of ints - need to use quotes - String randomId = Identifiers.makeRandomLowercaseId(4); - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + BasicStartable.class.getName(), - " brooklyn.children:", - " - type: " + KubernetesPod.class.getName(), - " id: wordpress-mysql", - " name: mysql", - " brooklyn.config:", - " docker.container.imageName: mysql:5.6", - " docker.container.inboundPorts:", - " - \"3306\"", - " docker.container.environment:", - " MYSQL_ROOT_PASSWORD: \"password\"", - " deployment: wordpress-mysql-" + randomId, - " - type: " + KubernetesPod.class.getName(), - " id: wordpress", - " name: wordpress", - " brooklyn.config:", - " docker.container.imageName: wordpress:4-apache", - " docker.container.inboundPorts:", - " - \"80\"", - " docker.container.environment:", - " WORDPRESS_DB_HOST: \"wordpress-mysql-" + randomId + "\"", - " WORDPRESS_DB_PASSWORD: \"password\"", - " deployment: wordpress-" + randomId); - - runWordpress(yaml, randomId); - } - - @Test(groups={"Live"}) - public void testWordpressInPods() throws Exception { - // TODO docker.container.inboundPorts doesn't accept list of ints - need to use quotes - String randomId = Identifiers.makeRandomLowercaseId(4); - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesPod.class.getName(), - " id: wordpress-mysql", - " name: mysql", - " brooklyn.config:", - " docker.container.imageName: mysql:5.6", - " docker.container.inboundPorts:", - " - \"3306\"", - " docker.container.environment:", - " MYSQL_ROOT_PASSWORD: \"password\"", - " deployment: wordpress-mysql-" + randomId, - " - type: " + KubernetesPod.class.getName(), - " id: wordpress", - " name: wordpress", - " brooklyn.config:", - " docker.container.imageName: wordpress:4-apache", - " docker.container.inboundPorts:", - " - \"80\"", - " docker.container.environment:", - " WORDPRESS_DB_HOST: \"wordpress-mysql-" + randomId + "\"", - " WORDPRESS_DB_PASSWORD: \"password\"", - " deployment: wordpress-" + randomId); - - runWordpress(yaml, randomId); - } - - /** - * Assumes that the {@link DockerContainer} entities have display names of "mysql" and "wordpress", - * and that they use ports 3306 and 80 respectively. - */ - protected void runWordpress(String yaml, String randomId) throws Exception { - Entity app = createStartWaitAndLogApplication(yaml); - Entities.dumpInfo(app); - - Iterable<DockerContainer> containers = Entities.descendantsAndSelf(app, DockerContainer.class); - DockerContainer mysql = Iterables.find(containers, EntityPredicates.displayNameEqualTo("mysql")); - DockerContainer wordpress = Iterables.find(containers, EntityPredicates.displayNameEqualTo("wordpress")); - - String mysqlPublicPort = assertAttributeEventuallyNonNull(mysql, Sensors.newStringSensor("docker.port.3306.mapped.public")); - assertReachableEventually(HostAndPort.fromString(mysqlPublicPort)); - assertAttributeEquals(mysql, KubernetesPod.KUBERNETES_NAMESPACE, "amp"); - assertAttributeEquals(mysql, KubernetesPod.KUBERNETES_SERVICE, "wordpress-mysql-" + randomId); - - String wordpressPublicPort = assertAttributeEventuallyNonNull(wordpress, Sensors.newStringSensor("docker.port.80.mapped.public")); - assertReachableEventually(HostAndPort.fromString(wordpressPublicPort)); - assertAttributeEquals(wordpress, KubernetesPod.KUBERNETES_NAMESPACE, "amp"); - assertAttributeEquals(wordpress, KubernetesPod.KUBERNETES_SERVICE, "wordpress-" + randomId); - - // TODO more assertions (e.g. wordpress can successfully reach the database) - } - - @Test(groups={"Live"}) - public void testPod() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesPod.class.getName(), - " brooklyn.config:", - " docker.container.imageName: tomcat", - " docker.container.inboundPorts:", - " - \"8080\"", - " shell.env:", - " CLUSTER_ID: \"id\"", - " CLUSTER_TOKEN: \"token\""); - - Entity app = createStartWaitAndLogApplication(yaml); - checkPod(app, KubernetesPod.class); - } - - /* Test disabled as QA framework AMP does not have catalog entries deployed yet */ - @Test(groups={"Live"}, enabled=false) - public void testPodCatalogEntry() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: kubernetes-pod-entity", - " brooklyn.config:", - " docker.container.imageName: tomcat", - " docker.container.inboundPorts:", - " - \"8080\"", - " shell.env:", - " CLUSTER_ID: \"id\"", - " CLUSTER_TOKEN: \"token\""); - - Entity app = createStartWaitAndLogApplication(yaml); - checkPod(app, KubernetesPod.class); - } - - protected <T extends Entity> void checkPod(Entity app, Class<T> type) { - T container = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, type)); - - Entities.dumpInfo(app); - - String publicMapped = assertAttributeEventuallyNonNull(container, Sensors.newStringSensor("docker.port.8080.mapped.public")); - HostAndPort publicPort = HostAndPort.fromString(publicMapped); - - assertReachableEventually(publicPort); - assertHttpStatusCodeEventuallyEquals("http://"+publicPort.getHostText()+":"+publicPort.getPort(), 200); - } - - @Test(groups={"Live"}) - public void testNginxReplicationController() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesResource.class.getName(), - " id: nginx-replication-controller", - " name: \"nginx-replication-controller\"", - " brooklyn.config:", - " resource: classpath://nginx-replication-controller.yaml"); - - Entity app = createStartWaitAndLogApplication(yaml); - checkNginxResource(app, KubernetesResource.class); - } - - protected <T extends Entity> void checkNginxResource(Entity app, Class<T> type) { - T entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, type)); - - Entities.dumpInfo(app); - - assertEntityHealthy(entity); - assertAttributeEqualsEventually(entity, KubernetesResource.RESOURCE_NAME, "nginx-replication-controller"); - assertAttributeEqualsEventually(entity, KubernetesResource.RESOURCE_TYPE, "ReplicationController"); - assertAttributeEqualsEventually(entity, KubernetesResource.KUBERNETES_NAMESPACE, "default"); - assertAttributeEventually(entity, SoftwareProcess.ADDRESS, and(notNull(), not(equalTo("0.0.0.0")))); - assertAttributeEventually(entity, SoftwareProcess.SUBNET_ADDRESS, and(notNull(), not(equalTo("0.0.0.0")))); - } - - @Test(groups={"Live"}) - public void testNginxService() throws Exception { - String yaml = Joiner.on("\n").join( - locationYaml, - "services:", - " - type: " + KubernetesResource.class.getName(), - " id: nginx-replication-controller", - " name: \"nginx-replication-controller\"", - " brooklyn.config:", - " resource: classpath://nginx-replication-controller.yaml", - " - type: " + KubernetesResource.class.getName(), - " id: nginx-service", - " name: \"nginx-service\"", - " brooklyn.config:", - " resource: classpath://nginx-service.yaml"); - Entity app = createStartWaitAndLogApplication(yaml); - - Iterable<KubernetesResource> resources = Entities.descendantsAndSelf(app, KubernetesResource.class); - KubernetesResource nginxReplicationController = Iterables.find(resources, EntityPredicates.displayNameEqualTo("nginx-replication-controller")); - KubernetesResource nginxService = Iterables.find(resources, EntityPredicates.displayNameEqualTo("nginx-service")); - - assertEntityHealthy(nginxReplicationController); - assertEntityHealthy(nginxService); - - Entities.dumpInfo(app); - - Integer httpPort = assertAttributeEventuallyNonNull(nginxService, Sensors.newIntegerSensor("kubernetes.http.port")); - assertEquals(httpPort, Integer.valueOf(80)); - String httpPublicPort = assertAttributeEventuallyNonNull(nginxService, Sensors.newStringSensor("kubernetes.http.endpoint.mapped.public")); - assertReachableEventually(HostAndPort.fromString(httpPublicPort)); - } - - protected void assertReachableEventually(final HostAndPort hostAndPort) { - succeedsEventually(new Runnable() { - public void run() { - assertTrue(Networking.isReachable(hostAndPort), "publicPort="+hostAndPort); - }}); - } - - public KubernetesClient getClient(Entity entity) { - MachineProvisioningLocation location = entity.sensors().get(SoftwareProcess.PROVISIONING_LOCATION); - if (location instanceof KubernetesLocation) { - KubernetesLocation kubernetes = (KubernetesLocation) location; - ConfigBag config = kubernetes.config().getBag(); - KubernetesClientRegistry registry = kubernetes.config().get(KubernetesLocationConfig.KUBERNETES_CLIENT_REGISTRY); - KubernetesClient client = registry.getKubernetesClient(config); - return client; - } - throw new IllegalStateException("Cannot find KubernetesLocation on entity: " + Iterables.toString(entity.getLocations())); - } -}
