示例#1
0
 def scan_grib(self, gribs, kwargs):
     v_selected = kwargs['shortName']
     v_pert = kwargs.get('perturbationNumber', -1)
     grib_append = gribs.append
     if not utils.is_container(v_selected):
         v_selected = [v_selected]
     if self._grbindx:
         for v in v_selected:
             grib_index_select(self._grbindx, 'shortName', str(v))
             if v_pert != -1:
                 grib_index_select(self._grbindx, 'perturbationNumber',
                                   int(v_pert))
             while 1:
                 gid = grib_new_from_index(self._grbindx)
                 if gid is None:
                     break
                 if GRIBReader._find(gid, **kwargs):
                     grib_append(gid)
                 else:
                     # release unused grib
                     grib_release(gid)
     elif self._file_handler:
         while 1:
             gid = grib_new_from_file(self._file_handler)
             if gid is None:
                 break
             if GRIBReader._find(gid, **kwargs):
                 grib_append(gid)
             else:
                 # release unused grib
                 grib_release(gid)
示例#2
0
 def _find(gid, **kwargs):
     for k, v in kwargs.iteritems():
         if not grib_is_defined(gid, k):
             return False
         iscontainer = utils.is_container(v)
         iscallable = utils.is_callable(v)
         if (not iscontainer and not iscallable and grib_get(gid, k) == v) or\
                 (iscontainer and grib_get(gid, k) in v) or \
                 (iscallable and v(grib_get(gid, k))):
             continue
         else:
             return False
     return True
示例#3
0
def reportusage(base):
    from models import HostingContainer, DataService
    toreport = []

    if utils.is_container(base):
        toreport =  [base]

    if utils.is_service(base):
        container = HostingContainer.objects.get(dataservice=base)
        toreport =  [container,base]

    if utils.is_mfile(base):
        service   = DataService.objects.get(mfile=base)
        toreport =  [service.container,service,base]

    for ob in toreport:
        ob.reportnum += 1
        ob.save()
