Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/tests/charts/test_extra_configmaps_secrets.py: 15%
48 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17from __future__ import annotations
19import textwrap
20from base64 import b64encode
21from unittest import mock
23import pytest
24import yaml
26from tests.charts.helm_template_generator import prepare_k8s_lookup_dict, render_chart
28RELEASE_NAME = "test-extra-configmaps-secrets"
31class TestExtraConfigMapsSecrets:
32 def test_extra_configmaps(self):
33 values_str = textwrap.dedent(
34 """
35 extraConfigMaps:
36 "{{ .Release.Name }}-airflow-variables":
37 data: |
38 AIRFLOW_VAR_HELLO_MESSAGE: "Hi!"
39 AIRFLOW_VAR_KUBERNETES_NAMESPACE: "{{ .Release.Namespace }}"
40 "{{ .Release.Name }}-other-variables":
41 data: |
42 HELLO_WORLD: "Hi again!"
43 """
44 )
45 values = yaml.safe_load(values_str)
46 k8s_objects = render_chart(
47 RELEASE_NAME, values=values, show_only=["templates/configmaps/extra-configmaps.yaml"]
48 )
49 k8s_objects_by_key = prepare_k8s_lookup_dict(k8s_objects)
51 all_expected_keys = [
52 ("ConfigMap", f"{RELEASE_NAME}-airflow-variables"),
53 ("ConfigMap", f"{RELEASE_NAME}-other-variables"),
54 ]
55 assert set(k8s_objects_by_key.keys()) == set(all_expected_keys)
57 all_expected_data = [
58 {"AIRFLOW_VAR_HELLO_MESSAGE": "Hi!", "AIRFLOW_VAR_KUBERNETES_NAMESPACE": "default"},
59 {"HELLO_WORLD": "Hi again!"},
60 ]
61 for expected_key, expected_data in zip(all_expected_keys, all_expected_data):
62 configmap_obj = k8s_objects_by_key[expected_key]
63 assert configmap_obj["data"] == expected_data
65 def test_extra_secrets(self):
66 values_str = textwrap.dedent(
67 """
68 extraSecrets:
69 "{{ .Release.Name }}-airflow-connections":
70 data: |
71 AIRFLOW_CON_AWS: {{ printf "aws_connection_string" | b64enc }}
72 stringData: |
73 AIRFLOW_CON_GCP: "gcp_connection_string"
74 "{{ .Release.Name }}-other-secrets":
75 data: |
76 MY_SECRET_1: {{ printf "MY_SECRET_1" | b64enc }}
77 MY_SECRET_2: {{ printf "MY_SECRET_2" | b64enc }}
78 stringData: |
79 MY_SECRET_3: "MY_SECRET_3"
80 MY_SECRET_4: "MY_SECRET_4"
81 "{{ .Release.Name }}-other-secrets-with-type":
82 type: kubernetes.io/dockerconfigjson
83 data: |
84 MY_SECRET_5: {{ printf "MY_SECRET_5" | b64enc }}
85 MY_SECRET_6: {{ printf "MY_SECRET_6" | b64enc }}
86 stringData: |
87 MY_SECRET_7: "MY_SECRET_7"
88 MY_SECRET_8: "MY_SECRET_8"
89 """
90 )
91 values = yaml.safe_load(values_str)
92 k8s_objects = render_chart(
93 RELEASE_NAME, values=values, show_only=["templates/secrets/extra-secrets.yaml"]
94 )
95 k8s_objects_by_key = prepare_k8s_lookup_dict(k8s_objects)
97 all_expected_keys = [
98 ("Secret", f"{RELEASE_NAME}-airflow-connections"),
99 ("Secret", f"{RELEASE_NAME}-other-secrets"),
100 ("Secret", f"{RELEASE_NAME}-other-secrets-with-type"),
101 ]
102 assert set(k8s_objects_by_key.keys()) == set(all_expected_keys)
104 all_expected_data = [
105 {"AIRFLOW_CON_AWS": b64encode(b"aws_connection_string").decode("utf-8")},
106 {
107 "MY_SECRET_1": b64encode(b"MY_SECRET_1").decode("utf-8"),
108 "MY_SECRET_2": b64encode(b"MY_SECRET_2").decode("utf-8"),
109 },
110 {
111 "MY_SECRET_5": b64encode(b"MY_SECRET_5").decode("utf-8"),
112 "MY_SECRET_6": b64encode(b"MY_SECRET_6").decode("utf-8"),
113 },
114 ]
116 all_expected_string_data = [
117 {"AIRFLOW_CON_GCP": "gcp_connection_string"},
118 {"MY_SECRET_3": "MY_SECRET_3", "MY_SECRET_4": "MY_SECRET_4"},
119 {"MY_SECRET_7": "MY_SECRET_7", "MY_SECRET_8": "MY_SECRET_8"},
120 ]
121 all_expected_types = [None, None, "kubernetes.io/dockerconfigjson"]
122 for expected_key, expected_data, expected_string_data, expected_type in zip(
123 all_expected_keys, all_expected_data, all_expected_string_data, all_expected_types
124 ):
125 configmap_obj = k8s_objects_by_key[expected_key]
126 if expected_type:
127 assert configmap_obj["type"] == expected_type
128 else:
129 assert "type" not in configmap_obj
130 assert configmap_obj["data"] == expected_data
131 assert configmap_obj["stringData"] == expected_string_data
133 def test_extra_configmaps_secrets_labels(self):
134 k8s_objects = render_chart(
135 name=RELEASE_NAME,
136 values={
137 "labels": {"label1": "value1", "label2": "value2"},
138 "extraSecrets": {"{{ .Release.Name }}-extra-secret-1": {"stringData": "data: secretData"}},
139 "extraConfigMaps": {"{{ .Release.Name }}-extra-configmap-1": {"data": "data: configData"}},
140 },
141 show_only=["templates/configmaps/extra-configmaps.yaml", "templates/secrets/extra-secrets.yaml"],
142 )
143 expected_labels = {
144 "label1": "value1",
145 "label2": "value2",
146 "release": RELEASE_NAME,
147 "heritage": "Helm",
148 "chart": mock.ANY,
149 }
150 for k8s_object in k8s_objects:
151 assert k8s_object["metadata"]["labels"] == expected_labels
153 @pytest.mark.parametrize(
154 "chart_labels, local_labels",
155 [
156 ({}, {"label3": "value3", "label4": "value4"}),
157 ({"label1": "value1", "label2": "value2"}, {}),
158 ({"label1": "value1", "label2": "value2"}, {"label3": "value3", "label4": "value4"}),
159 ],
160 )
161 def test_extra_configmaps_secrets_additional_labels(self, chart_labels, local_labels):
162 k8s_objects = render_chart(
163 name=RELEASE_NAME,
164 values={
165 "labels": chart_labels,
166 "extraSecrets": {
167 "{{ .Release.Name }}-extra-secret-1": {
168 "labels": local_labels,
169 "stringData": "data: secretData",
170 }
171 },
172 "extraConfigMaps": {
173 "{{ .Release.Name }}-extra-configmap-1": {
174 "labels": local_labels,
175 "data": "data: configData",
176 }
177 },
178 },
179 show_only=["templates/configmaps/extra-configmaps.yaml", "templates/secrets/extra-secrets.yaml"],
180 )
181 common_labels = {
182 "release": RELEASE_NAME,
183 "heritage": "Helm",
184 "chart": mock.ANY,
185 }
186 for k8s_object in k8s_objects:
187 assert k8s_object["metadata"]["labels"] == {**common_labels, **chart_labels, **local_labels}