summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'roles/lib_openshift/src/test')
-rwxr-xr-xroles/lib_openshift/src/test/generate-and-run-tests.sh54
-rw-r--r--roles/lib_openshift/src/test/integration/filter_plugins/test_filters.py28
-rwxr-xr-xroles/lib_openshift/src/test/integration/group.yml229
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_adm_csr.yml28
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_adm_manage_node.yml69
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_clusterrole.yml106
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_configmap.yml95
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_env.yml75
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_label.yml334
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_obj.yml207
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_process.yml83
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_project.yml83
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_pvc.yml28
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_route.yml117
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_scale.yml111
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_secret.yml125
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_service.yml133
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_serviceaccount.yml101
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_serviceaccount_secret.yml79
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_storageclass.yml87
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_user.yml240
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_version.yml17
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py277
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_adm_registry.py370
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_adm_router.py480
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_clusterrole.py115
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_configmap.py239
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_env.py548
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_group.py253
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_image.py280
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_label.py285
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_objectvalidator.py923
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_process.py572
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_project.py280
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_pvc.py377
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_route.py374
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_scale.py256
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_secret.py192
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_service.py487
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_serviceaccount.py213
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_serviceaccount_secret.py414
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_storageclass.py93
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_user.py127
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_version.py162
-rwxr-xr-xroles/lib_openshift/src/test/unit/test_oc_volume.py633
45 files changed, 10379 insertions, 0 deletions
diff --git a/roles/lib_openshift/src/test/generate-and-run-tests.sh b/roles/lib_openshift/src/test/generate-and-run-tests.sh
new file mode 100755
index 000000000..cd7571372
--- /dev/null
+++ b/roles/lib_openshift/src/test/generate-and-run-tests.sh
@@ -0,0 +1,54 @@
+#!/bin/bash -e
+
+
+if [ $# -ne 1 ] ; then
+ echo "Usage: $(basename $0) <master name>"
+ exit 1
+fi
+
+MASTER=$1
+
+
+
+# Put us in the same dir as the script.
+cd $(dirname $0)
+
+
+echo
+echo "Running lib_utils generate-and-run-tests.sh"
+echo "-------------------------------------------"
+../../../lib_utils/src/test/generate-and-run-tests.sh
+
+
+echo
+echo "Running lib_openshift generate"
+echo "------------------------------"
+../generate.py
+
+
+echo
+echo "Running lib_openshift Unit Tests"
+echo "----------------------------"
+cd unit
+
+for test in *.py; do
+ echo
+ echo "--------------------------------------------------------------------------------"
+ echo
+ echo "Running $test..."
+ ./$test
+done
+
+
+echo
+echo "Running lib_openshift Integration Tests"
+echo "-----------------------------------"
+cd ../integration
+
+for test in *.yml; do
+ echo
+ echo "--------------------------------------------------------------------------------"
+ echo
+ echo "Running $test..."
+ ./$test -vvv -e cli_master_test="$MASTER"
+done
diff --git a/roles/lib_openshift/src/test/integration/filter_plugins/test_filters.py b/roles/lib_openshift/src/test/integration/filter_plugins/test_filters.py
new file mode 100644
index 000000000..f350bd25d
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/filter_plugins/test_filters.py
@@ -0,0 +1,28 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+'''
+Custom filters for use in testing
+'''
+
+
+class FilterModule(object):
+ ''' Custom filters for use in integration testing '''
+
+ @staticmethod
+ def label_dict_to_key_value_list(label_dict):
+ ''' Given a dict of labels/values, return list of key: <key> value: <value> pairs
+
+ These are only used in integration testing.
+ '''
+
+ label_list = []
+ for key in label_dict:
+ label_list.append({'key': key, 'value': label_dict[key]})
+
+ return label_list
+
+ def filters(self):
+ ''' returns a mapping of filters to methods '''
+ return {
+ "label_dict_to_key_value_list": self.label_dict_to_key_value_list,
+ }
diff --git a/roles/lib_openshift/src/test/integration/group.yml b/roles/lib_openshift/src/test/integration/group.yml
new file mode 100755
index 000000000..25aa5727b
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/group.yml
@@ -0,0 +1,229 @@
+#!/usr/bin/ansible-playbook
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+
+ vars:
+
+ post_tasks:
+ - name: delete test group (so future tests work)
+ oc_group:
+ state: absent
+ name: jgroup
+
+ - name: delete 2nd test group (so future tests work)
+ oc_group:
+ state: absent
+ name: jgroup2
+
+ - name: delete test user (so future tests work)
+ oc_user:
+ state: absent
+ username: jdiaz@redhat.com
+
+ - name: get group list
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert group 'jgroup' (test group) does not exist
+ assert:
+ that: group_out['results'][0] == {}
+
+ - name: get group list
+ oc_group:
+ state: list
+ name: jgroup2
+ register: group_out
+ #- debug: var=group_out
+ - name: assert group 'jgroup2' (test group) does not exist
+ assert:
+ that: group_out['results'][0] == {}
+
+ - name: get user list
+ oc_user:
+ state: list
+ username: 'jdiaz@redhat.com'
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user 'jdiaz@redhat.com' (test user) does not exist
+ assert:
+ that: group_out['results'][0] == {}
+
+ - name: create group
+ oc_group:
+ state: present
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert creating group marked changed
+ assert:
+ that: group_out['changed'] == True
+
+ - name: list group
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert group actually created
+ assert:
+ that: group_out['results'][0]['metadata']['name'] == 'jgroup'
+
+ - name: re-add group
+ oc_group:
+ state: present
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert re-adding group marked not changed
+ assert:
+ that: group_out['changed'] == False
+
+
+ - name: add user with group membership
+ oc_user:
+ state: present
+ username: jdiaz@redhat.com
+ full_name: Joel Diaz
+ groups:
+ - jgroup
+ register: group_out
+ #- debug: var=group_out
+
+ - name: get group
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ - name: assert user in group
+ assert:
+ that: group_out['results'][0]['users'][0] == 'jdiaz@redhat.com'
+
+ - name: add 2nd group
+ oc_group:
+ state: present
+ name: jgroup2
+
+ - name: change group membership
+ oc_user:
+ state: present
+ username: jdiaz@redhat.com
+ full_name: Joel Diaz
+ groups:
+ - jgroup2
+ register: group_out
+ - name: assert result changed
+ assert:
+ that: group_out['changed'] == True
+
+ - name: check jgroup user membership
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user not present in previous group
+ assert:
+ that: group_out['results'][0]['users'] == []
+
+ - name: check jgroup2 user membership
+ oc_group:
+ state: list
+ name: jgroup2
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user present in new group
+ assert:
+ that: group_out['results'][0]['users'][0] == 'jdiaz@redhat.com'
+
+ - name: multi-group membership
+ oc_user:
+ state: present
+ username: jdiaz@redhat.com
+ full_name: Joel Diaz
+ groups:
+ - jgroup
+ - jgroup2
+ register: group_out
+ - name: assert result changed
+ assert:
+ that: group_out['changed'] == True
+
+ - name: check jgroup user membership
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user present in group
+ assert:
+ that: group_out['results'][0]['users'][0] == 'jdiaz@redhat.com'
+
+ - name: check jgroup2 user membership
+ oc_group:
+ state: list
+ name: jgroup2
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user still present in group
+ assert:
+ that: group_out['results'][0]['users'][0] == 'jdiaz@redhat.com'
+
+ - name: user delete (group cleanup)
+ oc_user:
+ state: absent
+ username: jdiaz@redhat.com
+ register: group_out
+
+ - name: get user list for jgroup
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert that group jgroup has no members
+ assert:
+ that: group_out['results'][0]['users'] == []
+
+ - name: get user list for jgroup2
+ oc_group:
+ state: list
+ name: jgroup2
+ register: group_out
+ #- debug: var=group_out
+ - name: assert that group jgroup2 has no members
+ assert:
+ that: group_out['results'][0]['users'] == []
+
+ - name: user without groups defined
+ oc_user:
+ state: present
+ username: jdiaz@redhat.com
+ full_name: Joel Diaz
+ register: group_out
+ - name: assert result changed
+ assert:
+ that: group_out['changed'] == True
+
+ - name: check jgroup user membership
+ oc_group:
+ state: list
+ name: jgroup
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user not present in group
+ assert:
+ that: group_out['results'][0]['users'] == []
+
+ - name: check jgroup2 user membership
+ oc_group:
+ state: list
+ name: jgroup2
+ register: group_out
+ #- debug: var=group_out
+ - name: assert user not present in group
+ assert:
+ that: group_out['results'][0]['users'] == []
diff --git a/roles/lib_openshift/src/test/integration/oc_adm_csr.yml b/roles/lib_openshift/src/test/integration/oc_adm_csr.yml
new file mode 100755
index 000000000..cad8e36f5
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_adm_csr.yml
@@ -0,0 +1,28 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_adm_csr.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: masters
+ gather_facts: no
+ user: root
+ tasks:
+ - name: list csrs
+ oc_adm_csr:
+ state: list
+ register: csrout
+
+ - debug: var=csrout
+
+ - name: list csrs
+ oc_adm_csr:
+ state: approve
+ nodes:
+ - ip-172-31-51-0-ec2-internal
+ - ip-172-31-51-246-ec2-internal
+ - ip-172-31-54-12-ec2-internal
+ - ip-172-31-58-173-ec2-internal
+ - ip-172-31-58-212-ec2-internal
+ - ip-172-31-51-246-ec2-internal
+ - ip-172-31-54-12-ec2-internal
+
+ register: csrout
+ - debug: var=csrout
diff --git a/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml b/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml
new file mode 100755
index 000000000..1ed2ef11b
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml
@@ -0,0 +1,69 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+#
+# ./oc_adm_manage_node.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: get list of nodes
+ oc_obj:
+ state: list
+ kind: node
+ register: obj_out
+
+ - name: Set the node to work with
+ set_fact:
+ node_to_test: "{{ obj_out['results']['results'][0]['items'][0]['metadata']['name'] }}"
+
+ - name: list pods from a node
+ oc_adm_manage_node:
+ list_pods: True
+ node:
+ - "{{ node_to_test }}"
+ register: podout
+ - debug: var=podout
+
+ - assert:
+ that: "'{{ node_to_test }}' in podout.results.nodes"
+ msg: Pod data was not returned
+
+ - name: set node to unschedulable
+ oc_adm_manage_node:
+ schedulable: False
+ node:
+ - "{{ node_to_test }}"
+ register: nodeout
+ - debug: var=nodeout
+
+ - name: assert that schedulable=False
+ assert:
+ that: nodeout.results.nodes[0]['schedulable'] == False
+ msg: "{{ node_to_test }} schedulable set to True"
+
+ - name: get node scheduable
+ oc_obj:
+ kind: node
+ state: list
+ name: "{{ node_to_test }}"
+ namespace: None
+ register: nodeout
+
+ - debug: var=nodeout
+
+ - name: assert that schedulable=False
+ assert:
+ that: nodeout.results.results[0]['spec']['unschedulable']
+
+ - name: set node to schedulable
+ oc_adm_manage_node:
+ schedulable: True
+ node:
+ - "{{ node_to_test }}"
+ register: nodeout
+ - debug: var=nodeout
+
+ - name: assert that schedulable=False
+ assert:
+ that: nodeout.results.nodes[0]['schedulable']
+ msg: "{{ node_to_test }} schedulable set to False"
diff --git a/roles/lib_openshift/src/test/integration/oc_clusterrole.yml b/roles/lib_openshift/src/test/integration/oc_clusterrole.yml
new file mode 100755
index 000000000..91b143f55
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_clusterrole.yml
@@ -0,0 +1,106 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+## ./oc_configmap.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+
+ post_tasks:
+ - name: create a test project
+ oc_project:
+ name: test
+ description: for tests only
+
+ ###### create test ###########
+ - name: create a clusterrole
+ oc_clusterrole:
+ state: present
+ name: operations
+ rules:
+ - apiGroups:
+ - ""
+ resources:
+ - persistentvolumes
+ attributeRestrictions: null
+ verbs:
+ - create
+ - delete
+ - deletecollection
+ - get
+ - list
+ - patch
+ - update
+ - watch
+
+ - name: fetch the created clusterrole
+ oc_clusterrole:
+ name: operations
+ state: list
+ register: croleout
+
+ - debug: var=croleout
+
+ - name: assert clusterrole exists
+ assert:
+ that:
+ - croleout.results.results.metadata.name == 'operations'
+ - croleout.results.results.rules[0].resources[0] == 'persistentvolumes'
+ ###### end create test ###########
+
+ ###### update test ###########
+ - name: update a clusterrole
+ oc_clusterrole:
+ state: present
+ name: operations
+ rules:
+ - apiGroups:
+ - ""
+ resources:
+ - persistentvolumes
+ - serviceaccounts
+ - services
+ attributeRestrictions: null
+ verbs:
+ - create
+ - delete
+ - deletecollection
+ - get
+ - list
+ - patch
+ - update
+ - watch
+
+ - name: fetch the created clusterrole
+ oc_clusterrole:
+ name: operations
+ state: list
+ register: croleout
+
+ - debug: var=croleout
+
+ - name: assert clusterrole is updated
+ assert:
+ that:
+ - croleout.results.results.metadata.name == 'operations'
+ - "'persistentvolumes' in croleout.results.results.rules[0].resources"
+ - "'serviceaccounts' in croleout.results.results.rules[0].resources"
+ - "'services' in croleout.results.results.rules[0].resources"
+ ###### end create test ###########
+
+ ###### delete test ###########
+ - name: delete a clusterrole
+ oc_clusterrole:
+ state: absent
+ name: operations
+
+ - name: fetch the clusterrole
+ oc_clusterrole:
+ name: operations
+ state: list
+ register: croleout
+
+ - debug: var=croleout
+
+ - name: assert operations does not exist
+ assert:
+ that: "'\"operations\" not found' in croleout.results.stderr"
diff --git a/roles/lib_openshift/src/test/integration/oc_configmap.yml b/roles/lib_openshift/src/test/integration/oc_configmap.yml
new file mode 100755
index 000000000..6a452ccec
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_configmap.yml
@@ -0,0 +1,95 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+## ./oc_configmap.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ vars:
+ filename: /tmp/test_configmap_from_file
+
+ post_tasks:
+ - name: Setup a file with known contents
+ copy:
+ content: This is a file
+ dest: "{{ filename }}"
+
+ - name: create a test project
+ oc_project:
+ name: test
+ description: for tests only
+
+ ###### create test ###########
+ - name: create a configmap
+ oc_configmap:
+ state: present
+ name: configmaptest
+ namespace: test
+ from_file:
+ config: "{{ filename }}"
+ from_literal:
+ foo: bar
+
+ - name: fetch the created configmap
+ oc_configmap:
+ name: configmaptest
+ state: list
+ namespace: test
+ register: cmout
+
+ - debug: var=cmout
+
+ - name: assert configmaptest exists
+ assert:
+ that:
+ - cmout.results.results[0].metadata.name == 'configmaptest'
+ - cmout.results.results[0].data.foo == 'bar'
+ ###### end create test ###########
+
+ ###### update test ###########
+ - name: create a configmap
+ oc_configmap:
+ state: present
+ name: configmaptest
+ namespace: test
+ from_file:
+ config: "{{ filename }}"
+ from_literal:
+ foo: notbar
+ deployment_type: openshift-enterprise
+
+ - name: fetch the updated configmap
+ oc_configmap:
+ name: configmaptest
+ state: list
+ namespace: test
+ register: cmout
+
+ - debug: var=cmout
+
+ - name: assert configmaptest exists
+ assert:
+ that:
+ - cmout.results.results[0].metadata.name == 'configmaptest'
+ - cmout.results.results[0].data.deployment_type == 'openshift-enterprise'
+ - cmout.results.results[0].data.foo == 'notbar'
+ ###### end update test ###########
+
+ ###### delete test ###########
+ - name: delete a configmap
+ oc_configmap:
+ state: absent
+ name: configmaptest
+ namespace: test
+
+ - name: fetch the updated configmap
+ oc_configmap:
+ name: configmaptest
+ state: list
+ namespace: test
+ register: cmout
+
+ - debug: var=cmout
+
+ - name: assert configmaptest exists
+ assert:
+ that: "'\"configmaptest\" not found' in cmout.results.stderr"
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..cbb97ed46
--- /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/integration/oc_label.yml b/roles/lib_openshift/src/test/integration/oc_label.yml
new file mode 100755
index 000000000..22cf687c5
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_label.yml
@@ -0,0 +1,334 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+#
+# ./oc_label.yml -e "cli_master_test=$OPENSHIFT_MASTER
+#
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+
+ vars:
+ - def_namespace: default
+ - def_kind: node
+
+ pre_tasks:
+ - name: ensure needed vars are defined
+ fail:
+ msg: "{{ item }} not defined"
+ when: item is not defined
+ with_items:
+ - cli_master_test # ansible inventory instance to run playbook against
+
+ tasks:
+ - name: get list of nodes
+ oc_obj:
+ state: list
+ kind: node
+ register: obj_out
+
+ - name: Set the node to work with
+ set_fact:
+ node_to_test: "{{ obj_out['results']['results'][0]['items'][0]['metadata']['name'] }}"
+
+ - name: delete test labels (start from known starting position)
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ - key: testlabel3
+
+ - name: list to check whether our test labels already exist
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: original_labels
+ - name: assert that testlabel2 and testlabel3 test labels don't exist
+ assert:
+ that: original_labels['results']['labels'][0]['testlabel2'] is not defined and
+ original_labels['results']['labels'][0]['testlabel3'] is not defined
+ msg: "{{ original_labels['results']['labels'] }}"
+
+ - name: add label
+ oc_label:
+ state: add
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ value: "yes"
+ register: label_out
+ - name: assert adding label marked as changed
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: test if add label succeeded
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: assert that testlabel2 label actually added
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is defined and
+ label_out['results']['labels'][0]['testlabel2'] == "yes"
+ msg: "{{ label_out }}"
+
+ - name: test that re-adding does nothing
+ oc_label:
+ state: add
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ value: "yes"
+ register: label_out
+ - name: assert that re-adding made no changes
+ assert:
+ that: label_out['changed'] == False
+ msg: "{{ label_out }}"
+
+ - name: test that modifying existing label marked modified
+ oc_label:
+ state: add
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ value: "different"
+ register: label_out
+ - name: assert that modifying existing label marked modified
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: test if modify label actually did modification
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: assert that testlabel2 label actually modified
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is defined and
+ label_out['results']['labels'][0]['testlabel2'] == "different"
+ msg: "{{ label_out['results']['labels'] }}"
+
+ - name: delete non-existant label
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabelnone
+ register: label_out
+ - name: assert that deleting non-existant label marked not changed
+ assert:
+ that: label_out['changed'] == False
+ msg: "{{ label_out }}"
+
+ - name: delete label
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ register: label_out
+ - name: assert that deleting existing label marked changed
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: re-delete label
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ register: label_out
+ - name: assert that re-deleting label marked not changed
+ assert:
+ that: label_out['changed'] == False
+ msg: "{{ label_out }}"
+
+ - name: check whether really deleted
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: assert label actually deleted
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is not defined
+ msg: "{{ label_out }}"
+
+ - name: add two labels
+ oc_label:
+ state: add
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ value: "yes"
+ - key: testlabel3
+ value: "yes"
+ register: label_out
+ - name: assert that adding two labels marked as changed
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: check whether both labels are there
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: assert that both labels actually exist
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is defined and
+ label_out['results']['labels'][0]['testlabel2'] == 'yes' and
+ label_out['results']['labels'][0]['testlabel3'] is defined and
+ label_out['results']['labels'][0]['testlabel3'] == 'yes'
+ msg: "{{ label_out['results']['labels'] }}"
+
+ - name: check whether two deletes work
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ - key: testlabel3
+ register: label_out
+ - name: assert that change were made when delete both labels
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: check whether re-two deletes makes no changes
+ oc_label:
+ state: absent
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels:
+ - key: testlabel2
+ - key: testlabel3
+ register: label_out
+ - name: assert that change was not made when re-delete both labels
+ assert:
+ that: label_out['changed'] == False
+ msg: "{{ label_out }}"
+
+ - set_fact:
+ original_labels_as_key_value_list: "{{ original_labels['results']['labels'][0] | label_dict_to_key_value_list }}"
+
+ - name: check that present with original label list makes no changes
+ oc_label:
+ state: present
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels: "{{ original_labels_as_key_value_list }}"
+ register: label_out
+ - name: assert that no changes are made when current list matches existing list
+ assert:
+ that: label_out['changed'] == False
+ msg: "{{ label_out }}"
+
+ - name: check that present with extra item makes changes
+ oc_label:
+ state: present
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels: "{{ original_labels_as_key_value_list + [{'key': 'testlabel2', 'value': 'yes'}] }}"
+ register: label_out
+ - name: assert that changes were made
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: get current label list
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: asssert that new label was actually added
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is defined and
+ label_out['results']['labels'][0]['testlabel2'] == 'yes'
+ msg: "{{ label_out['results']['labels'] }}"
+
+ - name: check that present with changed item makes changes
+ oc_label:
+ state: present
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels: "{{ original_labels_as_key_value_list + [{'key': 'testlabel2', 'value': 'different'}]}}"
+ register: label_out
+ - name: assert that changes were made when existing key's value is changed
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: get current label list
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: asssert that changed label was actually changed
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is defined and
+ label_out['results']['labels'][0]['testlabel2'] == 'different'
+ msg: "{{ label_out['results']['labels'] }}"
+
+ - name: check that present with removed extra item makes changes
+ oc_label:
+ state: present
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ labels: "{{ original_labels_as_key_value_list }}"
+ register: label_out
+ - name: assert that changes were made
+ assert:
+ that: label_out['changed'] == True
+ msg: "{{ label_out }}"
+
+ - name: get current label list
+ oc_label:
+ state: list
+ namespace: "{{ def_namespace }}"
+ kind: "{{ def_kind }}"
+ name: "{{ node_to_test }}"
+ register: label_out
+ - name: asssert that present-removed actually removed
+ assert:
+ that: label_out['results']['labels'][0]['testlabel2'] is not defined
+ msg: "{{ label_out }}"
diff --git a/roles/lib_openshift/src/test/integration/oc_obj.yml b/roles/lib_openshift/src/test/integration/oc_obj.yml
new file mode 100755
index 000000000..c22a2f6a9
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_obj.yml
@@ -0,0 +1,207 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_obj.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create test project
+ oc_project:
+ name: test
+ description: all things test
+ node_selector: ""
+
+ # Create Check #
+ - name: create a dc
+ oc_obj:
+ state: present
+ name: mysql
+ namespace: test
+ kind: dc
+ content:
+ path: /tmp/dcout
+ data:
+ apiVersion: v1
+ kind: DeploymentConfig
+ metadata:
+ labels:
+ name: mysql
+ name: mysql
+ spec:
+ replicas: 1
+ selector: {}
+ strategy:
+ resources: {}
+ type: Recreate
+ template:
+ metadata:
+ labels:
+ name: mysql
+ spec:
+ containers:
+ - env:
+ - name: MYSQL_USER
+ value: mysql
+ - name: MYSQL_PASSWORD
+ value: mysql
+ - name: MYSQL_DATABASE
+ value: mysql
+ - name: MYSQL_ROOT_PASSWORD
+ value: mysql
+ image: openshift/mysql-55-centos7:latest
+ imagePullPolicy: Always
+ name: mysql
+ ports:
+ - containerPort: 3306
+ name: tcp-3306
+ protocol: TCP
+ resources: {}
+ securityContext:
+ capabilities: {}
+ privileged: false
+ terminationMessagePath: /dev/termination-log
+ dnsPolicy: ClusterFirst
+ restartPolicy: Always
+ securityContext: {}
+ terminationGracePeriodSeconds: 31
+ triggers:
+ - type: ConfigChange
+ - imageChangeParams:
+ automatic: true
+ containerNames:
+ - mysql
+ from:
+ kind: ImageStreamTag
+ name: mysql:latest
+ type: ImageChange
+
+ - name: fetch created dc
+ oc_obj:
+ name: mysql
+ kind: dc
+ state: list
+ namespace: test
+ register: dcout
+
+ - debug: var=dcout
+
+ - assert:
+ that:
+ - dcout.results.returncode == 0
+ - dcout.results.results[0].metadata.name == 'mysql'
+ # End Create Check #
+
+
+ # Delete Check #
+ - name: delete created dc
+ oc_obj:
+ name: mysql
+ kind: dc
+ state: absent
+ namespace: test
+ register: dcout
+
+ - name: fetch delete dc
+ oc_obj:
+ name: mysql
+ kind: dc
+ state: list
+ namespace: test
+ register: dcout
+
+ - debug: var=dcout
+
+ - assert:
+ that:
+ - dcout.results.returncode == 0
+ - "'\"mysql\" not found' in dcout.results.stderr"
+ # End Delete Check #
+
+ # Delete selector Check #
+ - name: create a dc
+ oc_obj:
+ state: present
+ name: mysql
+ namespace: test
+ kind: dc
+ content:
+ path: /tmp/dcout
+ data:
+ apiVersion: v1
+ kind: DeploymentConfig
+ metadata:
+ labels:
+ name: mysql
+ name: mysql
+ spec:
+ replicas: 1
+ selector: {}
+ strategy:
+ resources: {}
+ type: Recreate
+ template:
+ metadata:
+ labels:
+ name: mysql
+ spec:
+ containers:
+ - env:
+ - name: MYSQL_USER
+ value: mysql
+ - name: MYSQL_PASSWORD
+ value: mysql
+ - name: MYSQL_DATABASE
+ value: mysql
+ - name: MYSQL_ROOT_PASSWORD
+ value: mysql
+ image: openshift/mysql-55-centos7:latest
+ imagePullPolicy: Always
+ name: mysql
+ ports:
+ - containerPort: 3306
+ name: tcp-3306
+ protocol: TCP
+ resources: {}
+ securityContext:
+ capabilities: {}
+ privileged: false
+ terminationMessagePath: /dev/termination-log
+ dnsPolicy: ClusterFirst
+ restartPolicy: Always
+ securityContext: {}
+ terminationGracePeriodSeconds: 31
+ triggers:
+ - type: ConfigChange
+ - imageChangeParams:
+ automatic: true
+ containerNames:
+ - mysql
+ from:
+ kind: ImageStreamTag
+ name: mysql:latest
+ type: ImageChange
+
+ - name: delete using selector
+ oc_obj:
+ namespace: test
+ selector: name=mysql
+ kind: dc
+ state: absent
+ register: dcout
+
+ - debug: var=dcout
+
+ - name: get the dc
+ oc_obj:
+ namespace: test
+ selector: name=mysql
+ kind: dc
+ state: list
+ register: dcout
+
+ - debug: var=dcout
+
+ - assert:
+ that:
+ - dcout.results.returncode == 0
+ - dcout.results.results[0]["items"]|length == 0
diff --git a/roles/lib_openshift/src/test/integration/oc_process.yml b/roles/lib_openshift/src/test/integration/oc_process.yml
new file mode 100755
index 000000000..7ea4c6b99
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_process.yml
@@ -0,0 +1,83 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/:../../../../lib_utils/library
+
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ vars:
+ template_name: mysql-ephemeral
+ ns_name: test
+
+ post_tasks:
+ - name: get the mysql-ephemeral template
+ oc_obj:
+ name: mysql-ephemeral
+ state: list
+ namespace: openshift
+ kind: template
+ register: mysqltempl
+
+ - name: fix namespace
+ yedit:
+ src: /tmp/mysql-template
+ key: metadata.namespace
+ value: test
+ backup: false
+ content: "{{ mysqltempl.results.results[0] | to_yaml }}"
+
+ - name: create the test namespace
+ oc_obj:
+ name: test
+ state: present
+ namespace: test
+ kind: namespace
+ content:
+ path: /tmp/ns_test
+ data:
+ apiVersion: v1
+ kind: Namespace
+ metadata:
+ name: test
+ spec:
+ finalizers:
+ - openshift.io/origin
+ - kubernetes
+ register: mysqltempl
+
+ - name: create the mysql-ephemeral template
+ oc_obj:
+ name: mysql-ephemeral
+ state: present
+ namespace: test
+ kind: template
+ files:
+ - /tmp/mysql-template
+ delete_after: True
+ register: mysqltempl
+
+ - name: process mysql-ephemeral
+ oc_process:
+ template_name: mysql-ephemeral
+ namespace: test
+ params:
+ NAMESPACE: test
+ DATABASE_SERVICE_NAME: testdb
+ create: False
+ reconcile: false
+ register: procout
+
+ - assert:
+ that:
+ - not procout.changed
+ - procout.results.results['items'][0]['metadata']['name'] == 'testdb'
+ - procout.results.results['items'][0]['kind'] == 'Service'
+ - procout.results.results['items'][1]['metadata']['name'] == 'testdb'
+ - procout.results.results['items'][1]['kind'] == 'DeploymentConfig'
+ msg: process failed on template
+
+ - name: remove namespace test
+ oc_obj:
+ kind: namespace
+ name: test
+ namespace: test
+ state: absent
diff --git a/roles/lib_openshift/src/test/integration/oc_project.yml b/roles/lib_openshift/src/test/integration/oc_project.yml
new file mode 100755
index 000000000..9f700c62c
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_project.yml
@@ -0,0 +1,83 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_project.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create a project
+ oc_project:
+ display_name: operations project
+ name: operations
+ state: present
+ description: All things operations
+ node_selector:
+ - ops_only=true
+ register: projout
+ - debug: var=projout
+
+ - assert:
+ that:
+ - "projout.results.results['metadata']['name'] == 'operations'"
+ - projout.changed
+ msg: project create failed.
+
+ - name: create a project
+ oc_project:
+ display_name: operations project
+ name: operations
+ state: present
+ description: All things operations
+ node_selector:
+ - ops_only=true
+ register: projout
+ - debug: var=projout
+
+ - assert:
+ that:
+ - "projout.results.results['metadata']['name'] == 'operations'"
+ - projout.changed == False
+ msg: project create failed.
+
+ - name: update a project
+ oc_project:
+ display_name: operations project one
+ name: operations
+ state: present
+ description: All things operations
+ node_selector:
+ - ops_only=true
+ register: projout
+ - debug: var=projout
+
+ - assert:
+ that:
+ - "projout.results.results['metadata']['annotations']['openshift.io/display-name'] == 'operations project one'"
+ - projout.changed == True
+ msg: project create failed.
+
+ - name: update a project
+ oc_project:
+ name: operations
+ state: list
+ register: projout
+ - debug: var=projout
+
+ - assert:
+ that:
+ - "projout.results['metadata']['annotations']['openshift.io/display-name'] == 'operations project one'"
+ - projout.changed == False
+ - projout.state == 'list'
+ msg: project list failed.
+
+ - name: delete a project
+ oc_project:
+ name: operations
+ state: absent
+ register: projout
+ - debug: var=projout
+
+ - assert:
+ that:
+ - projout.changed == True
+ msg: project delete failed.
diff --git a/roles/lib_openshift/src/test/integration/oc_pvc.yml b/roles/lib_openshift/src/test/integration/oc_pvc.yml
new file mode 100755
index 000000000..fb3a4781f
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_pvc.yml
@@ -0,0 +1,28 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_pvc.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create pvc
+ oc_pvc:
+ state: present
+ name: oc-pvc-create-test
+ namespace: default
+ volume_capacity: 3G
+ access_modes:
+ - ReadWriteOnce
+ selector:
+ foo: bar
+ storage_class_name: my-storage-class-name
+ register: pvcout
+ - debug: var=pvcout
+
+ - assert:
+ that:
+ - pvcout.results.results[0]['metadata']['name'] == 'oc-pvc-create-test'
+ - pvcout.results.results[0]['spec']['storageClassName'] == 'my-storage-class-name'
+ - pvcout.results.results[0]['spec']['selector']['matchLabels']['foo'] == 'bar'
+ - pvcout.changed
+ msg: pvc create failed.
diff --git a/roles/lib_openshift/src/test/integration/oc_route.yml b/roles/lib_openshift/src/test/integration/oc_route.yml
new file mode 100755
index 000000000..b9d635eaa
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_route.yml
@@ -0,0 +1,117 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_route.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create route
+ oc_route:
+ name: test
+ namespace: default
+ tls_termination: edge
+ cert_content: testing cert
+ cacert_content: testing cacert
+ key_content: key content
+ service_name: test
+ host: test.example
+ register: routeout
+ - debug: var=routeout
+
+ - assert:
+ that: "routeout.results.results[0]['metadata']['name'] == 'test'"
+ msg: route create failed
+
+ - name: get route
+ oc_route:
+ state: list
+ name: test
+ namespace: default
+ register: routeout
+ - debug: var=routeout
+
+ - assert:
+ that: "routeout.results[0]['metadata']['name'] == 'test'"
+ msg: get route failed
+
+ - name: delete route
+ oc_route:
+ state: absent
+ name: test
+ namespace: default
+ register: routeout
+ - debug: var=routeout
+
+ - assert:
+ that: "routeout.results.returncode == 0"
+ msg: delete route failed
+
+ - name: create route
+ oc_route:
+ name: test
+ namespace: default
+ tls_termination: edge
+ cert_content: testing cert
+ cacert_content: testing cacert
+ key_content: testing key
+ service_name: test
+ host: test.example
+ register: routeout
+ - debug: var=routeout
+
+ - name: create route noop
+ oc_route:
+ name: test
+ namespace: default
+ tls_termination: edge
+ cert_content: testing cert
+ cacert_content: testing cacert
+ key_content: testing key
+ service_name: test
+ host: test.example
+ register: routeout
+ - debug: var=routeout
+
+ - assert:
+ that: "routeout.changed == False"
+ msg: Route create not idempotent
+
+ - name: delete route
+ oc_route:
+ name: test
+ namespace: default
+ state: absent
+ register: routeout
+
+ - name: create route
+ oc_route:
+ name: test
+ namespace: default
+ tls_termination: passthrough
+ service_name: test
+ host: test.example
+ port: 8443
+ register: routeout
+
+ - assert:
+ that:
+ - "routeout.changed == True"
+ - "routeout.results['results'][0]['spec']['port']['targetPort'] == 8443"
+ msg: Route create not idempotent
+
+ - name: create route
+ oc_route:
+ name: test
+ namespace: default
+ tls_termination: passthrough
+ service_name: test
+ host: test.example
+ port: 8444
+ register: routeout
+ - debug: var=routeout
+
+ - assert:
+ that:
+ - "routeout.changed == True"
+ - "routeout.results.results[0]['spec']['port']['targetPort'] == 8444"
+ msg: Route update not idempotent
diff --git a/roles/lib_openshift/src/test/integration/oc_scale.yml b/roles/lib_openshift/src/test/integration/oc_scale.yml
new file mode 100755
index 000000000..43a42c589
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_scale.yml
@@ -0,0 +1,111 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_scale.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: list oc scale for default router dc
+ oc_scale:
+ state: list
+ name: router
+ namespace: default
+ kind: dc
+ register: scaleout
+ - debug: var=scaleout
+
+ - assert:
+ that:
+ - "'result' in scaleout"
+ - scaleout.result > 0
+ msg: "Did not find 'result' in returned value or result not > 0."
+
+ - name: get the rc for router
+ oc_obj:
+ state: list
+ kind: dc
+ namespace: default
+ selector: router=router
+ register: rcout
+ - debug:
+ msg: "{{ rcout.results.results[0]['items'][-1]['metadata']['name'] }}"
+
+ - name: scale dc to 1
+ oc_scale:
+ name: router
+ namespace: default
+ kind: dc
+ replicas: 1
+ register: scaleout
+ - debug: var=scaleout
+
+ # The preferred method here would be to let the module
+ # detect when its finished and time out
+ - name: let the scale happen
+ pause:
+ seconds: 10
+ when: scaleout.changed
+
+ - name: fetch the current router pods
+ oc_obj:
+ selector: router=router
+ namespace: default
+ kind: pod
+ state: list
+ register: pods
+ - debug: var=pods
+
+ - assert:
+ that:
+ - "'results' in pods and 'results' in pods.results"
+ - "{{ pods.results.results[0]['items']|length }} == 1"
+ msg: "Did not find 1 replica in scale results."
+
+ - name: scale dc to 2
+ oc_scale:
+ name: router
+ namespace: default
+ kind: dc
+ replicas: 2
+ register: scaleout
+ - debug: var=scaleout
+
+ # The preferred method here would be to let the module
+ # detect when its finished and time out
+ - name: let the scale happen
+ pause:
+ seconds: 30
+
+ - name: fetch the current router pods
+ oc_obj:
+ selector: router=router
+ namespace: default
+ kind: pod
+ state: list
+ register: pods
+ - debug: var=pods
+
+ - assert:
+ that:
+ - "'results' in pods and 'results' in pods.results"
+ - "{{ pods.results.results[0]['items']|length }} == 2"
+ msg: "Did not find 1 replica in scale results."
+
+
+ # Test scale on non-existent dc
+ - name: scale non-existent dc
+ oc_scale:
+ name: not_there
+ kind: dc
+ replicas: 2
+ register: scaleout
+ ignore_errors: True
+
+ - debug: var=scaleout
+
+ - assert:
+ that:
+ - scaleout.changed == False
+ - scaleout.msg.returncode == 1
+ - "'msg' in scaleout and 'stderr' in scaleout.msg"
+ msg: "Deploymentconfig exists. This should error."
diff --git a/roles/lib_openshift/src/test/integration/oc_secret.yml b/roles/lib_openshift/src/test/integration/oc_secret.yml
new file mode 100755
index 000000000..e0456bd6e
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_secret.yml
@@ -0,0 +1,125 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ vars_prompt:
+ - name: cli_master_test
+ prompt: "Master to run against"
+ private: false
+ default: localhost
+
+ vars:
+ secret_name: secret-int-test
+ ns_name: default
+ config_path: "/tmp/{{ secret_name }}--config.yml"
+ passwords_path: "/tmp/{{ secret_name }}--passwords.yml"
+
+ post_tasks:
+
+ - name: Setup our files to test with
+ copy:
+ dest: "{{ item.name }}"
+ content: "{{ item.content }}"
+ with_items:
+ - name: "{{ config_path }}"
+ content: |
+ value: True
+ - name: "{{ passwords_path }}"
+ content: |
+ test1
+ test2
+ test3
+ test4
+
+
+ - name: Make sure we're starting with a clean slate
+ oc_secret:
+ state: absent
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ register: secret_out
+
+ - name: Test adding a secret - Act
+ oc_secret:
+ state: present
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ files:
+ - name: config.yml
+ path: "{{ config_path }}"
+ - name: passwords.yml
+ path: "{{ passwords_path }}"
+ register: secret_out
+
+ - name: Test adding a secret - Assert
+ assert:
+ that:
+ - "secret_out.results.returncode == 0"
+ - "secret_out.changed == True"
+
+ - name: Test secret present idempotentcy - Act
+ oc_secret:
+ state: present
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ files:
+ - name: config.yml
+ path: "{{ config_path }}"
+ - name: passwords.yml
+ path: "{{ passwords_path }}"
+ register: secret_out
+
+ - name: Test secret present idempotentcy - Assert
+ assert:
+ that:
+ - "secret_out.changed == false"
+
+ - name: Test list secrets - Act
+ oc_secret:
+ state: list
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ register: secret_out
+
+ - name: Test list secrets - Assert
+ assert:
+ that:
+ - "secret_out.changed == false"
+ - "secret_out.results.exists == true"
+
+
+ - name: Test secret absent - Act
+ oc_secret:
+ state: absent
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ register: secret_out
+
+ - name: Test secret absent - Assert
+ assert:
+ that:
+ - "secret_out.changed == true"
+ - "secret_out.results.returncode == 0"
+
+ - name: Test secret absent idempotentcy - Act
+ oc_secret:
+ state: absent
+ namespace: "{{ ns_name }}"
+ name: "{{ secret_name }}"
+ register: secret_out
+
+ - name: Test secret idempotentcy - Assert
+ assert:
+ that:
+ - "secret_out.changed == false"
+
+
+ - name: Clean up the files we created
+ file:
+ state: absent
+ path: "{{ item }}"
+ with_items:
+ - "/tmp/{{ secret_name }}--config.yml"
+ - "/tmp/{{ secret_name }}--passwords.yml"
diff --git a/roles/lib_openshift/src/test/integration/oc_service.yml b/roles/lib_openshift/src/test/integration/oc_service.yml
new file mode 100755
index 000000000..29535f24a
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_service.yml
@@ -0,0 +1,133 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_service.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create the default registry service
+ oc_service:
+ namespace: default
+ name: test-registry
+ ports:
+ - name: 9000-tcp
+ port: 9000
+ protocol: TCP
+ targetPort: 9000
+ selector:
+ test-registtry: default
+ session_affinity: ClientIP
+ service_type: ClusterIP
+ labels:
+ component: test-registry
+ infra: registry
+ register: svc_out
+ - debug: var=svc_out
+
+ - assert:
+ that:
+ - "svc_out.results.results[0]['metadata']['name'] == 'test-registry'"
+ - svc_out.changed
+ - "svc_out.results.results[0]['metadata']['labels']['component'] == 'test-registry'"
+ - "svc_out.results.results[0]['metadata']['labels']['infra'] == 'registry'"
+ msg: service create failed.
+
+ # Test idempotent create
+ - name: NOOP create the default registry service
+ oc_service:
+ namespace: default
+ name: test-registry
+ ports:
+ - name: 9000-tcp
+ port: 9000
+ protocol: TCP
+ targetPort: 9000
+ selector:
+ test-registtry: default
+ session_affinity: ClientIP
+ service_type: ClusterIP
+ register: svc_out
+
+ - assert:
+ that:
+ - "svc_out.results.results[0]['metadata']['name'] == 'test-registry'"
+ - svc_out.changed == False
+ msg: service create failed. No changes expected
+
+ - name: create the default registry service
+ oc_service:
+ namespace: default
+ name: test-registry
+ ports:
+ - name: 9000-tcp
+ port: 9000
+ protocol: TCP
+ targetPort: 9000
+ selector:
+ test-registtry: default
+ session_affinity: ClientIP
+ service_type: ClusterIP
+ register: svc_out
+
+ - assert:
+ that: "svc_out.results.results[0]['metadata']['name'] == 'test-registry'"
+ msg: service create failed
+
+ - name: oc_service
+ oc_service:
+ name: test-registry
+ namespace: default
+ state: list
+ register: svc_out
+
+ - assert:
+ that: "svc_out.results.results[0]['metadata']['name'] == 'test-registry'"
+ msg: service create failed
+
+ - name: create the default registry service
+ oc_service:
+ namespace: default
+ name: test-registry
+ ports:
+ - name: 9001-tcp
+ port: 9001
+ protocol: TCP
+ targetPort: 9001
+ selector:
+ test-registtry: default
+ session_affinity: ClientIP
+ service_type: ClusterIP
+ register: svc_out
+
+ - assert:
+ that: "svc_out.results.results[0]['spec']['ports'][0]['name'] == '9001-tcp'"
+ msg: service update failed
+
+ - name: oc delete service
+ oc_service:
+ name: test-registry
+ namespace: default
+ state: absent
+ register: svc_out
+ - debug: var=svc_out
+
+ - assert:
+ that:
+ - "svc_out.results['returncode'] == 0"
+ - "svc_out.results.results == ''"
+ msg: service delete failed
+
+ - name: oc get service
+ oc_service:
+ name: test-registry
+ namespace: default
+ state: list
+ register: svc_out
+ - debug: var=svc_out
+
+ - assert:
+ that:
+ - svc_out.changed == False
+ - svc_out.results.returncode == 0
+ - "'not found' in svc_out.results.stderr"
+ msg: service get failed
diff --git a/roles/lib_openshift/src/test/integration/oc_serviceaccount.yml b/roles/lib_openshift/src/test/integration/oc_serviceaccount.yml
new file mode 100755
index 000000000..46369b8f4
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_serviceaccount.yml
@@ -0,0 +1,101 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ vars_prompt:
+ - name: cli_master_test
+ prompt: "Master to run against"
+ private: false
+ default: localhost
+
+ vars:
+ service_account_name: serviceaccount-int-test
+ ns_name: default
+
+ post_tasks:
+ - name: Make sure we start clean - Arrange
+ oc_serviceaccount:
+ state: absent
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+
+ - name: List when account does not exist - Act
+ oc_serviceaccount:
+ state: list
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+ register: saout
+
+ - name: List when account does not exist - Assert
+ assert:
+ that:
+ - "saout.changed == False"
+ - "saout.state == 'list'"
+ - "saout.results == [{}]"
+
+ - name: create serviceaccount - Act
+ oc_serviceaccount:
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+ secrets:
+ - one
+ - two
+ - three
+ register: saout
+
+ - name: create serviceaccount - Assert
+ assert:
+ that:
+ - "saout.changed == True"
+ - "saout.state == 'present'"
+ - "saout.results.returncode == 0"
+ - "saout.results.results.0.metadata.name == '{{ service_account_name }}'"
+ - "saout.results.results.0.metadata.namespace == '{{ ns_name }}'"
+
+ - name: create serviceaccount - check idempotency - Act
+ oc_serviceaccount:
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+ secrets:
+ - one
+ - two
+ - three
+ register: saout
+
+ - name: create serviceaccount - check idempotency - Assert
+ assert:
+ that:
+ - "saout.changed == False"
+ - "saout.state == 'present'"
+ - "saout.results.returncode == 0"
+ - "saout.results.results.0.metadata.name == '{{ service_account_name }}'"
+ - "saout.results.results.0.metadata.namespace == '{{ ns_name }}'"
+
+ - name: Delete serviceaccount - Act
+ oc_serviceaccount:
+ state: absent
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+ register: saout
+
+ - name: Delete serviceaccount - Assert
+ assert:
+ that:
+ - "saout.changed == True"
+ - "saout.state == 'absent'"
+ - "saout.results.returncode == 0"
+
+ - name: Delete serviceaccount - check idempotency - Act
+ oc_serviceaccount:
+ state: absent
+ name: "{{ service_account_name }}"
+ namespace: "{{ ns_name }}"
+ register: saout
+
+ - name: Delete serviceaccount - check idempotency - Assert
+ assert:
+ that:
+ - "saout.changed == False"
+ - "saout.state == 'absent'"
diff --git a/roles/lib_openshift/src/test/integration/oc_serviceaccount_secret.yml b/roles/lib_openshift/src/test/integration/oc_serviceaccount_secret.yml
new file mode 100755
index 000000000..d3bd9f3aa
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_serviceaccount_secret.yml
@@ -0,0 +1,79 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+
+ vars:
+ namespace: default
+ service_account_name: someserviceaccountname
+ secret_name: somesecretname
+
+ vars_prompt:
+ - name: cli_master_test
+ prompt: "Master to run against"
+ private: false
+ default: localhost
+
+ post_tasks:
+ - name: create service account to test with - Arrange
+ oc_serviceaccount:
+ namespace: "{{ namespace }}"
+ name: "{{ service_account_name }}"
+
+ - name: create secret to test with - Arrange
+ oc_secret:
+ namespace: "{{ namespace }}"
+ name: "{{ secret_name }}"
+ contents:
+ - path: blah
+ data: blahdeblah
+
+ - name: Ensure the service account and secret are not linked - Arrange
+ oc_serviceaccount_secret:
+ state: absent
+ service_account: "{{ service_account_name }}"
+ secret: "{{ secret_name }}"
+ namespace: "{{ namespace }}"
+
+ - name: get secrets of a service account - Act
+ oc_serviceaccount_secret:
+ state: list
+ service_account: builder
+ namespace: "{{ namespace }}"
+ register: sasecretout
+
+ - name: get secrets of a service account - Assert
+ assert:
+ that:
+ - "sasecretout.changed == False"
+ - "sasecretout.state == 'list'"
+ - "sasecretout.results | length > 0"
+
+ - name: Test linking a service account and secret - Act
+ oc_serviceaccount_secret:
+ service_account: "{{ service_account_name }}"
+ secret: "{{ secret_name }}"
+ namespace: "{{ namespace }}"
+ register: sasecretout
+
+ - name: Test linking a service account and secret - Assert
+ assert:
+ that:
+ - "sasecretout.changed == True"
+ - "sasecretout.state == 'present'"
+ - "sasecretout.results.returncode == 0"
+ - "sasecretout.results.results | length > 0"
+
+ - name: Test linking a service account and secret - idempotency - Act
+ oc_serviceaccount_secret:
+ service_account: "{{ service_account_name }}"
+ secret: "{{ secret_name }}"
+ namespace: "{{ namespace }}"
+ register: sasecretout
+
+ - name: Test linking a service account and secret - idempotency - Assert
+ assert:
+ that:
+ - "sasecretout.changed == False"
+ - "sasecretout.state == 'present'"
diff --git a/roles/lib_openshift/src/test/integration/oc_storageclass.yml b/roles/lib_openshift/src/test/integration/oc_storageclass.yml
new file mode 100755
index 000000000..c82f9dedb
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_storageclass.yml
@@ -0,0 +1,87 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_storageclass.yml -M ../../../library -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: create a storageclass
+ oc_storageclass:
+ name: testsc
+ parameters:
+ type: gp2
+ default_storage_class: "true"
+ register: sc_out
+ - debug: var=sc_out
+
+ - assert:
+ that:
+ - "sc_out.results.results[0]['metadata']['name'] == 'testsc'"
+ - sc_out.changed
+ - "sc_out.results.results[0]['parameters']['type'] == 'gp2'"
+ msg: storageclass create failed.
+
+ # Test idempotent create
+ - name: NOOP create the storageclass
+ oc_storageclass:
+ name: testsc
+ parameters:
+ type: gp2
+ default_storage_class: "true"
+ register: sc_out
+
+ - assert:
+ that:
+ - "sc_out.results.results[0]['metadata']['name'] == 'testsc'"
+ - sc_out.changed == False
+ msg: storageclass create failed. No changes expected
+
+ - name: test list storageclass
+ oc_storageclass:
+ name: testsc
+ state: list
+ register: sc_out
+ - debug: var=sc_out
+
+ - assert:
+ that: "sc_out.results[0]['metadata']['name'] == 'testsc'"
+ msg: storageclass list failed
+
+ - name: update the storageclass
+ oc_storageclass:
+ name: testsc
+ parameters:
+ type: gp2
+ encrypted: "true"
+ default_storage_class: "true"
+ register: sc_out
+
+ - assert:
+ that: "sc_out.results.results[0]['parameters']['encrypted'] == 'true'"
+ msg: storageclass update failed
+
+ - name: oc delete storageclass
+ oc_storageclass:
+ name: testsc
+ state: absent
+ register: sc_out
+ - debug: var=sc_out
+
+ - assert:
+ that:
+ - "sc_out.results['returncode'] == 0"
+ - "sc_out.results.results == {}"
+ msg: storageclass delete failed
+
+ - name: oc get storageclass
+ oc_storageclass:
+ name: testsc
+ state: list
+ register: sc_out
+ - debug: var=sc_out
+
+ - assert:
+ that:
+ - sc_out.changed == False
+ - "sc_out.results == [{}]"
+ msg: storageclass get failed
diff --git a/roles/lib_openshift/src/test/integration/oc_user.yml b/roles/lib_openshift/src/test/integration/oc_user.yml
new file mode 100755
index 000000000..9b4290052
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_user.yml
@@ -0,0 +1,240 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+#
+# ./oc_user.yml -e "cli_master_test=$OPENSHIFT_MASTER
+#
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+
+ vars:
+ test_user: testuser@email.com
+ test_user_fullname: "Test User"
+ pre_tasks:
+ - name: ensure needed vars are defined
+ fail:
+ msg: "{{ item }} no defined"
+ when: item is not defined
+ with_items:
+ - cli_master_test # ansible inventory instance to run playbook against
+
+ tasks:
+ - name: delete test user (so future tests work)
+ oc_user:
+ state: absent
+ username: "{{ test_user }}"
+
+ - name: get user list
+ oc_user:
+ state: list
+ username: "{{ test_user }}"
+ register: user_out
+ - name: "assert test user does not exist"
+ assert:
+ that: user_out['results'][0] == {}
+ msg: "{{ user_out }}"
+
+ - name: get all list
+ oc_user:
+ state: list
+ register: user_out
+ #- debug: var=user_out
+
+ - name: add test user
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ full_name: "{{ test_user_fullname }}"
+ register: user_out
+ - name: assert result set to changed
+ assert:
+ that: user_out['changed'] == True
+ msg: "{{ user_out }}"
+
+ - name: check test user actually added
+ oc_user:
+ state: list
+ username: "{{ test_user }}"
+ register: user_out
+ - name: assert user actually added
+ assert:
+ that: user_out['results'][0]['metadata']['name'] == "{{ test_user }}" and
+ user_out['results'][0]['fullName'] == "{{ test_user_fullname }}"
+ msg: "{{ user_out }}"
+
+ - name: re-add test user
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ full_name: "{{ test_user_fullname }}"
+ register: user_out
+ - name: assert re-add result set to not changed
+ assert:
+ that: user_out['changed'] == False
+ msg: "{{ user_out }}"
+
+ - name: modify existing user
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ full_name: 'Something Different'
+ register: user_out
+ - name: assert modify existing user result set to changed
+ assert:
+ that: user_out['changed'] == True
+ msg: "{{ user_out }}"
+
+ - name: check modify test user
+ oc_user:
+ state: list
+ username: "{{ test_user }}"
+ register: user_out
+ - name: assert modification successful
+ assert:
+ that: user_out['results'][0]['metadata']['name'] == "{{ test_user }}" and
+ user_out['results'][0]['fullName'] == 'Something Different'
+ msg: "{{ user_out }}"
+
+ - name: delete test user
+ oc_user:
+ state: absent
+ username: "{{ test_user }}"
+ register: user_out
+ - name: assert delete marked changed
+ assert:
+ that: user_out['changed'] == True
+ msg: "{{ user_out }}"
+
+ - name: check delete user
+ oc_user:
+ state: list
+ username: "{{ test_user }}"
+ register: user_out
+ - name: assert deletion successful
+ assert:
+ that: user_out['results'][0] == {}
+ msg: "{{ user_out }}"
+
+ - name: re-delete test user
+ oc_user:
+ state: absent
+ username: "{{ test_user }}"
+ register: user_out
+ - name: check re-delete marked not changed
+ assert:
+ that: user_out['changed'] == False
+ msg: "{{ user_out }}"
+
+ - name: delete test group
+ oc_obj:
+ kind: group
+ state: absent
+ name: integration-test-group
+
+ - name: create test group
+ command: oadm groups new integration-test-group
+
+ - name: check group creation
+ oc_obj:
+ kind: group
+ state: list
+ name: integration-test-group
+ register: user_out
+ - name: assert test group created
+ assert:
+ that: user_out['results']['results'][0]['metadata']['name'] == "integration-test-group"
+ msg: "{{ user_out }}"
+
+ - name: create user with group membership
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ groups:
+ - "integration-test-group"
+ register: user_out
+ - debug: var=user_out
+ - name: get group user members
+ oc_obj:
+ kind: group
+ state: list
+ name: integration-test-group
+ register: user_out
+ - name: assert user group membership
+ assert:
+ that: "'{{ test_user }}' in user_out['results']['results'][0]['users'][0]"
+ msg: "{{ user_out }}"
+
+ - name: delete second test group
+ oc_obj:
+ kind: group
+ state: absent
+ name: integration-test-group2
+
+ - name: create empty second group
+ command: oadm groups new integration-test-group2
+
+ - name: update user with second group membership
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ groups:
+ - "integration-test-group"
+ - "integration-test-group2"
+ register: user_out
+ - name: assert adding more group changed
+ assert:
+ that: user_out['changed'] == True
+
+ - name: get group memberships
+ oc_obj:
+ kind: group
+ state: list
+ name: "{{ item }}"
+ with_items:
+ - integration-test-group
+ - integration-test-group2
+ register: user_out
+ - name: assert user member of above groups
+ assert:
+ that: "'{{ test_user }}' in user_out['results'][0]['results']['results'][0]['users'] and \
+ '{{ test_user }}' in user_out['results'][1]['results']['results'][0]['users']"
+ msg: "{{ user_out }}"
+
+ - name: update user with only one group
+ oc_user:
+ state: present
+ username: "{{ test_user }}"
+ groups:
+ - "integration-test-group2"
+ register: user_out
+ - assert:
+ that: user_out['changed'] == True
+
+ - name: get group memberships
+ oc_obj:
+ kind: group
+ state: list
+ name: "{{ item }}"
+ with_items:
+ - "integration-test-group"
+ - "integration-test-group2"
+ register: user_out
+ - debug: var=user_out
+ - name: assert proper user membership
+ assert:
+ that: "'{{ test_user }}' not in user_out['results'][0]['results']['results'][0]['users'] and \
+ '{{ test_user }}' in user_out['results'][1]['results']['results'][0]['users']"
+
+ - name: clean up test groups
+ oc_obj:
+ kind: group
+ state: absent
+ name: "{{ item }}"
+ with_items:
+ - "integration-test-group"
+ - "integration-test-group2"
+
+ - name: clean up test user
+ oc_user:
+ state: absent
+ username: "{{ test_user }}"
diff --git a/roles/lib_openshift/src/test/integration/oc_version.yml b/roles/lib_openshift/src/test/integration/oc_version.yml
new file mode 100755
index 000000000..52336d8da
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_version.yml
@@ -0,0 +1,17 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_version.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: Get openshift version
+ oc_version:
+ register: versionout
+
+ - debug: var=versionout
+
+ - assert:
+ that:
+ - "'oc_numeric' in versionout.results.keys()"
+ msg: "Did not find 'oc_numeric' in version results."
diff --git a/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py b/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py
new file mode 100755
index 000000000..312b1ecbb
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py
@@ -0,0 +1,277 @@
+'''
+ Unit tests for oc_adm_manage_node
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_adm_manage_node import ManageNode, locate_oc_binary # noqa: E402
+
+
+class ManageNodeTest(unittest.TestCase):
+ '''
+ Test class for oc_adm_manage_node
+ '''
+
+ @mock.patch('oc_adm_manage_node.Utils.create_tmpfile_copy')
+ @mock.patch('oc_adm_manage_node.ManageNode.openshift_cmd')
+ def test_list_pods(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'node': ['ip-172-31-49-140.ec2.internal'],
+ 'schedulable': None,
+ 'selector': None,
+ 'pod_selector': None,
+ 'list_pods': True,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'evacuate': False,
+ 'grace_period': False,
+ 'dry_run': False,
+ 'force': False}
+
+ pod_list = '''{
+ "metadata": {},
+ "items": [
+ {
+ "metadata": {
+ "name": "docker-registry-1-xuhik",
+ "generateName": "docker-registry-1-",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/pods/docker-registry-1-xuhik",
+ "uid": "ae2a25a2-e316-11e6-80eb-0ecdc51fcfc4",
+ "resourceVersion": "1501",
+ "creationTimestamp": "2017-01-25T15:55:23Z",
+ "labels": {
+ "deployment": "docker-registry-1",
+ "deploymentconfig": "docker-registry",
+ "docker-registry": "default"
+ },
+ "annotations": {
+ "openshift.io/deployment-config.latest-version": "1",
+ "openshift.io/deployment-config.name": "docker-registry",
+ "openshift.io/deployment.name": "docker-registry-1",
+ "openshift.io/scc": "restricted"
+ }
+ },
+ "spec": {}
+ },
+ {
+ "metadata": {
+ "name": "router-1-kp3m3",
+ "generateName": "router-1-",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/pods/router-1-kp3m3",
+ "uid": "9e71f4a5-e316-11e6-80eb-0ecdc51fcfc4",
+ "resourceVersion": "1456",
+ "creationTimestamp": "2017-01-25T15:54:56Z",
+ "labels": {
+ "deployment": "router-1",
+ "deploymentconfig": "router",
+ "router": "router"
+ },
+ "annotations": {
+ "openshift.io/deployment-config.latest-version": "1",
+ "openshift.io/deployment-config.name": "router",
+ "openshift.io/deployment.name": "router-1",
+ "openshift.io/scc": "hostnetwork"
+ }
+ },
+ "spec": {}
+ }]
+}'''
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --list-pods",
+ "results": pod_list,
+ "returncode": 0}
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = ManageNode.run_ansible(params, False)
+
+ # returned a single node
+ self.assertTrue(len(results['results']['nodes']) == 1)
+ # returned 2 pods
+ self.assertTrue(len(results['results']['nodes']['ip-172-31-49-140.ec2.internal']) == 2)
+
+ @mock.patch('oc_adm_manage_node.Utils.create_tmpfile_copy')
+ @mock.patch('oc_adm_manage_node.ManageNode.openshift_cmd')
+ def test_schedulable_false(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'node': ['ip-172-31-49-140.ec2.internal'],
+ 'schedulable': False,
+ 'selector': None,
+ 'pod_selector': None,
+ 'list_pods': False,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'evacuate': False,
+ 'grace_period': False,
+ 'dry_run': False,
+ 'force': False}
+
+ node = [{
+ "apiVersion": "v1",
+ "kind": "Node",
+ "metadata": {
+ "creationTimestamp": "2017-01-26T14:34:43Z",
+ "labels": {
+ "beta.kubernetes.io/arch": "amd64",
+ "beta.kubernetes.io/instance-type": "m4.large",
+ "beta.kubernetes.io/os": "linux",
+ "failure-domain.beta.kubernetes.io/region": "us-east-1",
+ "failure-domain.beta.kubernetes.io/zone": "us-east-1c",
+ "hostname": "opstest-node-compute-0daaf",
+ "kubernetes.io/hostname": "ip-172-31-51-111.ec2.internal",
+ "ops_node": "old",
+ "region": "us-east-1",
+ "type": "compute"
+ },
+ "name": "ip-172-31-51-111.ec2.internal",
+ "resourceVersion": "6936",
+ "selfLink": "/api/v1/nodes/ip-172-31-51-111.ec2.internal",
+ "uid": "93d7fdfb-e3d4-11e6-a982-0e84250fc302"
+ },
+ "spec": {
+ "externalID": "i-06bb330e55c699b0f",
+ "providerID": "aws:///us-east-1c/i-06bb330e55c699b0f",
+ }}]
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": "/usr/bin/oc get node -o json ip-172-31-49-140.ec2.internal",
+ "results": node,
+ "returncode": 0},
+ {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --schedulable=False",
+ "results": "NAME STATUS AGE\n" +
+ "ip-172-31-49-140.ec2.internal Ready,SchedulingDisabled 5h\n",
+ "returncode": 0}]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = ManageNode.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['nodes'][0]['name'], 'ip-172-31-49-140.ec2.internal')
+ self.assertEqual(results['results']['nodes'][0]['schedulable'], False)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_adm_registry.py b/roles/lib_openshift/src/test/unit/test_oc_adm_registry.py
new file mode 100755
index 000000000..77787fe87
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_adm_registry.py
@@ -0,0 +1,370 @@
+#!/usr/bin/env python
+'''
+ Unit tests for oc adm registry
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_adm_registry import Registry, locate_oc_binary # noqa: E402
+
+
+# pylint: disable=too-many-public-methods
+class RegistryTest(unittest.TestCase):
+ '''
+ Test class for Registry
+ '''
+ dry_run = '''{
+ "kind": "List",
+ "apiVersion": "v1",
+ "metadata": {},
+ "items": [
+ {
+ "kind": "ServiceAccount",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "registry",
+ "creationTimestamp": null
+ }
+ },
+ {
+ "kind": "ClusterRoleBinding",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "registry-registry-role",
+ "creationTimestamp": null
+ },
+ "userNames": [
+ "system:serviceaccount:default:registry"
+ ],
+ "groupNames": null,
+ "subjects": [
+ {
+ "kind": "ServiceAccount",
+ "namespace": "default",
+ "name": "registry"
+ }
+ ],
+ "roleRef": {
+ "kind": "ClusterRole",
+ "name": "system:registry"
+ }
+ },
+ {
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "docker-registry",
+ "creationTimestamp": null,
+ "labels": {
+ "docker-registry": "default"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ }
+ ],
+ "replicas": 1,
+ "test": false,
+ "selector": {
+ "docker-registry": "default"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "docker-registry": "default"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "registry-storage",
+ "emptyDir": {}
+ }
+ ],
+ "containers": [
+ {
+ "name": "registry",
+ "image": "openshift3/ose-docker-registry:v3.5.0.39",
+ "ports": [
+ {
+ "containerPort": 5000
+ }
+ ],
+ "env": [
+ {
+ "name": "REGISTRY_HTTP_ADDR",
+ "value": ":5000"
+ },
+ {
+ "name": "REGISTRY_HTTP_NET",
+ "value": "tcp"
+ },
+ {
+ "name": "REGISTRY_HTTP_SECRET",
+ "value": "WQjSGeUu5KFZRTwGeIXgwIjyraNDLmdJblsFbtzZdF8="
+ },
+ {
+ "name": "REGISTRY_MIDDLEWARE_REPOSITORY_OPENSHIFT_ENFORCEQUOTA",
+ "value": "false"
+ }
+ ],
+ "resources": {
+ "requests": {
+ "cpu": "100m",
+ "memory": "256Mi"
+ }
+ },
+ "volumeMounts": [
+ {
+ "name": "registry-storage",
+ "mountPath": "/registry"
+ }
+ ],
+ "livenessProbe": {
+ "httpGet": {
+ "path": "/healthz",
+ "port": 5000
+ },
+ "initialDelaySeconds": 10,
+ "timeoutSeconds": 5
+ },
+ "readinessProbe": {
+ "httpGet": {
+ "path": "/healthz",
+ "port": 5000
+ },
+ "timeoutSeconds": 5
+ },
+ "securityContext": {
+ "privileged": false
+ }
+ }
+ ],
+ "nodeSelector": {
+ "type": "infra"
+ },
+ "serviceAccountName": "registry",
+ "serviceAccount": "registry"
+ }
+ }
+ },
+ "status": {
+ "latestVersion": 0,
+ "observedGeneration": 0,
+ "replicas": 0,
+ "updatedReplicas": 0,
+ "availableReplicas": 0,
+ "unavailableReplicas": 0
+ }
+ },
+ {
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "docker-registry",
+ "creationTimestamp": null,
+ "labels": {
+ "docker-registry": "default"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "5000-tcp",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "docker-registry": "default"
+ },
+ "clusterIP": "172.30.119.110",
+ "sessionAffinity": "ClientIP"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }
+ ]}'''
+
+ @mock.patch('oc_adm_registry.locate_oc_binary')
+ @mock.patch('oc_adm_registry.Utils._write')
+ @mock.patch('oc_adm_registry.Utils.create_tmpfile_copy')
+ @mock.patch('oc_adm_registry.Registry._run')
+ def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write, mock_oc_binary):
+ ''' Testing state present '''
+ params = {'state': 'present',
+ 'debug': False,
+ 'namespace': 'default',
+ 'name': 'docker-registry',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'images': None,
+ 'latest_images': None,
+ 'labels': {"docker-registry": "default", "another-label": "val"},
+ 'ports': ['5000'],
+ 'replicas': 1,
+ 'selector': 'type=infra',
+ 'service_account': 'registry',
+ 'mount_host': None,
+ 'volume_mounts': None,
+ 'env_vars': {},
+ 'enforce_quota': False,
+ 'force': False,
+ 'daemonset': False,
+ 'tls_key': None,
+ 'tls_certificate': None,
+ 'edits': []}
+
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server (NotFound): deploymentconfigs "docker-registry" not found'),
+ (1, '', 'Error from server (NotFound): service "docker-registry" not found'),
+ (0, RegistryTest.dry_run, ''),
+ (0, '', ''),
+ (0, '', ''),
+ ]
+
+ mock_tmpfile_copy.return_value = '/tmp/mocked_kubeconfig'
+
+ mock_oc_binary.return_value = 'oc'
+
+ results = Registry.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ for result in results['results']['results']:
+ self.assertEqual(result['returncode'], 0)
+
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'dc', 'docker-registry', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'svc', 'docker-registry', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'adm', 'registry',
+ "--labels=another-label=val,docker-registry=default",
+ '--ports=5000', '--replicas=1', '--selector=type=infra',
+ '--service-account=registry', '--dry-run=True', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None), ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_adm_router.py b/roles/lib_openshift/src/test/unit/test_oc_adm_router.py
new file mode 100755
index 000000000..dcf768e08
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_adm_router.py
@@ -0,0 +1,480 @@
+#!/usr/bin/env python
+'''
+ Unit tests for oc adm router
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_adm_router import Router, locate_oc_binary # noqa: E402
+
+
+# pylint: disable=too-many-public-methods
+class RouterTest(unittest.TestCase):
+ '''
+ Test class for Router
+ '''
+ dry_run = '''{
+ "kind": "List",
+ "apiVersion": "v1",
+ "metadata": {},
+ "items": [
+ {
+ "kind": "ServiceAccount",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "creationTimestamp": null
+ }
+ },
+ {
+ "kind": "ClusterRoleBinding",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router-router-role",
+ "creationTimestamp": null
+ },
+ "userNames": [
+ "system:serviceaccount:default:router"
+ ],
+ "groupNames": null,
+ "subjects": [
+ {
+ "kind": "ServiceAccount",
+ "namespace": "default",
+ "name": "router"
+ }
+ ],
+ "roleRef": {
+ "kind": "ClusterRole",
+ "name": "system:router"
+ }
+ },
+ {
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "creationTimestamp": null,
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "type": "Rolling",
+ "rollingParams": {
+ "maxUnavailable": "25%",
+ "maxSurge": 0
+ },
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ }
+ ],
+ "replicas": 2,
+ "test": false,
+ "selector": {
+ "router": "router"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "server-certificate",
+ "secret": {
+ "secretName": "router-certs"
+ }
+ }
+ ],
+ "containers": [
+ {
+ "name": "router",
+ "image": "openshift3/ose-haproxy-router:v3.5.0.39",
+ "ports": [
+ {
+ "containerPort": 80
+ },
+ {
+ "containerPort": 443
+ },
+ {
+ "name": "stats",
+ "containerPort": 1936,
+ "protocol": "TCP"
+ }
+ ],
+ "env": [
+ {
+ "name": "DEFAULT_CERTIFICATE_DIR",
+ "value": "/etc/pki/tls/private"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HOSTNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_INSECURE",
+ "value": "false"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_INTERNAL_ADDRESS"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_PARTITION_PATH"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_PASSWORD"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_PRIVKEY",
+ "value": "/etc/secret-volume/router.pem"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_USERNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_VXLAN_GW_CIDR"
+ },
+ {
+ "name": "ROUTER_SERVICE_HTTPS_PORT",
+ "value": "443"
+ },
+ {
+ "name": "ROUTER_SERVICE_HTTP_PORT",
+ "value": "80"
+ },
+ {
+ "name": "ROUTER_SERVICE_NAME",
+ "value": "router"
+ },
+ {
+ "name": "ROUTER_SERVICE_NAMESPACE",
+ "value": "default"
+ },
+ {
+ "name": "ROUTER_SUBDOMAIN"
+ },
+ {
+ "name": "STATS_PASSWORD",
+ "value": "eSfUICQyyr"
+ },
+ {
+ "name": "STATS_PORT",
+ "value": "1936"
+ },
+ {
+ "name": "STATS_USERNAME",
+ "value": "admin"
+ }
+ ],
+ "resources": {
+ "requests": {
+ "cpu": "100m",
+ "memory": "256Mi"
+ }
+ },
+ "volumeMounts": [
+ {
+ "name": "server-certificate",
+ "readOnly": true,
+ "mountPath": "/etc/pki/tls/private"
+ }
+ ],
+ "livenessProbe": {
+ "httpGet": {
+ "path": "/healthz",
+ "port": 1936,
+ "host": "localhost"
+ },
+ "initialDelaySeconds": 10
+ },
+ "readinessProbe": {
+ "httpGet": {
+ "path": "/healthz",
+ "port": 1936,
+ "host": "localhost"
+ },
+ "initialDelaySeconds": 10
+ },
+ "imagePullPolicy": "IfNotPresent"
+ }
+ ],
+ "nodeSelector": {
+ "type": "infra"
+ },
+ "serviceAccountName": "router",
+ "serviceAccount": "router",
+ "hostNetwork": true,
+ "securityContext": {}
+ }
+ }
+ },
+ "status": {
+ "latestVersion": 0,
+ "observedGeneration": 0,
+ "replicas": 0,
+ "updatedReplicas": 0,
+ "availableReplicas": 0,
+ "unavailableReplicas": 0
+ }
+ },
+ {
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "creationTimestamp": null,
+ "labels": {
+ "router": "router"
+ },
+ "annotations": {
+ "service.alpha.openshift.io/serving-cert-secret-name": "router-certs"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ }
+ ],
+ "selector": {
+ "router": "router"
+ }
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }
+ ]
+}'''
+
+ @mock.patch('oc_adm_router.locate_oc_binary')
+ @mock.patch('oc_adm_router.Utils._write')
+ @mock.patch('oc_adm_router.Utils.create_tmpfile_copy')
+ @mock.patch('oc_adm_router.Router._run')
+ def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write, mock_oc_binary):
+ ''' Testing a create '''
+ params = {'state': 'present',
+ 'debug': False,
+ 'namespace': 'default',
+ 'name': 'router',
+ 'default_cert': None,
+ 'cert_file': None,
+ 'key_file': None,
+ 'cacert_file': None,
+ 'labels': {"router": "router", "another-label": "val"},
+ 'ports': ['80:80', '443:443'],
+ 'images': None,
+ 'latest_images': None,
+ 'clusterip': None,
+ 'portalip': None,
+ 'session_affinity': None,
+ 'service_type': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'replicas': 2,
+ 'selector': 'type=infra',
+ 'service_account': 'router',
+ 'router_type': None,
+ 'host_network': None,
+ 'external_host': None,
+ 'external_host_vserver': None,
+ 'external_host_insecure': False,
+ 'external_host_partition_path': None,
+ 'external_host_username': None,
+ 'external_host_password': None,
+ 'external_host_private_key': None,
+ 'expose_metrics': False,
+ 'metrics_image': None,
+ 'stats_user': None,
+ 'stats_password': None,
+ 'stats_port': 1936,
+ 'edits': []}
+
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server (NotFound): deploymentconfigs "router" not found'),
+ (1, '', 'Error from server (NotFound): service "router" not found'),
+ (1, '', 'Error from server (NotFound): serviceaccount "router" not found'),
+ (1, '', 'Error from server (NotFound): secret "router-certs" not found'),
+ (1, '', 'Error from server (NotFound): clsuterrolebinding "router-router-role" not found'),
+ (0, RouterTest.dry_run, ''),
+ (0, '', ''),
+ (0, '', ''),
+ (0, '', ''),
+ (0, '', ''),
+ (0, '', ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc',
+ ]
+
+ results = Router.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ for result in results['results']['results']:
+ self.assertEqual(result['returncode'], 0)
+
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'dc', 'router', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'svc', 'router', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'sa', 'router', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'secret', 'router-certs', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'clusterrolebinding', 'router-router-role', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'adm', 'router', 'router', '--expose-metrics=False', '--external-host-insecure=False',
+ "--labels=another-label=val,router=router",
+ '--ports=80:80,443:443', '--replicas=2', '--selector=type=infra', '--service-account=router',
+ '--stats-port=1936', '--dry-run=True', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None)])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_clusterrole.py b/roles/lib_openshift/src/test/unit/test_oc_clusterrole.py
new file mode 100755
index 000000000..189f16bda
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_clusterrole.py
@@ -0,0 +1,115 @@
+'''
+ Unit tests for oc clusterrole
+'''
+
+import copy
+import os
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_clusterrole import OCClusterRole # noqa: E402
+
+
+class OCClusterRoleTest(unittest.TestCase):
+ '''
+ Test class for OCClusterRole
+ '''
+
+ # run_ansible input parameters
+ params = {
+ 'state': 'present',
+ 'name': 'operations',
+ 'rules': [
+ {'apiGroups': [''],
+ 'attributeRestrictions': None,
+ 'verbs': ['create', 'delete', 'deletecollection',
+ 'get', 'list', 'patch', 'update', 'watch'],
+ 'resources': ['persistentvolumes']}
+ ],
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False,
+ }
+
+ @mock.patch('oc_clusterrole.locate_oc_binary')
+ @mock.patch('oc_clusterrole.Utils.create_tmpfile_copy')
+ @mock.patch('oc_clusterrole.Utils._write')
+ @mock.patch('oc_clusterrole.OCClusterRole._run')
+ def test_adding_a_clusterrole(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_loc_binary):
+ ''' Testing adding a project '''
+
+ params = copy.deepcopy(OCClusterRoleTest.params)
+
+ clusterrole = '''{
+ "apiVersion": "v1",
+ "kind": "ClusterRole",
+ "metadata": {
+ "creationTimestamp": "2017-03-27T14:19:09Z",
+ "name": "operations",
+ "resourceVersion": "23",
+ "selfLink": "/oapi/v1/clusterrolesoperations",
+ "uid": "57d358fe-12f8-11e7-874a-0ec502977670"
+ },
+ "rules": [
+ {
+ "apiGroups": [
+ ""
+ ],
+ "attributeRestrictions": null,
+ "resources": [
+ "persistentvolumes"
+ ],
+ "verbs": [
+ "create",
+ "delete",
+ "deletecollection",
+ "get",
+ "list",
+ "patch",
+ "update",
+ "watch"
+ ]
+ }
+ ]
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: clusterrole "operations" not found'),
+ (1, '', 'Error from server: namespaces "operations" not found'),
+ (0, '', ''), # created
+ (0, clusterrole, ''), # fetch it
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_loc_binary.side_effect = [
+ 'oc',
+ ]
+
+ # Act
+ results = OCClusterRole.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['results']['results']['metadata']['name'], 'operations')
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'clusterrole', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'get', 'clusterrole', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY], None),
+ mock.call(['oc', 'get', 'clusterrole', 'operations', '-o', 'json'], None),
+ ])
diff --git a/roles/lib_openshift/src/test/unit/test_oc_configmap.py b/roles/lib_openshift/src/test/unit/test_oc_configmap.py
new file mode 100755
index 000000000..27042c64b
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_configmap.py
@@ -0,0 +1,239 @@
+'''
+ Unit tests for oc configmap
+'''
+
+import copy
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_configmap import OCConfigMap, locate_oc_binary # noqa: E402
+
+
+class OCConfigMapTest(unittest.TestCase):
+ '''
+ Test class for OCConfigMap
+ '''
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'configmap',
+ 'from_file': {},
+ 'from_literal': {},
+ 'namespace': 'test'}
+
+ @mock.patch('oc_configmap.Utils._write')
+ @mock.patch('oc_configmap.Utils.create_tmpfile_copy')
+ @mock.patch('oc_configmap.OCConfigMap._run')
+ def test_create_configmap(self, mock_run, mock_tmpfile_copy, mock_write):
+ ''' Testing a configmap create '''
+ # TODO
+ return
+ params = copy.deepcopy(OCConfigMapTest.params)
+ params['from_file'] = {'test': '/root/file'}
+ params['from_literal'] = {'foo': 'bar'}
+
+ configmap = '''{
+ "apiVersion": "v1",
+ "data": {
+ "foo": "bar",
+ "test": "this is a file\\n"
+ },
+ "kind": "ConfigMap",
+ "metadata": {
+ "creationTimestamp": "2017-03-20T20:24:35Z",
+ "name": "configmap",
+ "namespace": "test"
+ }
+ }'''
+
+ mock_run.side_effect = [
+ (1, '', 'Error from server (NotFound): configmaps "configmap" not found'),
+ (0, '', ''),
+ (0, configmap, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCConfigMap.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'configmap')
+
+ @mock.patch('oc_configmap.Utils._write')
+ @mock.patch('oc_configmap.Utils.create_tmpfile_copy')
+ @mock.patch('oc_configmap.OCConfigMap._run')
+ def test_update_configmap(self, mock_run, mock_tmpfile_copy, mock_write):
+ ''' Testing a configmap create '''
+ params = copy.deepcopy(OCConfigMapTest.params)
+ params['from_file'] = {'test': '/root/file'}
+ params['from_literal'] = {'foo': 'bar', 'deployment_type': 'openshift-enterprise'}
+
+ configmap = '''{
+ "apiVersion": "v1",
+ "data": {
+ "foo": "bar",
+ "test": "this is a file\\n"
+ },
+ "kind": "ConfigMap",
+ "metadata": {
+ "creationTimestamp": "2017-03-20T20:24:35Z",
+ "name": "configmap",
+ "namespace": "test"
+
+ }
+ }'''
+
+ mod_configmap = '''{
+ "apiVersion": "v1",
+ "data": {
+ "foo": "bar",
+ "deployment_type": "openshift-enterprise",
+ "test": "this is a file\\n"
+ },
+ "kind": "ConfigMap",
+ "metadata": {
+ "creationTimestamp": "2017-03-20T20:24:35Z",
+ "name": "configmap",
+ "namespace": "test"
+
+ }
+ }'''
+
+ mock_run.side_effect = [
+ (0, configmap, ''),
+ (0, mod_configmap, ''),
+ (0, configmap, ''),
+ (0, '', ''),
+ (0, mod_configmap, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCConfigMap.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'configmap')
+ self.assertEqual(results['results']['results'][0]['data']['deployment_type'], 'openshift-enterprise')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_env.py b/roles/lib_openshift/src/test/unit/test_oc_env.py
new file mode 100755
index 000000000..2f416c05e
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_env.py
@@ -0,0 +1,548 @@
+'''
+ Unit tests for oc_env
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_env import OCEnv, locate_oc_binary # noqa: E402
+
+
+class OCEnvTest(unittest.TestCase):
+ '''
+ Test class for OCEnv
+ '''
+
+ @mock.patch('oc_env.locate_oc_binary')
+ @mock.patch('oc_env.Utils.create_tmpfile_copy')
+ @mock.patch('oc_env.OCEnv._run')
+ def test_listing_all_env_vars(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing listing all environment variables from a dc'''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'state': 'list',
+ 'namespace': 'default',
+ 'name': 'router',
+ 'kind': 'dc',
+ 'env_vars': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False,
+ }
+
+ 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": "DEFAULT_CERTIFICATE_PATH",
+ "value": "/etc/pki/tls/private/tls.crt"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HOSTNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER"
+ },
+ {
+ "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_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ 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', 'get', 'dc', 'router', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_env.locate_oc_binary')
+ @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, mock_oc_binary):
+ ''' 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": "DEFAULT_CERTIFICATE_PATH",
+ "value": "/etc/pki/tls/private/tls.crt"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HOSTNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER"
+ },
+ {
+ "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": "DEFAULT_CERTIFICATE_PATH",
+ "value": "/etc/pki/tls/private/tls.crt"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HOSTNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_INSECURE",
+ "value": "false"
+ },
+ {
+ "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_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ 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', 'get', 'dc', 'router', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_env.locate_oc_binary')
+ @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, mock_oc_binary):
+ ''' 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_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"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HOSTNAME"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTPS_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_HTTP_VSERVER"
+ },
+ {
+ "name": "ROUTER_EXTERNAL_HOST_INSECURE",
+ "value": "false"
+ },
+ {
+ "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_before, ''),
+ (0, dc_results_before, ''),
+ (0, '', ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mock_adminkubeconfig',
+ ]
+
+ # Act
+ results = OCEnv.run_ansible(params, False)
+
+ # Assert
+ 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', 'get', 'dc', 'router', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_group.py b/roles/lib_openshift/src/test/unit/test_oc_group.py
new file mode 100755
index 000000000..8eef37810
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_group.py
@@ -0,0 +1,253 @@
+'''
+ Unit tests for oc group
+'''
+
+import copy
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_group import OCGroup, locate_oc_binary # noqa: E402
+
+
+class OCGroupTest(unittest.TestCase):
+ '''
+ Test class for OCGroup
+ '''
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'acme',
+ 'namespace': 'test'}
+
+ @mock.patch('oc_group.Utils.create_tmpfile_copy')
+ @mock.patch('oc_group.OCGroup._run')
+ def test_create_group(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a group create '''
+ params = copy.deepcopy(OCGroupTest.params)
+
+ group = '''{
+ "kind": "Group",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "acme"
+ },
+ "users": []
+ }'''
+
+ mock_run.side_effect = [
+ (1, '', 'Error from server: groups "acme" not found'),
+ (1, '', 'Error from server: groups "acme" not found'),
+ (0, '', ''),
+ (0, group, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCGroup.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'acme')
+
+ @mock.patch('oc_group.Utils.create_tmpfile_copy')
+ @mock.patch('oc_group.OCGroup._run')
+ def test_failed_get_group(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a group create '''
+ params = copy.deepcopy(OCGroupTest.params)
+ params['state'] = 'list'
+ params['name'] = 'noexist'
+
+ mock_run.side_effect = [
+ (1, '', 'Error from server: groups "acme" not found'),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCGroup.run_ansible(params, False)
+
+ self.assertTrue(results['failed'])
+
+ @mock.patch('oc_group.Utils.create_tmpfile_copy')
+ @mock.patch('oc_group.OCGroup._run')
+ def test_delete_group(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a group create '''
+ params = copy.deepcopy(OCGroupTest.params)
+ params['state'] = 'absent'
+
+ group = '''{
+ "kind": "Group",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "acme"
+ },
+ "users": [
+ "user1"
+ ]
+ }'''
+
+ mock_run.side_effect = [
+ (0, group, ''),
+ (0, '', ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCGroup.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+
+ @mock.patch('oc_group.Utils.create_tmpfile_copy')
+ @mock.patch('oc_group.OCGroup._run')
+ def test_get_group(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a group create '''
+ params = copy.deepcopy(OCGroupTest.params)
+ params['state'] = 'list'
+
+ group = '''{
+ "kind": "Group",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "acme"
+ },
+ "users": [
+ "user1"
+ ]
+ }'''
+
+ mock_run.side_effect = [
+ (0, group, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCGroup.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results'][0]['metadata']['name'], 'acme')
+ self.assertEqual(results['results'][0]['users'][0], 'user1')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_image.py b/roles/lib_openshift/src/test/unit/test_oc_image.py
new file mode 100755
index 000000000..943c8ca17
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_image.py
@@ -0,0 +1,280 @@
+'''
+ Unit tests for oc image
+'''
+import os
+import sys
+import unittest
+import mock
+import six
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_image import OCImage, locate_oc_binary # noqa: E402
+
+
+class OCImageTest(unittest.TestCase):
+ '''
+ Test class for OCImage
+ '''
+
+ @mock.patch('oc_image.Utils.create_tmpfile_copy')
+ @mock.patch('oc_image.OCImage._run')
+ def test_state_list(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a label list '''
+ params = {'registry_url': 'registry.ops.openshift.com',
+ 'image_name': 'oso-rhel7-zagg-web',
+ 'image_tag': 'int',
+ 'namespace': 'default',
+ 'state': 'list',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ istream = '''{
+ "kind": "ImageStream",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web",
+ "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be",
+ "resourceVersion": "8135944",
+ "generation": 1,
+ "creationTimestamp": "2017-01-17T17:36:05Z",
+ "annotations": {
+ "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z"
+ }
+ },
+ "spec": {
+ "tags": [
+ {
+ "name": "int",
+ "annotations": null,
+ "from": {
+ "kind": "DockerImage",
+ "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int"
+ },
+ "generation": 1,
+ "importPolicy": {}
+ }
+ ]
+ },
+ "status": {
+ "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web",
+ "tags": [
+ {
+ "tag": "int",
+ "items": [
+ {
+ "created": "2017-01-17T17:36:05Z",
+ "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392",
+ "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392",
+ "generation": 1
+ }
+ ]
+ }
+ ]
+ }
+ }
+ '''
+
+ mock_cmd.side_effect = [
+ (0, istream, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCImage.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEquals(results['results']['results'][0]['metadata']['name'], 'oso-rhel7-zagg-web')
+
+ @mock.patch('oc_image.Utils.create_tmpfile_copy')
+ @mock.patch('oc_image.OCImage._run')
+ def test_state_present(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a image present '''
+ params = {'registry_url': 'registry.ops.openshift.com',
+ 'image_name': 'oso-rhel7-zagg-web',
+ 'image_tag': 'int',
+ 'namespace': 'default',
+ 'state': 'present',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ istream = '''{
+ "kind": "ImageStream",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web",
+ "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be",
+ "resourceVersion": "8135944",
+ "generation": 1,
+ "creationTimestamp": "2017-01-17T17:36:05Z",
+ "annotations": {
+ "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z"
+ }
+ },
+ "spec": {
+ "tags": [
+ {
+ "name": "int",
+ "annotations": null,
+ "from": {
+ "kind": "DockerImage",
+ "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int"
+ },
+ "generation": 1,
+ "importPolicy": {}
+ }
+ ]
+ },
+ "status": {
+ "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web",
+ "tags": [
+ {
+ "tag": "int",
+ "items": [
+ {
+ "created": "2017-01-17T17:36:05Z",
+ "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392",
+ "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392",
+ "generation": 1
+ }
+ ]
+ }
+ ]
+ }
+ }
+ '''
+
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: imagestreams "oso-rhel7-zagg-web" not found'),
+ (0, '', ''),
+ (0, istream, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCImage.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['results'][0]['metadata']['name'] == 'oso-rhel7-zagg-web')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_label.py b/roles/lib_openshift/src/test/unit/test_oc_label.py
new file mode 100755
index 000000000..5453266c1
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_label.py
@@ -0,0 +1,285 @@
+'''
+ Unit tests for oc label
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_label import OCLabel, locate_oc_binary # noqa: E402
+
+
+class OCLabelTest(unittest.TestCase):
+ '''
+ Test class for OCLabel
+ '''
+
+ @mock.patch('oc_label.Utils.create_tmpfile_copy')
+ @mock.patch('oc_label.OCLabel._run')
+ def test_state_list(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a label list '''
+ params = {'name': 'default',
+ 'namespace': 'default',
+ 'labels': None,
+ 'state': 'list',
+ 'kind': 'namespace',
+ 'selector': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ ns = '''{
+ "kind": "Namespace",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "default",
+ "selfLink": "/api/v1/namespaces/default",
+ "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
+ "resourceVersion": "403024",
+ "creationTimestamp": "2017-01-26T14:28:55Z",
+ "labels": {
+ "storage_pv_quota": "False"
+ },
+ "annotations": {
+ "openshift.io/node-selector": "",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c1,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
+ "openshift.io/sa.scc.uid-range": "1000000000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ mock_cmd.side_effect = [
+ (0, ns, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCLabel.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertTrue(results['results']['labels'] == [{'storage_pv_quota': 'False'}])
+
+ @mock.patch('oc_label.Utils.create_tmpfile_copy')
+ @mock.patch('oc_label.OCLabel._run')
+ def test_state_present(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a label list '''
+ params = {'name': 'default',
+ 'namespace': 'default',
+ 'labels': [
+ {'key': 'awesomens', 'value': 'testinglabel'},
+ {'key': 'storage_pv_quota', 'value': 'False'}
+ ],
+ 'state': 'present',
+ 'kind': 'namespace',
+ 'selector': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ ns = '''{
+ "kind": "Namespace",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "default",
+ "selfLink": "/api/v1/namespaces/default",
+ "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
+ "resourceVersion": "403024",
+ "creationTimestamp": "2017-01-26T14:28:55Z",
+ "labels": {
+ "storage_pv_quota": "False"
+ },
+ "annotations": {
+ "openshift.io/node-selector": "",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c1,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
+ "openshift.io/sa.scc.uid-range": "1000000000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ ns1 = '''{
+ "kind": "Namespace",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "default",
+ "selfLink": "/api/v1/namespaces/default",
+ "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22",
+ "resourceVersion": "403024",
+ "creationTimestamp": "2017-01-26T14:28:55Z",
+ "labels": {
+ "storage_pv_quota": "False",
+ "awesomens": "testinglabel"
+ },
+ "annotations": {
+ "openshift.io/node-selector": "",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c1,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
+ "openshift.io/sa.scc.uid-range": "1000000000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ mock_cmd.side_effect = [
+ (0, ns, ''),
+ (0, '', ''),
+ (0, ns1, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCLabel.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['results']['labels'][0] ==
+ {'storage_pv_quota': 'False', 'awesomens': 'testinglabel'})
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_objectvalidator.py b/roles/lib_openshift/src/test/unit/test_oc_objectvalidator.py
new file mode 100755
index 000000000..b19a5a880
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_objectvalidator.py
@@ -0,0 +1,923 @@
+'''
+ Unit tests for oc_objectvalidator
+'''
+
+import os
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_objectvalidator import OCObjectValidator # noqa: E402
+
+
+class OCObjectValidatorTest(unittest.TestCase):
+ '''
+ Test class for OCObjectValidator
+ '''
+
+ maxDiff = None
+
+ @mock.patch('oc_objectvalidator.locate_oc_binary')
+ @mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
+ @mock.patch('oc_objectvalidator.OCObjectValidator._run')
+ def test_no_data(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing when both all objects are empty '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ }
+
+ empty = '''{
+ "apiVersion": "v1",
+ "items": [],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+}'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (0, empty, ''),
+
+ # Second call to mock
+ (0, empty, ''),
+
+ # Third call to mock
+ (0, empty, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc',
+ ]
+
+ # Act
+ results = OCObjectValidator.run_ansible(params)
+
+ # Assert
+ self.assertNotIn('failed', results)
+ self.assertEqual(results['msg'], 'All objects are valid.')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'hostsubnet', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'netnamespace', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'namespace', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_objectvalidator.locate_oc_binary')
+ @mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
+ @mock.patch('oc_objectvalidator.OCObjectValidator._run')
+ def test_error_code(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing when we fail to get objects '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ }
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (1, '', 'Error.'),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ error_results = {
+ 'returncode': 1,
+ 'stderr': 'Error.',
+ 'stdout': '',
+ 'cmd': 'oc get hostsubnet -o json -n default',
+ 'results': [{}]
+ }
+
+ # Act
+ results = OCObjectValidator.run_ansible(params)
+
+ # Assert
+ self.assertTrue(results['failed'])
+ self.assertEqual(results['msg'], 'Failed to GET hostsubnet.')
+ self.assertEqual(results['state'], 'list')
+ self.assertEqual(results['results'], error_results)
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'hostsubnet', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_objectvalidator.locate_oc_binary')
+ @mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
+ @mock.patch('oc_objectvalidator.OCObjectValidator._run')
+ def test_valid_both(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing when both all objects are valid '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ }
+
+ valid_hostsubnet = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "host": "bar0",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:09Z",
+ "name": "bar0",
+ "namespace": "",
+ "resourceVersion": "986",
+ "selfLink": "/oapi/v1/hostsubnetsbar0",
+ "uid": "528dbb41-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ },
+ {
+ "apiVersion": "v1",
+ "host": "bar1",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:18Z",
+ "name": "bar1",
+ "namespace": "",
+ "resourceVersion": "988",
+ "selfLink": "/oapi/v1/hostsubnetsbar1",
+ "uid": "57710d84-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ },
+ {
+ "apiVersion": "v1",
+ "host": "bar2",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:26Z",
+ "name": "bar2",
+ "namespace": "",
+ "resourceVersion": "991",
+ "selfLink": "/oapi/v1/hostsubnetsbar2",
+ "uid": "5c59a28c-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+ }'''
+
+ valid_netnamespace = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:16Z",
+ "name": "foo0",
+ "namespace": "",
+ "resourceVersion": "959",
+ "selfLink": "/oapi/v1/netnamespacesfoo0",
+ "uid": "0f1c85b2-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo0"
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:26Z",
+ "name": "foo1",
+ "namespace": "",
+ "resourceVersion": "962",
+ "selfLink": "/oapi/v1/netnamespacesfoo1",
+ "uid": "14effa0d-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo1"
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:36Z",
+ "name": "foo2",
+ "namespace": "",
+ "resourceVersion": "965",
+ "selfLink": "/oapi/v1/netnamespacesfoo2",
+ "uid": "1aabdf84-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo2"
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+ }'''
+
+ valid_namespace = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c1,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
+ "openshift.io/sa.scc.uid-range": "1000000000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:49Z",
+ "name": "default",
+ "namespace": "",
+ "resourceVersion": "165",
+ "selfLink": "/api/v1/namespacesdefault",
+ "uid": "23c0c6aa-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:49Z",
+ "name": "kube-system",
+ "namespace": "",
+ "resourceVersion": "533",
+ "selfLink": "/api/v1/namespaceskube-system",
+ "uid": "23c21758-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/description": "",
+ "openshift.io/display-name": "",
+ "openshift.io/requester": "developer",
+ "openshift.io/sa.scc.mcs": "s0:c9,c4",
+ "openshift.io/sa.scc.supplemental-groups": "1000080000/10000",
+ "openshift.io/sa.scc.uid-range": "1000080000/10000"
+ },
+ "creationTimestamp": "2017-03-02T02:17:16Z",
+ "name": "myproject",
+ "namespace": "",
+ "resourceVersion": "2898",
+ "selfLink": "/api/v1/namespacesmyproject",
+ "uid": "5ae3764d-feee-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "openshift.io/origin",
+ "kubernetes"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c6,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000030000/10000",
+ "openshift.io/sa.scc.uid-range": "1000030000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:51Z",
+ "name": "openshift",
+ "namespace": "",
+ "resourceVersion": "171",
+ "selfLink": "/api/v1/namespacesopenshift",
+ "uid": "24f7b34d-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c5,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000020000/10000",
+ "openshift.io/sa.scc.uid-range": "1000020000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:51Z",
+ "name": "openshift-infra",
+ "namespace": "",
+ "resourceVersion": "169",
+ "selfLink": "/api/v1/namespacesopenshift-infra",
+ "uid": "24a2ed75-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/description": "",
+ "openshift.io/display-name": "",
+ "openshift.io/requester": "developer1",
+ "openshift.io/sa.scc.mcs": "s0:c10,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000090000/10000",
+ "openshift.io/sa.scc.uid-range": "1000090000/10000"
+ },
+ "creationTimestamp": "2017-03-02T02:17:56Z",
+ "name": "yourproject",
+ "namespace": "",
+ "resourceVersion": "2955",
+ "selfLink": "/api/v1/namespacesyourproject",
+ "uid": "72df7fb9-feee-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "openshift.io/origin",
+ "kubernetes"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+}'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (0, valid_hostsubnet, ''),
+
+ # Second call to mock
+ (0, valid_netnamespace, ''),
+
+ # Third call to mock
+ (0, valid_namespace, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ # Act
+ results = OCObjectValidator.run_ansible(params)
+
+ # Assert
+ self.assertNotIn('failed', results)
+ self.assertEqual(results['msg'], 'All objects are valid.')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'hostsubnet', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'netnamespace', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'namespace', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_objectvalidator.locate_oc_binary')
+ @mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
+ @mock.patch('oc_objectvalidator.OCObjectValidator._run')
+ def test_invalid_both(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing when all objects are invalid '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ }
+
+ invalid_hostsubnet = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "host": "bar0",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:09Z",
+ "name": "bar0",
+ "namespace": "",
+ "resourceVersion": "986",
+ "selfLink": "/oapi/v1/hostsubnetsbar0",
+ "uid": "528dbb41-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ },
+ {
+ "apiVersion": "v1",
+ "host": "bar1",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:18Z",
+ "name": "bar1",
+ "namespace": "",
+ "resourceVersion": "988",
+ "selfLink": "/oapi/v1/hostsubnetsbar1",
+ "uid": "57710d84-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ },
+ {
+ "apiVersion": "v1",
+ "host": "bar2",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:26Z",
+ "name": "bar2",
+ "namespace": "",
+ "resourceVersion": "991",
+ "selfLink": "/oapi/v1/hostsubnetsbar2",
+ "uid": "5c59a28c-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ },
+ {
+ "apiVersion": "v1",
+ "host": "baz1",
+ "hostIP": "1.1.1.1",
+ "kind": "HostSubnet",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:47:49Z",
+ "name": "baz0",
+ "namespace": "",
+ "resourceVersion": "996",
+ "selfLink": "/oapi/v1/hostsubnetsbaz0",
+ "uid": "69f75f87-f478-11e6-aae0-507b9dac97ff"
+ },
+ "subnet": "1.1.0.0/24"
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+}'''
+
+ invalid_netnamespace = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:52Z",
+ "name": "bar0",
+ "namespace": "",
+ "resourceVersion": "969",
+ "selfLink": "/oapi/v1/netnamespacesbar0",
+ "uid": "245d416e-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "bar1"
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:16Z",
+ "name": "foo0",
+ "namespace": "",
+ "resourceVersion": "959",
+ "selfLink": "/oapi/v1/netnamespacesfoo0",
+ "uid": "0f1c85b2-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo0"
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:26Z",
+ "name": "foo1",
+ "namespace": "",
+ "resourceVersion": "962",
+ "selfLink": "/oapi/v1/netnamespacesfoo1",
+ "uid": "14effa0d-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo1"
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "NetNamespace",
+ "metadata": {
+ "creationTimestamp": "2017-02-16T18:45:36Z",
+ "name": "foo2",
+ "namespace": "",
+ "resourceVersion": "965",
+ "selfLink": "/oapi/v1/netnamespacesfoo2",
+ "uid": "1aabdf84-f478-11e6-aae0-507b9dac97ff"
+ },
+ "netid": 100,
+ "netname": "foo2"
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+}'''
+
+ invalid_namespace = '''{
+ "apiVersion": "v1",
+ "items": [
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c1,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000000000/10000",
+ "openshift.io/sa.scc.uid-range": "1000000000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:49Z",
+ "name": "default",
+ "namespace": "",
+ "resourceVersion": "165",
+ "selfLink": "/api/v1/namespacesdefault",
+ "uid": "23c0c6aa-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/requester": "",
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:49Z",
+ "name": "kube-system",
+ "namespace": "",
+ "resourceVersion": "3052",
+ "selfLink": "/api/v1/namespaceskube-system",
+ "uid": "23c21758-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/description": "",
+ "openshift.io/display-name": "",
+ "openshift.io/requester": "developer",
+ "openshift.io/sa.scc.mcs": "s0:c9,c4",
+ "openshift.io/sa.scc.supplemental-groups": "1000080000/10000",
+ "openshift.io/sa.scc.uid-range": "1000080000/10000"
+ },
+ "creationTimestamp": "2017-03-02T02:17:16Z",
+ "name": "myproject",
+ "namespace": "",
+ "resourceVersion": "2898",
+ "selfLink": "/api/v1/namespacesmyproject",
+ "uid": "5ae3764d-feee-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "openshift.io/origin",
+ "kubernetes"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/requester": "",
+ "openshift.io/sa.scc.mcs": "s0:c6,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000030000/10000",
+ "openshift.io/sa.scc.uid-range": "1000030000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:51Z",
+ "name": "openshift",
+ "namespace": "",
+ "resourceVersion": "3057",
+ "selfLink": "/api/v1/namespacesopenshift",
+ "uid": "24f7b34d-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/description": "",
+ "openshift.io/display-name": "",
+ "openshift.io/requester": "system:admin",
+ "openshift.io/sa.scc.mcs": "s0:c10,c5",
+ "openshift.io/sa.scc.supplemental-groups": "1000100000/10000",
+ "openshift.io/sa.scc.uid-range": "1000100000/10000"
+ },
+ "creationTimestamp": "2017-03-02T02:21:15Z",
+ "name": "openshift-fancy",
+ "namespace": "",
+ "resourceVersion": "3072",
+ "selfLink": "/api/v1/namespacesopenshift-fancy",
+ "uid": "e958063c-feee-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "openshift.io/origin",
+ "kubernetes"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/sa.scc.mcs": "s0:c5,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000020000/10000",
+ "openshift.io/sa.scc.uid-range": "1000020000/10000"
+ },
+ "creationTimestamp": "2017-03-02T00:49:51Z",
+ "name": "openshift-infra",
+ "namespace": "",
+ "resourceVersion": "169",
+ "selfLink": "/api/v1/namespacesopenshift-infra",
+ "uid": "24a2ed75-fee2-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "Namespace",
+ "metadata": {
+ "annotations": {
+ "openshift.io/description": "",
+ "openshift.io/display-name": "",
+ "openshift.io/requester": "developer1",
+ "openshift.io/sa.scc.mcs": "s0:c10,c0",
+ "openshift.io/sa.scc.supplemental-groups": "1000090000/10000",
+ "openshift.io/sa.scc.uid-range": "1000090000/10000"
+ },
+ "creationTimestamp": "2017-03-02T02:17:56Z",
+ "name": "yourproject",
+ "namespace": "",
+ "resourceVersion": "2955",
+ "selfLink": "/api/v1/namespacesyourproject",
+ "uid": "72df7fb9-feee-11e6-b45a-507b9dac97ff"
+ },
+ "spec": {
+ "finalizers": [
+ "openshift.io/origin",
+ "kubernetes"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }
+ ],
+ "kind": "List",
+ "metadata": {},
+ "resourceVersion": "",
+ "selfLink": ""
+}'''
+
+ invalid_results = {
+ 'hostsubnets where metadata.name != host': [{
+ 'apiVersion': 'v1',
+ 'host': 'baz1',
+ 'hostIP': '1.1.1.1',
+ 'kind': 'HostSubnet',
+ 'metadata': {
+ 'creationTimestamp': '2017-02-16T18:47:49Z',
+ 'name': 'baz0',
+ 'namespace': '',
+ 'resourceVersion': '996',
+ 'selfLink': '/oapi/v1/hostsubnetsbaz0',
+ 'uid': '69f75f87-f478-11e6-aae0-507b9dac97ff'
+ },
+ 'subnet': '1.1.0.0/24'
+ }],
+ 'netnamespaces where metadata.name != netname': [{
+ 'apiVersion': 'v1',
+ 'kind': 'NetNamespace',
+ 'metadata': {
+ 'creationTimestamp': '2017-02-16T18:45:52Z',
+ 'name': 'bar0',
+ 'namespace': '',
+ 'resourceVersion': '969',
+ 'selfLink': '/oapi/v1/netnamespacesbar0',
+ 'uid': '245d416e-f478-11e6-aae0-507b9dac97ff'
+ },
+ 'netid': 100,
+ 'netname': 'bar1'
+ }],
+ 'namespaces that use reserved names and were not created by infrastructure components': [{
+ 'apiVersion': 'v1',
+ 'kind': 'Namespace',
+ 'metadata': {'annotations': {'openshift.io/requester': '',
+ 'openshift.io/sa.scc.mcs': 's0:c3,c2',
+ 'openshift.io/sa.scc.supplemental-groups': '1000010000/10000',
+ 'openshift.io/sa.scc.uid-range': '1000010000/10000'},
+ 'creationTimestamp': '2017-03-02T00:49:49Z',
+ 'name': 'kube-system',
+ 'namespace': '',
+ 'resourceVersion': '3052',
+ 'selfLink': '/api/v1/namespaceskube-system',
+ 'uid': '23c21758-fee2-11e6-b45a-507b9dac97ff'},
+ 'spec': {'finalizers': ['kubernetes', 'openshift.io/origin']},
+ 'status': {'phase': 'Active'}},
+ {'apiVersion': 'v1',
+ 'kind': 'Namespace',
+ 'metadata': {'annotations': {'openshift.io/requester': '',
+ 'openshift.io/sa.scc.mcs': 's0:c6,c0',
+ 'openshift.io/sa.scc.supplemental-groups': '1000030000/10000',
+ 'openshift.io/sa.scc.uid-range': '1000030000/10000'},
+ 'creationTimestamp': '2017-03-02T00:49:51Z',
+ 'name': 'openshift',
+ 'namespace': '',
+ 'resourceVersion': '3057',
+ 'selfLink': '/api/v1/namespacesopenshift',
+ 'uid': '24f7b34d-fee2-11e6-b45a-507b9dac97ff'},
+ 'spec': {'finalizers': ['kubernetes', 'openshift.io/origin']},
+ 'status': {'phase': 'Active'}},
+ {'apiVersion': 'v1',
+ 'kind': 'Namespace',
+ 'metadata': {'annotations': {'openshift.io/description': '',
+ 'openshift.io/display-name': '',
+ 'openshift.io/requester': 'system:admin',
+ 'openshift.io/sa.scc.mcs': 's0:c10,c5',
+ 'openshift.io/sa.scc.supplemental-groups': '1000100000/10000',
+ 'openshift.io/sa.scc.uid-range': '1000100000/10000'},
+ 'creationTimestamp': '2017-03-02T02:21:15Z',
+ 'name': 'openshift-fancy',
+ 'namespace': '',
+ 'resourceVersion': '3072',
+ 'selfLink': '/api/v1/namespacesopenshift-fancy',
+ 'uid': 'e958063c-feee-11e6-b45a-507b9dac97ff'},
+ 'spec': {'finalizers': ['openshift.io/origin', 'kubernetes']},
+ 'status': {'phase': 'Active'}
+ }],
+ }
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (0, invalid_hostsubnet, ''),
+
+ # Second call to mock
+ (0, invalid_netnamespace, ''),
+
+ # Third call to mock
+ (0, invalid_namespace, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ # Act
+ results = OCObjectValidator.run_ansible(params)
+
+ # Assert
+ self.assertTrue(results['failed'])
+ self.assertIn('All objects are not valid.', results['msg'])
+ self.assertEqual(results['state'], 'list')
+ self.assertEqual(results['results'], invalid_results)
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'hostsubnet', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'netnamespace', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'namespace', '-o', 'json', '-n', 'default'], None),
+ ])
diff --git a/roles/lib_openshift/src/test/unit/test_oc_process.py b/roles/lib_openshift/src/test/unit/test_oc_process.py
new file mode 100755
index 000000000..d887f7636
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_process.py
@@ -0,0 +1,572 @@
+'''
+ Unit tests for oc process
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_process import OCProcess, locate_oc_binary # noqa: E402
+
+
+# pylint: disable=too-many-public-methods
+class OCProcessTest(unittest.TestCase):
+ '''
+ Test class for OCProcess
+ '''
+ mysql = '''{
+ "kind": "Template",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "mysql-ephemeral",
+ "namespace": "openshift",
+ "selfLink": "/oapi/v1/namespaces/openshift/templates/mysql-ephemeral",
+ "uid": "fb8b5f04-e3d3-11e6-a982-0e84250fc302",
+ "resourceVersion": "480",
+ "creationTimestamp": "2017-01-26T14:30:27Z",
+ "annotations": {
+ "iconClass": "icon-mysql-database",
+ "openshift.io/display-name": "MySQL (Ephemeral)",
+ "tags": "database,mysql"
+ }
+ },
+ "objects": [
+ {
+ "apiVersion": "v1",
+ "kind": "Service",
+ "metadata": {
+ "creationTimestamp": null,
+ "name": "${DATABASE_SERVICE_NAME}"
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "mysql",
+ "nodePort": 0,
+ "port": 3306,
+ "protocol": "TCP",
+ "targetPort": 3306
+ }
+ ],
+ "selector": {
+ "name": "${DATABASE_SERVICE_NAME}"
+ },
+ "sessionAffinity": "None",
+ "type": "ClusterIP"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "DeploymentConfig",
+ "metadata": {
+ "creationTimestamp": null,
+ "name": "${DATABASE_SERVICE_NAME}"
+ },
+ "spec": {
+ "replicas": 1,
+ "selector": {
+ "name": "${DATABASE_SERVICE_NAME}"
+ },
+ "strategy": {
+ "type": "Recreate"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "name": "${DATABASE_SERVICE_NAME}"
+ }
+ },
+ "spec": {
+ "containers": [
+ {
+ "capabilities": {},
+ "env": [
+ {
+ "name": "MYSQL_USER",
+ "value": "${MYSQL_USER}"
+ },
+ {
+ "name": "MYSQL_PASSWORD",
+ "value": "${MYSQL_PASSWORD}"
+ },
+ {
+ "name": "MYSQL_DATABASE",
+ "value": "${MYSQL_DATABASE}"
+ }
+ ],
+ "image": " ",
+ "imagePullPolicy": "IfNotPresent",
+ "livenessProbe": {
+ "initialDelaySeconds": 30,
+ "tcpSocket": {
+ "port": 3306
+ },
+ "timeoutSeconds": 1
+ },
+ "name": "mysql",
+ "ports": [
+ {
+ "containerPort": 3306,
+ "protocol": "TCP"
+ }
+ ],
+ "readinessProbe": {
+ "exec": {
+ "command": [
+ "/bin/sh",
+ "-i",
+ "-c",
+ "MYSQL_PWD=$MYSQL_PASSWORD mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -e 'SELECT 1'"
+ ]
+ },
+ "initialDelaySeconds": 5,
+ "timeoutSeconds": 1
+ },
+ "resources": {
+ "limits": {
+ "memory": "${MEMORY_LIMIT}"
+ }
+ },
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ },
+ "terminationMessagePath": "/dev/termination-log",
+ "volumeMounts": [
+ {
+ "mountPath": "/var/lib/mysql/data",
+ "name": "${DATABASE_SERVICE_NAME}-data"
+ }
+ ]
+ }
+ ],
+ "dnsPolicy": "ClusterFirst",
+ "restartPolicy": "Always",
+ "volumes": [
+ {
+ "emptyDir": {
+ "medium": ""
+ },
+ "name": "${DATABASE_SERVICE_NAME}-data"
+ }
+ ]
+ }
+ },
+ "triggers": [
+ {
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "mysql"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "name": "mysql:${MYSQL_VERSION}",
+ "namespace": "${NAMESPACE}"
+ },
+ "lastTriggeredImage": ""
+ },
+ "type": "ImageChange"
+ },
+ {
+ "type": "ConfigChange"
+ }
+ ]
+ },
+ "status": {}
+ }
+ ],
+ "parameters": [
+ {
+ "name": "MEMORY_LIMIT",
+ "displayName": "Memory Limit",
+ "description": "Maximum amount of memory the container can use.",
+ "value": "512Mi"
+ },
+ {
+ "name": "NAMESPACE",
+ "displayName": "Namespace",
+ "description": "The OpenShift Namespace where the ImageStream resides.",
+ "value": "openshift"
+ },
+ {
+ "name": "DATABASE_SERVICE_NAME",
+ "displayName": "Database Service Name",
+ "description": "The name of the OpenShift Service exposed for the database.",
+ "value": "mysql",
+ "required": true
+ },
+ {
+ "name": "MYSQL_USER",
+ "displayName": "MySQL Connection Username",
+ "description": "Username for MySQL user that will be used for accessing the database.",
+ "generate": "expression",
+ "from": "user[A-Z0-9]{3}",
+ "required": true
+ },
+ {
+ "name": "MYSQL_PASSWORD",
+ "displayName": "MySQL Connection Password",
+ "description": "Password for the MySQL connection user.",
+ "generate": "expression",
+ "from": "[a-zA-Z0-9]{16}",
+ "required": true
+ },
+ {
+ "name": "MYSQL_DATABASE",
+ "displayName": "MySQL Database Name",
+ "description": "Name of the MySQL database accessed.",
+ "value": "sampledb",
+ "required": true
+ },
+ {
+ "name": "MYSQL_VERSION",
+ "displayName": "Version of MySQL Image",
+ "description": "Version of MySQL image to be used (5.5, 5.6 or latest).",
+ "value": "5.6",
+ "required": true
+ }
+ ],
+ "labels": {
+ "template": "mysql-ephemeral-template"
+ }
+}'''
+
+ @mock.patch('oc_process.Utils.create_tmpfile_copy')
+ @mock.patch('oc_process.OCProcess._run')
+ def test_state_list(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'template_name': 'mysql-ephermeral',
+ 'namespace': 'test',
+ 'content': None,
+ 'state': 'list',
+ 'reconcile': False,
+ 'create': False,
+ 'params': {'NAMESPACE': 'test', 'DATABASE_SERVICE_NAME': 'testdb'},
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ mock_cmd.side_effect = [
+ (0, OCProcessTest.mysql, '')
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mock_kubeconfig',
+ ]
+
+ results = OCProcess.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'mysql-ephemeral')
+
+ @mock.patch('oc_process.Utils.create_tmpfile_copy')
+ @mock.patch('oc_process.OCProcess._run')
+ def test_process_no_create(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a process with no create '''
+ params = {'template_name': 'mysql-ephermeral',
+ 'namespace': 'test',
+ 'content': None,
+ 'state': 'present',
+ 'reconcile': False,
+ 'create': False,
+ 'params': {'NAMESPACE': 'test', 'DATABASE_SERVICE_NAME': 'testdb'},
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ mysqlproc = '''{
+ "kind": "List",
+ "apiVersion": "v1",
+ "metadata": {},
+ "items": [
+ {
+ "apiVersion": "v1",
+ "kind": "Service",
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "template": "mysql-ephemeral-template"
+ },
+ "name": "testdb"
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "mysql",
+ "nodePort": 0,
+ "port": 3306,
+ "protocol": "TCP",
+ "targetPort": 3306
+ }
+ ],
+ "selector": {
+ "name": "testdb"
+ },
+ "sessionAffinity": "None",
+ "type": "ClusterIP"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ },
+ {
+ "apiVersion": "v1",
+ "kind": "DeploymentConfig",
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "template": "mysql-ephemeral-template"
+ },
+ "name": "testdb"
+ },
+ "spec": {
+ "replicas": 1,
+ "selector": {
+ "name": "testdb"
+ },
+ "strategy": {
+ "type": "Recreate"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "name": "testdb"
+ }
+ },
+ "spec": {
+ "containers": [
+ {
+ "capabilities": {},
+ "env": [
+ {
+ "name": "MYSQL_USER",
+ "value": "userHJJ"
+ },
+ {
+ "name": "MYSQL_PASSWORD",
+ "value": "GITOAduAMaV6k688"
+ },
+ {
+ "name": "MYSQL_DATABASE",
+ "value": "sampledb"
+ }
+ ],
+ "image": " ",
+ "imagePullPolicy": "IfNotPresent",
+ "livenessProbe": {
+ "initialDelaySeconds": 30,
+ "tcpSocket": {
+ "port": 3306
+ },
+ "timeoutSeconds": 1
+ },
+ "name": "mysql",
+ "ports": [
+ {
+ "containerPort": 3306,
+ "protocol": "TCP"
+ }
+ ],
+ "readinessProbe": {
+ "exec": {
+ "command": [
+ "/bin/sh",
+ "-i",
+ "-c",
+ "MYSQL_PWD=$MYSQL_PASSWORD mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -e 'SELECT 1'"
+ ]
+ },
+ "initialDelaySeconds": 5,
+ "timeoutSeconds": 1
+ },
+ "resources": {
+ "limits": {
+ "memory": "512Mi"
+ }
+ },
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ },
+ "terminationMessagePath": "/dev/termination-log",
+ "volumeMounts": [
+ {
+ "mountPath": "/var/lib/mysql/data",
+ "name": "testdb-data"
+ }
+ ]
+ }
+ ],
+ "dnsPolicy": "ClusterFirst",
+ "restartPolicy": "Always",
+ "volumes": [
+ {
+ "emptyDir": {
+ "medium": ""
+ },
+ "name": "testdb-data"
+ }
+ ]
+ }
+ },
+ "triggers": [
+ {
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "mysql"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "name": "mysql:5.6",
+ "namespace": "test"
+ },
+ "lastTriggeredImage": ""
+ },
+ "type": "ImageChange"
+ },
+ {
+ "type": "ConfigChange"
+ }
+ ]
+ }
+ }
+ ]
+}'''
+
+ mock_cmd.side_effect = [
+ (0, OCProcessTest.mysql, ''),
+ (0, OCProcessTest.mysql, ''),
+ (0, mysqlproc, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mock_kubeconfig',
+ ]
+
+ results = OCProcess.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['results']['items'][0]['metadata']['name'], 'testdb')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_project.py b/roles/lib_openshift/src/test/unit/test_oc_project.py
new file mode 100755
index 000000000..fa454d035
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_project.py
@@ -0,0 +1,280 @@
+'''
+ Unit tests for oc project
+'''
+
+import copy
+import os
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_project import OCProject # noqa: E402
+
+
+class OCProjectTest(unittest.TestCase):
+ '''
+ Test class for OCProject
+ '''
+
+ # run_ansible input parameters
+ params = {
+ 'state': 'present',
+ 'display_name': 'operations project',
+ 'name': 'operations',
+ 'node_selector': ['ops_only=True'],
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False,
+ 'admin': None,
+ 'admin_role': 'admin',
+ 'description': 'All things operations project',
+ }
+
+ @mock.patch('oc_project.locate_oc_binary')
+ @mock.patch('oc_project.Utils.create_tmpfile_copy')
+ @mock.patch('oc_project.Utils._write')
+ @mock.patch('oc_project.OCProject._run')
+ def test_adding_a_project(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_loc_oc_bin):
+ ''' Testing adding a project '''
+
+ params = copy.deepcopy(OCProjectTest.params)
+
+ # run_ansible input parameters
+ project_results = '''{
+ "kind": "Project",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "operations",
+ "selfLink": "/oapi/v1/projects/operations",
+ "uid": "5e52afb8-ee33-11e6-89f4-0edc441d9666",
+ "resourceVersion": "1584",
+ "labels": {},
+ "annotations": {
+ "openshift.io/node-selector": "ops_only=True",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: namespaces "operations" not found'),
+ (1, '', 'Error from server: namespaces "operations" not found'),
+ (0, '', ''), # created
+ (0, project_results, ''), # fetch it
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_loc_oc_bin.side_effect = [
+ 'oc',
+ ]
+
+ # Act
+ results = OCProject.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['results']['results']['metadata']['name'], 'operations')
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'adm', 'new-project', 'operations', mock.ANY,
+ mock.ANY, mock.ANY, mock.ANY], None),
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+
+ ])
+
+ @mock.patch('oc_project.locate_oc_binary')
+ @mock.patch('oc_project.Utils.create_tmpfile_copy')
+ @mock.patch('oc_project.Utils._write')
+ @mock.patch('oc_project.OCProject._run')
+ def test_modifying_a_project_no_attributes(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_loc_oc_bin):
+ ''' Testing adding a project '''
+ params = copy.deepcopy(self.params)
+ params['display_name'] = None
+ params['node_selector'] = None
+ params['description'] = None
+
+ # run_ansible input parameters
+ project_results = '''{
+ "kind": "Project",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "operations",
+ "selfLink": "/oapi/v1/projects/operations",
+ "uid": "5e52afb8-ee33-11e6-89f4-0edc441d9666",
+ "resourceVersion": "1584",
+ "labels": {},
+ "annotations": {
+ "openshift.io/node-selector": "",
+ "openshift.io/description: "This is a description",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ (0, project_results, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_loc_oc_bin.side_effect = [
+ 'oc',
+ ]
+
+ # Act
+ results = OCProject.run_ansible(params, False)
+
+ # Assert
+ self.assertFalse(results['changed'])
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ ])
+
+ @mock.patch('oc_project.locate_oc_binary')
+ @mock.patch('oc_project.Utils.create_tmpfile_copy')
+ @mock.patch('oc_project.Utils._write')
+ @mock.patch('oc_project.OCProject._run')
+ def test_modifying_project_attributes(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_loc_oc_bin):
+ ''' Testing adding a project '''
+ params = copy.deepcopy(self.params)
+ params['display_name'] = 'updated display name'
+ params['node_selector'] = 'type=infra'
+ params['description'] = 'updated description'
+
+ # run_ansible input parameters
+ project_results = '''{
+ "kind": "Project",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "operations",
+ "selfLink": "/oapi/v1/projects/operations",
+ "uid": "5e52afb8-ee33-11e6-89f4-0edc441d9666",
+ "resourceVersion": "1584",
+ "labels": {},
+ "annotations": {
+ "openshift.io/node-selector": "",
+ "openshift.io/description": "This is a description",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ mod_project_results = '''{
+ "kind": "Project",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "operations",
+ "selfLink": "/oapi/v1/projects/operations",
+ "uid": "5e52afb8-ee33-11e6-89f4-0edc441d9666",
+ "resourceVersion": "1584",
+ "labels": {},
+ "annotations": {
+ "openshift.io/node-selector": "type=infra",
+ "openshift.io/description": "updated description",
+ "openshift.io/display-name": "updated display name",
+ "openshift.io/sa.initialized-roles": "true",
+ "openshift.io/sa.scc.mcs": "s0:c3,c2",
+ "openshift.io/sa.scc.supplemental-groups": "1000010000/10000",
+ "openshift.io/sa.scc.uid-range": "1000010000/10000"
+ }
+ },
+ "spec": {
+ "finalizers": [
+ "kubernetes",
+ "openshift.io/origin"
+ ]
+ },
+ "status": {
+ "phase": "Active"
+ }
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ (0, project_results, ''),
+ (0, project_results, ''),
+ (0, '', ''),
+ (0, mod_project_results, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ mock_loc_oc_bin.side_effect = [
+ 'oc',
+ ]
+
+ # Act
+ results = OCProject.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['results']['results']['metadata']['annotations']['openshift.io/description'], 'updated description')
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ mock.call(['oc', 'replace', '-f', mock.ANY], None),
+ mock.call(['oc', 'get', 'namespace', 'operations', '-o', 'json'], None),
+ ])
diff --git a/roles/lib_openshift/src/test/unit/test_oc_pvc.py b/roles/lib_openshift/src/test/unit/test_oc_pvc.py
new file mode 100755
index 000000000..a96f2e4a7
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_pvc.py
@@ -0,0 +1,377 @@
+'''
+ Unit tests for oc pvc
+'''
+
+import copy
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_pvc import OCPVC, locate_oc_binary # noqa: E402
+
+
+class OCPVCTest(unittest.TestCase):
+ '''
+ Test class for OCPVC
+ '''
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'mypvc',
+ 'namespace': 'test',
+ 'volume_capacity': '1G',
+ 'selector': {'foo': 'bar', 'abc': 'a123'},
+ 'storage_class_name': 'mystorage',
+ 'access_modes': 'ReadWriteMany'}
+
+ @mock.patch('oc_pvc.Utils.create_tmpfile_copy')
+ @mock.patch('oc_pvc.OCPVC._run')
+ def test_create_pvc(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a pvc create '''
+ params = copy.deepcopy(OCPVCTest.params)
+
+ pvc = '''{"kind": "PersistentVolumeClaim",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "mypvc",
+ "namespace": "test",
+ "selfLink": "/api/v1/namespaces/test/persistentvolumeclaims/mypvc",
+ "uid": "77597898-d8d8-11e6-aea5-0e3c0c633889",
+ "resourceVersion": "126510787",
+ "creationTimestamp": "2017-01-12T15:04:50Z",
+ "labels": {
+ "mypvc": "database"
+ },
+ "annotations": {
+ "pv.kubernetes.io/bind-completed": "yes",
+ "pv.kubernetes.io/bound-by-controller": "yes",
+ "v1.2-volume.experimental.kubernetes.io/provisioning-required": "volume.experimental.kubernetes.io/provisioning-completed"
+ }
+ },
+ "spec": {
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "resources": {
+ "requests": {
+ "storage": "1Gi"
+ }
+ },
+ "selector": {
+ "matchLabels": {
+ "foo": "bar",
+ "abc": "a123"
+ }
+ },
+ "storageClassName": "myStorage",
+ "volumeName": "pv-aws-ow5vl"
+ },
+ "status": {
+ "phase": "Bound",
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "capacity": {
+ "storage": "1Gi"
+ }
+ }
+ }'''
+
+ mock_run.side_effect = [
+ (1, '', 'Error from server: persistentvolumeclaims "mypvc" not found'),
+ (1, '', 'Error from server: persistentvolumeclaims "mypvc" not found'),
+ (0, '', ''),
+ (0, pvc, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCPVC.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'mypvc')
+ self.assertEqual(results['results']['results'][0]['spec']['storageClassName'], 'myStorage')
+ self.assertEqual(results['results']['results'][0]['spec']['selector']['matchLabels']['foo'], 'bar')
+
+ @mock.patch('oc_pvc.Utils.create_tmpfile_copy')
+ @mock.patch('oc_pvc.OCPVC._run')
+ def test_update_pvc(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a pvc create '''
+ params = copy.deepcopy(OCPVCTest.params)
+ params['access_modes'] = 'ReadWriteMany'
+
+ pvc = '''{"kind": "PersistentVolumeClaim",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "mypvc",
+ "namespace": "test",
+ "selfLink": "/api/v1/namespaces/test/persistentvolumeclaims/mypvc",
+ "uid": "77597898-d8d8-11e6-aea5-0e3c0c633889",
+ "resourceVersion": "126510787",
+ "creationTimestamp": "2017-01-12T15:04:50Z",
+ "labels": {
+ "mypvc": "database"
+ },
+ "annotations": {
+ "pv.kubernetes.io/bind-completed": "yes",
+ "pv.kubernetes.io/bound-by-controller": "yes",
+ "v1.2-volume.experimental.kubernetes.io/provisioning-required": "volume.experimental.kubernetes.io/provisioning-completed"
+ }
+ },
+ "spec": {
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "resources": {
+ "requests": {
+ "storage": "1Gi"
+ }
+ },
+ "volumeName": "pv-aws-ow5vl"
+ },
+ "status": {
+ "phase": "Bound",
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "capacity": {
+ "storage": "1Gi"
+ }
+ }
+ }'''
+
+ mod_pvc = '''{"kind": "PersistentVolumeClaim",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "mypvc",
+ "namespace": "test",
+ "selfLink": "/api/v1/namespaces/test/persistentvolumeclaims/mypvc",
+ "uid": "77597898-d8d8-11e6-aea5-0e3c0c633889",
+ "resourceVersion": "126510787",
+ "creationTimestamp": "2017-01-12T15:04:50Z",
+ "labels": {
+ "mypvc": "database"
+ },
+ "annotations": {
+ "pv.kubernetes.io/bind-completed": "yes",
+ "pv.kubernetes.io/bound-by-controller": "yes",
+ "v1.2-volume.experimental.kubernetes.io/provisioning-required": "volume.experimental.kubernetes.io/provisioning-completed"
+ }
+ },
+ "spec": {
+ "accessModes": [
+ "ReadWriteMany"
+ ],
+ "resources": {
+ "requests": {
+ "storage": "1Gi"
+ }
+ },
+ "volumeName": "pv-aws-ow5vl"
+ },
+ "status": {
+ "phase": "Bound",
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "capacity": {
+ "storage": "1Gi"
+ }
+ }
+ }'''
+
+ mock_run.side_effect = [
+ (0, pvc, ''),
+ (0, pvc, ''),
+ (0, '', ''),
+ (0, mod_pvc, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCPVC.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['msg'], '##### - This volume is currently bound. Will not update - ####')
+
+ @mock.patch('oc_pvc.Utils.create_tmpfile_copy')
+ @mock.patch('oc_pvc.OCPVC._run')
+ def test_delete_pvc(self, mock_run, mock_tmpfile_copy):
+ ''' Testing a pvc create '''
+ params = copy.deepcopy(OCPVCTest.params)
+ params['state'] = 'absent'
+
+ pvc = '''{"kind": "PersistentVolumeClaim",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "mypvc",
+ "namespace": "test",
+ "selfLink": "/api/v1/namespaces/test/persistentvolumeclaims/mypvc",
+ "uid": "77597898-d8d8-11e6-aea5-0e3c0c633889",
+ "resourceVersion": "126510787",
+ "creationTimestamp": "2017-01-12T15:04:50Z",
+ "labels": {
+ "mypvc": "database"
+ },
+ "annotations": {
+ "pv.kubernetes.io/bind-completed": "yes",
+ "pv.kubernetes.io/bound-by-controller": "yes",
+ "v1.2-volume.experimental.kubernetes.io/provisioning-required": "volume.experimental.kubernetes.io/provisioning-completed"
+ }
+ },
+ "spec": {
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "resources": {
+ "requests": {
+ "storage": "1Gi"
+ }
+ },
+ "volumeName": "pv-aws-ow5vl"
+ },
+ "status": {
+ "phase": "Bound",
+ "accessModes": [
+ "ReadWriteOnce"
+ ],
+ "capacity": {
+ "storage": "1Gi"
+ }
+ }
+ }'''
+
+ mock_run.side_effect = [
+ (0, pvc, ''),
+ (0, '', ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCPVC.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_route.py b/roles/lib_openshift/src/test/unit/test_oc_route.py
new file mode 100755
index 000000000..5699f123b
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_route.py
@@ -0,0 +1,374 @@
+'''
+ Unit tests for oc route
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_route import OCRoute, locate_oc_binary # noqa: E402
+
+
+class OCRouteTest(unittest.TestCase):
+ '''
+ Test class for OCRoute
+ '''
+
+ @mock.patch('oc_route.locate_oc_binary')
+ @mock.patch('oc_route.Utils.create_tmpfile_copy')
+ @mock.patch('oc_route.OCRoute._run')
+ def test_list_route(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing getting a route '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'list',
+ 'debug': False,
+ 'name': 'test',
+ 'namespace': 'default',
+ 'labels': {'route': 'route'},
+ 'tls_termination': 'passthrough',
+ 'dest_cacert_path': None,
+ 'cacert_path': None,
+ 'cert_path': None,
+ 'key_path': None,
+ 'dest_cacert_content': None,
+ 'cacert_content': None,
+ 'cert_content': None,
+ 'key_content': None,
+ 'service_name': 'testservice',
+ 'host': 'test.openshift.com',
+ 'wildcard_policy': None,
+ 'weight': None,
+ 'port': None
+ }
+
+ route_result = '''{
+ "kind": "Route",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "test",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/routes/test",
+ "uid": "1b127c67-ecd9-11e6-96eb-0e0d9bdacd26",
+ "resourceVersion": "439182",
+ "creationTimestamp": "2017-02-07T01:59:48Z",
+ "labels": {
+ "route": "route"
+ }
+ },
+ "spec": {
+ "host": "test.example",
+ "to": {
+ "kind": "Service",
+ "name": "test",
+ "weight": 100
+ },
+ "port": {
+ "targetPort": 8443
+ },
+ "tls": {
+ "termination": "passthrough"
+ },
+ "wildcardPolicy": "None"
+ },
+ "status": {
+ "ingress": [
+ {
+ "host": "test.example",
+ "routerName": "router",
+ "conditions": [
+ {
+ "type": "Admitted",
+ "status": "True",
+ "lastTransitionTime": "2017-02-07T01:59:48Z"
+ }
+ ],
+ "wildcardPolicy": "None"
+ }
+ ]
+ }
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (0, route_result, ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mock.kubeconfig',
+ ]
+
+ # Act
+ results = OCRoute.run_ansible(params, False)
+
+ # Assert
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['state'], 'list')
+ self.assertEqual(results['results'][0]['metadata']['name'], 'test')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @mock.patch('oc_route.locate_oc_binary')
+ @mock.patch('oc_route.Utils.create_tmpfile_copy')
+ @mock.patch('oc_route.Yedit._write')
+ @mock.patch('oc_route.OCRoute._run')
+ def test_create_route(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing getting a route '''
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'test',
+ 'namespace': 'default',
+ 'labels': {'route': 'route'},
+ 'tls_termination': 'edge',
+ 'dest_cacert_path': None,
+ 'cacert_path': None,
+ 'cert_path': None,
+ 'key_path': None,
+ 'dest_cacert_content': None,
+ 'cacert_content': 'testing',
+ 'cert_content': 'testing',
+ 'key_content': 'testing',
+ 'service_name': 'testservice',
+ 'host': 'test.openshift.com',
+ 'wildcard_policy': None,
+ 'weight': None,
+ 'port': None
+ }
+
+ route_result = '''{
+ "apiVersion": "v1",
+ "kind": "Route",
+ "metadata": {
+ "creationTimestamp": "2017-02-07T20:55:10Z",
+ "name": "test",
+ "namespace": "default",
+ "resourceVersion": "517745",
+ "selfLink": "/oapi/v1/namespaces/default/routes/test",
+ "uid": "b6f25898-ed77-11e6-9755-0e737db1e63a",
+ "labels": {"route": "route"}
+ },
+ "spec": {
+ "host": "test.openshift.com",
+ "tls": {
+ "caCertificate": "testing",
+ "certificate": "testing",
+ "key": "testing",
+ "termination": "edge"
+ },
+ "to": {
+ "kind": "Service",
+ "name": "testservice",
+ "weight": 100
+ },
+ "wildcardPolicy": "None"
+ },
+ "status": {
+ "ingress": [
+ {
+ "conditions": [
+ {
+ "lastTransitionTime": "2017-02-07T20:55:10Z",
+ "status": "True",
+ "type": "Admitted"
+ }
+ ],
+ "host": "test.openshift.com",
+ "routerName": "router",
+ "wildcardPolicy": "None"
+ }
+ ]
+ }
+ }'''
+
+ test_route = '''\
+kind: Route
+spec:
+ tls:
+ caCertificate: testing
+ termination: edge
+ certificate: testing
+ key: testing
+ to:
+ kind: Service
+ name: testservice
+ weight: 100
+ host: test.openshift.com
+ wildcardPolicy: None
+apiVersion: v1
+metadata:
+ namespace: default
+ name: test
+'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (1, '', 'Error from server: routes "test" not found'),
+ (1, '', 'Error from server: routes "test" not found'),
+ (0, 'route "test" created', ''),
+ (0, route_result, ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mock.kubeconfig',
+ ]
+
+ mock_write.assert_has_calls = [
+ # First call to mock
+ mock.call('/tmp/test', test_route)
+ ]
+
+ # Act
+ results = OCRoute.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['state'], 'present')
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'test')
+ self.assertEqual(results['results']['results'][0]['metadata']['labels']['route'], 'route')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'get', 'route', 'test', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_scale.py b/roles/lib_openshift/src/test/unit/test_oc_scale.py
new file mode 100755
index 000000000..d810735f2
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_scale.py
@@ -0,0 +1,256 @@
+'''
+ Unit tests for oc scale
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_scale import OCScale, locate_oc_binary # noqa: E402
+
+
+class OCScaleTest(unittest.TestCase):
+ '''
+ Test class for OCVersion
+ '''
+
+ @mock.patch('oc_scale.Utils.create_tmpfile_copy')
+ @mock.patch('oc_scale.OCScale.openshift_cmd')
+ def test_state_list(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'replicas': 2,
+ 'state': 'list',
+ 'kind': 'dc',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ dc = '''{"kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router",
+ "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42",
+ "resourceVersion": "6558",
+ "generation": 8,
+ "creationTimestamp": "2017-01-23T20:58:07Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "replicas": 2,
+ }
+ }'''
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": '/usr/bin/oc get dc router -n default',
+ 'results': dc,
+ 'returncode': 0}]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCScale.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['result'][0], 2)
+
+ @mock.patch('oc_scale.Utils.create_tmpfile_copy')
+ @mock.patch('oc_scale.OCScale.openshift_cmd')
+ def test_scale(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'replicas': 3,
+ 'state': 'list',
+ 'kind': 'dc',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ dc = '''{"kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router",
+ "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42",
+ "resourceVersion": "6558",
+ "generation": 8,
+ "creationTimestamp": "2017-01-23T20:58:07Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "replicas": 3,
+ }
+ }'''
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": '/usr/bin/oc get dc router -n default',
+ 'results': dc,
+ 'returncode': 0},
+ {"cmd": '/usr/bin/oc create -f /tmp/router -n default',
+ 'results': '',
+ 'returncode': 0}
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCScale.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['result'][0], 3)
+
+ @mock.patch('oc_scale.Utils.create_tmpfile_copy')
+ @mock.patch('oc_scale.OCScale.openshift_cmd')
+ def test_no_dc_scale(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'name': 'not_there',
+ 'namespace': 'default',
+ 'replicas': 3,
+ 'state': 'present',
+ 'kind': 'dc',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": '/usr/bin/oc -n default get dc not_there -o json',
+ 'results': [{}],
+ 'returncode': 1,
+ 'stderr': "Error from server: deploymentconfigs \"not_there\" not found\n",
+ 'stdout': ""},
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCScale.run_ansible(params, False)
+
+ self.assertTrue(results['failed'])
+ self.assertEqual(results['msg']['returncode'], 1)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_secret.py b/roles/lib_openshift/src/test/unit/test_oc_secret.py
new file mode 100755
index 000000000..323b3423c
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_secret.py
@@ -0,0 +1,192 @@
+'''
+ Unit tests for oc secret
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_secret import OCSecret, locate_oc_binary # noqa: E402
+
+
+class OCSecretTest(unittest.TestCase):
+ '''
+ Test class for OCSecret
+ '''
+
+ @mock.patch('oc_secret.locate_oc_binary')
+ @mock.patch('oc_secret.Utils.create_tmpfile_copy')
+ @mock.patch('oc_secret.Utils._write')
+ @mock.patch('oc_secret.OCSecret._run')
+ def test_adding_a_secret(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing adding a secret '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'state': 'present',
+ 'namespace': 'default',
+ 'name': 'testsecretname',
+ 'type': 'Opaque',
+ 'contents': [{
+ 'path': "/tmp/somesecret.json",
+ 'data': "{'one': 1, 'two': 2, 'three': 3}",
+ }],
+ 'decode': False,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False,
+ 'files': None,
+ 'delete_after': True,
+ 'force': False,
+ }
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: secrets "testsecretname" not found'),
+ (0, 'secret/testsecretname', ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ # Act
+ results = OCSecret.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'secrets', 'testsecretname', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'secrets', 'new', 'testsecretname', '--type=Opaque', mock.ANY, '-n', 'default'], None),
+ ])
+
+ mock_write.assert_has_calls([
+ mock.call(mock.ANY, "{'one': 1, 'two': 2, 'three': 3}"),
+ ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_service.py b/roles/lib_openshift/src/test/unit/test_oc_service.py
new file mode 100755
index 000000000..9c21a262f
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_service.py
@@ -0,0 +1,487 @@
+'''
+ Unit tests for oc service
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_service import OCService, locate_oc_binary # noqa: E402
+
+
+# pylint: disable=too-many-public-methods
+class OCServiceTest(unittest.TestCase):
+ '''
+ Test class for OCService
+ '''
+
+ @mock.patch('oc_service.Utils.create_tmpfile_copy')
+ @mock.patch('oc_service.OCService._run')
+ def test_state_list(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': None,
+ 'state': 'list',
+ 'labels': None,
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': None,
+ 'session_affinity': None,
+ 'service_type': None,
+ 'external_ips': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (0, service, '')
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+
+ @mock.patch('oc_service.Utils.create_tmpfile_copy')
+ @mock.patch('oc_service.OCService._run')
+ def test_create(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a create service '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': {'name': '9000-tcp',
+ 'port': 9000,
+ 'protocol': 'TCP',
+ 'targetPOrt': 9000},
+ 'state': 'present',
+ 'labels': None,
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': {'router': 'router'},
+ 'session_affinity': 'ClientIP',
+ 'service_type': 'ClusterIP',
+ 'external_ips': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: services "router" not found'),
+ (1, '', 'Error from server: services "router" not found'),
+ (0, service, ''),
+ (0, service, '')
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['returncode'] == 0)
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @mock.patch('oc_service.Utils.create_tmpfile_copy')
+ @mock.patch('oc_service.OCService._run')
+ def test_create_with_labels(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a create service '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': {'name': '9000-tcp',
+ 'port': 9000,
+ 'protocol': 'TCP',
+ 'targetPOrt': 9000},
+ 'state': 'present',
+ 'labels': {'component': 'some_component', 'infra': 'true'},
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': {'router': 'router'},
+ 'session_affinity': 'ClientIP',
+ 'service_type': 'ClusterIP',
+ 'external_ips': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {"component": "some_component", "infra": "true"}
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: services "router" not found'),
+ (1, '', 'Error from server: services "router" not found'),
+ (0, service, ''),
+ (0, service, '')
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['returncode'] == 0)
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+ self.assertEqual(results['results']['results'][0]['metadata']['labels'], {"component": "some_component", "infra": "true"})
+
+ @mock.patch('oc_service.Utils.create_tmpfile_copy')
+ @mock.patch('oc_service.OCService._run')
+ def test_create_with_external_ips(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a create service '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': {'name': '9000-tcp',
+ 'port': 9000,
+ 'protocol': 'TCP',
+ 'targetPOrt': 9000},
+ 'state': 'present',
+ 'labels': {'component': 'some_component', 'infra': 'true'},
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': {'router': 'router'},
+ 'session_affinity': 'ClientIP',
+ 'service_type': 'ClusterIP',
+ 'external_ips': ['1.2.3.4', '5.6.7.8'],
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {"component": "some_component", "infra": "true"}
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "externalIPs": ["1.2.3.4", "5.6.7.8"],
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: services "router" not found'),
+ (1, '', 'Error from server: services "router" not found'),
+ (0, service, ''),
+ (0, service, '')
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['returncode'] == 0)
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+ self.assertEqual(results['results']['results'][0]['metadata']['labels'], {"component": "some_component", "infra": "true"})
+ self.assertEqual(results['results']['results'][0]['spec']['externalIPs'], ["1.2.3.4", "5.6.7.8"])
diff --git a/roles/lib_openshift/src/test/unit/test_oc_serviceaccount.py b/roles/lib_openshift/src/test/unit/test_oc_serviceaccount.py
new file mode 100755
index 000000000..5772d2f00
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_serviceaccount.py
@@ -0,0 +1,213 @@
+'''
+ Unit tests for oc serviceaccount
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_serviceaccount import OCServiceAccount, locate_oc_binary # noqa: E402
+
+
+class OCServiceAccountTest(unittest.TestCase):
+ '''
+ Test class for OCServiceAccount
+ '''
+
+ @mock.patch('oc_serviceaccount.locate_oc_binary')
+ @mock.patch('oc_serviceaccount.Utils.create_tmpfile_copy')
+ @mock.patch('oc_serviceaccount.OCServiceAccount._run')
+ def test_adding_a_serviceaccount(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing adding a serviceaccount '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'testserviceaccountname',
+ 'namespace': 'default',
+ 'secrets': None,
+ 'image_pull_secrets': None,
+ }
+
+ valid_result_json = '''{
+ "kind": "ServiceAccount",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "testserviceaccountname",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/serviceaccounts/testserviceaccountname",
+ "uid": "4d8320c9-e66f-11e6-8edc-0eece8f2ce22",
+ "resourceVersion": "328450",
+ "creationTimestamp": "2017-01-29T22:07:19Z"
+ },
+ "secrets": [
+ {
+ "name": "testserviceaccountname-dockercfg-4lqd0"
+ },
+ {
+ "name": "testserviceaccountname-token-9h0ej"
+ }
+ ],
+ "imagePullSecrets": [
+ {
+ "name": "testserviceaccountname-dockercfg-4lqd0"
+ }
+ ]
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (1, '', 'Error from server: serviceaccounts "testserviceaccountname" not found'),
+
+ # Second call to mock
+ (0, 'serviceaccount "testserviceaccountname" created', ''),
+
+ # Third call to mock
+ (0, valid_result_json, ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ # Act
+ results = OCServiceAccount.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'sa', 'testserviceaccountname', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'get', 'sa', 'testserviceaccountname', '-o', 'json', '-n', 'default'], None),
+ ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_serviceaccount_secret.py b/roles/lib_openshift/src/test/unit/test_oc_serviceaccount_secret.py
new file mode 100755
index 000000000..b22525068
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_serviceaccount_secret.py
@@ -0,0 +1,414 @@
+'''
+ Unit tests for oc secret add
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_serviceaccount_secret import OCServiceAccountSecret, locate_oc_binary # noqa: E402
+
+try:
+ import ruamel.yaml as yaml # noqa: EF401
+ YAML_TYPE = 'ruamel'
+except ImportError:
+ YAML_TYPE = 'pyyaml'
+
+
+class OCServiceAccountSecretTest(unittest.TestCase):
+ '''
+ Test class for OCServiceAccountSecret
+ '''
+
+ @mock.patch('oc_serviceaccount_secret.locate_oc_binary')
+ @mock.patch('oc_serviceaccount_secret.Utils.create_tmpfile_copy')
+ @mock.patch('oc_serviceaccount_secret.Yedit._write')
+ @mock.patch('oc_serviceaccount_secret.OCServiceAccountSecret._run')
+ def test_adding_a_secret_to_a_serviceaccount(self, mock_cmd, mock_write, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing adding a secret to a service account '''
+
+ # Arrange
+
+ # run_ansible input parameters
+ params = {
+ 'state': 'present',
+ 'namespace': 'default',
+ 'secret': 'newsecret',
+ 'service_account': 'builder',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False,
+ }
+
+ oc_get_sa_before = '''{
+ "apiVersion": "v1",
+ "imagePullSecrets": [
+ {
+ "name": "builder-dockercfg-rsrua"
+ }
+ ],
+ "kind": "ServiceAccount",
+ "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"
+ }
+
+ ]
+ }
+ '''
+
+ oc_get_sa_after = '''{
+ "apiVersion": "v1",
+ "imagePullSecrets": [
+ {
+ "name": "builder-dockercfg-rsrua"
+ }
+ ],
+ "kind": "ServiceAccount",
+ "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"
+ }
+
+ ]
+ }
+ '''
+ builder_ryaml_file = '''\
+secrets:
+- name: builder-dockercfg-rsrua
+- name: builder-token-akqxi
+- name: newsecret
+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
+'''
+
+ builder_pyyaml_file = '''\
+apiVersion: v1
+imagePullSecrets:
+- name: builder-dockercfg-rsrua
+kind: ServiceAccount
+metadata:
+ creationTimestamp: '2017-02-05T17:02:00Z'
+ name: builder
+ namespace: default
+ resourceVersion: '302879'
+ selfLink: /api/v1/namespaces/default/serviceaccounts/builder
+ uid: cf47bca7-ebc4-11e6-b041-0ed9df7abc38
+secrets:
+- name: builder-dockercfg-rsrua
+- name: builder-token-akqxi
+- name: newsecret
+'''
+
+ # 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
+ (0, oc_get_sa_after, ''), # Fourth call to the mock
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ # Act
+ results = OCServiceAccountSecret.run_ansible(params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mocks were called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'replace', '-f', mock.ANY, '-n', 'default'], None),
+ mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None)
+ ])
+
+ yaml_file = builder_pyyaml_file
+
+ if YAML_TYPE == 'ruamel':
+ yaml_file = builder_ryaml_file
+ mock_write.assert_has_calls([
+ mock.call(mock.ANY, yaml_file)
+ ])
+
+ @mock.patch('oc_serviceaccount_secret.locate_oc_binary')
+ @mock.patch('oc_serviceaccount_secret.Utils.create_tmpfile_copy')
+ @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, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing removing 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 = '''{
+ "apiVersion": "v1",
+ "imagePullSecrets": [
+ {
+ "name": "builder-dockercfg-rsrua"
+ }
+ ],
+ "kind": "ServiceAccount",
+ "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"
+ }
+
+ ]
+ }
+ '''
+
+ builder_ryaml_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
+'''
+
+ builder_pyyaml_file = '''\
+apiVersion: v1
+imagePullSecrets:
+- name: builder-dockercfg-rsrua
+kind: ServiceAccount
+metadata:
+ creationTimestamp: '2017-02-05T17:02:00Z'
+ name: builder
+ namespace: default
+ resourceVersion: '302879'
+ selfLink: /api/v1/namespaces/default/serviceaccounts/builder
+ uid: cf47bca7-ebc4-11e6-b041-0ed9df7abc38
+secrets:
+- name: builder-dockercfg-rsrua
+- name: builder-token-akqxi
+'''
+
+ # 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
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ # 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', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'get', 'sa', 'builder', '-o', 'json', '-n', 'default'], None),
+ mock.call(['oc', 'replace', '-f', mock.ANY, '-n', 'default'], None),
+ ])
+
+ yaml_file = builder_pyyaml_file
+
+ if YAML_TYPE == 'ruamel':
+ yaml_file = builder_ryaml_file
+ mock_write.assert_has_calls([
+ mock.call(mock.ANY, yaml_file)
+ ])
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_storageclass.py b/roles/lib_openshift/src/test/unit/test_oc_storageclass.py
new file mode 100755
index 000000000..4fd02a8b1
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_storageclass.py
@@ -0,0 +1,93 @@
+'''
+ Unit tests for oc serviceaccount
+'''
+
+import os
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_storageclass import OCStorageClass # noqa: E402
+
+
+class OCStorageClassTest(unittest.TestCase):
+ '''
+ Test class for OCStorageClass
+ '''
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'present',
+ 'debug': False,
+ 'name': 'testsc',
+ 'provisioner': 'kubernetes.io/aws-ebs',
+ 'annotations': {'storageclass.beta.kubernetes.io/is-default-class': "true"},
+ 'parameters': {'type': 'gp2'},
+ 'api_version': 'v1',
+ 'default_storage_class': 'true'}
+
+ @mock.patch('oc_storageclass.locate_oc_binary')
+ @mock.patch('oc_storageclass.Utils.create_tmpfile_copy')
+ @mock.patch('oc_storageclass.OCStorageClass._run')
+ def test_adding_a_storageclass(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
+ ''' Testing adding a storageclass '''
+
+ # Arrange
+
+ # run_ansible input parameters
+
+ valid_result_json = '''{
+ "kind": "StorageClass",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "testsc",
+ "selfLink": "/apis/storage.k8s.io/v1/storageclasses/gp2",
+ "uid": "4d8320c9-e66f-11e6-8edc-0eece8f2ce22",
+ "resourceVersion": "2828",
+ "creationTimestamp": "2017-01-29T22:07:19Z",
+ "annotations": {"storageclass.beta.kubernetes.io/is-default-class": "true"}
+ },
+ "provisioner": "kubernetes.io/aws-ebs",
+ "parameters": {"type": "gp2"}
+ }'''
+
+ # Return values of our mocked function call. These get returned once per call.
+ mock_cmd.side_effect = [
+ # First call to mock
+ (1, '', 'Error from server: storageclass "testsc" not found'),
+
+ # Second call to mock
+ (0, 'storageclass "testsc" created', ''),
+
+ # Third call to mock
+ (0, valid_result_json, ''),
+ ]
+
+ mock_oc_binary.side_effect = [
+ 'oc'
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ # Act
+ results = OCStorageClass.run_ansible(OCStorageClassTest.params, False)
+
+ # Assert
+ self.assertTrue(results['changed'])
+ self.assertEqual(results['results']['returncode'], 0)
+ self.assertEqual(results['state'], 'present')
+
+ # Making sure our mock was called as we expected
+ mock_cmd.assert_has_calls([
+ mock.call(['oc', 'get', 'storageclass', 'testsc', '-o', 'json'], None),
+ mock.call(['oc', 'create', '-f', mock.ANY], None),
+ mock.call(['oc', 'get', 'storageclass', 'testsc', '-o', 'json'], None),
+ ])
diff --git a/roles/lib_openshift/src/test/unit/test_oc_user.py b/roles/lib_openshift/src/test/unit/test_oc_user.py
new file mode 100755
index 000000000..f7a17cc2c
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_user.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python2
+'''
+ Unit tests for oc user
+'''
+# To run
+# ./oc_user.py
+#
+# ..
+# ----------------------------------------------------------------------
+# Ran 2 tests in 0.003s
+#
+# OK
+
+import os
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_user import OCUser # noqa: E402
+
+
+class OCUserTest(unittest.TestCase):
+ '''
+ Test class for OCUser
+ '''
+
+ def setUp(self):
+ ''' setup method will create a file and set to known configuration '''
+ pass
+
+ @mock.patch('oc_user.Utils.create_tmpfile_copy')
+ @mock.patch('oc_user.OCUser._run')
+ def test_state_list(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a user list '''
+ params = {'username': 'testuser@email.com',
+ 'state': 'list',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'full_name': None,
+ 'groups': [],
+ 'debug': False}
+
+ user = '''{
+ "kind": "User",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "testuser@email.com",
+ "selfLink": "/oapi/v1/users/testuser@email.com",
+ "uid": "02fee6c9-f20d-11e6-b83b-12e1a7285e80",
+ "resourceVersion": "38566887",
+ "creationTimestamp": "2017-02-13T16:53:58Z"
+ },
+ "fullName": "Test User",
+ "identities": null,
+ "groups": null
+ }'''
+
+ mock_cmd.side_effect = [
+ (0, user, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCUser.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertTrue(results['results'][0]['metadata']['name'] == "testuser@email.com")
+
+ @mock.patch('oc_user.Utils.create_tmpfile_copy')
+ @mock.patch('oc_user.OCUser._run')
+ def test_state_present(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a user list '''
+ params = {'username': 'testuser@email.com',
+ 'state': 'present',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'full_name': 'Test User',
+ 'groups': [],
+ 'debug': False}
+
+ created_user = '''{
+ "kind": "User",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "testuser@email.com",
+ "selfLink": "/oapi/v1/users/testuser@email.com",
+ "uid": "8d508039-f224-11e6-b83b-12e1a7285e80",
+ "resourceVersion": "38646241",
+ "creationTimestamp": "2017-02-13T19:42:28Z"
+ },
+ "fullName": "Test User",
+ "identities": null,
+ "groups": null
+ }'''
+
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: users "testuser@email.com" not found'), # get
+ (1, '', 'Error from server: users "testuser@email.com" not found'), # get
+ (0, 'user "testuser@email.com" created', ''), # create
+ (0, created_user, ''), # get
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCUser.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['results'][0]['metadata']['name'] ==
+ "testuser@email.com")
+
+ def tearDown(self):
+ '''TearDown method'''
+ pass
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/roles/lib_openshift/src/test/unit/test_oc_version.py b/roles/lib_openshift/src/test/unit/test_oc_version.py
new file mode 100755
index 000000000..c287bad0b
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_version.py
@@ -0,0 +1,162 @@
+'''
+ Unit tests for oc version
+'''
+
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error,wrong-import-position
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_version import OCVersion, locate_oc_binary # noqa: E402
+
+
+class OCVersionTest(unittest.TestCase):
+ '''
+ Test class for OCVersion
+ '''
+
+ @mock.patch('oc_version.Utils.create_tmpfile_copy')
+ @mock.patch('oc_version.OCVersion.openshift_cmd')
+ def test_get(self, mock_openshift_cmd, mock_tmpfile_copy):
+ ''' Testing a get '''
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'list',
+ 'debug': False}
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": "oc version",
+ "results": "oc v3.4.0.39\nkubernetes v1.4.0+776c994\n" +
+ "features: Basic-Auth GSSAPI Kerberos SPNEGO\n\n" +
+ "Server https://internal.api.opstest.openshift.com" +
+ "openshift v3.4.0.39\n" +
+ "kubernetes v1.4.0+776c994\n",
+ "returncode": 0}
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCVersion.run_ansible(params)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['oc_short'], '3.4')
+ self.assertEqual(results['results']['oc_numeric'], '3.4.0.39')
+ self.assertEqual(results['results']['kubernetes_numeric'], '1.4.0')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
diff --git a/roles/lib_openshift/src/test/unit/test_oc_volume.py b/roles/lib_openshift/src/test/unit/test_oc_volume.py
new file mode 100755
index 000000000..d91e22bc7
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/test_oc_volume.py
@@ -0,0 +1,633 @@
+'''
+ Unit tests for oc volume
+'''
+
+import copy
+import os
+import six
+import sys
+import unittest
+import mock
+
+# Removing invalid variable names for tests so that I can
+# keep them brief
+# pylint: disable=invalid-name,no-name-in-module
+# Disable import-error b/c our libraries aren't loaded in jenkins
+# pylint: disable=import-error
+# place class in our python path
+module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501
+sys.path.insert(0, module_path)
+from oc_volume import OCVolume, locate_oc_binary # noqa: E402
+
+
+class OCVolumeTest(unittest.TestCase):
+ '''
+ Test class for OCVolume
+ '''
+ params = {'name': 'oso-rhel7-zagg-web',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'namespace': 'test',
+ 'labels': None,
+ 'state': 'present',
+ 'kind': 'dc',
+ 'mount_path': None,
+ 'secret_name': None,
+ 'mount_type': 'pvc',
+ 'claim_name': 'testclaim',
+ 'claim_size': '1G',
+ 'configmap_name': None,
+ 'vol_name': 'test-volume',
+ 'debug': False}
+
+ @mock.patch('oc_volume.Utils.create_tmpfile_copy')
+ @mock.patch('oc_volume.OCVolume._run')
+ def test_create_pvc(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a label list '''
+ params = copy.deepcopy(OCVolumeTest.params)
+
+ dc = '''{
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "new-monitoring",
+ "selfLink": "/oapi/v1/namespaces/new-monitoring/deploymentconfigs/oso-rhel7-zagg-web",
+ "uid": "f56e9dd2-7c13-11e6-b046-0e8844de0587",
+ "resourceVersion": "137095771",
+ "generation": 4,
+ "creationTimestamp": "2016-09-16T13:46:24Z",
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "name": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "type": "Rolling",
+ "rollingParams": {
+ "updatePeriodSeconds": 1,
+ "intervalSeconds": 1,
+ "timeoutSeconds": 600,
+ "maxUnavailable": "25%",
+ "maxSurge": "25%"
+ },
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ },
+ {
+ "type": "ImageChange",
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "oso-rhel7-zagg-web"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "namespace": "new-monitoring",
+ "name": "oso-rhel7-zagg-web:latest"
+ },
+ "lastTriggeredImage": "notused"
+ }
+ }
+ ],
+ "replicas": 10,
+ "test": false,
+ "selector": {
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "monitoring-secrets",
+ "secret": {
+ "secretName": "monitoring-secrets"
+ }
+ }
+ ],
+ "containers": [
+ {
+ "name": "oso-rhel7-zagg-web",
+ "image": "notused",
+ "resources": {},
+ "volumeMounts": [
+ {
+ "name": "monitoring-secrets",
+ "mountPath": "/secrets"
+ }
+ ],
+ "terminationMessagePath": "/dev/termination-log",
+ "imagePullPolicy": "Always",
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ }
+ }
+ ],
+ "restartPolicy": "Always",
+ "terminationGracePeriodSeconds": 30,
+ "dnsPolicy": "ClusterFirst",
+ "securityContext": {}
+ }
+ }
+ }
+ }'''
+
+ post_dc = '''{
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "new-monitoring",
+ "selfLink": "/oapi/v1/namespaces/new-monitoring/deploymentconfigs/oso-rhel7-zagg-web",
+ "uid": "f56e9dd2-7c13-11e6-b046-0e8844de0587",
+ "resourceVersion": "137095771",
+ "generation": 4,
+ "creationTimestamp": "2016-09-16T13:46:24Z",
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "name": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "type": "Rolling",
+ "rollingParams": {
+ "updatePeriodSeconds": 1,
+ "intervalSeconds": 1,
+ "timeoutSeconds": 600,
+ "maxUnavailable": "25%",
+ "maxSurge": "25%"
+ },
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ },
+ {
+ "type": "ImageChange",
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "oso-rhel7-zagg-web"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "namespace": "new-monitoring",
+ "name": "oso-rhel7-zagg-web:latest"
+ },
+ "lastTriggeredImage": "notused"
+ }
+ }
+ ],
+ "replicas": 10,
+ "test": false,
+ "selector": {
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "monitoring-secrets",
+ "secret": {
+ "secretName": "monitoring-secrets"
+ }
+ },
+ {
+ "name": "test-volume",
+ "persistentVolumeClaim": {
+ "claimName": "testclass",
+ "claimSize": "1G"
+ }
+ }
+ ],
+ "containers": [
+ {
+ "name": "oso-rhel7-zagg-web",
+ "image": "notused",
+ "resources": {},
+ "volumeMounts": [
+ {
+ "name": "monitoring-secrets",
+ "mountPath": "/secrets"
+ },
+ {
+ "name": "test-volume",
+ "mountPath": "/data"
+ }
+ ],
+ "terminationMessagePath": "/dev/termination-log",
+ "imagePullPolicy": "Always",
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ }
+ }
+ ],
+ "restartPolicy": "Always",
+ "terminationGracePeriodSeconds": 30,
+ "dnsPolicy": "ClusterFirst",
+ "securityContext": {}
+ }
+ }
+ }
+ }'''
+
+ mock_cmd.side_effect = [
+ (0, dc, ''),
+ (0, dc, ''),
+ (0, '', ''),
+ (0, post_dc, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCVolume.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['results'][-1]['name'] == 'test-volume')
+
+ @mock.patch('oc_volume.Utils.create_tmpfile_copy')
+ @mock.patch('oc_volume.OCVolume._run')
+ def test_create_configmap(self, mock_cmd, mock_tmpfile_copy):
+ ''' Testing a label list '''
+ params = copy.deepcopy(OCVolumeTest.params)
+ params.update({'mount_path': '/configmap',
+ 'mount_type': 'configmap',
+ 'configmap_name': 'configtest',
+ 'vol_name': 'configvol'})
+
+ dc = '''{
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "new-monitoring",
+ "selfLink": "/oapi/v1/namespaces/new-monitoring/deploymentconfigs/oso-rhel7-zagg-web",
+ "uid": "f56e9dd2-7c13-11e6-b046-0e8844de0587",
+ "resourceVersion": "137095771",
+ "generation": 4,
+ "creationTimestamp": "2016-09-16T13:46:24Z",
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "name": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "type": "Rolling",
+ "rollingParams": {
+ "updatePeriodSeconds": 1,
+ "intervalSeconds": 1,
+ "timeoutSeconds": 600,
+ "maxUnavailable": "25%",
+ "maxSurge": "25%"
+ },
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ },
+ {
+ "type": "ImageChange",
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "oso-rhel7-zagg-web"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "namespace": "new-monitoring",
+ "name": "oso-rhel7-zagg-web:latest"
+ },
+ "lastTriggeredImage": "notused"
+ }
+ }
+ ],
+ "replicas": 10,
+ "test": false,
+ "selector": {
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "monitoring-secrets",
+ "secret": {
+ "secretName": "monitoring-secrets"
+ }
+ }
+ ],
+ "containers": [
+ {
+ "name": "oso-rhel7-zagg-web",
+ "image": "notused",
+ "resources": {},
+ "volumeMounts": [
+ {
+ "name": "monitoring-secrets",
+ "mountPath": "/secrets"
+ }
+ ],
+ "terminationMessagePath": "/dev/termination-log",
+ "imagePullPolicy": "Always",
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ }
+ }
+ ],
+ "restartPolicy": "Always",
+ "terminationGracePeriodSeconds": 30,
+ "dnsPolicy": "ClusterFirst",
+ "securityContext": {}
+ }
+ }
+ }
+ }'''
+
+ post_dc = '''{
+ "kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "oso-rhel7-zagg-web",
+ "namespace": "new-monitoring",
+ "selfLink": "/oapi/v1/namespaces/new-monitoring/deploymentconfigs/oso-rhel7-zagg-web",
+ "uid": "f56e9dd2-7c13-11e6-b046-0e8844de0587",
+ "resourceVersion": "137095771",
+ "generation": 4,
+ "creationTimestamp": "2016-09-16T13:46:24Z",
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "name": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "strategy": {
+ "type": "Rolling",
+ "rollingParams": {
+ "updatePeriodSeconds": 1,
+ "intervalSeconds": 1,
+ "timeoutSeconds": 600,
+ "maxUnavailable": "25%",
+ "maxSurge": "25%"
+ },
+ "resources": {}
+ },
+ "triggers": [
+ {
+ "type": "ConfigChange"
+ },
+ {
+ "type": "ImageChange",
+ "imageChangeParams": {
+ "automatic": true,
+ "containerNames": [
+ "oso-rhel7-zagg-web"
+ ],
+ "from": {
+ "kind": "ImageStreamTag",
+ "namespace": "new-monitoring",
+ "name": "oso-rhel7-zagg-web:latest"
+ },
+ "lastTriggeredImage": "notused"
+ }
+ }
+ ],
+ "replicas": 10,
+ "test": false,
+ "selector": {
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "template": {
+ "metadata": {
+ "creationTimestamp": null,
+ "labels": {
+ "app": "oso-rhel7-ops-base",
+ "deploymentconfig": "oso-rhel7-zagg-web"
+ },
+ "annotations": {
+ "openshift.io/generated-by": "OpenShiftNewApp"
+ }
+ },
+ "spec": {
+ "volumes": [
+ {
+ "name": "monitoring-secrets",
+ "secret": {
+ "secretName": "monitoring-secrets"
+ }
+ },
+ {
+ "name": "configvol",
+ "configMap": {
+ "name": "configtest"
+ }
+ }
+ ],
+ "containers": [
+ {
+ "name": "oso-rhel7-zagg-web",
+ "image": "notused",
+ "resources": {},
+ "volumeMounts": [
+ {
+ "name": "monitoring-secrets",
+ "mountPath": "/secrets"
+ },
+ {
+ "name": "configvol",
+ "mountPath": "/configmap"
+ }
+ ],
+ "terminationMessagePath": "/dev/termination-log",
+ "imagePullPolicy": "Always",
+ "securityContext": {
+ "capabilities": {},
+ "privileged": false
+ }
+ }
+ ],
+ "restartPolicy": "Always",
+ "terminationGracePeriodSeconds": 30,
+ "dnsPolicy": "ClusterFirst",
+ "securityContext": {}
+ }
+ }
+ }
+ }'''
+
+ mock_cmd.side_effect = [
+ (0, dc, ''),
+ (0, dc, ''),
+ (0, '', ''),
+ (0, post_dc, ''),
+ ]
+
+ mock_tmpfile_copy.side_effect = [
+ '/tmp/mocked_kubeconfig',
+ ]
+
+ results = OCVolume.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['results'][-1]['name'] == 'configvol')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_path_exists.side_effect = lambda _: False
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY3, 'py2 test only')
+ @mock.patch('os.path.exists')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_path_exists.side_effect = lambda f: f == oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup fallback '''
+
+ mock_env_get.side_effect = lambda _v, _d: ''
+
+ mock_shutil_which.side_effect = lambda _f, path=None: None
+
+ self.assertEqual(locate_oc_binary(), 'oc')
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in path '''
+
+ oc_bin = '/usr/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in /usr/local/bin '''
+
+ oc_bin = '/usr/local/bin/oc'
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)
+
+ @unittest.skipIf(six.PY2, 'py3 test only')
+ @mock.patch('shutil.which')
+ @mock.patch('os.environ.get')
+ def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which):
+ ''' Testing binary lookup in ~/bin '''
+
+ oc_bin = os.path.expanduser('~/bin/oc')
+
+ mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin'
+
+ mock_shutil_which.side_effect = lambda _f, path=None: oc_bin
+
+ self.assertEqual(locate_oc_binary(), oc_bin)