示例#4
0
class TestAddons(object):
    @pytest.fixture(scope="session", autouse=True)
    def clean_up(self):
        """
        Clean up after a test
        """
        yield
        microk8s_reset()

    def test_invalid_addon(self):
        with pytest.raises(sh.ErrorReturnCode_1):
            sh.microk8s.enable.foo()

    def test_help_text(self):
        status = yaml.load(sh.microk8s.status(format="yaml").stdout)
        expected = {a["name"]: "disabled" for a in status["addons"]}
        expected["ha-cluster"] = "enabled"

        assert expected == {a["name"]: a["status"] for a in status["addons"]}

        for addon in status["addons"]:
            sh.microk8s.enable(addon["name"], "--", "--help")

        assert expected == {a["name"]: a["status"] for a in status["addons"]}

        for addon in status["addons"]:
            sh.microk8s.disable(addon["name"], "--", "--help")

        assert expected == {a["name"]: a["status"] for a in status["addons"]}

    @pytest.mark.skipif(
        platform.machine() != "s390x",
        reason="This test is for the limited set of addons s390x has")
    def test_basic_s390x(self):
        """
        Sets up and tests dashboard, dns, storage, registry, ingress, metrics server.

        """
        ip_ranges = "8.8.8.8,1.1.1.1"
        print("Enabling DNS")
        microk8s_enable("{}:{}".format("dns", ip_ranges), timeout_insec=500)
        wait_for_pod_state("",
                           "kube-system",
                           "running",
                           label="k8s-app=kube-dns")
        print("Validating DNS config")
        validate_coredns_config(ip_ranges)
        print("Enabling metrics-server")
        microk8s_enable("metrics-server")
        print("Enabling dashboard")
        microk8s_enable("dashboard")
        print("Validating dashboard")
        validate_dns_dashboard()
        print("Validating Port Forward")
        validate_forward()
        print("Validating the Metrics Server")
        validate_metrics_server()
        print("Disabling metrics-server")
        microk8s_disable("metrics-server")
        print("Disabling dashboard")
        microk8s_disable("dashboard")

    @pytest.mark.skipif(platform.machine() == "s390x",
                        reason="Not available on s390x")
    def test_basic(self):
        """
        Sets up and tests dashboard, dns, storage, registry, ingress, metrics server.

        """
        ip_ranges = "8.8.8.8,1.1.1.1"
        print("Enabling DNS")
        microk8s_enable("{}:{}".format("dns", ip_ranges), timeout_insec=500)
        wait_for_pod_state("",
                           "kube-system",
                           "running",
                           label="k8s-app=kube-dns")
        print("Validating DNS config")
        validate_coredns_config(ip_ranges)
        print("Enabling ingress")
        microk8s_enable("ingress")
        print("Enabling metrics-server")
        microk8s_enable("metrics-server")
        print("Validating ingress")
        validate_ingress()
        print("Disabling ingress")
        microk8s_disable("ingress")
        print("Enabling dashboard")
        microk8s_enable("dashboard")
        print("Validating dashboard")
        validate_dns_dashboard()
        print("Enabling storage")
        microk8s_enable("storage")
        print("Validating storage")
        validate_storage()
        microk8s_enable("registry")
        print("Validating registry")
        validate_registry()
        print("Validating Port Forward")
        validate_forward()
        print("Validating the Metrics Server")
        validate_metrics_server()
        print("Disabling metrics-server")
        microk8s_disable("metrics-server")
        print("Disabling registry")
        microk8s_disable("registry")
        print("Disabling dashboard")
        microk8s_disable("dashboard")
        print("Disabling storage")
        microk8s_disable("storage:destroy-storage")
        """
        We would disable DNS here but this freezes any terminating pods.
        We let microk8s reset to do the cleanup.
        print("Disabling DNS")
        microk8s_disable("dns")
        """

    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping GPU tests as we are under time pressure",
    )
    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="GPU tests are only relevant in x86 architectures")
    def test_gpu(self):
        """
        Sets up nvidia gpu in a gpu capable system. Skip otherwise.

        """
        try:
            print("Enabling gpu")
            microk8s_enable("gpu")
        except CalledProcessError:
            # Failed to enable gpu. Skip the test.
            print("Could not enable GPU support")
            return
        validate_gpu()
        print("Disable gpu")
        microk8s_disable("gpu")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Istio tests are only relevant in x86 architectures")
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping istio and knative tests as we are under time pressure",
    )
    def test_knative_istio(self):
        """
        Sets up and validate istio.

        """

        print("Enabling Knative and Istio")
        microk8s_enable("knative")
        print("Validating Istio")
        validate_istio()
        print("Validating Knative")
        validate_knative()
        print("Disabling Knative")
        microk8s_disable("knative")
        wait_for_namespace_termination("knative-serving", timeout_insec=600)
        print("Disabling Istio")
        microk8s_disable("istio")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason=
        "Fluentd, prometheus, jaeger tests are only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason=
        "Skipping jaeger, prometheus and fluentd tests as we are under time pressure",
    )
    def test_monitoring_addons(self):
        """
        Test jaeger, prometheus and fluentd.

        """

        print("Enabling fluentd")
        microk8s_enable("fluentd")
        print("Enabling jaeger")
        microk8s_enable("jaeger")
        print("Validating the Jaeger operator")
        validate_jaeger()
        print("Validating the Fluentd")
        validate_fluentd()
        print("Disabling jaeger")
        microk8s_disable("jaeger")
        print("Disabling fluentd")
        microk8s_disable("fluentd")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Prometheus is only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        os.environ.get("SKIP_PROMETHEUS") == "True",
        reason="Skipping prometheus if it crash loops on lxd",
    )
    def test_prometheus(self):
        """
        Test prometheus.
        """

        print("Enabling prometheus")
        microk8s_enable("prometheus")
        print("Validating Prometheus")
        validate_prometheus()
        print("Disabling prometheus")
        microk8s_disable("prometheus")
        microk8s_reset()

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Cilium tests are only relevant in x86 architectures")
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping cilium tests as we are under time pressure",
    )
    def test_cilium(self):
        """
        Sets up and validates Cilium.
        """
        print("Enabling Cilium")
        run(
            "/snap/bin/microk8s.enable cilium".split(),
            stdout=PIPE,
            input=b"N\n",
            stderr=STDOUT,
            check=True,
        )
        print("Validating Cilium")
        validate_cilium()
        print("Disabling Cilium")
        microk8s_disable("cilium")
        microk8s_reset()

    @pytest.mark.skip("disabling the test while we work on a 1.20 release")
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping Linkerd tests as we are under time pressure",
    )
    def test_linkerd(self):
        """
        Sets up and validate linkerd

        """
        print("Enabling Linkerd")
        microk8s_enable("linkerd")
        print("Validating Linkerd")
        validate_linkerd()
        print("Disabling Linkerd")
        microk8s_disable("linkerd")

    def test_rbac_addon(self):
        """
        Test RBAC.

        """
        print("Enabling RBAC")
        microk8s_enable("rbac")
        print("Validating RBAC")
        validate_rbac()
        print("Disabling RBAC")
        microk8s_disable("rbac")

    @pytest.mark.skip(
        "disabling the kubelfow addon until the new bundle becomes available")
    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Kubeflow tests are only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping kubeflow test as we are under time pressure",
    )
    def test_kubeflow_addon(self):
        """
        Test kubeflow.

        """

        print("Enabling Kubeflow")
        microk8s_enable("kubeflow")
        print("Validating Kubeflow")
        validate_kubeflow()
        print("Disabling kubeflow")
        microk8s_disable("kubeflow")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Metallb tests are only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping metallb test as we are under time pressure",
    )
    def test_metallb_addon(self):
        addon = "metallb"
        ip_ranges = "192.168.0.105-192.168.0.105,192.168.0.110-192.168.0.111,192.168.1.240/28"
        print("Enabling metallb")
        microk8s_enable("{}:{}".format(addon, ip_ranges), timeout_insec=500)
        validate_metallb_config(ip_ranges)
        print("Disabling metallb")
        microk8s_disable("metallb")

    @pytest.mark.skip("disabling the test while we work on a 1.20 release")
    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Ambassador tests are only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping ambassador tests as we are under time pressure",
    )
    def test_ambassador(self):
        """
        Test Ambassador.

        """
        print("Enabling Ambassador")
        microk8s_enable("ambassador")
        print("Validating ambassador")
        validate_ambassador()
        print("Disabling Ambassador")
        microk8s_disable("ambassador")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Multus tests are only relevant in x86 architectures")
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping multus tests as we are under time pressure",
    )
    def test_multus(self):
        """
        Sets up and validates Multus.
        """
        print("Enabling Multus")
        microk8s_enable("multus")
        print("Validating Multus")
        validate_multus()
        print("Disabling Multus")
        microk8s_disable("multus")

    @pytest.mark.skipif(platform.machine() == "s390x",
                        reason="Not available on s390x")
    def test_portainer(self):
        """
        Sets up and validates Portainer.
        """
        print("Enabling Portainer")
        microk8s_enable("portainer")
        print("Validating Portainer")
        validate_portainer()
        print("Disabling Portainer")
        microk8s_disable("portainer")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="OpenFaaS tests are only relevant in x86 architectures",
    )
    def test_openfaas(self):
        """
        Sets up and validates OpenFaaS.
        """
        print("Enabling openfaas")
        microk8s_enable("openfaas")
        print("Validating openfaas")
        validate_openfaas()
        print("Disabling openfaas")
        microk8s_disable("openfaas")

    @pytest.mark.skipif(platform.machine() == "s390x",
                        reason="Not available on s390x")
    def test_traefik(self):
        """
        Sets up and validates traefik.
        """
        print("Enabling traefik")
        microk8s_enable("traefik")
        print("Validating traefik")
        validate_traefik()
        print("Disabling traefik")
        microk8s_disable("traefik")

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="KEDA tests are only relevant in x86 architectures")
    @pytest.mark.skipif(
        os.environ.get("UNDER_TIME_PRESSURE") == "True",
        reason="Skipping KEDA tests as we are under time pressure",
    )
    def test_keda(self):
        """
        Sets up and validates keda.
        """
        print("Enabling keda")
        microk8s_enable("keda")
        print("Validating keda")
        validate_keda()
        print("Disabling keda")
        microk8s_disable("keda")

    def test_backup_restore(self):
        """
        Test backup and restore commands.
        """
        print("Checking dbctl backup and restore")
        if os.path.exists("backupfile.tar.gz"):
            os.remove("backupfile.tar.gz")
        check_call(
            "/snap/bin/microk8s.dbctl --debug backup -o backupfile".split())
        check_call(
            "/snap/bin/microk8s.dbctl --debug restore backupfile.tar.gz".split(
            ))

    @pytest.mark.skipif(
        platform.machine() == "s390x",
        reason="OpenEBS is not available on s390x",
    )
    def test_openebs(self):
        """
        Sets up and validates openebs.
        """
        print("Enabling OpenEBS")
        try:
            check_output(
                "systemctl is-enabled iscsid".split()).strip().decode("utf8")
            microk8s_enable("openebs")
            print("Validating OpenEBS")
            validate_openebs()
            print("Disabling OpenEBS")
            microk8s_disable("openebs:force")
        except CalledProcessError:
            print("Nothing to do, since iscsid is not available")
            return

    @pytest.mark.skipif(
        platform.machine() != "x86_64",
        reason="Kata tests are only relevant in x86 architectures",
    )
    @pytest.mark.skipif(
        is_container(),
        reason="Kata tests are only possible on real hardware",
    )
    def test_kata(self):
        """
        Sets up and validates kata.
        """
        print("Enabling kata")
        microk8s_enable("kata")
        print("Validating Kata")
        validate_kata()
        print("Disabling kata")
        microk8s_disable("kata")
