Example #1
0
def test_certificate_signing_request(chart_info):
    signing_request = CertificateSigningRequest(
        ObjectMeta(name="signing-request"),
        CertificateSigningRequestSpec(
            "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQ1ZqQ0NBVDRDQV"
            "FBd0VURVBNQTBHQTFVRUF3d0dZVzVuWld4aE1JSUJJakFOQmdrcWhraUc5dzBCQVFF"
            "RgpBQU9DQVE4QU1JSUJDZ0tDQVFFQTByczhJTHRHdTYxakx2dHhWTTJSVlRWMDNHWl"
            "JTWWw0dWluVWo4RElaWjBOCnR2MUZtRVFSd3VoaUZsOFEzcWl0Qm0wMUFSMkNJVXBG"
            "d2ZzSjZ4MXF3ckJzVkhZbGlBNVhwRVpZM3ExcGswSDQKM3Z3aGJlK1o2MVNrVHF5SV"
            "BYUUwrTWM5T1Nsbm0xb0R2N0NtSkZNMUlMRVI3QTVGZnZKOEdFRjJ6dHBoaUlFMwpu"
            "b1dtdHNZb3JuT2wzc2lHQ2ZGZzR4Zmd4eW8ybmlneFNVekl1bXNnVm9PM2ttT0x1RV"
            "F6cXpkakJ3TFJXbWlECklmMXBMWnoyalVnald4UkhCM1gyWnVVV1d1T09PZnpXM01L"
            "aE8ybHEvZi9DdS8wYk83c0x0MCt3U2ZMSU91TFcKcW90blZtRmxMMytqTy82WDNDKz"
            "BERHk5aUtwbXJjVDBnWGZLemE1dHJRSURBUUFCb0FBd0RRWUpLb1pJaHZjTgpBUUVM"
            "QlFBRGdnRUJBR05WdmVIOGR4ZzNvK21VeVRkbmFjVmQ1N24zSkExdnZEU1JWREkyQT"
            "Z1eXN3ZFp1L1BVCkkwZXpZWFV0RVNnSk1IRmQycVVNMjNuNVJsSXJ3R0xuUXFISUh5"
            "VStWWHhsdnZsRnpNOVpEWllSTmU3QlJvYXgKQVlEdUI5STZXT3FYbkFvczFqRmxNUG"
            "5NbFpqdU5kSGxpT1BjTU1oNndLaTZzZFhpVStHYTJ2RUVLY01jSVUyRgpvU2djUWdM"
            "YTk0aEpacGk3ZnNMdm1OQUxoT045UHdNMGM1dVJVejV4T0dGMUtCbWRSeEgvbUNOS2"
            "JKYjFRQm1HCkkwYitEUEdaTktXTU0xMzhIQXdoV0tkNjVoVHdYOWl4V3ZHMkh4TG1W"
            "Qzg0L1BHT0tWQW9FNkpsYWFHdTlQVmkKdjlOSjVaZlZrcXdCd0hKbzZXdk9xVlA3SV"
            "FjZmg3d0drWm89Ci0tLS0tRU5EIENFUlRJRklDQVRFIFJFUVVFU1QtLS0tLQo="),
    )
    builder = ChartBuilder(
        chart_info,
        [signing_request],
    )
    with ChartInstallationContext(builder):
        certificate_signing_request_frame = DataFrame(
            kubectl_get("certificatesigningrequest"))
        certificate_signing_request_frame = certificate_signing_request_frame[
            certificate_signing_request_frame["NAME"] ==
            signing_request.metadata.name].reset_index()
        assert certificate_signing_request_frame["NAME"][
            0] == "signing-request"
Example #2
0
def test_endpoints_with_subset(chart_info: ChartInfo,
                               endpoints_with_subset: Endpoints):
    builder = ChartBuilder(chart_info, [endpoints_with_subset])
    with ChartInstallationContext(builder):
        endpoints_info = get_endpoints_info()
        assert endpoints_info["NAME"][0] == "test-endpoints"
        assert endpoints_info["ENDPOINTS"][0] == "10.9.8.7"
Example #3
0
def test_eviction(chart_info):
    builder = ChartBuilder(
        chart_info,
        [Eviction(ObjectMeta(name="test-eviction"), None)],  # type: ignore
    )
    with ChartInstallationContext(builder):
        kubectl_get("eviction")
Example #4
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"))
Example #5
0
def test_network_policy(
    chart_info,
    name: str,
    selector: Optional[LabelSelector],
    egress: Optional[List[NetworkPolicyEgressRule]],
    ingress: Optional[List[NetworkPolicyIngressRule]],
    policy_types: Optional[List[str]],
):
    builder = ChartBuilder(
        chart_info,
        [
            NetworkPolicy(
                ObjectMeta(name=name),
                NetworkPolicySpec(egress, ingress, selector, policy_types),
            )
        ],
    )
    with ChartInstallationContext(builder):
        network_policy_info = kubectl_get("networkpolicy")
        assert network_policy_info["NAME"][0] == name
        expected_selector = "<none>"
        if isinstance(selector, LabelSelector):
            match_labels = selector.matchLabels
            if match_labels is None:
                raise Exception("Match labels cannot be none in this case")
            first_key = list(match_labels.keys())[0]
            expected_selector = f"{first_key}={match_labels[first_key]}"
        assert network_policy_info["POD-SELECTOR"][0] == expected_selector
