def test_delete_all_real(gke_cluster_with_pv): """Test deleting all jobs, pvcs, and pvs on a real GKE cluster""" # cluster name name = gke_cluster_with_pv # make sure cluster exists assert name in gcp.get_gke_clusters() # ... and has jobs and persistent disks disks = kubernetes.get_persistent_disks() assert len(disks) > 0 assert len(kubernetes.get_jobs()) > 0 # delete everything kubernetes.delete_all() # test that jobs and volumes were deleted assert len(kubernetes.get_jobs()) == 0 assert len(kubernetes.get_persistent_disks()) == 0
def test_get_persistent_disk_empty(mocker): """Test getting k8s cluster persistent disks with no disks""" def safe_exec_no_disks(cmd): """Mocked safe_exec""" result = {'items': []} return MockedCompletedProcess(json.dumps(result)) mocker.patch('elb.kubernetes.safe_exec', side_effect=safe_exec_no_disks) disks = kubernetes.get_persistent_disks() assert disks is not None assert not disks kubernetes.safe_exec.assert_called()
def test_get_persistent_disks_real(gke_cluster_with_pv): """Test listing GKE persistent disks using real kubectl""" # cluster name name = gke_cluster_with_pv # make sure that cluster exists assert name in gcp.get_gke_clusters() disks = kubernetes.get_persistent_disks() # there must be exactly one persistent disk assert len(disks) == 1 # GCP disk name must be the same as in kubernetes assert len([d for d in gcp.get_disks() if disks[0] in d]) == 1
def gke_cluster_with_pv(): """Fixture function that creates GKE cluster with persistent volume""" # test setup # create cluster name = os.environ['USER'] + '-elastic-blast-test-suite' gcp_zone = 'us-east4-b' cmd = f'gcloud container clusters create {name} --num-nodes 1 --preemptible --machine-type n1-standard-1 --labels project=elastic-blast-test' kubernetes.safe_exec(cmd.split()) try: cmd = [] cmd.append(f'gcloud container clusters get-credentials {name}') cmd.append(f'kubectl apply -f {get_yamldir()}/test-storage-gcp.yaml') cmd.append(f'kubectl apply -f {get_yamldir()}/test-pvc.yaml') cmd.append(f'kubectl apply -f {get_yamldir()}/test-job-init-pv.yaml') for c in cmd: kubernetes.safe_exec(c.split()) except: cluster_cleanup(name) raise # we need to wait for kubenetes to act on the cluster time.sleep(30) disks = kubernetes.get_persistent_disks() yield name # test teardown if not name in gcp.get_gke_clusters(): raise RuntimeError( f'Error cluster {name} should be present after the test') errors = cluster_cleanup(name, disks) message = '\n'.join(errors) # make sure that GCP resources were cleaned up assert name not in gcp.get_gke_clusters(), message gcp_disks = gcp.get_disks() for d in disks: assert d not in gcp_disks, message # and there were no errors assert not errors, message
def test_get_persistent_disk(kubectl_mock): """Test getting k8s cluster persistent disks""" disks = kubernetes.get_persistent_disks() assert sorted(disks) == sorted(GCP_DISKS) kubernetes.safe_exec.assert_called()