class TestJob: """ 测试 Job 相关接口 """ manifest = load_demo_manifest('workloads/simple_job') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/workloads/jobs/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/jobs/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'JobList' def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Job' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestService: """测试 Service 相关接口""" manifest = load_demo_manifest('networks/simple_service') create_url = f'{DAU_PREFIX}/networks/services/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/networks/services/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'ServiceList' def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Service' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0
def test_reschedule(self, api_client): """ 测试重新调度 Pod TODO 可考虑 mock 掉下发集群操作,仅验证接口功能 """ # 创建有父级资源的 Pod,测试重新调度 deploy_manifest = load_demo_manifest('workloads/simple_deployment') deploy_name = deploy_manifest['metadata']['name'] api_client.post(f'{DAU_PREFIX}/workloads/deployments/', data={'manifest': deploy_manifest}) # 等待 Deployment 下属 Pod 创建 time.sleep(3) # 找到 Deployment 下属的 第一个 Pod Name resp = api_client.get( f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/', data={ 'label_selector': 'app=nginx', 'owner_kind': 'Deployment', 'owner_name': deploy_name }, ) pods = getitems(resp.json(), 'data.manifest.items', []) pod_name = getitems(pods[0], 'metadata.name') resp = api_client.put( f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/{pod_name}/reschedule/' ) assert resp.json()['code'] == 0 assert getitems(resp.json(), 'data.metadata.name') == pod_name # 清理测试用的资源 resp = api_client.delete( f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/deployments/{deploy_name}/' ) assert resp.json()['code'] == 0
def manifests(self, request, project_id, cluster_id): """ 指定资源类型的 Demo 配置信息 """ params = self.params_validate(FetchResourceDemoManifestSLZ) config = load_resource_template(params['kind']) config['references'] = load_resource_references(params['kind']) for t in config['items']: t['manifest'] = load_demo_manifest( f"{config['class']}/{t['name']}") return Response(config)
class TestPod: """ 测试 Pod 相关接口 """ manifest = load_demo_manifest('workloads/simple_pod') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/workloads/pods/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PodList' def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Pod' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0 def test_list_pod_pvcs(self, api_client, patch_pod_client): """ 测试获取 Pod 关联 PersistentVolumeClaim """ response = api_client.get(f'{self.detail_url}pvcs/') assert response.json()['code'] == 0 def test_list_pod_configmaps(self, api_client, patch_pod_client): """ 测试获取 Pod 关联 ConfigMap """ response = api_client.get(f'{self.detail_url}configmaps/') assert response.json()['code'] == 0 def test_list_pod_secrets(self, api_client, patch_pod_client): """ 测试获取单个资源接口 """ response = api_client.get(f'{self.detail_url}secrets/') assert response.json()['code'] == 0
class TestServiceAccount: """测试 ServiceAccount 相关接口""" manifest = load_demo_manifest('rbac/simple_service_account') create_url = f'{DAU_PREFIX}/rbac/service_accounts/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/rbac/service_accounts/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'ServiceAccountList' def test_update(self, api_client): """测试更新资源接口""" self.manifest['metadata']['annotations'] = {'t_key': 't_val'} response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'ServiceAccount' assert getitems(response.data, 'manifest.metadata.annotations.t_key') == 't_val' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0 def test_list_shared_cluster_service_account(self, api_client, project_id): """获取共享集群 ServiceAccount,预期是被拦截(PermissionDenied)""" url = f'/api/dashboard/projects/{project_id}/clusters/{TEST_SHARED_CLUSTER_ID}/rbac/service_accounts/' assert api_client.get(url).json()['code'] == 400
class TestStorageClass: """测试 StorageClass 相关接口""" manifest = load_demo_manifest('storages/simple_storage_class') batch_url = f'{DAU_PREFIX}/storages/storage_classes/' detail_url = f"{batch_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'StorageClassList' def test_update(self, api_client): """测试更新资源接口""" self.manifest['metadata']['annotations'] = {'t_key': 't_val'} response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'StorageClass' assert getitems(response.data, 'manifest.metadata.annotations.t_key') == 't_val' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.detail_url) assert response.json()['code'] == 0 def test_list_shared_cluster_sc(self, api_client, project_id): """获取共享集群 SC,预期是被拦截(PermissionDenied)""" url = f'/api/dashboard/projects/{project_id}/clusters/{TEST_SHARED_CLUSTER_ID}/storages/storage_classes/' assert api_client.get(url).json()['code'] == 400
class TestPersistentVolume: """测试 PersistentVolume 相关接口""" manifest = load_demo_manifest('storages/simple_persistent_volume') batch_url = f'{DAU_PREFIX}/storages/persistent_volumes/' detail_url = f"{batch_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolumeList' def test_update(self, api_client): """测试更新资源接口""" self.manifest['spec']['capacity']['storage'] = '2Gi' response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolume' assert getitems(response.data, 'manifest.spec.capacity.storage') == '2Gi' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.detail_url) assert response.json()['code'] == 0 def test_list_shared_cluster_pv(self, api_client, project_id): """获取共享集群 PV,预期是被拦截(PermissionDenied)""" url = f'/api/dashboard/projects/{project_id}/clusters/{TEST_SHARED_CLUSTER_ID}/storages/persistent_volumes/' assert api_client.get(url).json()['code'] == 400
class TestHPA: """测试 HPA 相关接口""" manifest = load_demo_manifest('hpa/simple_hpa') create_url = f'{DAU_PREFIX}/hpa/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/hpa/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'HorizontalPodAutoscalerList' def test_update(self, api_client): """测试更新资源接口""" # 修改 minReplicas 数量 self.manifest['spec']['minReplicas'] = 2 response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'HorizontalPodAutoscaler' assert getitems(response.data, 'manifest.spec.minReplicas') == 2 def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0 def test_list_shared_cluster_hpa(self, api_client, project_id): """获取共享集群 HPA,预期是被拦截(PermissionDenied)""" url = f'/api/dashboard/projects/{project_id}/clusters/{TEST_SHARED_CLUSTER_ID}/namespaces/{TEST_NAMESPACE}/hpa/' # noqa assert api_client.get(url).json()['code'] == 400
class TestDaemonSet: """ 测试 DaemonSet 相关接口 """ manifest = load_demo_manifest('workloads/simple_daemonset') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/workloads/daemonsets/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/daemonsets/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'DaemonSetList' def test_update(self, api_client): """ 测试更新资源接口 """ # 修改 daemonset 配置中的 label self.manifest['spec']['template']['metadata']['labels'][ 'test'] = 'test' response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'DaemonSet' assert getitems( response.data, 'manifest.spec.template.metadata.labels.test') == 'test' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestPersistentVolumeClaim: """ 测试 PersistentVolumeClaim 相关接口 """ manifest = load_demo_manifest('storages/simple_persistent_volume_claim') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/storages/persistent_volume_claims/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/storages/persistent_volume_claims/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolumeClaimList' def test_update(self, api_client): """ 测试更新资源接口 """ self.manifest['metadata']['annotations'] = {'t_key': 't_val'} response = api_client.put(self.detail_url, data={'manifest': self.manifest}) print(response.data, response.json()['message']) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolumeClaim' assert getitems(response.data, 'manifest.metadata.annotations.t_key') == 't_val' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestCronJob: """ 测试 CronJob 相关接口 """ manifest = load_demo_manifest('workloads/simple_cronjob') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/workloads/cronjobs/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/cronjobs/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'CronJobList' def test_update(self, api_client): """ 测试更新资源接口 """ # 调整调度规则 self.manifest['spec']['schedule'] = '*/5 * * * *' response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'CronJob' assert getitems(response.data, 'manifest.spec.schedule') == '*/5 * * * *' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestHPA: """ 测试 HPA 相关接口 """ manifest = load_demo_manifest('hpa/simple_hpa') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/hpa/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/hpa/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest'][ 'kind'] == 'HorizontalPodAutoscalerList' def test_update(self, api_client): """ 测试更新资源接口 """ # 修改 minReplicas 数量 self.manifest['spec']['minReplicas'] = 2 response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'HorizontalPodAutoscaler' assert getitems(response.data, 'manifest.spec.minReplicas') == 2 def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestEndpoints: """测试 Endpoints 相关接口""" manifest = load_demo_manifest('networks/simple_endpoints') create_url = f'{DAU_PREFIX}/networks/endpoints/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/networks/endpoints/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'EndpointsList' def test_update(self, api_client): """测试更新资源接口""" self.manifest['subsets'][0]['addresses'][0]['ip'] = '1.0.0.2' response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Endpoints' assert response.data['manifest']['subsets'][0]['addresses'][0][ 'ip'] == '1.0.0.2' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0
class TestDeployment: """ 测试 Deployment 相关接口 """ manifest = load_demo_manifest('workloads/simple_deployment') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/workloads/deployments/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/deployments/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'DeploymentList' def test_update(self, api_client): """ 测试更新资源接口 """ # 修改 replicas 数量 self.manifest['spec']['replicas'] = 5 response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Deployment' assert getitems(response.data, 'manifest.spec.replicas') == 5 def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestSecret: """测试 Secret 相关接口""" manifest = load_demo_manifest('configs/simple_secret') create_url = f'{DAU_PREFIX}/configs/secrets/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/configs/secrets/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'SecretList' def test_update(self, api_client): """测试更新资源接口""" self.manifest['metadata']['annotations'] = {'t_key': 't_val'} response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Secret' assert getitems(response.data, 'manifest.metadata.annotations.t_key') == 't_val' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0
class TestStatefulSet: """测试 StatefulSet 相关接口""" manifest = load_demo_manifest('workloads/simple_statefulset') create_url = f'{DAU_PREFIX}/workloads/statefulsets/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/statefulsets/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'StatefulSetList' def test_update(self, api_client): """测试更新资源接口""" # 调整调度规则 self.manifest['spec']['replicas'] = 3 response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'StatefulSet' assert getitems(response.data, 'manifest.spec.replicas') == 3 def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0
class TestPersistentVolume: """ 测试 PersistentVolume 相关接口 """ manifest = load_demo_manifest('storages/simple_persistent_volume') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/storages/persistent_volumes/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/storages/persistent_volumes/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolumeList' def test_update(self, api_client): """ 测试更新资源接口 """ self.manifest['spec']['capacity']['storage'] = '2Gi' response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PersistentVolume' assert getitems(response.data, 'manifest.spec.capacity.storage') == '2Gi' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestIngress: """ 测试 Ingress 相关接口 """ manifest = load_demo_manifest('networks/simple_ingress') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/networks/ingresses/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/networks/ingresses/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'IngressList' def test_update(self, api_client): """ 测试更新资源接口 """ self.manifest['metadata']['annotations'] = {'t_key': 't_val'} response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Ingress' assert getitems(response.data, 'manifest.metadata.annotations.t_key') == 't_val' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestConfigMap: """ 测试 ConfigMap 相关接口 """ manifest = load_demo_manifest('configs/simple_configmap') name = getitems(manifest, 'metadata.name') batch_url = f'{DAU_PREFIX}/configs/configmaps/' detail_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/configs/configmaps/{name}/' def test_create(self, api_client): """ 测试创建资源接口 """ response = api_client.post(self.batch_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """ 测试获取资源列表接口 """ response = api_client.get(self.batch_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'ConfigMapList' def test_update(self, api_client): """ 测试更新资源接口 """ self.manifest['data']['t_key'] = 't_val' response = api_client.put(self.detail_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """ 测试获取单个资源接口 """ response = api_client.get(self.detail_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'ConfigMap' assert getitems(response.data, 'manifest.data.t_key') == 't_val' def test_destroy(self, api_client): """ 测试删除单个资源 """ response = api_client.delete(self.detail_url) assert response.json()['code'] == 0
class TestPod: """测试 Pod 相关接口""" manifest = load_demo_manifest('workloads/simple_pod') create_url = f'{DAU_PREFIX}/workloads/pods/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'PodList' def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Pod' def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0 def test_list_pod_pvcs(self, api_client, patch_pod_client): """测试获取 Pod 关联 PersistentVolumeClaim""" response = api_client.get(f'{self.inst_url}pvcs/') assert response.json()['code'] == 0 def test_list_pod_configmaps(self, api_client, patch_pod_client): """测试获取 Pod 关联 ConfigMap""" response = api_client.get(f'{self.inst_url}configmaps/') assert response.json()['code'] == 0 def test_list_pod_secrets(self, api_client, patch_pod_client): """测试获取单个资源接口""" response = api_client.get(f'{self.inst_url}secrets/') assert response.json()['code'] == 0 def test_reschedule(self, api_client): """ 测试重新调度 Pod TODO 可考虑 mock 掉下发集群操作,仅验证接口功能 """ # 创建有父级资源的 Pod,测试重新调度 deploy_manifest = load_demo_manifest('workloads/simple_deployment') deploy_name = deploy_manifest['metadata']['name'] api_client.post(f'{DAU_PREFIX}/workloads/deployments/', data={'manifest': deploy_manifest}) # 等待 Deployment 下属 Pod 创建 time.sleep(3) # 找到 Deployment 下属的 第一个 Pod Name resp = api_client.get( f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/', data={'label_selector': 'app=nginx', 'owner_kind': 'Deployment', 'owner_name': deploy_name}, ) pods = getitems(resp.json(), 'data.manifest.items', []) pod_name = getitems(pods[0], 'metadata.name') resp = api_client.put(f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/pods/{pod_name}/reschedule/') assert resp.json()['code'] == 0 assert getitems(resp.json(), 'data.metadata.name') == pod_name # 清理测试用的资源 resp = api_client.delete(f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/deployments/{deploy_name}/') assert resp.json()['code'] == 0
class TestDeployment: """测试 Deployment 相关接口""" manifest = load_demo_manifest('workloads/simple_deployment') create_url = f'{DAU_PREFIX}/workloads/deployments/' list_url = f'{DAU_PREFIX}/namespaces/{TEST_NAMESPACE}/workloads/deployments/' inst_url = f"{list_url}{getitems(manifest, 'metadata.name')}/" shared_cluster_url_prefix = f'/api/dashboard/projects/{TEST_PROJECT_ID}/clusters/{TEST_SHARED_CLUSTER_ID}' def test_create(self, api_client): """测试创建资源接口""" response = api_client.post(self.create_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_list(self, api_client): """测试获取资源列表接口""" response = api_client.get(self.list_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'DeploymentList' def test_update(self, api_client): """测试更新资源接口""" # 修改 replicas 数量 self.manifest['spec']['replicas'] = 5 response = api_client.put(self.inst_url, data={'manifest': self.manifest}) assert response.json()['code'] == 0 def test_retrieve(self, api_client): """测试获取单个资源接口""" response = api_client.get(self.inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Deployment' assert getitems(response.data, 'manifest.spec.replicas') == 5 def test_destroy(self, api_client): """测试删除单个资源""" response = api_client.delete(self.inst_url) assert response.json()['code'] == 0 def test_list_shared_cluster_deploys(self, api_client, shared_cluster_ns_mgr): """测试获取共享集群 Deploy""" shared_cluster_ns = shared_cluster_ns_mgr response = api_client.get( f'{self.shared_cluster_url_prefix}/namespaces/{shared_cluster_ns}/workloads/deployments/' ) assert response.json()['code'] == 0 # 获取不是项目拥有的共享集群命名空间,导致 PermissionDenied response = api_client.get( f'{self.shared_cluster_url_prefix}/namespaces/default/workloads/deployments/' ) assert response.json()['code'] == 400 def test_operate_shared_cluster_deploys(self, api_client, shared_cluster_ns_mgr): """测试 创建 / 获取 / 删除 共享集群 Pod""" shared_cluster_ns = shared_cluster_ns_mgr pc_deploy_manifest = deepcopy(self.manifest) pc_deploy_manifest['metadata']['namespace'] = shared_cluster_ns pc_create_url = f'{self.shared_cluster_url_prefix}/workloads/deployments/' response = api_client.post(pc_create_url, data={'manifest': pc_deploy_manifest}) assert response.json()['code'] == 0 pc_inst_url = ( f"{self.shared_cluster_url_prefix}/namespaces/{shared_cluster_ns}/" + f"workloads/deployments/{getitems(pc_deploy_manifest, 'metadata.name')}/" ) # 修改 replicas 数量,测试 Update pc_deploy_manifest['spec']['replicas'] = 3 response = api_client.put(pc_inst_url, data={'manifest': pc_deploy_manifest}) assert response.json()['code'] == 0 response = api_client.get(pc_inst_url) assert response.json()['code'] == 0 assert response.data['manifest']['kind'] == 'Deployment' # Retrieve 验证 Update 结果 assert getitems(response.data, 'manifest.spec.replicas') == 3 # 回收 Deploy 资源 response = api_client.delete(pc_inst_url) assert response.json()['code'] == 0 def test_operate_shared_cluster_no_perm_ns_deploys(self, api_client): """测试越权操作共享集群不属于项目的命名空间""" deploy_manifest = deepcopy(self.manifest) deploy_manifest['metadata']['namespace'] = 'default' pc_create_url = f'{self.shared_cluster_url_prefix}/workloads/deployments/' response = api_client.post(pc_create_url, data={'manifest': deploy_manifest}) # PermissionDenied assert response.json()['code'] == 400 pc_inst_url = ( f"{self.shared_cluster_url_prefix}/namespaces/default/" + f"workloads/deployments/{getitems(deploy_manifest, 'metadata.name')}/" ) assert api_client.get(pc_inst_url).json()['code'] == 400 assert api_client.delete(pc_inst_url).json()['code'] == 400