def test_namespace_deployments_list(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()

    response = api_client.get(
        f'/api/{settings.API_VERSION}/namespaces/kube-system/')
    assert 200 == response.status_code
    response_data = json.loads(response.content)
    assert len(response_data) > 0
    deployments_url = response_data['deployments_url']

    # fetch list of deployments
    response = api_client.get_absolute(deployments_url)
    assert 200 == response.status_code
    response_data = json.loads(response.content)
    assert len(response_data) > 0
    deployment_urls = response_data['deployment_urls']

    # fetch first deployment info
    response = api_client.get_absolute(deployment_urls[0])
    assert 200 == response.status_code
    deployment = json.loads(response.content)
    assert "coredns" in deployment['name']

    # fetch first pod info
    assert len(deployment['pod_urls']) > 0
    response = api_client.get_absolute(deployment['pod_urls'][0])
    assert 200 == response.status_code
    pod = json.loads(response.content)
    assert "coredns" in pod['name']
示例#2
0
def test_pod_double_create(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    try:
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/pods/', {
                'name': 'test-pod2',
                'containers': [
                    {
                        'name': 'busybox',
                        'image': 'busybox'
                    },
                ]
            })
        assert 201 == response.status_code
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/pods/', {
                'name': 'test-pod2',
                'containers': [
                    {
                        'name': 'busybox',
                        'image': 'busybox'
                    },
                ]
            })
        assert 409 == response.status_code
    finally:
        api.get_portal_core_v1().delete_namespaced_pod(name="test-pod2",
                                                       namespace="kube-system")
示例#3
0
def test_get_pod(api_client, admin_user):
    run_minikube_sync()

    # Minikube system pod names differ, depending on the version
    pods = api.get_namespaced_pods("kube-system", admin_user)
    test_pod_name = None
    for k8s_pod in pods:
        if "apiserver" in k8s_pod.metadata.name:
            test_pod_name = k8s_pod.metadata.name
            break
    assert test_pod_name

    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()

    pod = api.get_portal_core_v1().read_namespaced_pod(test_pod_name,
                                                       'kube-system')

    response = api_client.get(
        f'/api/{settings.API_VERSION}/pods/{pod.metadata.namespace}_{pod.metadata.name}/'
    )
    assert 200 == response.status_code
    data = json.loads(response.content)
    assert data['name'] == test_pod_name
    assert data['containers'][0]['name'] == 'kube-apiserver'
示例#4
0
def test_user_services_list(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()

    response = api_client.get(
        f'/api/{settings.API_VERSION}/namespaces/kube-system/')
    assert 200 == response.status_code
    data = json.loads(response.content)

    services_url = urlparse(data["services_url"])

    response = api_client.get(services_url.path)
    assert 200 == response.status_code
    data = json.loads(response.content)
    service_url = urlparse(data["service_urls"][0])

    response = api_client.get(service_url.path)
    assert 200 == response.status_code
    data = json.loads(response.content)

    assert "kube-dns" == data['name']
    assert "ClusterIP" == data['type']
    assert 53 == data['ports'][0]['port']
    assert 53 == data['ports'][0]['target_port']
    assert 'UDP' == data['ports'][0]['protocol']
    assert 53 == data['ports'][1]['port']
    assert 'k8s-app' == data['selector']['key']
示例#5
0
def test_user_services_create(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    old_count = len(api.get_namespaced_services("kube-system", admin_user))
    try:
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/services/', {
                'name': 'my-service',
                'type': 'NodePort',
                'selector': [
                    {
                        'key': 'app',
                        'value': 'kubeportal'
                    },
                ],
                'ports': [{
                    'port': 8000,
                    'protocol': 'TCP'
                }]
            })
        assert 201 == response.status_code
        new_count = len(api.get_namespaced_services("kube-system", admin_user))
        assert old_count + 1 == new_count
    finally:
        api.get_portal_core_v1().delete_namespaced_service(
            name="my-service", namespace="kube-system")
def test_user_ingresses_create(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    try:
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/ingresses/', {
                'name':
                'my-ingress',
                'annotations': [{
                    'key': 'nginx.ingress.kubernetes.io/rewrite-target',
                    'value': '/'
                }],
                'tls':
                True,
                'rules': [{
                    'host':
                    'www.example.com',
                    'paths': [{
                        'path': '/svc',
                        'service_name': 'my-svc',
                        'service_port': 8000
                    }, {
                        'path': '/docs',
                        'service_name': 'my-docs-svc',
                        'service_port': 5000
                    }]
                }]
            })

        assert 201 == response.status_code
    finally:
        api.get_portal_net_v1().delete_namespaced_ingress(
            name="my-ingress", namespace="kube-system")
