Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keyword for role create and delete #97

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
419d410
Added keyword for create and delete role
rohini-nubolab Oct 1, 2021
8a336d3
Added index.html
rohini-nubolab Oct 1, 2021
e6af6c2
Added role binding
rohini-nubolab Oct 1, 2021
60f27dd
Update role_kw.robot
rohini-nubolab Oct 1, 2021
e7cb010
Update .coveragerc
rohini-nubolab Oct 1, 2021
fde6cc0
Update role_kw.robot
rohini-nubolab Oct 1, 2021
b50a0d9
Merge branch 'master' into master
rohini-nubolab Oct 8, 2021
5f1a50a
Added keywords
rohini-nubolab Oct 12, 2021
289031e
Update KubeLibrary.py
rohini-nubolab Oct 18, 2021
1103cc0
Update KubeLibrary.py
rohini-nubolab Oct 18, 2021
bc10f48
Update KubeLibrary.py
rohini-nubolab Oct 18, 2021
e95df5a
Update KubeLibrary.py
rohini-nubolab Oct 21, 2021
5cc49d7
Update configmap.robot
rohini-nubolab Oct 21, 2021
4f0e2fc
Update cronjob.robot
rohini-nubolab Oct 21, 2021
8398a0d
Update daemonsets.robot
rohini-nubolab Oct 21, 2021
066dd58
Update configmap_kw.robot
rohini-nubolab Oct 21, 2021
dee5c0a
Update cronjob_kw.robot
rohini-nubolab Oct 22, 2021
6de8dc3
Update daemonsets_kw.robot
rohini-nubolab Oct 22, 2021
09f9a53
Update deployment_kw.robot
rohini-nubolab Oct 22, 2021
0e95f7d
Update ingress.robot
rohini-nubolab Oct 22, 2021
659f022
Update ingress_kw.robot
rohini-nubolab Oct 22, 2021
7066d87
Update ingress_kw.robot
rohini-nubolab Oct 22, 2021
50c75f9
Update pod.robot
rohini-nubolab Oct 22, 2021
3738af8
Update pod_kw.robot
rohini-nubolab Oct 22, 2021
af4511e
Update pvc.robot
rohini-nubolab Oct 22, 2021
6e22d3b
Update pvc_kw.robot
rohini-nubolab Oct 22, 2021
00fc6e3
Update replicaset.robot
rohini-nubolab Oct 22, 2021
cbd3868
Update replicaset_kw.robot
rohini-nubolab Oct 22, 2021
ee9b1bd
Update role.robot
rohini-nubolab Oct 22, 2021
1d18007
Update role.robot
rohini-nubolab Oct 22, 2021
b4e9f2c
Update role_kw.robot
rohini-nubolab Oct 22, 2021
d67af0d
Update secret.robot
rohini-nubolab Oct 22, 2021
bbdc9ea
Update secret_kw.robot
rohini-nubolab Oct 22, 2021
5861191
Update service.robot
rohini-nubolab Oct 22, 2021
820a84e
Update service_kw.robot
rohini-nubolab Oct 22, 2021
391dd35
Update service_account.robot
rohini-nubolab Oct 22, 2021
3ae1bc4
Update service_account_kw.robot
rohini-nubolab Oct 22, 2021
561459c
Update deployment.robot
rohini-nubolab Oct 22, 2021
3dfee0a
added unittest for role create and delete
rohini-nubolab Nov 10, 2021
faa2e13
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
c3af1d6
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
f1e358f
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
babe32c
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
607a82e
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
208da09
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
c040059
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
a69b83c
Update test_KubeLibrary.py
rohini-nubolab Nov 10, 2021
4545100
Update .coveragerc
rohini-nubolab Nov 10, 2021
bfc588b
Update role_kw.robot
rohini-nubolab Nov 11, 2021
1c07727
Update KubeLibrary.py
rohini-nubolab Nov 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/index.html

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/KubeLibrary/KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,35 @@ def filter_endpoints_names(self, endpoints):
List of endpoint objects
"""
return [endpoints.metadata.name for endpoints in endpoints.items]

def read_namespaced_role(self, name, namespace):
"""Gets role details in given namespace.
Returns Role object representation.
Name of role.
- ``namespace``:
Namespace to check
"""
ret = self.rbac_authv1_api.read_namespaced_role(name, namespace)
return ret

def create_role_in_namespace(self, namespace, body):
"""Creates role in a namespace
Returns created role
- ``body``:
Role object.
- ``namespace``:
Namespace to check
"""
ret = self.rbac_authv1_api.create_namespaced_role(namespace=namespace, body=body)
return ret

def delete_role_in_namespace(self, name, namespace):
"""Deletes role in a namespace
Returns V1 status
- ``name``:
Role name
- ``namespace``:
Namespace to check
"""
ret = self.rbac_authv1_api.delete_namespaced_role(name=name, namespace=namespace)
return ret
66 changes: 66 additions & 0 deletions test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ def mock_list_namespaced_role_bindings(namespace, watch=False):
return list_of_role_bind


def mock_delete_role_in_namespace(name, namespace):
if namespace == 'default':
with open('test/resources/role.json') as json_file:
role_details_content = json.load(json_file)
role_details = AttributeDict({'items': role_details_content})
return role_details


def mock_create_role_in_namespace(namespace, body):
if namespace == 'default':
with open('test/resources/role.json') as json_file:
role_details_content = json.load(json_file)
read_role_details = AttributeDict({'items': role_details_content})
return read_role_details


bearer_token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjdXVWJMOUdTaDB1TjcyNmF0Sjk4RWlzQ05RaWdSUFoyN004TmlGT1pSX28ifQ.' \
'eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1' \
'lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im15c2EtdG' \
Expand Down Expand Up @@ -678,3 +694,53 @@ def test_get_cron_job_details_in_namespace(self, mock_lnp):
kl = KubeLibrary(kube_config='test/resources/k3d')
cron_job_details = kl.get_cron_job_details_in_namespace('hello', 'default')
self.assertEqual('mytestlabel', cron_job_details.items.metadata.labels.TestLabel)

@mock.patch('kubernetes.client.RbacAuthorizationV1Api.delete_namespaced_role')
def test_delete_role_in_namespace(self, mock_lnp):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could test handling situation when there is no role to remove. Other than that the mocked response is wrong because as I understand it will return V1 status.

mock_lnp.side_effect = mock_delete_role_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
role_list = kl.delete_role_in_namespace('pod-reader', 'default')
self.assertEqual(['pod-reader'], [item.metadata.name for item in role_list.items])

@mock.patch('kubernetes.client.RbacAuthorizationV1Api.create_namespaced_role')
def test_create_role_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_create_role_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
name = 'pod-reader'
role_manifest = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this test has no value, we are not testing anything as everything is mocked. One thing that might be useful is testing handling situation when role is already created with some meaningful output.

"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "Role",
"metadata": {
"creationTimestamp": "2021-03-10T04:54:29Z",
"managedFields": [
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"fieldsType": "FieldsV1",
"fieldsV1": {
"f:rules": {}
},
"manager": "kubectl",
"operation": "Update",
"time": "2021-03-10T04:54:29Z"
}
],
"name": name
},
"rules": [
{
"apiGroups": [
""
],
"resources": [
"pods"
],
"verbs": [
"get",
"watch",
"list"
]
}
]
}
role_details = kl.create_role_in_namespace('default', role_manifest)
self.assertEqual(['pod-reader'], [item.metadata.name for item in role_details.items])
2 changes: 1 addition & 1 deletion testcases/configmap/configmap.robot
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Configmap test case example

Configmap by label
[Tags] grafana
List all configmaps in namespace default app=grafana
List all configmaps in namespace default app=grafana
2 changes: 0 additions & 2 deletions testcases/configmap/configmap_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ Log key value pairs
FOR ${key} ${value} IN &{configmap_data}
Log ${key} = ${value} console=True
END


1 change: 0 additions & 1 deletion testcases/cronjob/cronjob.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ Working on Cron Job
Edit obtained cron job test-cronjob
Create new cron job in namespace kubelib-tests
Delete created cron job in namespace test-cronjob kubelib-tests

4 changes: 0 additions & 4 deletions testcases/deployment/deployment_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ Show Grafana Deployment
Assert Replica Status
Should be Equal ${DEPLOYMENT.status.available_replicas} ${DEPLOYMENT.status.replicas}
... msg=Available replica count (${DEPLOYMENT.status.available_replicas}) doesn't match current replica count (${DEPLOYMENT.status.replicas})




1 change: 0 additions & 1 deletion testcases/ingress/ingress.robot
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ Resource ./ingress_kw.robot
Ingresses by label
[Tags] other
List ingresses by label kubelib-tests app.kubernetes.io/instance=kubelib-test

2 changes: 1 addition & 1 deletion testcases/pod/pod.robot
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ List pods by label
[Tags] other
Given waited for pods matching "${KLIB_POD_PATTERN}" in namespace "${KLIB_POD_NAMESPACE}" to be running
When getting pods matching label "${KLIB_POD_LABELS}" in namespace "${KLIB_POD_NAMESPACE}"
Then pods have labels "${KLIB_POD_LABELS}"
Then pods have labels "${KLIB_POD_LABELS}"
2 changes: 1 addition & 1 deletion testcases/pod/pod_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ logs of pod can be retrived
Set Test Variable ${POD_LOGS} ${pod_logs}

logs contain expected string
Should Contain ${POD_LOGS} I am
Should Contain ${POD_LOGS} I am
2 changes: 1 addition & 1 deletion testcases/pvc/pvc.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Resource ./pvc_kw.robot
*** Test Cases ***
List Persitent Volume Claims by label
[Tags] grafana
List pvcs by label default app=grafana
List pvcs by label default app=grafana
1 change: 0 additions & 1 deletion testcases/replicaset/replicaset_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ List all replicasets in namespace
FOR ${replicaset} IN @{namespace_replicasets}
Log To Console ${replicaset.metadata.name} console=True
END

7 changes: 7 additions & 0 deletions testcases/role/role.robot
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ Role test case example
[Tags] other
List all roles in namespace default
List all role bindings in namespace default

Working on Role
[Tags] other prerelease
List all roles in namespace default
Edit obtained role mini-role
Create new role in namespace default
Delete created role in namespace mini-role default
26 changes: 25 additions & 1 deletion testcases/role/role_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,33 @@ List all roles in namespace
@{namespace_roles}= Get Roles In Namespace ${namespace}
Length Should Be ${namespace_roles} 2
Log \nRoles in namespace ${namespace_roles}: console=True

FOR ${role} IN @{namespace_roles}
${role_details}= Read Namespaced Role ${role} ${namespace}
Log ${role_details.metadata.name} console=True
Set Global Variable ${role_name} ${role_details.metadata.name}
Set Global Variable ${role} ${role_details}
END

List all role bindings in namespace
[Arguments] ${namespace}
@{namespace_role_bindings}= Get Role Bindings In Namespace ${namespace}
Length Should Be ${namespace_role_bindings} 2
Log \nRole_binding in namespace ${namespace_role_bindings}: console=True

Edit obtained role
[Arguments] ${role_name}
${role.metadata.name}= Set Variable ${role_name}
${role.metadata.resource_version}= Set Variable ${None}
Set Global Variable ${new_role} ${role}

Create new role in namespace
[Arguments] ${namespace}
Log \nCreate new role in namespace ${namespace} console=True
${new_role}= Create Role In Namespace ${namespace} ${new_role}
Log ${new_role} console=True

Delete created role in namespace
[Arguments] ${role_name} ${namespace}
Log \nDelete role in namespace ${namespace} console=True
${status}= Delete Role In Namespace ${role_name} ${namespace}
Log ${status}
2 changes: 1 addition & 1 deletion testcases/service/service.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Resource ./service_kw.robot
*** Test Cases ***
Services by label
[Tags] other
List services by label kubelib-tests app.kubernetes.io/instance=kubelib-test
List services by label kubelib-tests app.kubernetes.io/instance=kubelib-test
2 changes: 0 additions & 2 deletions testcases/service/service_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ List services by label
Dictionary Should Contain Item ${sevice_details.metadata.labels} ${label_key} ${label_value}
... msg=Expected labels do not match.
END


2 changes: 1 addition & 1 deletion testcases/service_account/service_account.robot
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Working on Service Accounts
List all service accounts for matching name pattern in namespace default kubelib-tests
Edit obtained service account test-sa
Create new service account in namespace kubelib-tests
Delete created service account in namespace test-sa kubelib-tests
Delete created service account in namespace test-sa kubelib-tests
2 changes: 1 addition & 1 deletion testcases/service_account/service_account_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ Create new service account in namespace
Delete created service account in namespace
[Arguments] ${service_account_name} ${namespace}
${status}= Delete Service Account In Namespace ${service_account_name} ${namespace}
Log ${status}
Log ${status}