Exemple #1
0
def test_chart_installation(config_map):
    builder = ChartBuilder(
        ChartInfo(
            api_version="3.2.4",
            name="test",
            version="0.1.0",
            app_version="v1",
            maintainers=[
                ChartMaintainer("A Name Jr.", "*****@*****.**",
                                "www.example.com")
            ],
        ),
        [config_map],
    )
    assert not builder.is_installed
    with ChartInstallationContext(builder):
        # Check helm release
        helm_installation = get_helm_installations()
        assert helm_installation["NAME"][0] == "test"
        assert helm_installation["REVISION"][0] == "1"
        assert helm_installation["STATUS"][0] == "deployed"

        assert builder.is_installed

        config_maps = kubectl_get("configmaps")
        assert config_maps["NAME"][0] == "test-config-map"
        assert config_maps["DATA"][0] == "1"
Exemple #2
0
def test_replica_set(chart_info, replica_set):
    builder = ChartBuilder(chart_info, [replica_set])
    with ChartInstallationContext(builder):
        replica_set_info = kubectl_get("replicasets")
        assert replica_set_info["NAME"][0] == "test-replica-set"
        assert replica_set_info["DESIRED"][0] == "1"
        assert replica_set_info["CURRENT"][0] == "1"
Exemple #3
0
def test_create_pod_template(chart_info: ChartInfo, pod_template: PodTemplate):
    builder = ChartBuilder(chart_info, [pod_template])
    with ChartInstallationContext(builder):
        template_info = kubectl_get("podtemplates")
        assert template_info["NAME"][0] == "test-pod-template"
        assert template_info["CONTAINERS"][0] == "test-container-0"
        assert template_info["IMAGES"][0] == "k8s.gcr.io/echoserver:1.4"
Exemple #4
0
def test_object_meta(chart_info: ChartInfo, object_meta: ObjectMeta):
    config_map = ConfigMap(object_meta, {"test": "1"},)
    builder = ChartBuilder(chart_info, [config_map])
    with ChartInstallationContext(builder):
        config_maps = kubectl_get("configmaps")
        assert config_maps["NAME"][0] == config_map.metadata.name
        assert config_maps["DATA"][0] == str(len(config_map.data))
Exemple #5
0
def test_controller_revision(chart_info, controller_revision):
    builder = ChartBuilder(chart_info, [controller_revision])
    with ChartInstallationContext(builder):
        controller_revision_info = kubectl_get("controllerrevisions")
        assert controller_revision_info["NAME"][
            0] == "test-controller-revision"
        assert controller_revision_info["REVISION"][0] == "1"
Exemple #6
0
def test_replication_controller(chart_info, replication_controller):
    builder = ChartBuilder(chart_info, [replication_controller])
    with ChartInstallationContext(builder):
        replication_info = kubectl_get("replicationcontrollers")
        assert replication_info["NAME"][0] == "test-replication-controller"
        assert replication_info["DESIRED"][0] == "1"
        assert replication_info["CURRENT"][0] == "1"
Exemple #7
0
def test_deployment(
    chart_info,
    test_labels,
    pod_template_spec,
    selector,
    deployment_strategy: Optional[DeploymentStrategy],
):
    builder = ChartBuilder(
        chart_info,
        [
            Deployment(
                metadata=ObjectMeta(name="test-deployment",
                                    labels=test_labels),
                spec=DeploymentSpec(
                    replicas=1,
                    template=pod_template_spec,
                    selector=selector,
                    strategy=deployment_strategy,
                ),
            )
        ],
    )
    with ChartInstallationContext(builder):
        deployment_info = kubectl_get("deployments")
        assert deployment_info["NAME"][0] == "test-deployment"
        assert deployment_info["READY"][0] == "1/1"
Exemple #8
0
def test_api_resource(chart_info):
    builder = ChartBuilder(
        chart_info,
        [APIResource("test", None, None, None, None, None, None, None, None)],  # type: ignore
    )
    with ChartInstallationContext(builder):
        print(kubectl_get("apiresource"))
Exemple #9
0
def test_empty_persistent_volume_claim(chart_info,
                                       empty_persistent_volume_claim):
    builder = ChartBuilder(chart_info, [empty_persistent_volume_claim])
    with ChartInstallationContext(builder):
        volume_info = kubectl_get("persistentvolumeclaims")
        assert volume_info["NAME"][0] == "test-persistent-volume-claim"
        assert volume_info["CAPACITY"][0] == "1"
        assert volume_info["ACCESS MODES"][0] == modes_expected_value
