|
25 | 25 | import static org.junit.Assert.assertNotNull; |
26 | 26 | import static org.junit.Assert.assertTrue; |
27 | 27 |
|
| 28 | +import java.io.ByteArrayInputStream; |
28 | 29 | import java.net.URI; |
29 | 30 | import java.util.List; |
30 | 31 | import java.util.Map; |
|
64 | 65 | import io.fabric8.kubernetes.api.model.ContainerStatus; |
65 | 66 | import io.fabric8.kubernetes.api.model.EnvVar; |
66 | 67 | import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 68 | +import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; |
| 69 | +import io.fabric8.kubernetes.api.model.PersistentVolumeClaimList; |
67 | 70 | import io.fabric8.kubernetes.api.model.Pod; |
68 | 71 | import io.fabric8.kubernetes.api.model.PodList; |
69 | 72 | import io.fabric8.kubernetes.api.model.Quantity; |
70 | 73 | import io.fabric8.kubernetes.api.model.ResourceRequirements; |
| 74 | +import io.fabric8.kubernetes.api.model.Secret; |
71 | 75 | import io.fabric8.kubernetes.api.model.SecretBuilder; |
| 76 | +import io.fabric8.kubernetes.api.model.SecretList; |
72 | 77 | import io.fabric8.kubernetes.api.model.Service; |
73 | 78 | import io.fabric8.kubernetes.api.model.ServiceAccountBuilder; |
74 | 79 | import io.fabric8.kubernetes.api.model.ServiceList; |
@@ -474,7 +479,7 @@ public void launchProxyWithPodPatches() throws Exception { |
474 | 479 | } |
475 | 480 |
|
476 | 481 | /** |
477 | | - * Test whethet the merging of properties works properly |
| 482 | + * Test whether the merging of properties works properly |
478 | 483 | */ |
479 | 484 | @Test |
480 | 485 | public void launchProxyWithPatchesWithMerging() throws Exception { |
@@ -524,6 +529,169 @@ public void launchProxyWithPatchesWithMerging() throws Exception { |
524 | 529 | assertEquals(0, proxyService.getProxies(null, true).size()); |
525 | 530 | } |
526 | 531 |
|
| 532 | + /** |
| 533 | + * Tests the creation and deleting of additional manifests. |
| 534 | + * The first manifest contains a namespace definition. |
| 535 | + * The second manifest does not contain a namespace definition, but in the end should have the same namespace as the pod. |
| 536 | + */ |
| 537 | + @Test |
| 538 | + public void launchProxyWithAdditionalManifests() throws Exception { |
| 539 | + final String overridenNamespace = "it-b9fa0a24-overriden"; |
| 540 | + try { |
| 541 | + System.out.println(client); |
| 542 | + client.namespaces().create(new NamespaceBuilder() |
| 543 | + .withNewMetadata() |
| 544 | + .withName(overridenNamespace) |
| 545 | + .endMetadata() |
| 546 | + .build()); |
| 547 | + |
| 548 | + String specId = environment.getProperty("proxy.specs[8].id"); |
| 549 | + |
| 550 | + ProxySpec baseSpec = proxyService.findProxySpec(s -> s.getId().equals(specId), true); |
| 551 | + ProxySpec spec = proxyService.resolveProxySpec(baseSpec, null, null); |
| 552 | + Proxy proxy = proxyService.startProxy(spec, true); |
| 553 | + String containerId = proxy.getContainers().get(0).getId(); |
| 554 | + |
| 555 | + PodList podList = client.pods().inNamespace(overridenNamespace).list(); |
| 556 | + assertEquals(1, podList.getItems().size()); |
| 557 | + Pod pod = podList.getItems().get(0); |
| 558 | + assertEquals("Running", pod.getStatus().getPhase()); |
| 559 | + assertEquals(overridenNamespace, pod.getMetadata().getNamespace()); |
| 560 | + assertEquals("sp-pod-" + containerId, pod.getMetadata().getName()); |
| 561 | + assertEquals(1, pod.getStatus().getContainerStatuses().size()); |
| 562 | + ContainerStatus container = pod.getStatus().getContainerStatuses().get(0); |
| 563 | + assertEquals(true, container.getReady()); |
| 564 | + assertEquals("openanalytics/shinyproxy-demo:latest", container.getImage()); |
| 565 | + |
| 566 | + PersistentVolumeClaimList claimList = client.persistentVolumeClaims().inNamespace(overridenNamespace).list(); |
| 567 | + assertEquals(1, claimList.getItems().size()); |
| 568 | + PersistentVolumeClaim claim = claimList.getItems().get(0); |
| 569 | + assertEquals(overridenNamespace, claim.getMetadata().getNamespace()); |
| 570 | + assertEquals("manifests-pvc", claim.getMetadata().getName()); |
| 571 | + |
| 572 | + // secret has no namespace defined -> should be created in the namespace defined by the pod patches |
| 573 | + SecretList sercetList = client.secrets().inNamespace(overridenNamespace).list(); |
| 574 | + assertEquals(2, sercetList.getItems().size()); |
| 575 | + for (Secret secret : sercetList.getItems()) { |
| 576 | + if (secret.getMetadata().getName().startsWith("default-token")) { |
| 577 | + continue; |
| 578 | + } |
| 579 | + assertEquals(overridenNamespace, secret.getMetadata().getNamespace()); |
| 580 | + assertEquals("manifests-secret", secret.getMetadata().getName()); |
| 581 | + } |
| 582 | + |
| 583 | + proxyService.stopProxy(proxy, false, true); |
| 584 | + |
| 585 | + // Give Kube the time to clean |
| 586 | + Thread.sleep(2000); |
| 587 | + |
| 588 | + // all pods should be deleted |
| 589 | + podList = client.pods().inNamespace(session.getNamespace()).list(); |
| 590 | + assertEquals(0, podList.getItems().size()); |
| 591 | + // all additional manifests should be deleted |
| 592 | + assertEquals(1, client.secrets().inNamespace(overridenNamespace).list().getItems().size()); |
| 593 | + assertTrue(client.secrets().inNamespace(overridenNamespace).list() |
| 594 | + .getItems().get(0).getMetadata().getName().startsWith("default-token")); |
| 595 | + assertEquals(0, client.persistentVolumeClaims().inNamespace(overridenNamespace).list().getItems().size()); |
| 596 | + |
| 597 | + assertEquals(0, proxyService.getProxies(null, true).size()); |
| 598 | + } finally { |
| 599 | + // just to be sure both the namespace and service account are cleaned up |
| 600 | + client.namespaces().withName(overridenNamespace).delete(); |
| 601 | + } |
| 602 | + } |
| 603 | + |
| 604 | + /** |
| 605 | + * Tests the creation and deleting of additional manifests. |
| 606 | + * The first manifest contains a namespace definition. |
| 607 | + * The second manifest does not contain a namespace definition, but in the end should have the same namespace as the pod. |
| 608 | + * |
| 609 | + * This is exactly the same test as the previous one, except that the PVC already exists (and should not be re-created). |
| 610 | + */ |
| 611 | + @Test |
| 612 | + public void launchProxyWithAdditionalManifestsOfWhichOneAlreadyExists() throws Exception { |
| 613 | + final String overridenNamespace = "it-b9fa0a24-overriden"; |
| 614 | + try { |
| 615 | + System.out.println(client); |
| 616 | + client.namespaces().create(new NamespaceBuilder() |
| 617 | + .withNewMetadata() |
| 618 | + .withName(overridenNamespace) |
| 619 | + .endMetadata() |
| 620 | + .build()); |
| 621 | + |
| 622 | + // create the PVC |
| 623 | + String pvcSpec = |
| 624 | + "apiVersion: v1\n" + |
| 625 | + "kind: PersistentVolumeClaim\n" + |
| 626 | + "metadata:\n" + |
| 627 | + " name: manifests-pvc\n" + |
| 628 | + " namespace: it-b9fa0a24-overriden\n" + |
| 629 | + "spec:\n" + |
| 630 | + " storageClassName: standard\n" + |
| 631 | + " accessModes:\n" + |
| 632 | + " - ReadWriteOnce\n" + |
| 633 | + " resources:\n" + |
| 634 | + " requests:\n" + |
| 635 | + " storage: 5Gi"; |
| 636 | + |
| 637 | + client.load(new ByteArrayInputStream(pvcSpec.getBytes())).createOrReplace(); |
| 638 | + |
| 639 | + String specId = environment.getProperty("proxy.specs[8].id"); |
| 640 | + |
| 641 | + ProxySpec baseSpec = proxyService.findProxySpec(s -> s.getId().equals(specId), true); |
| 642 | + ProxySpec spec = proxyService.resolveProxySpec(baseSpec, null, null); |
| 643 | + Proxy proxy = proxyService.startProxy(spec, true); |
| 644 | + String containerId = proxy.getContainers().get(0).getId(); |
| 645 | + |
| 646 | + PodList podList = client.pods().inNamespace(overridenNamespace).list(); |
| 647 | + assertEquals(1, podList.getItems().size()); |
| 648 | + Pod pod = podList.getItems().get(0); |
| 649 | + assertEquals("Running", pod.getStatus().getPhase()); |
| 650 | + assertEquals(overridenNamespace, pod.getMetadata().getNamespace()); |
| 651 | + assertEquals("sp-pod-" + containerId, pod.getMetadata().getName()); |
| 652 | + assertEquals(1, pod.getStatus().getContainerStatuses().size()); |
| 653 | + ContainerStatus container = pod.getStatus().getContainerStatuses().get(0); |
| 654 | + assertEquals(true, container.getReady()); |
| 655 | + assertEquals("openanalytics/shinyproxy-demo:latest", container.getImage()); |
| 656 | + |
| 657 | + PersistentVolumeClaimList claimList = client.persistentVolumeClaims().inNamespace(overridenNamespace).list(); |
| 658 | + assertEquals(1, claimList.getItems().size()); |
| 659 | + PersistentVolumeClaim claim = claimList.getItems().get(0); |
| 660 | + assertEquals(overridenNamespace, claim.getMetadata().getNamespace()); |
| 661 | + assertEquals("manifests-pvc", claim.getMetadata().getName()); |
| 662 | + |
| 663 | + // secret has no namespace defined -> should be created in the namespace defined by the pod patches |
| 664 | + SecretList sercetList = client.secrets().inNamespace(overridenNamespace).list(); |
| 665 | + assertEquals(2, sercetList.getItems().size()); |
| 666 | + for (Secret secret : sercetList.getItems()) { |
| 667 | + if (secret.getMetadata().getName().startsWith("default-token")) { |
| 668 | + continue; |
| 669 | + } |
| 670 | + assertEquals(overridenNamespace, secret.getMetadata().getNamespace()); |
| 671 | + assertEquals("manifests-secret", secret.getMetadata().getName()); |
| 672 | + } |
| 673 | + |
| 674 | + proxyService.stopProxy(proxy, false, true); |
| 675 | + |
| 676 | + // Give Kube the time to clean |
| 677 | + Thread.sleep(2000); |
| 678 | + |
| 679 | + // all pods should be deleted |
| 680 | + podList = client.pods().inNamespace(session.getNamespace()).list(); |
| 681 | + assertEquals(0, podList.getItems().size()); |
| 682 | + // all additional manifests should be deleted |
| 683 | + assertEquals(1, client.secrets().inNamespace(overridenNamespace).list().getItems().size()); |
| 684 | + assertTrue(client.secrets().inNamespace(overridenNamespace).list() |
| 685 | + .getItems().get(0).getMetadata().getName().startsWith("default-token")); |
| 686 | + assertEquals(0, client.persistentVolumeClaims().inNamespace(overridenNamespace).list().getItems().size()); |
| 687 | + |
| 688 | + assertEquals(0, proxyService.getProxies(null, true).size()); |
| 689 | + } finally { |
| 690 | + // just to be sure both the namespace and service account are cleaned up |
| 691 | + client.namespaces().withName(overridenNamespace).delete(); |
| 692 | + } |
| 693 | + } |
| 694 | + |
527 | 695 | public static class TestConfiguration { |
528 | 696 | @Bean |
529 | 697 | @Primary |
|
0 commit comments