This is an automated email from the ASF dual-hosted git repository.

kaxilnaik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/master by this push:
     new 922668f  More test coverage on pgbouncer in the helm chart (#15684)
922668f is described below

commit 922668f32e4997ff9e4b80d5c041a38719e6c758
Author: Jed Cunningham <[email protected]>
AuthorDate: Wed May 5 17:49:40 2021 -0600

    More test coverage on pgbouncer in the helm chart (#15684)
    
    Simply more test coverage on pgbouncer in the helm chart.
---
 chart/templates/pgbouncer/pgbouncer-service.yaml |   2 -
 chart/tests/test_pgbouncer.py                    | 170 ++++++++++++++++++++---
 2 files changed, 152 insertions(+), 20 deletions(-)

diff --git a/chart/templates/pgbouncer/pgbouncer-service.yaml 
b/chart/templates/pgbouncer/pgbouncer-service.yaml
index 6913a47..fd37b38 100644
--- a/chart/templates/pgbouncer/pgbouncer-service.yaml
+++ b/chart/templates/pgbouncer/pgbouncer-service.yaml
@@ -48,9 +48,7 @@ spec:
     - name: pgbouncer
       protocol: TCP
       port: {{ .Values.ports.pgbouncer }}
-      targetPort: {{ .Values.ports.pgbouncer }}
     - name: pgbouncer-metrics
       protocol: TCP
       port: {{ .Values.ports.pgbouncerScrape }}
-      targetPort: {{ .Values.ports.pgbouncerScrape }}
 {{- end }}
diff --git a/chart/tests/test_pgbouncer.py b/chart/tests/test_pgbouncer.py
index ff3fc1f..7677a7b 100644
--- a/chart/tests/test_pgbouncer.py
+++ b/chart/tests/test_pgbouncer.py
@@ -19,21 +19,83 @@ import base64
 import unittest
 
 import jmespath
+from parameterized import parameterized
 
 from tests.helm_template_generator import render_chart
 
 
 class PgbouncerTest(unittest.TestCase):
+    @parameterized.expand(["pgbouncer-deployment", "pgbouncer-service"])
+    def test_pgbouncer_resources_not_created_by_default(self, yaml_filename):
+        docs = render_chart(
+            show_only=[f"templates/pgbouncer/{yaml_filename}.yaml"],
+        )
+        assert docs == []
+
     def test_should_create_pgbouncer(self):
         docs = render_chart(
             values={"pgbouncer": {"enabled": True}},
             show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
         )
 
+        assert "Deployment" == jmespath.search("kind", docs[0])
         assert "RELEASE-NAME-pgbouncer" == jmespath.search("metadata.name", 
docs[0])
-
         assert "pgbouncer" == 
jmespath.search("spec.template.spec.containers[0].name", docs[0])
 
+    def test_should_create_pgbouncer_service(self):
+        docs = render_chart(
+            values={"pgbouncer": {"enabled": True}},
+            show_only=["templates/pgbouncer/pgbouncer-service.yaml"],
+        )
+
+        assert "Service" == jmespath.search("kind", docs[0])
+        assert "RELEASE-NAME-pgbouncer" == jmespath.search("metadata.name", 
docs[0])
+        assert "true" == 
jmespath.search('metadata.annotations."prometheus.io/scrape"', docs[0])
+        assert "9127" == 
jmespath.search('metadata.annotations."prometheus.io/port"', docs[0])
+
+        assert {"prometheus.io/scrape": "true", "prometheus.io/port": "9127"} 
== jmespath.search(
+            "metadata.annotations", docs[0]
+        )
+
+        assert {"name": "pgbouncer", "protocol": "TCP", "port": 6543} in 
jmespath.search(
+            "spec.ports", docs[0]
+        )
+        assert {"name": "pgbouncer-metrics", "protocol": "TCP", "port": 9127} 
in jmespath.search(
+            "spec.ports", docs[0]
+        )
+
+    def test_pgbouncer_service_with_custom_ports(self):
+        docs = render_chart(
+            values={
+                "pgbouncer": {"enabled": True},
+                "ports": {"pgbouncer": 1111, "pgbouncerScrape": 2222},
+            },
+            show_only=["templates/pgbouncer/pgbouncer-service.yaml"],
+        )
+
+        assert "true" == 
jmespath.search('metadata.annotations."prometheus.io/scrape"', docs[0])
+        assert "2222" == 
jmespath.search('metadata.annotations."prometheus.io/port"', docs[0])
+        assert {"name": "pgbouncer", "protocol": "TCP", "port": 1111} in 
jmespath.search(
+            "spec.ports", docs[0]
+        )
+        assert {"name": "pgbouncer-metrics", "protocol": "TCP", "port": 2222} 
in jmespath.search(
+            "spec.ports", docs[0]
+        )
+
+    def test_pgbouncer_service_extra_annotations(self):
+        docs = render_chart(
+            values={
+                "pgbouncer": {"enabled": True, "service": {"extraAnnotations": 
{"foo": "bar"}}},
+            },
+            show_only=["templates/pgbouncer/pgbouncer-service.yaml"],
+        )
+
+        assert {
+            "prometheus.io/scrape": "true",
+            "prometheus.io/port": "9127",
+            "foo": "bar",
+        } == jmespath.search("metadata.annotations", docs[0])
+
     def test_should_create_valid_affinity_tolerations_and_node_selector(self):
         docs = render_chart(
             values={
@@ -61,7 +123,6 @@ class PgbouncerTest(unittest.TestCase):
             show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
         )
 
-        assert "Deployment" == jmespath.search("kind", docs[0])
         assert "foo" == jmespath.search(
             "spec.template.spec.affinity.nodeAffinity."
             "requiredDuringSchedulingIgnoredDuringExecution."
@@ -137,14 +198,23 @@ class PgbouncerTest(unittest.TestCase):
 
 
 class PgbouncerConfigTest(unittest.TestCase):
-    def test_databases_default(self):
+    def test_config_not_created_by_default(self):
         docs = render_chart(
-            values={"pgbouncer": {"enabled": True}},
             show_only=["templates/secrets/pgbouncer-config-secret.yaml"],
         )
 
+        assert docs == []
+
+    def _get_pgbouncer_ini(self, values: dict) -> str:
+        docs = render_chart(
+            values=values,
+            show_only=["templates/secrets/pgbouncer-config-secret.yaml"],
+        )
         encoded_ini = jmespath.search('data."pgbouncer.ini"', docs[0])
-        ini = base64.b64decode(encoded_ini).decode()
+        return base64.b64decode(encoded_ini).decode()
+
+    def test_databases_default(self):
+        ini = self._get_pgbouncer_ini({"pgbouncer": {"enabled": True}})
 
         assert (
             "RELEASE-NAME-metadata = host=RELEASE-NAME-postgresql.default 
dbname=postgres port=5432"
@@ -155,20 +225,84 @@ class PgbouncerConfigTest(unittest.TestCase):
             " pool_size=5" in ini
         )
 
-    def test_hostname_override(self):
-        docs = render_chart(
-            values={
-                "pgbouncer": {"enabled": True, "metadataPoolSize": 12, 
"resultBackendPoolSize": 7},
-                "data": {
-                    "metadataConnection": {"host": "meta_host", "db": 
"meta_db", "port": 1111},
-                    "resultBackendConnection": {"host": "rb_host", "db": 
"rb_db", "port": 2222},
-                },
+    def test_databases_override(self):
+        values = {
+            "pgbouncer": {"enabled": True, "metadataPoolSize": 12, 
"resultBackendPoolSize": 7},
+            "data": {
+                "metadataConnection": {"host": "meta_host", "db": "meta_db", 
"port": 1111},
+                "resultBackendConnection": {"host": "rb_host", "db": "rb_db", 
"port": 2222},
             },
-            show_only=["templates/secrets/pgbouncer-config-secret.yaml"],
-        )
-
-        encoded_ini = jmespath.search('data."pgbouncer.ini"', docs[0])
-        ini = base64.b64decode(encoded_ini).decode()
+        }
+        ini = self._get_pgbouncer_ini(values)
 
         assert "RELEASE-NAME-metadata = host=meta_host dbname=meta_db 
port=1111 pool_size=12" in ini
         assert "RELEASE-NAME-result-backend = host=rb_host dbname=rb_db 
port=2222 pool_size=7" in ini
+
+    def test_config_defaults(self):
+        ini = self._get_pgbouncer_ini({"pgbouncer": {"enabled": True}})
+
+        assert "listen_port = 6543" in ini
+        assert "stats_users = postgres" in ini
+        assert "max_client_conn = 100" in ini
+        assert "verbose = 0" in ini
+        assert "log_disconnections = 0" in ini
+        assert "log_connections = 0" in ini
+        assert "server_tls_sslmode = prefer" in ini
+        assert "server_tls_ciphers = normal" in ini
+
+        assert "server_tls_ca_file = " not in ini
+        assert "server_tls_cert_file = " not in ini
+        assert "server_tls_key_file = " not in ini
+
+    def test_config_overrides(self):
+        values = {
+            "pgbouncer": {
+                "enabled": True,
+                "maxClientConn": 111,
+                "verbose": 2,
+                "logDisconnections": 1,
+                "logConnections": 1,
+                "sslmode": "verify-full",
+                "ciphers": "secure",
+            },
+            "ports": {"pgbouncer": 7777},
+            "data": {"metadataConnection": {"user": "someuser"}},
+        }
+        ini = self._get_pgbouncer_ini(values)
+
+        assert "listen_port = 7777" in ini
+        assert "stats_users = someuser" in ini
+        assert "max_client_conn = 111" in ini
+        assert "verbose = 2" in ini
+        assert "log_disconnections = 1" in ini
+        assert "log_connections = 1" in ini
+        assert "server_tls_sslmode = verify-full" in ini
+        assert "server_tls_ciphers = secure" in ini
+
+    def test_ssl_defaults_dont_create_cert_secret(self):
+        docs = render_chart(
+            values={"pgbouncer": {"enabled": True}},
+            show_only=["templates/secrets/pgbouncer-certificates-secret.yaml"],
+        )
+
+        assert docs == []
+
+    def test_ssl_config(self):
+        values = {
+            "pgbouncer": {"enabled": True, "ssl": {"ca": "someca", "cert": 
"somecert", "key": "somekey"}}
+        }
+        ini = self._get_pgbouncer_ini(values)
+
+        assert "server_tls_ca_file = /etc/pgbouncer/root.crt" in ini
+        assert "server_tls_cert_file = /etc/pgbouncer/server.crt" in ini
+        assert "server_tls_key_file = /etc/pgbouncer/server.key" in ini
+
+        docs = render_chart(
+            values=values,
+            show_only=["templates/secrets/pgbouncer-certificates-secret.yaml"],
+        )
+
+        for key, expected in [("root.crt", "someca"), ("server.crt", 
"somecert"), ("server.key", "somekey")]:
+            encoded = jmespath.search(f'data."{key}"', docs[0])
+            value = base64.b64decode(encoded).decode()
+            assert expected == value

Reply via email to