Skip to content

Commit

Permalink
VBV-1589 Enable ProvisioningOnKubernetesIT
Browse files Browse the repository at this point in the history
Change-Id: I08273ba39024fd7b8d683f77d2864a6c7376423e
Reviewed-on: http://bellevue-ci.eng.vmware.com:8080/23556
Closures-Verified: jenkins <[email protected]>
Upgrade-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
CS-Verified: jenkins <[email protected]>
Reviewed-by: Antonio Filipov <[email protected]>
  • Loading branch information
mshipkovenski committed Jan 2, 2018
1 parent a66645f commit af336e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public ContainerHostRemovalTaskState(List<String> resourceLinks) {
public void handlePost(Operation post) {
logFine("Operation headers: [%s]", post.getRequestHeaders());

if (isOperationOverAllClustes(post)) {
if (isOperationOverAllClusters(post)) {
createCluster(post);
return;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public void handleDelete(Operation delete) {

@Override
public void handleGet(Operation get) {
if (isOperationOverAllClustes(get)) {
if (isOperationOverAllClusters(get)) {
getAllClusters(get);
return;
}
Expand All @@ -368,7 +368,7 @@ public void handleGet(Operation get) {
get.fail(Operation.STATUS_CODE_NOT_FOUND);
}

private boolean isOperationOverAllClustes(Operation op) {
private boolean isOperationOverAllClusters(Operation op) {
return PATTERN_CLUSTER_LINK.matcher(op.getUri().getPath()).matches();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,60 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import static com.vmware.admiral.compute.ComputeConstants.HOST_AUTH_CREDENTIALS_PROP_NAME;
import static com.vmware.admiral.compute.ContainerHostService.CONTAINER_HOST_TYPE_PROP_NAME;
import static com.vmware.admiral.compute.ContainerHostService.HOST_DOCKER_ADAPTER_TYPE_PROP_NAME;
import static com.vmware.admiral.test.integration.TestPropertiesUtil.getTestRequiredProp;

import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.vmware.admiral.compute.ComputeConstants;
import com.vmware.admiral.compute.ContainerHostService;
import com.vmware.admiral.common.util.OperationUtil;
import com.vmware.admiral.compute.ContainerHostService.ContainerHostSpec;
import com.vmware.admiral.compute.ContainerHostService.ContainerHostType;
import com.vmware.admiral.compute.PlacementZoneConstants;
import com.vmware.admiral.compute.PlacementZoneConstants.PlacementZoneType;
import com.vmware.admiral.compute.ContainerHostService.DockerAdapterType;
import com.vmware.admiral.compute.cluster.ClusterService;
import com.vmware.admiral.compute.cluster.ClusterService.ClusterDto;
import com.vmware.admiral.compute.kubernetes.KubernetesHostConstants;
import com.vmware.admiral.request.util.TestRequestStateFactory;
import com.vmware.admiral.test.integration.BaseIntegrationSupportIT;
import com.vmware.admiral.test.integration.SimpleHttpsClient;
import com.vmware.admiral.test.integration.SimpleHttpsClient.HttpMethod;
import com.vmware.admiral.test.integration.SimpleHttpsClient.HttpResponse;
import com.vmware.admiral.test.integration.data.IntegratonTestStateFactory;
import com.vmware.photon.controller.model.resources.ComputeService.ComputeState;
import com.vmware.photon.controller.model.resources.ResourcePoolService;
import com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState;
import com.vmware.photon.controller.model.security.util.AuthCredentialsType;
import com.vmware.xenon.common.Utils;
import com.vmware.xenon.services.common.AuthCredentialsService;
import com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState;

/**
* Class for test that adds a k8s host
*/
@RunWith(Parameterized.class)
@Ignore("VBV-1589")
public class ProvisioningOnKubernetesIT extends BaseIntegrationSupportIT {

protected ComputeState kubernetesHostCompute;
protected ClusterDto kubernetesCluster;

private AuthCredentialsServiceState kubernetesHostAuthCredentials;
private AuthCredentialsServiceState kubernetesAuthCredentials;
private ResourcePoolState placementZone;
private final AuthCredentialsType credentialsTypeToUse;

@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
// "PublicKey" should be supported as well, but the caching in the remote service client, disallows adding the same host twice with different authentication type.
return Arrays.asList(new Object[][] {
{ "Password" }
{ "PublicKey" }
});
}

Expand All @@ -72,12 +79,12 @@ public ProvisioningOnKubernetesIT(String credentialsType) {
@After
public void provisioningTearDown() throws Exception {
// remove the host
if (kubernetesHostCompute != null) {
removeHost(kubernetesHostCompute);
if (kubernetesCluster != null) {
removeCluster(kubernetesCluster);
}

if (kubernetesHostAuthCredentials != null) {
delete(kubernetesHostAuthCredentials);
if (kubernetesAuthCredentials != null) {
delete(kubernetesAuthCredentials);
}

if (placementZone != null) {
Expand All @@ -90,86 +97,48 @@ public void testAddKubernetesHost() throws Throwable {
logger.info("********************************************************************");
logger.info("------- Setup: Add CentOS VM as KubernetesHost ComputeState -------");
logger.info("********************************************************************");
logger.info(
"---------- 1. Setup auth credentials for K8S. " + "--------");
kubernetesHostAuthCredentials = createAuthCredentials(credentialsTypeToUse);
kubernetesHostAuthCredentials = postDocument(AuthCredentialsService.FACTORY_LINK,
kubernetesHostAuthCredentials);

assertNotNull("Failed to create host credentials", kubernetesHostAuthCredentials);

String credLink = kubernetesHostAuthCredentials.documentSelfLink;

logger.info("---------- 2. Creating Placement Zone for K8S. --------");
placementZone = createSchedulerPlacementZone();

logger.info("---------- 3. Create Kubernetes Host ComputeState for K8S. --------");
kubernetesHostCompute = createKubernetesHost(getTestRequiredProp("kubernetes.host.address"),
credLink, placementZone.documentSelfLink);

assertNotNull(kubernetesHostCompute);

// TODO: update with check for specific entities
// logger.info("---------- 4. Waiting for data collected container. --------");

// waitFor((v) -> {
// return !listContainers().isEmpty();
// });
//
// assertFalse(listContainers().isEmpty());
//
// waitFor((v) -> {
// ComputeState compute;
// try {
// compute = getDocument(kubernetesHostCompute.documentSelfLink, ComputeState.class);
// int containers = Integer.valueOf(compute.customProperties.get("__Containers"));
// return containers > 0;
// } catch (Exception e) {
// return false;
// }
// });
//
// ComputeState compute = getDocument(kubernetesHostCompute.documentSelfLink,
// ComputeState.class);
// int containers = Integer.valueOf(compute.customProperties.get("__Containers"));
// assertTrue(containers > 0);
logger.info("---------- 1. Setup auth credentials for K8S. " + "--------");
kubernetesAuthCredentials = createAuthCredentials(credentialsTypeToUse);

assertNotNull("Failed to create host credentials", kubernetesAuthCredentials);

logger.info("---------- 2. Create Kubernetes Cluster. --------");
kubernetesCluster = createKubernetesCluster(
getTestRequiredProp("kubernetes.host.address"),
kubernetesAuthCredentials.documentSelfLink, null);

assertNotNull(kubernetesCluster);
}

@Test
public void testAddKubernetesHostWithInvalidNamespace() throws Exception {
logger.info("********************************************************************");
logger.info("------- Setup: Add CentOS VM as KubernetesHost ComputeState -------");
logger.info("********************************************************************");
logger.info(
"---------- 1. Setup auth credentials for K8S. " + "--------");
kubernetesHostAuthCredentials = createAuthCredentials(credentialsTypeToUse);
kubernetesHostAuthCredentials = postDocument(AuthCredentialsService.FACTORY_LINK,
kubernetesHostAuthCredentials);

assertNotNull("Failed to create host credentials", kubernetesHostAuthCredentials);
logger.info("---------- 1. Setup auth credentials for K8S. " + "--------");

String credLink = kubernetesHostAuthCredentials.documentSelfLink;
kubernetesAuthCredentials = createAuthCredentials(credentialsTypeToUse);
assertNotNull("Failed to create host credentials", kubernetesAuthCredentials);

logger.info("---------- 2. Create Kubernetes Host ComputeState for K8S. --------");

ComputeState compute = IntegratonTestStateFactory.createKubernetesComputeHost();
compute.address = getTestRequiredProp("kubernetes.host.address");
compute.customProperties.put(KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME,
"Invalid");

// link credentials to host
compute.customProperties.put(ComputeConstants.HOST_AUTH_CREDENTIALS_PROP_NAME, credLink);
Map<String, String> invalidProps = Collections.singletonMap(
KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME, "Invalid");

try {
kubernetesHostCompute = addHost(compute);
fail("Add host with invalid namespace was supposed to fail");
} catch (Throwable t) {
kubernetesCluster = createKubernetesCluster(
getTestRequiredProp("kubernetes.host.address"),
kubernetesAuthCredentials.documentSelfLink,
invalidProps);
fail("Add cluster with invalid namespace was supposed to fail");
} catch (IllegalArgumentException e) {
}
assertNull(kubernetesHostCompute);
assertNull(kubernetesCluster);
}

private static AuthCredentialsServiceState createAuthCredentials(
AuthCredentialsType credentialsTypeToUse) {
AuthCredentialsType credentialsTypeToUse) throws Exception {
AuthCredentialsServiceState authCredentials = new AuthCredentialsServiceState();
authCredentials.type = credentialsTypeToUse.name();
if (AuthCredentialsType.Password.equals(credentialsTypeToUse)) {
Expand All @@ -184,35 +153,58 @@ private static AuthCredentialsServiceState createAuthCredentials(
throw new IllegalArgumentException(
"Unsupported credentials type: " + credentialsTypeToUse);
}
return authCredentials;
return postDocument(AuthCredentialsService.FACTORY_LINK, authCredentials);
}

private static ComputeState createKubernetesHost(String address, String credLink,
String placementZoneLink)
throws Exception {
ComputeState compute = IntegratonTestStateFactory.createKubernetesComputeHost();
private static ClusterDto createKubernetesCluster(String address, String credLink,
Map<String, String> customProperties) throws Exception {

ComputeState compute = new ComputeState();
compute.address = address;
compute.resourcePoolLink = placementZoneLink;

// link credentials to host
compute.customProperties.put(ComputeConstants.HOST_AUTH_CREDENTIALS_PROP_NAME, credLink);
compute.customProperties.put(ContainerHostService.CONTAINER_HOST_TYPE_PROP_NAME,
compute.customProperties = new HashMap<>();
if (customProperties != null) {
compute.customProperties.putAll(customProperties);
}
compute.customProperties.put(HOST_AUTH_CREDENTIALS_PROP_NAME, credLink);
compute.customProperties.put(CONTAINER_HOST_TYPE_PROP_NAME,
ContainerHostType.KUBERNETES.name());
return addHost(compute);
compute.customProperties.put(HOST_DOCKER_ADAPTER_TYPE_PROP_NAME,
DockerAdapterType.API.name());

return addCluster(compute);
}

private ResourcePoolState createSchedulerPlacementZone() throws Throwable {
private static ClusterDto addCluster(ComputeState compute) throws Exception {
ContainerHostSpec hostSpec = new ContainerHostSpec();
hostSpec.acceptCertificate = true;
hostSpec.hostState = compute;

Map<String, String> headers = Collections.singletonMap(OperationUtil.PROJECT_ADMIRAL_HEADER,
"/projects/test");
HttpResponse httpResponse = SimpleHttpsClient.execute(HttpMethod.POST,
getBaseUrl() + buildServiceUri(ClusterService.SELF_LINK),
Utils.toJson(hostSpec), headers, null);

if (HttpURLConnection.HTTP_OK != httpResponse.statusCode) {
throw new IllegalArgumentException("Add cluster failed with status code: "
+ httpResponse.statusCode);
}

ResourcePoolState placementZone = TestRequestStateFactory.createResourcePool();
placementZone.id = "k8s-pzone";
placementZone.name = placementZone.id;
placementZone.documentSelfLink = ResourcePoolService.FACTORY_LINK + "/" + placementZone.id;

placementZone.customProperties = new HashMap<>();
placementZone.customProperties.put(
PlacementZoneConstants.PLACEMENT_ZONE_TYPE_CUSTOM_PROP_NAME,
PlacementZoneType.SCHEDULER.toString());
return Utils.fromJson(httpResponse.responseBody, ClusterDto.class);
}

return postDocument(ResourcePoolService.FACTORY_LINK, placementZone);
private void removeCluster(ClusterDto cluster) throws Exception {
logger.info("---------- Request remove cluster. --------");
if (cluster != null) {
String body = sendRequest(HttpMethod.GET, cluster.documentSelfLink, null);
if (body != null && !body.isEmpty()) {
// cluster is found, remove it
delete(cluster);
} else {
logger.info("Cluster not found. Skipping removal");
}
}
}
}

0 comments on commit af336e2

Please sign in to comment.