Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ spec:
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
value: 'true'
- name: PATRONI_KUBERNETES_LABELS
value: '{application: ivyhac, cluster-name: ivorysql-patroni-hac}'
value: {{ dict "application" .Values.applicationName "cluster-name" .Values.clusterName | toJson | quote }}
- name: PATRONI_KUBERNETES_PORTS
value: '[{"name": "ivorysql", "port": 5432}]'
- name: IVORYSQL_PASSWORD
Expand Down
41 changes: 41 additions & 0 deletions tests/test_helm_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Render regression tests. Requires Helm 3 and PyYAML (pip install pyyaml)."""
import json
import os
from pathlib import Path
import subprocess
import unittest

CHART = Path(__file__).resolve().parents[1] / 'k8s-cluster/ha-cluster/helm_charts'

def render(overrides=None):
command = [os.environ.get('HELM', 'helm'), 'template', 'test', str(CHART)]
if overrides:
command += ['--values', '-']
result = subprocess.run(command, input=json.dumps(overrides or {}),
text=True, encoding='utf-8', capture_output=True, check=True)
import yaml
return list(yaml.safe_load_all(result.stdout))

def statefulset(documents):
return next(doc for doc in documents if doc and doc['kind'] == 'StatefulSet')

class LabelsTest(unittest.TestCase):
def test_patroni_matches_pod_labels(self):
for overrides in [{}, {'applicationName': 'custom-app', 'clusterName': 'custom-cluster'}]:
with self.subTest(overrides=overrides):
documents = render(overrides)
sts = statefulset(documents)
pod = sts['spec']['template']
env = {e['name']: e.get('value') for e in pod['spec']['containers'][0]['env']}
import yaml
labels = yaml.safe_load(env['PATRONI_KUBERNETES_LABELS'])
self.assertEqual(labels, pod['metadata']['labels'])
self.assertEqual(labels['cluster-name'], env['PATRONI_SCOPE'])
for service in documents:
if service and service['kind'] == 'Service':
for key, value in service['spec'].get('selector', {}).items():
if key != 'role':
self.assertEqual(labels[key], value)

if __name__ == '__main__':
unittest.main()