summaryrefslogtreecommitdiffstats
path: root/k8s/tests/test_k8sclient_deploy.py
blob: c7b0646579a85fc110c7adefb8edb791822dae98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# ============LICENSE_START=======================================================
# org.onap.dcae
# ================================================================================
# Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved.
# Copyright (c) 2020-2021 Nokia. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================

# Test k8sclient deployment functions
# Verify that for a given configuration and set of inputs, k8sclient generates the proper
# Kubernetes entities
import pytest
from common import do_deploy, verify_ports, verify_image, verify_rediness_probe, verify_volumes, \
    verify_logs, verify_env_variables, verify_deployment_desc, verify_label
from common import verify_external_cert
from common import verify_cert_post_processor

K8S_CONFIGURATION = {
    "image_pull_secrets": ["secret0", "secret1"],
    "filebeat": {
        "log_path": "/var/log/onap",
        "data_path": "/usr/share/filebeat/data",
        "config_path": "/usr/share/filebeat/filebeat.yml",
        "config_subpath": "filebeat.yml",
        "image": "filebeat-repo/filebeat:latest",
        "config_map": "dcae-filebeat-configmap"
    },
    "tls": {
        "cert_path": "/opt/certs",
        "image": "tlsrepo/tls-init-container:1.2.3",
        "component_cert_dir": "/opt/dcae/cacert"
    },
    "external_cert": {
        "image_tag": "repo/oom-certservice-client:2.1.0",
        "request_url": "https://request:1010/url",
        "timeout": "30000",
        "country": "US",
        "organization": "Linux-Foundation",
        "state": "California",
        "organizational_unit": "ONAP",
        "location": "San-Francisco",
        "keystore_password": "secret1",
        "truststore_password": "secret2"
    },
    "cert_post_processor": {
        "image_tag": "repo/oom-cert-post-processor:2.1.0"
    },
    "cbs": {
        "base_url": "https://config-binding-service:10443/service_component_all/test-component"
    }
}

BASIC_KWARGS = {
    "volumes": [
        {
            "host": {
                "path": "/path/on/host"
            },
            "container": {
                "bind": "/path/on/container",
                "mode": "rw"
            }
        }
    ],
    "ports": [
        "80:0",
        "443:0"
    ],
    "env": {
        "NAME0": "value0",
        "NAME1": "value1"
    },
    "log_info": {
        "log_directory": "/path/to/container/log/directory"
    },
    "readiness": {
        "type": "http",
        "endpoint": "/ready"
    },
    "resources": {
        "limits": {
            "cpu": 0.5,
            "memory": "2Gi"
        },
        "requests": {
            "cpu": 0.5,
            "memory": "2Gi"
        }
    }
}

KWARGS_WITH_FULL_TLS = {"tls_info": {"use_tls": True, "cert_directory": "/path/to/container/cert/directory"}}
KWARGS_TLS_OFF = {"tls_info": {"use_tls": False, "cert_directory": "/path/to/container/cert/directory"}}
KWARGS_WITH_EXTERNAL_CERT = {"external_cert": {"external_cert_directory": "/path/to/container/cert/directory/",
                                               "use_external_tls": True,
                                               "cert_type": "P12",
                                               "ca_name": "myname",
                                               "external_certificate_parameters": {
                                                   "common_name": "mycommonname",
                                                   "sans": "mysans"}
                                               }}

KWARGS_WITH_CONFIG_MAP = {"config_volume": {"name": "myConfigMap"},
                          "container": {"bind": "/path/to/configMap", "mode": "ro"}}


test_data = [(KWARGS_WITH_EXTERNAL_CERT, "/opt/dcae/cacert"),
             (BASIC_KWARGS, "/opt/dcae/cacert"),
             (KWARGS_TLS_OFF, "/path/to/container/cert/directory"),
             (KWARGS_WITH_FULL_TLS, "/path/to/container/cert/directory")]


@pytest.mark.parametrize("blueprint_dict, path", test_data)
def test_deploy(mockk8sapi, blueprint_dict, path):
    # given
    kwargs = dict(BASIC_KWARGS)
    kwargs.update(blueprint_dict)

    # when
    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
    app_container = dep.spec.template.spec.containers[0]
    log_container = dep.spec.template.spec.containers[1]

    # then
    verify_label(dep)
    assert app_container.volume_mounts[2].mount_path == path
    verify_ports(app_container)
    verify_image(app_container)
    verify_rediness_probe(app_container)
    verify_volumes(app_container)
    verify_logs(log_container)
    verify_env_variables(app_container)
    verify_deployment_desc(deployment_description)


def test_deploy_external_cert(mockk8sapi):
    """ Deploy component with external TLS configuration """
    # given
    kwargs = dict(BASIC_KWARGS)
    kwargs.update(KWARGS_WITH_EXTERNAL_CERT)

    # when
    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)

    # then
    verify_external_cert(dep)
    verify_cert_post_processor(dep)


def test_deploy_config_map(mockk8sapi):
    """ Deploy component with configMap in volumes """
    # given
    kwargs = dict(BASIC_KWARGS)
    kwargs['volumes'].append(KWARGS_WITH_CONFIG_MAP)

    # when
    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
    app_container = dep.spec.template.spec.containers[0]

    # then
    assert app_container.volume_mounts[1].mount_path == "/path/to/configMap"