def test_role_binding_to_service_account(
    chart_info, role, role_binding_to_service_account, empty_service_account
):
    builder = ChartBuilder(
        chart_info, [role, role_binding_to_service_account, empty_service_account]
    )
    with ChartInstallationContext(builder):
        bindings = kubectl_get("rolebinding")
        assert bindings["NAME"][0] == "test-role-binding"
        assert bindings["ROLE"][0] == "Role/test-role"
Exemple #11
0
def test_container_probes(chart_info, probe: Probe):
    pod = get_pod_with_options(readiness_probe=probe)
    builder = ChartBuilder(chart_info, [pod])
    with ChartInstallationContext(builder,
                                  expected_status={"1/1"},
                                  status_field="READY"):
        pod_info = kubectl_get("pods")
        assert pod_info["NAME"][0] == "test-pod"
        assert pod_info["READY"][0] == "1/1"
        assert pod_info["STATUS"][0] == "Running"
def test_cluster_role_binding(chart_info, cluster_role_binding: ClusterRoleBinding):
    builder = ChartBuilder(chart_info, [cluster_role_binding, CLUSTER_ROLE])
    with ChartInstallationContext(builder):
        cluster_role_binding_info = kubectl_get("clusterrolebinding")
        info_frame = DataFrame(cluster_role_binding_info)
        info_frame = info_frame[
            info_frame["NAME"] == cluster_role_binding.metadata.name
        ].reset_index()
        assert info_frame["NAME"][0] == cluster_role_binding.metadata.name
        assert info_frame["ROLE"][0] == f"ClusterRole/{CLUSTER_ROLE.metadata.name}"
Exemple #13
0
def test_csi_driver(chart_info, driver: CSIDriver):
    builder = ChartBuilder(chart_info, [driver])
    with ChartInstallationContext(builder):
        driver_info = kubectl_get("csidriver")
        assert driver_info["NAME"][0] == driver.metadata.name
        assert (driver_info["ATTACHREQUIRED"][0] == str(
            bool(driver.spec.attachRequired)).lower()
                if driver.spec.attachRequired is not None else "true")
        assert driver_info["MODES"][0] == (driver.spec.volumeLifecycleModes[0]
                                           if driver.spec.volumeLifecycleModes
                                           else "Persistent")
Exemple #14
0
def test_pod(chart_info, pod: Pod,
             other_resources: List[KubernetesBaseObject]):
    if other_resources is None:
        other_resources = []
    builder = ChartBuilder(chart_info, other_resources + [pod])
    with ChartInstallationContext(builder):
        pod_info = DataFrame(kubectl_get("pods"))
        pod_info = pod_info[pod_info["NAME"] == pod.metadata.name].reset_index(
            drop=True)
        assert pod_info["NAME"][0] == pod.metadata.name
        assert pod_info["READY"][0] == "1/1"
        assert pod_info["STATUS"][0] == "Running"
Exemple #15
0
def test_storage_class(chart_info, storage_class: StorageClass):
    builder = ChartBuilder(
        chart_info,
        [storage_class],
    )
    with ChartInstallationContext(builder):
        storage_class_info_dict = DataFrame(kubectl_get("storageclass"))
        filtered = storage_class_info_dict[
            storage_class_info_dict["NAME"] ==
            storage_class.metadata.name].reset_index()
        assert filtered["NAME"][0] == storage_class.metadata.name
        assert filtered["PROVISIONER"][0] == storage_class.provisioner
Exemple #16
0
def test_csi_node(chart_info, csi_node: CSINode):
    builder = ChartBuilder(
        chart_info,
        [csi_node],
    )
    with ChartInstallationContext(builder):
        csi_node_info = kubectl_get("csinode")
        csi_frame = DataFrame(csi_node_info)
        csi_frame = csi_frame[csi_frame["NAME"] != "minikube"].reset_index(
            drop=True)
        assert csi_frame["NAME"][0] == csi_node.metadata.name
        assert csi_frame["DRIVERS"][0] == str(len(csi_node.spec.drivers))
