diff options
Diffstat (limited to 'roles/lib_openshift/src')
-rw-r--r-- | roles/lib_openshift/src/class/oc_env.py | 6 | ||||
-rwxr-xr-x | roles/lib_openshift/src/test/integration/oc_env.yml | 75 | ||||
-rwxr-xr-x | roles/lib_openshift/src/test/unit/oc_env.py | 581 |
3 files changed, 408 insertions, 254 deletions
diff --git a/roles/lib_openshift/src/class/oc_env.py b/roles/lib_openshift/src/class/oc_env.py index a7ea0c9eb..10d44412a 100644 --- a/roles/lib_openshift/src/class/oc_env.py +++ b/roles/lib_openshift/src/class/oc_env.py @@ -107,7 +107,7 @@ class OCEnv(OpenShiftCLI): api_rval = ocenv.delete() - return {'changed': True, 'results': results, 'state': 'absent'} + return {'changed': True, 'state': 'absent'} return {'changed': False, 'state': 'absent'} @@ -134,9 +134,9 @@ class OCEnv(OpenShiftCLI): if api_rval['returncode'] != 0: return {'failed': True, 'msg': api_rval} - return {'changed': True, 'results': results, 'state': 'present'} + return {'changed': True, 'results': api_rval['results'], 'state': 'present'} - return {'changed': False, 'results': results, 'state': 'present'} + return {'changed': False, 'results': api_rval['results'], 'state': 'present'} return {'failed': True, diff --git a/roles/lib_openshift/src/test/integration/oc_env.yml b/roles/lib_openshift/src/test/integration/oc_env.yml new file mode 100755 index 000000000..97a7c8922 --- /dev/null +++ b/roles/lib_openshift/src/test/integration/oc_env.yml @@ -0,0 +1,75 @@ +#!/usr/bin/ansible-playbook --module-path=../../../library/ +# ./oc_env.yml -e "cli_master_test=$OPENSHIFT_MASTER +--- +- hosts: "{{ cli_master_test }}" + gather_facts: no + user: root + vars: + my_env_var: + SOMEKEY : SOMEVALUE + + check_env_var: + name: DEFAULT_CERTIFICATE_DIR + value: /etc/pki/tls/private + + tasks: + - name: list environment variables from router dc + oc_env: + state: list + name: router + namespace: default + kind: dc + register: envout + - debug: var=envout + + - assert: + that: + - "'{{ check_env_var.name }}' == '{{ envout.results[0].name }}'" + - "{{ envout.results|length }} > 0" + msg: "Did not find environment variables." + + - name: list environment variables from router dc + oc_env: + state: present + name: router + namespace: default + kind: dc + env_vars: "{{ my_env_var }}" + register: envout + - debug: var=envout + + - assert: + that: + - "'SOMEKEY' == '{{ envout.results[-1].name }}'" + - "'SOMEVALUE' == '{{ envout.results[-1].value }}'" + msg: "Did not find updated environment variables." + + - name: remove environment variables from router dc + oc_env: + state: absent + name: router + namespace: default + kind: dc + env_vars: "{{ my_env_var }}" + register: envout + - debug: var=envout + + - assert: + that: + - envout.changed == True + msg: "state: Absent failed." + + - name: list environment variables from router dc + oc_env: + state: list + name: router + namespace: default + kind: dc + register: envout + - debug: var=envout + + - assert: + that: + - "'SOMEKEY' != '{{ envout.results[-1].name }}'" + - "'SOMEVALUE' != '{{ envout.results[-1].value }}'" + msg: "Did find updated environment variables." diff --git a/roles/lib_openshift/src/test/unit/oc_env.py b/roles/lib_openshift/src/test/unit/oc_env.py index 5661065b9..eefd40b61 100755 --- a/roles/lib_openshift/src/test/unit/oc_env.py +++ b/roles/lib_openshift/src/test/unit/oc_env.py @@ -35,8 +35,9 @@ class OCEnvTest(unittest.TestCase): ''' setup method will create a file and set to known configuration ''' pass + @mock.patch('oc_env.Utils.create_tmpfile_copy') @mock.patch('oc_env.OCEnv._run') - def test_listing_all_env_vars(self, mock_cmd): + def test_listing_all_env_vars(self, mock_cmd, mock_tmpfile_copy): ''' Testing listing all environment variables from a dc''' # Arrange @@ -47,7 +48,6 @@ class OCEnvTest(unittest.TestCase): 'namespace': 'default', 'name': 'router', 'kind': 'dc', - 'list_all': False, 'env_vars': None, 'kubeconfig': '/etc/origin/master/admin.kubeconfig', 'debug': False, @@ -64,27 +64,13 @@ class OCEnvTest(unittest.TestCase): }, "name": "router", "namespace": "default", - "resourceVersion": "513678", - "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router", - "uid": "7c705902-e960-11e6-b041-0ed9df7abc38" + "resourceVersion": "513678" }, "spec": { "replicas": 2, "selector": { "router": "router" }, - "strategy": { - "activeDeadlineSeconds": 21600, - "resources": {}, - "rollingParams": { - "intervalSeconds": 1, - "maxSurge": "50%", - "maxUnavailable": "50%", - "timeoutSeconds": 600, - "updatePeriodSeconds": 1 - }, - "type": "Rolling" - }, "template": { "metadata": { "creationTimestamp": null, @@ -116,151 +102,368 @@ class OCEnvTest(unittest.TestCase): { "name": "ROUTER_EXTERNAL_HOST_INSECURE", "value": "false" + } + ], + "name": "router" + } + ] + } + }, + "test": false, + "triggers": [ + { + "type": "ConfigChange" + } + ] + } + }''' + + # Return values of our mocked function call. These get returned once per call. + mock_cmd.side_effect = [ + (0, dc_results, ''), # First call to the mock + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mock_adminkubeconfig', + ] + + # Act + results = OCEnv.run_ansible(params, False) + + # Assert + self.assertFalse(results['changed']) + for env_var in results['results']: + if env_var == {'name': 'DEFAULT_CERTIFICATE_DIR', 'value': '/etc/pki/tls/private'}: + break + else: + self.fail('Did not find environment variables in results.') + self.assertEqual(results['state'], 'list') + + # Making sure our mocks were called as we expected + mock_cmd.assert_has_calls([ + mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), + ]) + + @mock.patch('oc_env.Utils.create_tmpfile_copy') + @mock.patch('oc_env.OCEnv._run') + def test_adding_env_vars(self, mock_cmd, mock_tmpfile_copy): + ''' Test add environment variables to a dc''' + + # Arrange + + # run_ansible input parameters + params = { + 'state': 'present', + 'namespace': 'default', + 'name': 'router', + 'kind': 'dc', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False, + 'env_vars': {'SOMEKEY': 'SOMEVALUE'}, + } + + dc_results = '''{ + "apiVersion": "v1", + "kind": "DeploymentConfig", + "metadata": { + "creationTimestamp": "2017-02-02T15:58:49Z", + "generation": 8, + "labels": { + "router": "router" + }, + "name": "router", + "namespace": "default", + "resourceVersion": "513678" + }, + "spec": { + "replicas": 2, + "selector": { + "router": "router" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "router": "router" + } + }, + "spec": { + "containers": [ + { + "env": [ + { + "name": "DEFAULT_CERTIFICATE_DIR", + "value": "/etc/pki/tls/private" }, { - "name": "ROUTER_EXTERNAL_HOST_INTERNAL_ADDRESS" + "name": "DEFAULT_CERTIFICATE_PATH", + "value": "/etc/pki/tls/private/tls.crt" }, { - "name": "ROUTER_EXTERNAL_HOST_PARTITION_PATH" + "name": "ROUTER_EXTERNAL_HOST_HOSTNAME" }, { - "name": "ROUTER_EXTERNAL_HOST_PASSWORD" + "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER" }, { - "name": "ROUTER_EXTERNAL_HOST_PRIVKEY", - "value": "/etc/secret-volume/router.pem" + "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER" }, { - "name": "ROUTER_EXTERNAL_HOST_USERNAME" + "name": "ROUTER_EXTERNAL_HOST_INSECURE", + "value": "false" + } + ], + "name": "router" + } + ] + } + }, + "test": false, + "triggers": [ + { + "type": "ConfigChange" + } + ] + } + }''' + + dc_results_after = '''{ + "apiVersion": "v1", + "kind": "DeploymentConfig", + "metadata": { + "creationTimestamp": "2017-02-02T15:58:49Z", + "generation": 8, + "labels": { + "router": "router" + }, + "name": "router", + "namespace": "default", + "resourceVersion": "513678" + }, + "spec": { + "replicas": 2, + "selector": { + "router": "router" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "router": "router" + } + }, + "spec": { + "containers": [ + { + "env": [ + { + "name": "DEFAULT_CERTIFICATE_DIR", + "value": "/etc/pki/tls/private" }, { - "name": "ROUTER_EXTERNAL_HOST_VXLAN_GW_CIDR" + "name": "DEFAULT_CERTIFICATE_PATH", + "value": "/etc/pki/tls/private/tls.crt" }, { - "name": "ROUTER_SERVICE_HTTPS_PORT", - "value": "443" + "name": "ROUTER_EXTERNAL_HOST_HOSTNAME" }, { - "name": "ROUTER_SERVICE_HTTP_PORT", - "value": "80" + "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER" }, { - "name": "ROUTER_SERVICE_NAME", - "value": "router" + "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER" }, { - "name": "ROUTER_SERVICE_NAMESPACE", - "value": "default" + "name": "ROUTER_EXTERNAL_HOST_INSECURE", + "value": "false" }, { - "name": "ROUTER_SUBDOMAIN" + "name": "SOMEKEY", + "value": "SOMEVALUE" + } + ], + "name": "router" + } + ] + } + }, + "test": false, + "triggers": [ + { + "type": "ConfigChange" + } + ] + } + }''' + + # Return values of our mocked function call. These get returned once per call. + mock_cmd.side_effect = [ + (0, dc_results, ''), + (0, dc_results, ''), + (0, dc_results_after, ''), + (0, dc_results_after, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mock_adminkubeconfig', + ] + + # Act + results = OCEnv.run_ansible(params, False) + + # Assert + self.assertTrue(results['changed']) + for env_var in results['results']: + if env_var == {'name': 'SOMEKEY', 'value': 'SOMEVALUE'}: + break + else: + self.fail('Did not find environment variables in results.') + self.assertEqual(results['state'], 'present') + + # Making sure our mocks were called as we expected + mock_cmd.assert_has_calls([ + mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), + ]) + + @mock.patch('oc_env.Utils.create_tmpfile_copy') + @mock.patch('oc_env.OCEnv._run') + def test_removing_env_vars(self, mock_cmd, mock_tmpfile_copy): + ''' Test add environment variables to a dc''' + + # Arrange + + # run_ansible input parameters + params = { + 'state': 'absent', + 'namespace': 'default', + 'name': 'router', + 'kind': 'dc', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False, + 'env_vars': {'SOMEKEY': 'SOMEVALUE'}, + } + + dc_results_after = '''{ + "apiVersion": "v1", + "kind": "DeploymentConfig", + "metadata": { + "creationTimestamp": "2017-02-02T15:58:49Z", + "generation": 8, + "labels": { + "router": "router" + }, + "name": "router", + "namespace": "default", + "resourceVersion": "513678" + }, + "spec": { + "replicas": 2, + "selector": { + "router": "router" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "router": "router" + } + }, + "spec": { + "containers": [ + { + "env": [ + { + "name": "DEFAULT_CERTIFICATE_DIR", + "value": "/etc/pki/tls/private" }, { - "name": "STATS_PASSWORD", - "value": "UEKR5GCWGI" + "name": "DEFAULT_CERTIFICATE_PATH", + "value": "/etc/pki/tls/private/tls.crt" }, { - "name": "STATS_PORT", - "value": "1936" + "name": "ROUTER_EXTERNAL_HOST_HOSTNAME" }, { - "name": "STATS_USERNAME", - "value": "admin" + "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER" }, { - "name": "EXTENDED_VALIDATION", - "value": "false" + "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER" }, { - "name": "ROUTER_USE_PROXY_PROTOCOL", - "value": "true" + "name": "ROUTER_EXTERNAL_HOST_INSECURE", + "value": "false" } ], - "image": "openshift3/ose-haproxy-router:v3.5.0.17", - "imagePullPolicy": "IfNotPresent", - "livenessProbe": { - "failureThreshold": 3, - "httpGet": { - "host": "localhost", - "path": "/healthz", - "port": 1936, - "scheme": "HTTP" + "name": "router" + } + ] + } + }, + "test": false, + "triggers": [ + { + "type": "ConfigChange" + } + ] + } + }''' + + dc_results_before = '''{ + "apiVersion": "v1", + "kind": "DeploymentConfig", + "metadata": { + "creationTimestamp": "2017-02-02T15:58:49Z", + "generation": 8, + "labels": { + "router": "router" + }, + "name": "router", + "namespace": "default", + "resourceVersion": "513678" + }, + "spec": { + "replicas": 2, + "selector": { + "router": "router" + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "router": "router" + } + }, + "spec": { + "containers": [ + { + "env": [ + { + "name": "DEFAULT_CERTIFICATE_DIR", + "value": "/etc/pki/tls/private" + }, + { + "name": "DEFAULT_CERTIFICATE_PATH", + "value": "/etc/pki/tls/private/tls.crt" }, - "initialDelaySeconds": 10, - "periodSeconds": 10, - "successThreshold": 1, - "timeoutSeconds": 1 - }, - "name": "router", - "ports": [ - { - "containerPort": 80, - "hostPort": 80, - "protocol": "TCP" + { + "name": "ROUTER_EXTERNAL_HOST_HOSTNAME" }, { - "containerPort": 443, - "hostPort": 443, - "protocol": "TCP" + "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER" }, { - "containerPort": 5000, - "hostPort": 5000, - "protocol": "TCP" + "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER" }, { - "containerPort": 1936, - "hostPort": 1936, - "name": "stats", - "protocol": "TCP" - } - ], - "readinessProbe": { - "failureThreshold": 3, - "httpGet": { - "host": "localhost", - "path": "/healthz", - "port": 1936, - "scheme": "HTTP" + "name": "ROUTER_EXTERNAL_HOST_INSECURE", + "value": "false" }, - "initialDelaySeconds": 10, - "periodSeconds": 10, - "successThreshold": 1, - "timeoutSeconds": 1 - }, - "resources": { - "requests": { - "cpu": "100m", - "memory": "256Mi" - } - }, - "terminationMessagePath": "/dev/termination-log", - "volumeMounts": [ { - "mountPath": "/etc/pki/tls/private", - "name": "server-certificate", - "readOnly": true + "name": "SOMEKEY", + "value": "SOMEVALUE" } - ] - } - ], - "dnsPolicy": "ClusterFirst", - "hostNetwork": true, - "nodeSelector": { - "type": "infra" - }, - "restartPolicy": "Always", - "securityContext": {}, - "serviceAccount": "router", - "serviceAccountName": "router", - "terminationGracePeriodSeconds": 30, - "volumes": [ - { - "name": "server-certificate", - "secret": { - "defaultMode": 420, - "secretName": "router-certs" - } + ], + "name": "router" } ] } @@ -271,156 +474,32 @@ class OCEnvTest(unittest.TestCase): "type": "ConfigChange" } ] - }, - "status": { - "availableReplicas": 2, - "conditions": [ - { - "lastTransitionTime": "2017-02-02T15:59:12Z", - "lastUpdateTime": null, - "message": "Deployment config has minimum availability.", - "status": "True", - "type": "Available" - }, - { - "lastTransitionTime": "2017-02-07T19:55:26Z", - "lastUpdateTime": "2017-02-07T19:55:26Z", - "message": "replication controller router-2 has failed progressing", - "reason": "ProgressDeadlineExceeded", - "status": "False", - "type": "Progressing" - } - ], - "details": { - "causes": [ - { - "type": "ConfigChange" - } - ], - "message": "config change" - }, - "latestVersion": 2, - "observedGeneration": 8, - "readyReplicas": 2, - "replicas": 2, - "unavailableReplicas": 0, - "updatedReplicas": 0 } }''' # Return values of our mocked function call. These get returned once per call. mock_cmd.side_effect = [ - (0, dc_results, ''), # First call to the mock + (0, dc_results_before, ''), + (0, dc_results_before, ''), + (0, '', ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mock_adminkubeconfig', ] # Act results = OCEnv.run_ansible(params, False) # Assert - self.assertFalse(results['changed']) - for env_var in results['results']: - if env_var == {'name': 'DEFAULT_CERTIFICATE_DIR', 'value': '/etc/pki/tls/private'}: - break - else: - self.fail('Did not find envionrment variables in results.') - self.assertEqual(results['state'], 'list') + self.assertTrue(results['changed']) + self.assertEqual(results['state'], 'absent') # Making sure our mocks were called as we expected mock_cmd.assert_has_calls([ mock.call(['oc', '-n', 'default', 'get', 'dc', 'router', '-o', 'json'], None), ]) -# @mock.patch('oc_serviceaccount_secret.Yedit._write') -# @mock.patch('oc_serviceaccount_secret.OCServiceAccountSecret._run') -# def test_removing_a_secret_to_a_serviceaccount(self, mock_cmd, mock_write): -# ''' Testing adding a secret to a service account ''' -# -# # Arrange -# -# # run_ansible input parameters -# params = { -# 'state': 'absent', -# 'namespace': 'default', -# 'secret': 'newsecret', -# 'service_account': 'builder', -# 'kubeconfig': '/etc/origin/master/admin.kubeconfig', -# 'debug': False, -# } -# -# oc_get_sa_before = '''{ -# "kind": "ServiceAccount", -# "apiVersion": "v1", -# "metadata": { -# "name": "builder", -# "namespace": "default", -# "selfLink": "/api/v1/namespaces/default/serviceaccounts/builder", -# "uid": "cf47bca7-ebc4-11e6-b041-0ed9df7abc38", -# "resourceVersion": "302879", -# "creationTimestamp": "2017-02-05T17:02:00Z" -# }, -# "secrets": [ -# { -# "name": "builder-dockercfg-rsrua" -# }, -# { -# "name": "builder-token-akqxi" -# }, -# { -# "name": "newsecret" -# } -# -# ], -# "imagePullSecrets": [ -# { -# "name": "builder-dockercfg-rsrua" -# } -# ] -# } -# ''' -# -# builder_yaml_file = '''\ -#secrets: -#- name: builder-dockercfg-rsrua -#- name: builder-token-akqxi -#kind: ServiceAccount -#imagePullSecrets: -#- name: builder-dockercfg-rsrua -#apiVersion: v1 -#metadata: -# name: builder -# namespace: default -# resourceVersion: '302879' -# creationTimestamp: '2017-02-05T17:02:00Z' -# selfLink: /api/v1/namespaces/default/serviceaccounts/builder -# uid: cf47bca7-ebc4-11e6-b041-0ed9df7abc38 -#''' -# -# # Return values of our mocked function call. These get returned once per call. -# mock_cmd.side_effect = [ -# (0, oc_get_sa_before, ''), # First call to the mock -# (0, oc_get_sa_before, ''), # Second call to the mock -# (0, 'serviceaccount "builder" replaced', ''), # Third call to the mock -# ] -# -# # Act -# results = OCServiceAccountSecret.run_ansible(params, False) -# -# # Assert -# self.assertTrue(results['changed']) -# self.assertEqual(results['results']['returncode'], 0) -# self.assertEqual(results['state'], 'absent') -# -# # Making sure our mocks were called as we expected -# mock_cmd.assert_has_calls([ -# mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), -# mock.call(['oc', '-n', 'default', 'get', 'sa', 'builder', '-o', 'json'], None), -# mock.call(['oc', '-n', 'default', 'replace', '-f', '/tmp/builder'], None), -# ]) -# -# mock_write.assert_has_calls([ -# mock.call('/tmp/builder', builder_yaml_file) -# ]) - def tearDown(self): '''TearDown method''' pass |