Example #6
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))
Example #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"
Example #8
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"
Example #9
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"
Example #10
0
def test_chart_w_dependencies(grafana_dependency, dependency_chart_info):
    builder = ChartBuilder(dependency_chart_info, [])
    with ChartInstallationContext(builder, timeout=60):
        # 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

    # Test reinstalling the same helm chart / repo
    with ChartInstallationContext(builder, timeout=60):
        assert builder.is_installed

    remove_stable_repo()
Example #11
0
def test_install_local_dependency():
    # Create "fake" chart
    output_directory = Path.cwd() / "tmp"
    chart_info = ChartInfo("3.2.4", "local-chart", "0.1.0")
    builder = ChartBuilder(chart_info, [],
                           output_directory=str(output_directory))
    builder.generate_chart()

    # Create chart with local dependency
    local_dependency = ChartDependency(
        name=chart_info.name,
        version=chart_info.version,
        local_repo_name="local-repo",
        repository=f"file://{output_directory.resolve()}/{chart_info.name}",
        is_local=True,
    )
    builder = ChartBuilder(
        ChartInfo(
            api_version="3.2.4",
            name="local-repo-dep-test",
            version="0.1.0",
            app_version="v1",
            dependencies=[local_dependency],
        ),
        [],
        keep_chart=True,
    )
    builder.generate_chart()
    with ChartInstallationContext(builder):
        assert builder.is_installed
Example #12
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"
Example #13
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"
Example #14
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"
Example #15
0
def test_secret(chart_info, secret_data: dict):
    builder = ChartBuilder(
        chart_info, [Secret(ObjectMeta(name="test-secret"), secret_data)])
    with ChartInstallationContext(builder):
        secret_info = get_secret_info()
        assert secret_info["NAME"][0] == "test-secret"
        assert secret_info["DATA"][0] == str(
            len(secret_data)) if secret_data else "0"
Example #16
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
Example #17
0
def test_installation_with_namespace(chart_info):
    builder = ChartBuilder(chart_info, [], namespace="test")
    with ChartInstallationContext(builder, timeout=60):
        # Check helm release
        helm_installation = get_helm_installations("test")
        assert helm_installation["NAME"][0] == "test"
        assert helm_installation["REVISION"][0] == "1"
        assert helm_installation["STATUS"][0] == "deployed"
        assert helm_installation["NAMESPACE"][0] == "test"
Example #18
0
def test_installation_with_value_args(chart_info):
    builder = ChartBuilder(chart_info, [], namespace="test")
    with ChartInstallationContext(builder,
                                  extra_installation_args={"output": "json"}):
        helm_installation = get_helm_installations("test")
        assert helm_installation["NAME"][0] == "test"
        assert helm_installation["REVISION"][0] == "1"
        assert helm_installation["STATUS"][0] == "deployed"
        assert helm_installation["NAMESPACE"][0] == "test"
Example #19
0
def test_values_yaml_with_dependencies(
    config_map_value_yaml, values_yaml, dependency_chart_info
):
    builder = ChartBuilder(
        dependency_chart_info, [config_map_value_yaml], values=values_yaml
    )
    with ChartInstallationContext(builder):
        assert get_config_map_data() == {"my_nested_value": 0, "my_value": "some_value"}
        assert builder.is_installed
Example #20
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_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"
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}"
Example #23
0
def test_helm_upgrade(chart_info):
    builder = ChartBuilder(chart_info, [])
    with ChartInstallationContext(builder):
        # Check helm release
        builder.upgrade_chart()
        helm_installation = get_helm_installations()
        assert helm_installation["NAME"][0] == "test"
        assert helm_installation["REVISION"][0] == "2"
        assert helm_installation["STATUS"][0] == "deployed"
        assert helm_installation["NAMESPACE"][0] == "default"
Example #24
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")
Example #25
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))
Example #26
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 "")
Example #27
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"
Example #28
0
def test_pod_disruption_budget(chart_info, pod_disruption_budget):
    builder = ChartBuilder(
        chart_info,
        [pod_disruption_budget],
    )
    with ChartInstallationContext(builder):
        budget_info = kubectl_get("poddisruptionbudget")
        assert budget_info["NAME"][0] == pod_disruption_budget.metadata.name
        assert budget_info["MIN AVAILABLE"][0] == none_to_na(
            pod_disruption_budget.spec.minAvailable)
        assert budget_info["MAX UNAVAILABLE"][0] == none_to_na(
            pod_disruption_budget.spec.maxUnavailable)
Example #29
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
Example #30
0
def test_event(chart_info: ChartInfo, event: Event):
    builder = ChartBuilder(chart_info, [event])
    with ChartInstallationContext(builder):
        event_info = get_event_info()
        assert event_info["TYPE"][0] == (event.type
                                         if event.type is not None else "")
        assert event_info["REASON"][0] == (event.reason
                                           if event.reason is not None else "")
        assert event_info["OBJECT"][
            0] == f"objectreference/{event.involvedObject.name}"
        assert event_info["MESSAGE"][0] == (event.message if event.message
                                            is not None else "")