Exemple #17
0
def test_volume_attachment(chart_info, volume_attachment: VolumeAttachment):
    builder = ChartBuilder(chart_info, [volume_attachment])
    with ChartInstallationContext(builder):
        volume_attachment_info = kubectl_get("volumeattachment")
        assert volume_attachment_info["NAME"][
            0] == volume_attachment.metadata.name
        assert volume_attachment_info["ATTACHER"][
            0] == volume_attachment.spec.attacher
        source = volume_attachment.spec.source
        assert volume_attachment_info["PV"][0] == (
            source.persistentVolumeName
            if source.persistentVolumeName is not None else "")
Exemple #18
0
def test_persistent_volume_on_pod(chart_info, persistent_volume,
                                  persistent_volume_claim,
                                  mounts_or_devices: dict):
    builder = ChartBuilder(
        chart_info,
        [
            persistent_volume,
            get_pod_with_options(
                Volume(
                    "test-volume",
                    persistent_volume_claim=PersistentVolumeClaimVolumeSource(
                        persistent_volume_claim.metadata.name, read_only=True),
                ),
                # command=["bash", "-c", "mkdir ~/tmp; nginx -g 'daemon off;'"],
                **mounts_or_devices,
            ),
            persistent_volume_claim,
        ],
    )
    with ChartInstallationContext(builder):
        # Check pod ready
        pod_info = kubectl_get("pods", wide=True)
        assert pod_info["NAME"][0] == "test-pod"
        assert pod_info["READY"][0] == "1/1"

        # Check volume bound
        volume_info = kubectl_get("persistentvolume")
        assert volume_info["NAME"][0] == "test-persistent-volume"
        assert volume_info["CAPACITY"][0] == "1"
        assert volume_info["ACCESS MODES"][0] == "RWX"
        assert volume_info["RECLAIM POLICY"][0] == "Retain"
        assert volume_info["STATUS"][0] == "Bound"
        assert volume_info["CLAIM"][0] == "default/test-pv-claim"

        claim_info = kubectl_get("persistentvolumeclaims")
        assert claim_info["NAME"][0] == "test-pv-claim"
        assert claim_info["STATUS"][0] == "Bound"
        assert claim_info["CAPACITY"][0] == "1"
        assert claim_info["ACCESS MODES"][0] == "RWX"
Exemple #19
0
def test_persistent_volume(chart_info, persistent_volume_spec):
    persistent_volume = PersistentVolume(
        ObjectMeta(name="test-persistent-volume"), persistent_volume_spec)
    builder = ChartBuilder(
        chart_info,
        [persistent_volume],
    )
    with ChartInstallationContext(builder):
        volume_info = kubectl_get("persistentvolumes")
        assert volume_info["NAME"][0] == persistent_volume.metadata.name
        assert persistent_volume.spec.capacity is not None
        assert volume_info["CAPACITY"][0] == str(
            persistent_volume.spec.capacity["storage"])
        assert volume_info["ACCESS MODES"][0] == modes_expected_value
Exemple #20
0
def test_ingress(chart_info, ingress_spec: IngressSpec):
    ingress = Ingress(ObjectMeta(name="test-ingress"), ingress_spec)
    builder = ChartBuilder(chart_info, [ingress])
    with ChartInstallationContext(builder):
        ingress_info = kubectl_get("ingress")
        print(ingress_info)
        assert ingress_info["NAME"][0] == ingress.metadata.name
        assert (
            ingress_info["CLASS"][0] == ingress.spec.ingressClassName
            if ingress.spec.ingressClassName
            else "<none>"
        )
        assert ingress_info["HOSTS"][0] == "*"
        assert ingress_info["PORTS"][0] == "80" if not ingress.spec.tls else "80, 443"
Exemple #21
0
def test_priority_class(chart_info, priority_class):
    builder = ChartBuilder(chart_info, [priority_class])
    with ChartInstallationContext(builder):
        class_info = kubectl_get("priorityclass")
        class_info_frame = DataFrame(class_info)
        filter_frame = class_info_frame[
            class_info_frame["NAME"] == priority_class.metadata.name
        ].reset_index()
        assert filter_frame["NAME"][0] == priority_class.metadata.name
        assert filter_frame["VALUE"][0] == str(priority_class.value)
        assert (
            filter_frame["GLOBAL-DEFAULT"][0]
            == str(bool(priority_class.globalDefault)).lower()
        )
