From ef459a2d18701283afe7400227d790659e078d03 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Tue, 27 Jun 2017 17:32:12 -0400 Subject: [PATCH] Update the expected response layout for kubernetes config --- util/config/provider/k8sprovider.py | 12 +++++-- util/config/provider/test/test_k8sprovider.py | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/util/config/provider/k8sprovider.py b/util/config/provider/k8sprovider.py index 7af71634d..94b1f15f5 100644 --- a/util/config/provider/k8sprovider.py +++ b/util/config/provider/k8sprovider.py @@ -57,16 +57,22 @@ class KubernetesConfigProvider(FileConfigProvider): def volume_file_exists(self, filename): secret = self._lookup_secret() + if not secret: + return False return filename in secret def list_volume_directory(self, path): secret = self._lookup_secret() + if not secret: + return [] + paths = [] for filename in secret: if filename.startswith(path): paths.append(filename[len(path) + 1:]) + return paths def remove_volume_file(self, filename): super(KubernetesConfigProvider, self).remove_volume_file(filename) @@ -126,8 +132,10 @@ class KubernetesConfigProvider(FileConfigProvider): response = self._execute_k8s_api('GET', secret_url) if response.status_code != 200: return None - - return json.loads(response.text) + secret_resp = json.loads(response.text) + if "data" not in secret_resp: + return secret_resp + return secret_resp["data"] def _execute_k8s_api(self, method, relative_url, data=None): headers = { diff --git a/util/config/provider/test/test_k8sprovider.py b/util/config/provider/test/test_k8sprovider.py index c31f69596..197a97ff7 100644 --- a/util/config/provider/test/test_k8sprovider.py +++ b/util/config/provider/test/test_k8sprovider.py @@ -1,4 +1,5 @@ import pytest +from mock import Mock from util.config.provider import KubernetesConfigProvider @@ -9,6 +10,7 @@ class TestKubernetesConfigProvider(KubernetesConfigProvider): def __init__(self): self.yaml_filename = 'yaml_filename' self._service_token = 'service_token' + self._execute_k8s_api = Mock() @pytest.mark.parametrize('directory,filename,expected', [ @@ -23,3 +25,36 @@ def test_get_volume_path(directory, filename, expected): assert expected == provider.get_volume_path(directory, filename) +@pytest.mark.parametrize('response,expected', [ + (Mock(text="{\"license\":\"test\"}", status_code=200), {"license":"test"}), + (Mock(text="{\"data\": {\"license\":\"test\"}}", status_code=200), {"license":"test"}), + (Mock(text="{\"data\": {\"license\":\"test\"}}", status_code=404), None), +]) +def test_lookup_secret(response, expected): + provider = TestKubernetesConfigProvider() + provider._execute_k8s_api.return_value = response + assert expected == provider._lookup_secret() + + +@pytest.mark.parametrize('response,key,expected', [ + (Mock(text="{\"license\":\"test\"}", status_code=200), "license", True), + (Mock(text="{\"data\": {\"license\":\"test\"}}", status_code=200), "license", True), + (Mock(text="{\"license\":\"test\"}", status_code=200), "config.yaml", False), + (Mock(text="{\"data\": {\"license\":\"test\"}}", status_code=200), "config.yaml", False), + (Mock(text="", status_code=404), "license", False), +]) +def test_volume_file_exists(response, key, expected): + provider = TestKubernetesConfigProvider() + provider._execute_k8s_api.return_value = response + assert expected == provider.volume_file_exists(key) + + +@pytest.mark.parametrize('response,expected', [ + (Mock(text="{\"extra_license\":\"test\"}", status_code=200), ["license"]), + (Mock(text="{\"data\": {\"extra_license\":\"test\"}}", status_code=200), ["license"]), + (Mock(text="", status_code=404), []), +]) +def test_list_volume_directory(response, expected): + provider = TestKubernetesConfigProvider() + provider._execute_k8s_api.return_value = response + assert expected == provider.list_volume_directory("extra") \ No newline at end of file