def test_user_ingresses_create_empty_path(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    try:
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/ingresses/', {
                'name':
                'my-ingress',
                'rules': [{
                    'host':
                    'www.example.com',
                    'paths': [{
                        'service_name': 'my-svc1',
                        'service_port': 8000
                    }, {
                        'path': '',
                        'service_name': 'my-svc2',
                        'service_port': 5000
                    }, {
                        'path': '/',
                        'service_name': 'my-svc3',
                        'service_port': 5000
                    }]
                }]
            })
        assert 201 == response.status_code

        response = api_client.get(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/')
        assert 200 == response.status_code
        data = json.loads(response.content)
        from urllib.parse import urlparse
        ingresses_url = urlparse(data['ingresses_url'])

        response = api_client.get(ingresses_url.path)
        assert 200 == response.status_code
        data = json.loads(response.content)
        ingress_url = urlparse(data['ingress_urls'][0])

        response = api_client.get(ingress_url.path)
        assert 200 == response.status_code
        data = json.loads(response.content)

        assert data['rules'][0]['paths'][0]['path'] == ''
        assert data['rules'][0]['paths'][1]['path'] == ''
        assert data['rules'][0]['paths'][2]['path'] == '/'
    finally:
        api.get_portal_net_v1().delete_namespaced_ingress(
            name="my-ingress", namespace="kube-system")
示例#8
0
def test_namespace_pods_list(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()

    response = api_client.get(
        f'/api/{settings.API_VERSION}/namespaces/kube-system/')
    assert 200 == response.status_code
    data = json.loads(response.content)
    assert len(data) > 0
    from urllib.parse import urlparse
    pods_url = urlparse(data["pods_url"])
    response = api_client.get(pods_url.path)
    assert 200 == response.status_code
    data = json.loads(response.content)
    assert len(data) > 0
    pod_urls = data["pod_urls"]
    assert pod_urls[0].startswith("http")
def test_deployment(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")

    deployment = api.get_portal_apps_v1().read_namespaced_deployment(
        'coredns', 'kube-system')

    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    response = api_client.get(
        f'/api/{settings.API_VERSION}/deployments/{deployment.metadata.namespace}_{deployment.metadata.name}/'
    )
    assert 200 == response.status_code
    data = json.loads(response.content)
    assert 'puid' in data.keys()
    assert 'name' in data.keys()
    assert 'creation_timestamp' in data.keys()
    assert 'replicas' in data.keys()
    assert 'pod_urls' in data.keys()
    assert 'namespace_url' in data.keys()
def test_get_illegal_deployment(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    default_namespace = KubernetesNamespace.objects.get(name="default")

    deployment = api.get_portal_apps_v1().read_namespaced_deployment(
        'coredns', 'kube-system')

    admin_user.service_account = default_namespace.service_accounts.all()[0]
    admin_user.save()
    response = api_client.get(
        f'/api/{settings.API_VERSION}/deployments/{deployment.metadata.namespace}_{deployment.metadata.name}/'
    )
    assert 404 == response.status_code

    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    response = api_client.get(
        f'/api/{settings.API_VERSION}/deployments/{deployment.metadata.namespace}_{deployment.metadata.name}/'
    )
    assert 200 == response.status_code
示例#11
0
def test_pvc_create(api_client, admin_user):
    run_minikube_sync()
    namespace = KubernetesNamespace.objects.get(name="default")
    admin_user.service_account = namespace.service_accounts.all()[0]
    admin_user.save()
    pvcs = api.get_namespaced_pvcs("default", admin_user)
    try:
        assert len(pvcs) == 0
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/default/persistentvolumeclaims/',
            {
                'name': 'test-pvc',
                'access_modes': ['ReadWriteOnce', 'ReadWriteMany'],
                'storage_class_name': 'standard',
                'size': '10Mi'
            })
        assert 201 == response.status_code
        pvcs = api.get_namespaced_pvcs("default", admin_user)
        assert len(pvcs) > 0
    finally:
        api.get_portal_core_v1().delete_namespaced_persistent_volume_claim(
            name="test-pvc", namespace="default")
示例#12
0
def test_empty_service_selector(api_client, admin_user):
    run_minikube_sync()
    default_namespace = KubernetesNamespace.objects.get(name="default")
    admin_user.service_account = default_namespace.service_accounts.all()[0]
    admin_user.save()

    apply_k8s_yml(BASE_DIR + "fixtures/service1.yml")

    response = api_client.get(
        f'/api/{settings.API_VERSION}/namespaces/default/services/')
    assert response.status_code == 200
    data = json.loads(response.content)

    for service_url in data["service_urls"]:
        response = api_client.get(urlparse(service_url).path)
        assert response.status_code == 200
        data = json.loads(response.content)
        if data['name'] == 'service1':
            assert data['selector'] == None
            return

    assert False
def test_user_deployments_create(api_client, admin_user):
    run_minikube_sync()
    system_namespace = KubernetesNamespace.objects.get(name="kube-system")
    admin_user.service_account = system_namespace.service_accounts.all()[0]
    admin_user.save()
    try:
        response = api_client.post(
            f'/api/{settings.API_VERSION}/namespaces/kube-system/deployments/',
            {
                'name': 'test-deployment',
                'replicas': 1,
                'match_labels': [
                    {
                        'key': 'app',
                        'value': 'webapp'
                    },
                ],
                'pod_template': {
                    'name': 'webapp',
                    'labels': [
                        {
                            'key': 'app',
                            'value': 'webapp'
                        },
                    ],
                    'containers': [
                        {
                            'name': 'busybox',
                            'image': 'busybox'
                        },
                    ]
                }
            })
        assert 201 == response.status_code
    finally:
        api.get_portal_apps_v1().delete_namespaced_deployment(
            name="test-deployment", namespace="kube-system")
示例#14
0
def test_approval_assign_plus_comment(admin_client, admin_user, client,
                                      second_user, mailoutbox):
    client.force_login(second_user)
    assert second_user.state == second_user.NEW
    response = client.post('/access/request/',
                           {'selected-administrator': admin_user.username})
    assertRedirects(response, '/config/')
    second_user.refresh_from_db()
    assert second_user.state == second_user.ACCESS_REQUESTED
    approval_url = f"/admin/kubeportal/user/{second_user.approval_id}/approve/"
    # Perform approval with existing namespace as admin
    run_minikube_sync()
    response = admin_client.post(
        approval_url, {
            'choice': 'approve_choose',
            'approve_choose_name': 'default',
            'comments': 'The intern'
        })
    assertRedirects(response, '/admin/kubeportal/user/')
    second_user.refresh_from_db()
    assert second_user.k8s_namespaces()[0].name == 'default'
    assert second_user.state == second_user.ACCESS_APPROVED
    assert second_user.comments == "The intern"
    assert second_user.answered_by != None