Exemple #22
0
def test_lease(chart_info, holder_identity):
    builder = ChartBuilder(
        chart_info,
        [
            Lease(
                ObjectMeta(name="test-lease"),
                LeaseSpec(holder_identity=holder_identity),
            )
        ],
    )
    with ChartInstallationContext(builder):
        lease_info = kubectl_get("lease")
        assert lease_info["NAME"][0] == "test-lease"
        assert lease_info["HOLDER"][0] == (holder_identity
                                           if holder_identity else "")
Exemple #23
0
def test_daemon_set(
    chart_info,
    pod_template_spec,
    selector,
    update_strategy: Optional[DaemonSetUpdateStrategy],
):
    builder = ChartBuilder(
        chart_info,
        [
            DaemonSet(
                ObjectMeta(name="test-daemon-set"),
                DaemonSetSpec(pod_template_spec,
                              selector,
                              update_strategy=update_strategy),
            )
        ],
    )
    with ChartInstallationContext(builder):
        daemon_set_info = kubectl_get("daemonsets")
        assert daemon_set_info["NAME"][0] == "test-daemon-set"
        assert daemon_set_info["DESIRED"][0] == "1"
        assert daemon_set_info["CURRENT"][0] == "1"
Exemple #24
0
def test_installing_two_components(
    config_map,
    config_map2,
    chart_info: ChartInfo,
):
    config_map.metadata.name = "test-config-map-1"
    builder = ChartBuilder(
        chart_info,
        [config_map, config_map2],
    )
    with ChartInstallationContext(builder):
        # Check helm release
        helm_installation = get_helm_installations()
        assert helm_installation["NAME"][0] == "test"
        assert helm_installation["REVISION"][0] == "1"
        assert helm_installation["STATUS"][0] == "deployed"

        # Check kubernetes components
        config_maps = kubectl_get("configmaps")
        for i in range(2):
            assert config_maps["NAME"][i] == f"test-config-map-{i + 1}"
            assert config_maps["DATA"][i] == "1"
Exemple #25
0
def test_stateful_set(
    chart_info,
    pod_template_spec,
    selector,
    update_strategy: Optional[StatefulSetUpdateStrategy],
):
    builder = ChartBuilder(
        chart_info,
        [
            StatefulSet(
                ObjectMeta(name="test-stateful-set"),
                StatefulSetSpec(
                    pod_template_spec,
                    selector,
                    "my-service",
                    update_strategy=update_strategy,
                ),
            )
        ],
    )
    with ChartInstallationContext(builder):
        stateful_set_info = kubectl_get("statefulsets")
        assert stateful_set_info["NAME"][0] == "test-stateful-set"
        assert stateful_set_info["READY"][0] == "1/1"
Exemple #26
0
def test_job(chart_info, job_spec):
    builder = ChartBuilder(chart_info,
                           [Job(ObjectMeta(name="test-job"), job_spec)])
    with ChartInstallationContext(builder):
        job_info = kubectl_get("job")
        assert job_info["NAME"][0] == "test-job"
Exemple #27
0
def test_cron_job(chart_info, cron_job):
    builder = ChartBuilder(chart_info, [cron_job])
    with ChartInstallationContext(builder):
        cron_job_info = kubectl_get("cronjob")
        assert cron_job_info["NAME"][0] == "test-cron-job"
        assert cron_job_info["SCHEDULE"][0] == "23 * * * 6"
Exemple #28
0
def test_api_group(chart_info, api_group: APIGroup):
    builder = ChartBuilder(chart_info, [api_group])
    with ChartInstallationContext(builder):
        print(kubectl_get("apigroup"))
Exemple #29
0
def test_status_details(chart_info):
    builder = ChartBuilder(chart_info, [StatusDetails("test", None, None)])  # type: ignore
    with ChartInstallationContext(builder):
        print(kubectl_get("statusdetails"))
Exemple #30
0
def test_custom_resource(chart_info, custom_resource: CustomResourceDefinition):
    builder = ChartBuilder(chart_info, [custom_resource])
    with ChartInstallationContext(builder):
        custom_resource_info = kubectl_get("customresourcedefinitions")
        assert custom_resource_info["NAME"][0] == "tests.customs.com"