assemble k8s client; patch scaling test

This commit is contained in:
Sergey Dudoladov 2019-04-30 15:18:07 +02:00
parent af202bf550
commit 806ca7ba81
1 changed files with 40 additions and 23 deletions

View File

@ -5,10 +5,21 @@ from pprint import pprint
import timeout_decorator import timeout_decorator
import subprocess import subprocess
class K8sApi:
def __init__(self):
self.config = config.load_kube_config()
self.k8s_client = client.ApiClient()
self.core_v1 = client.CoreV1Api()
self.crd_api = client.CustomObjectsApi()
class SmokeTestCase(unittest.TestCase): class SmokeTestCase(unittest.TestCase):
''' '''
Test the most basic e2e functionality of the operator. Test the most basic e2e functionality of the operator.
''' '''
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
''' '''
@ -19,73 +30,79 @@ class SmokeTestCase(unittest.TestCase):
In the case of test failure the cluster will stay to enable manual examination; In the case of test failure the cluster will stay to enable manual examination;
next invocation of "make e2e" will re-create it. next invocation of "make e2e" will re-create it.
''' '''
_ = config.load_kube_config()
k8s_client = client.ApiClient()
# HACK # HACK
# 1. creating RBAC entites with a separate client fails with "AttributeError: object has no attribute 'select_header_accept'" # 1. creating RBAC entites with a separate client fails with "AttributeError: object has no attribute 'select_header_accept'"
# 2. utils.create_from_yaml cannot create multiple entites from a single file # 2. utils.create_from_yaml cannot create multiple entites from a single file
subprocess.run(["kubectl", "create", "-f", "manifests/operator-service-account-rbac.yaml"]) subprocess.run(["kubectl", "create", "-f", "manifests/operator-service-account-rbac.yaml"])
k8s_api = K8sApi()
for filename in ["configmap.yaml", "postgres-operator.yaml"]: for filename in ["configmap.yaml", "postgres-operator.yaml"]:
path = "manifests/" + filename path = "manifests/" + filename
utils.create_from_yaml(k8s_client, path) utils.create_from_yaml(k8s_api.k8s_client, path)
v1 = client.CoreV1Api()
pod_phase = None pod_phase = None
while pod_phase != 'Running': while pod_phase != 'Running':
pods = v1.list_namespaced_pod('default', label_selector='name=postgres-operator').items pods = k8s_api.core_v1.list_namespaced_pod('default', label_selector='name=postgres-operator').items
if pods: if pods:
operator_pod = pods[0] operator_pod = pods[0]
pod_phase = operator_pod.status.phase pod_phase = operator_pod.status.phase
print("Waiting for the operator pod to start. Current phase of pod lifecycle: " + str(pod_phase)) print("Waiting for the operator pod to start. Current phase of pod lifecycle: " + str(pod_phase))
time.sleep(5) time.sleep(5)
k8s_client = client.ApiClient()
# HACK around the lack of Python client for the acid.zalan.do resource # HACK around the lack of Python client for the acid.zalan.do resource
subprocess.run(["kubectl", "create", "-f", "manifests/minimal-postgres-manifest.yaml"]) subprocess.run(["kubectl", "create", "-f", "manifests/minimal-postgres-manifest.yaml"])
pod_phase = 'None' pod_phase = 'None'
while pod_phase != 'Running': while pod_phase != 'Running':
pods = v1.list_namespaced_pod('default', label_selector='spilo-role=master').items pods = k8s_api.core_v1.list_namespaced_pod('default', label_selector='spilo-role=master').items
if pods: if pods:
operator_pod = pods[0] operator_pod = pods[0]
pod_phase = operator_pod.status.phase pod_phase = operator_pod.status.phase
print("Waiting for the Spilo master pod to start. Current phase: " + str(pod_phase)) print("Waiting for the Spilo master pod to start. Current phase: " + str(pod_phase))
time.sleep(5) time.sleep(5)
@timeout_decorator.timeout(60) @timeout_decorator.timeout(240)
def test_master_is_unique(self): def test_master_is_unique(self):
""" """
Check that there is a single pod in the k8s cluster with the label "spilo-role=master". Check that there is a single pod in the k8s cluster with the label "spilo-role=master".
""" """
_ = config.load_kube_config() k8s = K8sApi()
v1 = client.CoreV1Api() master_pods = k8s.core_v1.list_namespaced_pod('default', label_selector='spilo-role=master,version=acid-minimal-cluster').items
master_pods = v1.list_namespaced_pod('default', label_selector='spilo-role=master,version=acid-minimal-cluster').items
self.assertEqual(len(master_pods), 1, "Expected 1 master pod,found " + str(len(master_pods))) self.assertEqual(len(master_pods), 1, "Expected 1 master pod,found " + str(len(master_pods)))
@timeout_decorator.timeout(60) @timeout_decorator.timeout(240)
def test_scaling(self): def test_scaling(self):
""" """
Scale up from 2 to 3 pods and back to 2 by updating the Postgres manifest at runtime. Scale up from 2 to 3 pods and back to 2 by updating the Postgres manifest at runtime.
""" """
k8s = K8sApi()
_ = config.load_kube_config()
crd_api = client.CustomObjectsApi()
v1 = client.CoreV1Api()
body = { body = {
"spec": { "spec": {
"numberOfInstances": 3 "numberOfInstances": 3
} }
} }
_ = crd_api.patch_namespaced_custom_object("acid.zalan.do", "v1", "default", "postgresqls", "acid-minimal-cluster", body) _ = k8s.crd_api.patch_namespaced_custom_object("acid.zalan.do", "v1", "default", "postgresqls", "acid-minimal-cluster", body)
while len(v1.list_namespaced_pod('default', label_selector='version=acid-minimal-cluster').items) != 3: while len(k8s.core_v1.list_namespaced_pod('default', label_selector='version=acid-minimal-cluster').items) != 3:
print("Waiting for the cluster to scale up to 3 podes.") print("Waiting for the cluster to scale up to 3 pods.")
time.sleep(5) time.sleep(5)
self.assertEqual(3, len(k8s.core_v1.list_namespaced_pod('default', label_selector='version=acid-minimal-cluster').items))
body = {
"spec": {
"numberOfInstances": 2
}
}
_ = k8s.crd_api.patch_namespaced_custom_object("acid.zalan.do", "v1", "default", "postgresqls", "acid-minimal-cluster", body)
while len(k8s.core_v1.list_namespaced_pod('default', label_selector='version=acid-minimal-cluster').items) != 2:
print("Waiting for the cluster to scale down to 2 pods.")
time.sleep(5)
self.assertEqual(2, len(k8s.core_v1.list_namespaced_pod('default', label_selector='version=acid-minimal-cluster').items))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()