示例#5
0
    def test_upgrade(self):
        """
        Deploy, probe, upgrade, validate nothing broke.

        """
        print("Testing upgrade from {} to {}".format(upgrade_from, upgrade_to))

        cmd = "sudo snap install microk8s --classic --channel={}".format(
            upgrade_from)
        run_until_success(cmd)
        wait_for_installation()
        if is_container():
            # In some setups (eg LXC on GCE) the hashsize nf_conntrack file under
            # sys is marked as rw but any update on it is failing causing kube-proxy
            # to fail.
            here = os.path.dirname(os.path.abspath(__file__))
            apply_patch = os.path.join(here, "patch-kube-proxy.sh")
            check_call("sudo {}".format(apply_patch).split())

        # Run through the validators and
        # select those that were valid for the original snap
        test_matrix = {}
        try:
            enable = microk8s_enable("dns")
            wait_for_pod_state("",
                               "kube-system",
                               "running",
                               label="k8s-app=kube-dns")
            assert "Nothing to do for" not in enable
            enable = microk8s_enable("dashboard")
            assert "Nothing to do for" not in enable
            validate_dns_dashboard()
            test_matrix["dns_dashboard"] = validate_dns_dashboard
        except CalledProcessError:
            print("Will not test dns-dashboard")

        try:
            enable = microk8s_enable("storage")
            assert "Nothing to do for" not in enable
            validate_storage()
            test_matrix["storage"] = validate_storage
        except CalledProcessError:
            print("Will not test storage")

        try:
            enable = microk8s_enable("ingress")
            assert "Nothing to do for" not in enable
            validate_ingress()
            test_matrix["ingress"] = validate_ingress
        except CalledProcessError:
            print("Will not test ingress")

        try:
            enable = microk8s_enable("gpu")
            assert "Nothing to do for" not in enable
            validate_gpu()
            test_matrix["gpu"] = validate_gpu
        except CalledProcessError:
            print("Will not test gpu")

        try:
            enable = microk8s_enable("registry")
            assert "Nothing to do for" not in enable
            validate_registry()
            test_matrix["registry"] = validate_registry
        except CalledProcessError:
            print("Will not test registry")

        try:
            validate_forward()
            test_matrix["forward"] = validate_forward
        except CalledProcessError:
            print("Will not test port forward")

        try:
            enable = microk8s_enable("metrics-server")
            assert "Nothing to do for" not in enable
            validate_metrics_server()
            test_matrix["metrics_server"] = validate_metrics_server
        except CalledProcessError:
            print("Will not test the metrics server")

        # AMD64 only tests
        if platform.machine() == "x86_64" and under_time_pressure == "False":
            """
            # Prometheus operator on our lxc is chashlooping disabling the test for now.
            try:
                enable = microk8s_enable("prometheus", timeout_insec=30)
                assert "Nothing to do for" not in enable
                validate_prometheus()
                test_matrix['prometheus'] = validate_prometheus
            except:
                print('Will not test the prometheus')

            # The kubeflow deployment is huge. It will not fit comfortably
            # with the rest of the addons on the same machine during an upgrade
            # we will need to find another way to test it.
            try:
                enable = microk8s_enable("kubeflow", timeout_insec=30)
                assert "Nothing to do for" not in enable
                validate_kubeflow()
                test_matrix['kubeflow'] = validate_kubeflow
            except:
                print('Will not test kubeflow')
            """

            try:
                enable = microk8s_enable("fluentd", timeout_insec=30)
                assert "Nothing to do for" not in enable
                validate_fluentd()
                test_matrix["fluentd"] = validate_fluentd
            except CalledProcessError:
                print("Will not test the fluentd")

            try:
                enable = microk8s_enable("jaeger", timeout_insec=30)
                assert "Nothing to do for" not in enable
                validate_jaeger()
                test_matrix["jaeger"] = validate_jaeger
            except CalledProcessError:
                print("Will not test the jaeger addon")

            # We are not testing cilium because we want to test the upgrade of the default CNI
            """
            try:
                enable = microk8s_enable("cilium", timeout_insec=300)
                assert "Nothing to do for" not in enable
                validate_cilium()
                test_matrix['cilium'] = validate_cilium
            except CalledProcessError:
                print('Will not test the cilium addon')
            """
            try:
                ip_ranges = (
                    "192.168.0.105-192.168.0.105,192.168.0.110-192.168.0.111,192.168.1.240/28"
                )
                enable = microk8s_enable("{}:{}".format("metallb", ip_ranges),
                                         timeout_insec=500)
                assert "MetalLB is enabled" in enable and "Nothing to do for" not in enable
                validate_metallb_config(ip_ranges)
                test_matrix["metallb"] = validate_metallb_config
            except CalledProcessError:
                print("Will not test the metallb addon")

            # We will not be testing multus because it takes too long for cilium and multus
            # to settle after the update and the multus test needs to be refactored so we do
            # delete and recreate the networks configured.
            """
            try:
                enable = microk8s_enable("multus", timeout_insec=150)
                assert "Nothing to do for" not in enable
                validate_multus()
                test_matrix['multus'] = validate_multus
            except CalledProcessError:
                print('Will not test the multus addon')
            """

        # Refresh the snap to the target
        if upgrade_to.endswith(".snap"):
            cmd = "sudo snap install {} --classic --dangerous".format(
                upgrade_to)
        else:
            cmd = "sudo snap refresh microk8s --channel={}".format(upgrade_to)
        run_until_success(cmd)
        # Allow for the refresh to be processed
        time.sleep(10)
        wait_for_installation()

        # Test any validations that were valid for the original snap
        for test, validation in test_matrix.items():
            print("Testing {}".format(test))
            validation()

        if not is_container():
            # On lxc umount docker overlay is not permitted.
            check_call("sudo snap remove microk8s".split())