def setup_class(self): self.newapp = Newapplication() self.middleware = Middleware() self.scs = Scs() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.zookeeper_name = "e2e-zookeeper-pub" self.kafka_name = "e2e-kafka-pub" scs_list_result = self.scs.list_scs() assert len(scs_list_result.json()) > 0, "没有存储类,不能创建zookeeper,创建zookeeper用例失败" self.scs_name = scs_list_result.json()[0]["kubernetes"]["metadata"]["name"] logger.info("查出来的第一个scs {}".format(self.scs_name)) # 创建zookeeper zookeeper_template_id = self.middleware.get_template_id("zookeeper") version_id = self.middleware.get_version_id(zookeeper_template_id) create_result = self.middleware.create_application('./test_data/middleware/zookeeper.json', {"$name": self.zookeeper_name, "$template_id": zookeeper_template_id, "$version_id": version_id, "$scs_name": self.scs_name}) assert create_result.status_code == 201, "测试kafka前提条件创建zookeeper应用失败 {}".format(create_result.text) logger.info("测试kafka前提条件创建zookeeper应用成功") app_status = self.middleware.get_application_status(self.namespace, self.zookeeper_name, "Running") assert app_status, "测试kafka前提条件创建zookeeper应用后,验证应用状态出错,app:{} is not Running".format(self.zookeeper_name) logger.info("测试kafka前提条件zookeeper应用为运行中")
def __init__(self): super(TearDown, self).__init__() self.global_info = FileUtils.load_file(self.global_info_path) self.namespace_client = Namespace() self.project_client = Project() self.newapp = Newapplication() self.cluster = Cluster() self.image_client = Image() self.delete()
class TestMysqlClusterSuite(object): def setup_class(self): self.middleware = Middleware() self.newapp = Newapplication() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.mysql_cluster_name = "e2e-mysql-clu" def teardown_class(self): self.newapp.delete_newapp(self.namespace, self.mysql_cluster_name) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.mysql_cluster_name), 404) @pytest.mark.middleware_mysql_cluster def test_mysql_cluster(self): result = {"flag": True} template_id = self.middleware.get_template_id("mysql-cluster") version_id = self.middleware.get_version_id(template_id) create_result = self.middleware.create_application('./test_data/middleware/mysql-cluster.json', {"$name": self.mysql_cluster_name, "$template_id": template_id, "$version_id": version_id}) assert create_result.status_code == 201, "中间件创建应用mysql-cluster失败 {}".format(create_result.text) logger.info("中间件创建mysql-cluster应用成功") app_status = self.middleware.get_application_status(self.namespace, self.mysql_cluster_name, "Running") assert app_status, "mysql-cluster应用的状态不在运行中" logger.info("mysql-cluster应用在运行中") delete_result = self.newapp.delete_newapp(self.namespace, self.mysql_cluster_name) assert delete_result.status_code == 204, "删除mysql-cluster应用失败 {}".format(delete_result.text) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.mysql_cluster_name), 404) logger.info("删除mysql-cluster应用成功") assert result['flag'], True
class TestRedisSuite(object): def setup_class(self): self.newapp = Newapplication() self.middleware = Middleware() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.redis_name = "e2e-redis-pub" def teardown_class(self): logger.info("清除redis应用资源") self.newapp.delete_newapp(self.namespace, self.redis_name) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.redis_name), 404) logger.info("清除完毕") @pytest.mark.middleware_redis def test_redis(self): result = {"flag": True} redis_template_id = self.middleware.get_template_id("redis") version_id = self.middleware.get_version_id(redis_template_id) create_result = self.middleware.create_application('./test_data/middleware/redis.json', {"$name": self.redis_name, "$template_id": redis_template_id, "$version_id": version_id}) assert create_result.status_code == 201, "中间件创建redis应用失败 {}".format(create_result.text) logger.info("创建redis应用成功") app_status = self.middleware.get_application_status(self.namespace, self.redis_name, "Running") assert app_status, "创建应用后,验证应用状态出错,app:{} is not Running".format(self.redis_name) logger.info("redis应用为运行中") delete_result = self.newapp.delete_newapp(self.namespace, self.redis_name) assert delete_result.status_code == 204, "删除redis应用失败 {}".format(delete_result) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.redis_name), 404) logger.info("删除redis应用成功") assert result["flag"], True
def setup_class(self): self.catalog = Catalog() self.middleware = Middleware() self.newapp = Newapplication() self.app_name = "e2e-mongodb-pri" self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.git_name = "alauda-git-repository" self.update_git_displayname = "update-git-repository" self.svn_name = "alauda-svn-repository" self.upadte_svn_displayname = "update-svn-repository"
def setup_class(self): self.newapp = Newapplication() self.middleware = Middleware() self.scs = Scs() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.zookeeper_name = "e2e-zookeeper-pub" self.kafka_name = "e2e-kafka-pub" scs_list_result = self.scs.list_scs() assert len( scs_list_result.json()) > 0, "没有存储类,不能创建zookeeper,创建zookeeper用例失败" self.scs_name = scs_list_result.json( )[0]["kubernetes"]["metadata"]["name"] logger.info("查出来的第一个scs {}".format(self.scs_name))
class TestMongodbSuite(object): def setup_class(self): self.middleware = Middleware() self.catalog = Catalog() self.newapp = Newapplication() self.name = "e2e-mongodb-pub" self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] def teardown_class(self): self.newapp.delete_newapp(self.namespace, self.name) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.name), 404) @pytest.mark.middleware_mongodb def test_mongodb(self): # 中间件获取mongodb的模板id-获取mongodb可用的version id-创建mongodb应用-获取应用状态-删除应用 result = {'flag': True} mongodb_template_id = self.middleware.get_template_id("mongodb") logger.info("中间件mongodb模板id:{}".format(mongodb_template_id)) version_id = self.middleware.get_version_id(mongodb_template_id) logger.info("中间件mongodb的可使用的version id:{}".format(version_id)) create_result = self.middleware.create_application( './test_data/middleware/mongodb.json', { "$name": self.name, "$template_id": mongodb_template_id, "$version_id": version_id }) assert create_result.status_code == 201, "创建mongodb失败 {}".format( create_result.text) logger.info("创建mongodb成功,name是:{}".format(self.name)) app_status = self.middleware.get_application_status( self.namespace, self.name, "Running") assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format( self.name) logger.info("mongodb的应用状态为:Running") # pods_result = self.middleware.get_pods(self.namespace, self.name) # logger.info("拿到了pod请求的返回值") # hostIP = pods_result.json()[0]["kubernetes"]["status"]["hostIP"] # logger.info("获取mongodb容器Ip为:{}".format(hostIP)) # podIP = pods_result.json()[0]["kubernetes"]["status"]["podIP"] # logger.info("获取mongodb所在podIp为:{}".format(podIP)) delete_result = self.newapp.delete_newapp(self.namespace, self.name) assert delete_result.status_code == 204, "删除中间件创建的mongodb应用失败 {}".format( delete_result.text) logger.info("删除中间件创建的mongodb应用成功") self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.name), 404) assert result["flag"], True
def setup_class(self): self.newapp = Newapplication() self.k8s_namespace = self.newapp.global_info["$K8S_NAMESPACE"] self.newapp_name = 'alauda-newapp' self.configmap = Configmap() self.configmap_name = 'alauda-cmforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.appwithcm_name = 'alauda-appwithcm-{}'.format(self.newapp.region_name).replace('_', '-') self.scs = Scs() self.scs_name = 'alauda-scsforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.pvc = Pvc() self.pvc_name = 'alauda-pvcforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.appwithpvc_name = 'alauda-appwithpvc-{}'.format(self.newapp.region_name).replace('_', '-') self.teardown_class(self)
def setup_class(self): self.networkpolicy = Networkpolicy() self.networkpolicy_name = 'networkpolicy-{}'.format(self.networkpolicy.region_name).replace('_', '-') self.k8s_namespace = self.networkpolicy.global_info["$K8S_NAMESPACE"] self.newapp = Newapplication() self.newapp_name = self.networkpolicy.global_info["$GLOBAL_APP_NAME"] self.teardown_class(self)
def setup_class(self): self.alb = Alb() self.alb2_name = 'nginx-{}'.format(self.alb.region_name).replace( '_', '-') self.k8s_namespace = self.alb.global_info["$K8S_NAMESPACE"] list_result = self.alb.list_alb() assert list_result.status_code == 200, "获取负载均衡列表失败 {}".format( list_result.text) if len(list_result.json()) == 0: return False, "集群负载均衡列表为空,请先部署alb2" self.alb_name = self.alb.get_value(list_result.json(), "-1.kubernetes.metadata.name") self.alb_namespace = self.alb.get_value( list_result.json(), "-1.kubernetes.metadata.namespace") self.tcpportname = "{}-999".format(self.alb_name) self.httpportname = "{}-777-http".format(self.alb_name) self.rulename = "rule-{}".format(self.httpportname) self.newapp = Newapplication() self.teardown_class(self)
class TearDown(AlaudaRequest): """ 测试结束后删除setup创建的资源 """ def __init__(self): super(TearDown, self).__init__() self.global_info = FileUtils.load_file(self.global_info_path) self.namespace_client = Namespace() self.project_client = Project() self.newapp = Newapplication() self.cluster = Cluster() self.image_client = Image() self.delete() def delete(self): self.newapp.delete_newapp(settings.K8S_NAMESPACE, global_app_name) self.newapp.check_exists( self.newapp.get_newapp_common_url(settings.K8S_NAMESPACE, global_app_name), 404) # delete repo self.image_client.delete_repo(settings.REPO_NAME) self.image_client.delete_repo(settings.REPO_NAME, reg_project_name=settings.REPO_PROJECT) self.image_client.delete_reg_project(settings.REPO_PROJECT) if "CREATE_NAMESPACE" in self.global_info: self.namespace_client.delete_general_namespaces( settings.K8S_NAMESPACE) if "CREATE_PROJECT" in self.global_info: self.project_client.delete_project_role(settings.PROJECT_NAME) self.project_client.delete_project(settings.PROJECT_NAME) if "CREATE_REGION" in self.global_info: self.cluster.uninstall_nevermore(settings.REGION_NAME) self.cluster.uninstall_registry(settings.REGION_NAME) self.namespace_client.delete_general_namespaces("default") self.namespace_client.delete_general_namespaces("kube-system") self.cluster.delete_cluster(settings.REGION_NAME) if instances_id: destroy_instance(instances_id)
class TestRabbitmq(object): def setup_class(self): self.middleware = Middleware() self.newapp = Newapplication() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.name = "e2e-rabbitmq-ha" def teardown_class(self): self.newapp.delete_newapp(self.namespace, self.name) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.name), 404) @pytest.mark.middleware_rabbitmq_ha def test_rabbitmq_ha(self): result = {"flag": True} template_id = self.middleware.get_template_id("rabbitmq-ha") version_id = self.middleware.get_version_id(template_id) create_result = self.middleware.create_application( './test_data/middleware/rabbitmq-ha.json', { "$name": self.name, "$template_id": template_id, "$version_id": version_id }) assert create_result.status_code == 201, "中间件创建rabbitmq-ha应用失败 {}".format( create_result.text) logger.info("中间件创建rabbitmq-ha应用成功") app_status = self.middleware.get_application_status( self.namespace, self.name, "Running") assert app_status, "rabbitmq-ha应用状态不是运行中" logger.info("rabbitmq-ha应用状态是运行中") delete_result = self.newapp.delete_newapp(self.namespace, self.name) assert delete_result.status_code == 204, "删除rabbitnq-ha应用失败 {}".format( delete_result.text) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.name), 404) logger.info("删除rabbitmq-ha应用成功") assert result["flag"], True
def setup_class(self): self.subnet = Subnet() self.subnet_name = 'alauda-subnet-{}'.format(self.subnet.region_name).replace('_', '-') # macvlan cidr需要和主机同网段 self.masterip = self.subnet.global_info['$MASTERIPS'].split(',')[0] gateway = self.masterip.split('.') gateway.pop() self.macvlan_gateway = '.'.join(gateway) self.macvlan_cidr = "{}.0/24".format(self.macvlan_gateway) self.cidr_update = "{}.0/20".format(self.macvlan_gateway) self.ip1 = "{}.110".format(self.macvlan_gateway) self.ip2 = "{}.111".format(self.macvlan_gateway) self.ip3 = "{}.112".format(self.macvlan_gateway) # calico cidr不能和主机重合 self.calico_gateway = '19.19.0' self.calico_cidr = "{}.0/24".format(self.calico_gateway) self.calico_ip1 = "{}.110".format(self.calico_gateway) self.newapp = Newapplication() self.k8s_namespace = self.newapp.global_info["$K8S_NAMESPACE"] self.newapp_name = 'subnet-statefulset' self.newapp_name_ip = 'ip-statefulset' self.teardown_class(self)
class TestMysqlSuite(object): def setup_class(self): self.middleware = Middleware() self.newapp = Newapplication() self.mysql_name = "e2e-mysql-pub" self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] def teardown_class(self): logger.info("清除mysql应用资源") self.newapp.delete_newapp(self.namespace, self.mysql_name) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.mysql_name), 404) logger.info("清除完毕") @pytest.mark.middleware_mysql def test_mysql(self): result = {"flag": True} mysql_template_id = self.middleware.get_template_id("mysql") version_id = self.middleware.get_version_id(mysql_template_id) create_result = self.middleware.create_application('./test_data/middleware/mysql.json', {"$name": self.mysql_name, "$template_id": mysql_template_id, "$version_id": version_id}) assert create_result.status_code == 201, "中间件创建mysql应用失败 {}".format(create_result.text) logger.info("中间间创建mysql应用成功,name:{}".format(self.mysql_name)) app_status = self.middleware.get_application_status(self.namespace, self.mysql_name, "Running") assert app_status, "创建应用后,验证应用状态出错,app:{} is not Running".format(self.mysql_name) logger.info("mysql应用状态在运行中") delete_result = self.newapp.delete_newapp(self.namespace, self.mysql_name) assert delete_result.status_code == 204, "删除中间件创建出的mysql应用失败{}".format(delete_result) logger.info("删除mysql应用成功") self.newapp.check_exists(self.newapp.get_newapp_common_url(self.namespace, self.mysql_name), 404) assert result["flag"], True
def setup_class(self): self.newapp = Newapplication() self.middleware = Middleware() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.redis_name = "e2e-redis-pub"
class TestSubnetSuite(object): def setup_class(self): self.subnet = Subnet() self.subnet_name = 'alauda-subnet-{}'.format(self.subnet.region_name).replace('_', '-') # macvlan cidr需要和主机同网段 self.masterip = self.subnet.global_info['$MASTERIPS'].split(',')[0] gateway = self.masterip.split('.') gateway.pop() self.macvlan_gateway = '.'.join(gateway) self.macvlan_cidr = "{}.0/24".format(self.macvlan_gateway) self.cidr_update = "{}.0/20".format(self.macvlan_gateway) self.ip1 = "{}.110".format(self.macvlan_gateway) self.ip2 = "{}.111".format(self.macvlan_gateway) self.ip3 = "{}.112".format(self.macvlan_gateway) # calico cidr不能和主机重合 self.calico_gateway = '19.19.0' self.calico_cidr = "{}.0/24".format(self.calico_gateway) self.calico_ip1 = "{}.110".format(self.calico_gateway) self.newapp = Newapplication() self.k8s_namespace = self.newapp.global_info["$K8S_NAMESPACE"] self.newapp_name = 'subnet-statefulset' self.newapp_name_ip = 'ip-statefulset' self.teardown_class(self) def teardown_class(self): self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name) self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name_ip) if self.subnet.global_info['$NETWORK_TYPE'] == 'macvlan': self.subnet.delete_ips(self.subnet_name, self.ip1) self.subnet.delete_ips(self.subnet_name, self.ip2) self.subnet.delete_ips(self.subnet_name, self.ip3) self.subnet.delete_subnet(self.subnet_name) @pytest.mark.BAT def test_macvlan_subnet(self): ''' macvlan子网测试:创建子网-更新子网-子网列表-子网详情-导入IP list-删除IP-导入IP range-IP列表-创建应用指定子网/IP-删除IP-删除子网 :return: ''' if self.subnet.global_info['$NETWORK_TYPE'] != 'macvlan': return True, "集群网络模式不是macvlan,不需要测试" crd_result = self.subnet.get_subnet_crd() assert crd_result.status_code == 200, "获取subnet的crd 失败:{}".format(crd_result.text) if len(crd_result.json()) == 0: assert False, "集群不支持subnet,请先部署subnet crd" result = {"flag": True} # create subnet createsubnet_result = self.subnet.create_subnet("./test_data/subnet/macvlan-subnet.json", {"$subnet_name": self.subnet_name, "$cidr": self.macvlan_cidr, '$gateway': self.macvlan_gateway}) assert createsubnet_result.status_code == 201, "创建子网失败:{}".format(createsubnet_result.text) # update subnet update_result = self.subnet.update_subnet(self.subnet_name, "./test_data/subnet/macvlan-subnet.json", {"$subnet_name": self.subnet_name, "$cidr": self.cidr_update}) assert update_result.status_code == 204, "更新子网出错:{}".format(update_result.text) # list subnet list_result = self.subnet.list_subnet() result = self.subnet.update_result(result, list_result.status_code == 200, list_result.text) result = self.subnet.update_result(result, self.subnet_name in list_result.text, "子网列表:新建子网不在列表中") # detail subnet detail_result = self.subnet.get_subnet_detail(self.subnet_name) result = self.subnet.update_result(result, detail_result.status_code == 200, detail_result.text) result = self.subnet.update_result(result, self.subnet.get_value(detail_result.json(), 'kubernetes.spec.cidr_block') == self.cidr_update, detail_result.text) # import list ip import_ip_list_result = self.subnet.create_ips(self.subnet_name, "./test_data/subnet/ip-list.json", {'$ip3': self.ip3}) assert import_ip_list_result.status_code == 201, "导入IP list失败:{}".format(import_ip_list_result.text) delete_result = self.subnet.delete_subnet(self.subnet_name) assert delete_result.status_code == 400, "子网非空 不能删除:{}".format(delete_result.text) # delete ip delete_ip_result = self.subnet.delete_ips(self.subnet_name, self.ip3) assert delete_ip_result.status_code == 204, "删除IP失败:{}".format(delete_ip_result.text) # wait for subnet empty self.subnet.check_value_in_response(self.subnet.get_private_ip_url(self.subnet_name), '[]') list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, self.ip3 not in list_ip_result.text, "删除IP后:IP还在列表中") # import range ip range_ip_result = self.subnet.create_ips(self.subnet_name, "./test_data/subnet/ip-range.json", {'$ip1': self.ip1, '$ip2': self.ip2}) assert range_ip_result.status_code == 201, "导入IP range失败:{}".format(range_ip_result.text) # list ip list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, list_ip_result.status_code == 200, list_ip_result.text) result = self.subnet.update_result(result, len(list_ip_result.json()) == 2, "IP列表:新建IP不在列表中") # create app with ip create_result = self.newapp.create_newapp('./test_data/newapp/macvlan-ip-statefulset.json', {'$newapp_name': self.newapp_name_ip, '$subnet_ip': self.ip2}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name_ip, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) # create app with subnet create_result = self.newapp.create_newapp('./test_data/newapp/macvlan-subnet-statefulset.json', {'$newapp_name': self.newapp_name, '$subnet_name': self.subnet_name}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, list_ip_result.status_code == 200, list_ip_result.text) result = self.subnet.update_result(result, self.subnet.get_value(list_ip_result.json(), '0.kubernetes.spec.used') is True, "ip状态不是使用中") result = self.subnet.update_result(result, self.subnet.get_value(list_ip_result.json(), '1.kubernetes.spec.used') is True, "ip状态不是使用中") delete_result = self.subnet.delete_ips(self.subnet_name, self.ip1) assert delete_result.status_code == 400, "IP使用中 不能删除:{}".format(delete_result.text) # delete app delete_result = self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name_ip) assert delete_result.status_code == 204, "删除应用失败 {}".format(delete_result.text) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.newapp_name_ip), 404) # delete app delete_result = self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name) assert delete_result.status_code == 204, "删除应用失败 {}".format(delete_result.text) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.newapp_name), 404) # wait for ip not used self.subnet.get_status(self.subnet.get_private_ip_url(self.subnet_name), '0.kubernetes.spec.used', False) self.subnet.get_status(self.subnet.get_private_ip_url(self.subnet_name), '1.kubernetes.spec.used', False) self.subnet.delete_ips(self.subnet_name, self.ip1) self.subnet.delete_ips(self.subnet_name, self.ip2) # wait for subnet empty self.subnet.check_value_in_response(self.subnet.get_private_ip_url(self.subnet_name), '[]') # delete subnet delete_result = self.subnet.delete_subnet(self.subnet_name) assert delete_result.status_code == 204, "删除子网失败:{}".format(delete_result.text) assert self.subnet.check_exists(self.subnet.get_common_subnet_url(self.subnet_name), 404) assert result['flag'], result @pytest.mark.BAT def test_calico_subnet(self): ''' calico子网测试:创建子网-子网列表-更新子网-子网详情-IP列表为空-创建应用指定子网-验证IP状态-创建应用指定IP-验证应用容器IP-IP列表-删除非空子网-更新非空子网-删除子网 :return: ''' if self.subnet.global_info['$NETWORK_TYPE'] != 'calico': return True, "集群网络模式不是calico,不需要测试" crd_result = self.subnet.get_subnet_crd() assert crd_result.status_code == 200, "获取subnet的crd 失败:{}".format(crd_result.text) if len(crd_result.json()) == 0: assert False, "集群不支持subnet,请先部署subnet crd" result = {"flag": True} # create subnet createsubnet_result = self.subnet.create_subnet_calico("./test_data/subnet/calico-subnet.json", {"$subnet_name": self.subnet_name, "$cidr": self.calico_cidr}) assert createsubnet_result.status_code == 201, "创建子网失败:{}".format(createsubnet_result.text) # list subnet list_result = self.subnet.list_subnet() result = self.subnet.update_result(result, list_result.status_code == 200, list_result.text) result = self.subnet.update_result(result, self.subnet_name in list_result.text, "子网列表:新建子网不在列表中") # update subnet update_result = self.subnet.update_subnet(self.subnet_name, './test_data/subnet/update-calico-subnet.json', {'$mode': 'Always', '"$nat"': 'false'}) assert update_result.status_code == 204, "更新子网出错:{}".format(update_result.text) # detail subnet detail_result = self.subnet.get_subnet_detail(self.subnet_name) result = self.subnet.update_result(result, detail_result.status_code == 200, detail_result.text) result = self.subnet.update_result(result, self.subnet.get_status(self.subnet.get_common_subnet_url(self.subnet_name), 'kubernetes.spec.ipip_mode', 'Always'), '更新子网后ipip_mode不是Always') # list IP list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, len(list_ip_result.json()) == 0, "IP列表不为空") # create app with subnet create_result = self.newapp.create_newapp_by_yaml(self.k8s_namespace, self.newapp_name, './test_data/newapp/calico-subnet-statefulset.yml', {'$newapp_name': self.newapp_name, '$subnet_cidr': self.calico_cidr}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, list_ip_result.status_code == 200, list_ip_result.text) result = self.subnet.update_result(result, len(list_ip_result.json()) == 1, "IP列表:不是1个") result = self.subnet.update_result(result, self.subnet.get_value(list_ip_result.json(), '0.kubernetes.spec.used') is True, "ip状态不是使用中") # delete app delete_result = self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name) assert delete_result.status_code == 204, "删除应用失败 {}".format(delete_result.text) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.newapp_name), 404) # create app with IP create_result = self.newapp.create_newapp_by_yaml(self.k8s_namespace, self.newapp_name_ip, './test_data/newapp/calico-ip-statefulset.yml', {'$newapp_name': self.newapp_name_ip, '$subnet_ip': self.calico_ip1}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name_ip, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name_ip) podip = self.newapp.get_status(self.newapp.get_newapp_pod_url(self.k8s_namespace, self.newapp_name_ip), '0.kubernetes.status.podIP', self.calico_ip1) assert podip, "应用运行后,容器IP不是{}".format(self.calico_ip1) # list ip list_ip_result = self.subnet.list_ips(self.subnet_name) result = self.subnet.update_result(result, list_ip_result.status_code == 200, list_ip_result.text) result = self.subnet.update_result(result, self.calico_ip1 in list_ip_result.text, "{}不在列表中".format(self.calico_ip1)) delete_result = self.subnet.delete_subnet(self.subnet_name) assert delete_result.status_code == 400, "子网非空 不能删除:{}".format(delete_result.text) # update subnet not empty update_result = self.subnet.update_subnet(self.subnet_name, './test_data/subnet/update-calico-subnet.json', {'$mode': 'Never', '"$nat"': 'true'}) assert update_result.status_code == 204, "子网非空时,更新子网出错:{}".format(update_result.text) result = self.subnet.update_result(result, self.subnet.get_status(self.subnet.get_common_subnet_url(self.subnet_name), 'kubernetes.spec.ipip_mode', 'Never'), '子网非空时,更新子网后ipip_mode不是never') result = self.subnet.update_result(result, self.subnet.get_status(self.subnet.get_common_subnet_url(self.subnet_name), 'kubernetes.spec.nat_outgoing', True), '子网非空时,更新子网后nat_outgoing不是True') # delete app delete_result = self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name_ip) assert delete_result.status_code == 204, "删除应用失败 {}".format(delete_result.text) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.newapp_name_ip), 404) # wait for subnet empty self.subnet.check_value_in_response(self.subnet.get_private_ip_url(self.subnet_name), '[]') # delete subnet delete_result = self.subnet.delete_subnet(self.subnet_name) assert delete_result.status_code == 204, "删除子网失败:{}".format(delete_result.text) assert self.subnet.check_exists(self.subnet.get_common_subnet_url(self.subnet_name), 404) assert result['flag'], result
def setup_class(self): self.middleware = Middleware() self.newapp = Newapplication() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.mysql_cluster_name = "e2e-mysql-clu"
class TestJenkinsBuildImageUpdateService(object): def setup_class(self): self.app_tool = Newapplication() self.jenkins_tool = Jenkins() self.integration_tool = Integrations() self.image_tool = Image() self.jenkins_auth = \ (self.jenkins_tool.global_info.get("$JENKINS_USER"), self.jenkins_tool.global_info.get("$JENKINS_TOKEN")) self.jenkins_url = self.jenkins_tool.global_info.get( "$JENKINS_ENDPOINT") self.pipeline_name = "alauda-jenkins-pipeline" self.git_buidl_pipeline = "alauda-jenkins-pipeline-git-build" self.svn_build_pipeline_no_sonar = "alauda-jenkins-pipeline-svn-build-no-sonar" self.svn_build_pipeline_with_sonar = "alauda-jenkins-pipeline-svn-build-with-sonar" self.update_service_pipeline = "alauda-jenkins-update-service-pipeline" self.sync_registry_pipeline = "alauda-jenkins-sync-registry-pipeline" self.code_credential_name = self.app_tool.global_info.get( '$SVN_CREDENTIAL') self.registry_credential_name = self.app_tool.global_info.get( '$REG_CREDENTIAL') self.git_code_credential_name = self.app_tool.global_info.get( '$GIT_CREDENTIAL') # self.get_publick_registry = self.app_tool.global_info.get('PUBLIC_REGISTRY') self.image = self.app_tool.global_info.get('$IMAGE') self.registry_name = self.app_tool.global_info.get("$REGISTRY") self.integration_name = "alauda-integration-instance-name-svn" self.sonar_integration_name = "alauda-sonar-integration-instance-name" self.namespace = self.app_tool.global_info["$K8S_NAMESPACE"] self.app_name = self.app_tool.global_info.get("$GLOBAL_APP_NAME") # self.app_id = self.app_tool.global_info.get("$GLOBAL_APP_ID") self.repo_tag = "alauda-e2e" self.repo_additional_tag = "alauda-e2e-additional" self.sync_repo_tag = "alauda-e2e-sync" self.branch = "master" self.template_name = "newalaudaBuildImageAndDeployService" self.build_template_name = "alaudaBuildImage" self.syncimage_template_name = "alaudaSyncImage" self.updateservice_template_name = "newalaudaDeployService" self.time_out = '300' self.repo = self.app_tool.global_info.get("$REPO_NAME") self.target_repo = self.app_tool.global_info.get("$TARGET_REPO_NAME") self.pipeline_description = "alauda jenkins pipeline" self.qualitygates_name = "SonarQube way" self.language_name = 'Java' self.teardown_class(self) # create jenkins integration instance self.create_integration = self.integration_tool.create_integration( './test_data/integration/ci_cd/create_integration.yaml', {"$INTEGRATION_NAME": self.integration_name}) # create sonar integration instance self.create_sonar_integration = self.integration_tool.create_integration( './test_data/integration/sonar/create_sonar_integration.yaml', {"$integration_name": self.sonar_integration_name}) def teardown_class(self): pipeline_id = self.jenkins_tool.get_pipeline_id(self.pipeline_name) git_build_pipeline_id = self.jenkins_tool.get_pipeline_id( self.git_buidl_pipeline) svn_build_pipeline_no_sonar_id = self.jenkins_tool.get_pipeline_id( self.svn_build_pipeline_no_sonar) svn_build_pipeline_with_sonar_id = self.jenkins_tool.get_pipeline_id( self.svn_build_pipeline_with_sonar) update_service_pipeline_id = self.jenkins_tool.get_pipeline_id( self.update_service_pipeline) sync_registry_pipeline_id = self.jenkins_tool.get_pipeline_id( self.sync_registry_pipeline) self.jenkins_tool.delete_pipeline(pipeline_id) self.jenkins_tool.delete_pipeline(git_build_pipeline_id) self.jenkins_tool.delete_pipeline(svn_build_pipeline_no_sonar_id) self.jenkins_tool.delete_pipeline(svn_build_pipeline_with_sonar_id) self.jenkins_tool.delete_pipeline(update_service_pipeline_id) self.jenkins_tool.delete_pipeline(sync_registry_pipeline_id) integration_id = self.integration_tool.get_integration_id( self.integration_name) sonar_integration_id = self.integration_tool.get_integration_id( self.sonar_integration_name) self.integration_tool.delete_integration(integration_id) self.integration_tool.delete_integration(sonar_integration_id) self.image_tool.delete_repo_tag(self.repo, self.repo_tag) self.image_tool.delete_repo_tag(self.repo, self.repo_additional_tag) self.image_tool.delete_repo_tag(self.repo, self.sync_repo_tag) def test_jenkins_buildimage_updateservice(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建集成中心实例失败" integration_id = self.create_integration.json()['id'] # get template id template_id = self.jenkins_tool.get_sys_template_id( self.template_name, 'uuid') assert template_id, "获取模板失败" # create code credential self.jenkins_tool.create_credential( './test_data/jenkins/create_svn_code_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.code_credential_name) assert ret, "创建svn代码库凭证失败或获取凭证失败" # create registry credential self.jenkins_tool.create_credential( './test_data/jenkins/create_registry_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.registry_credential_name) assert ret, "创建镜像仓库凭证失败" # get image info registry_endpoint = self.app_tool.get_uuid_accord_name( self.app_tool.global_info.get("PRIVATE_REGISTRY"), {"name": self.app_tool.global_info.get("$REGISTRY")}, "endpoint") # create jenkins pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_buildimage_updateservice_pipeline_svn.yaml', { "$pipeline_name": self.pipeline_name, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$REG_URL": registry_endpoint, "$imageTag": self.repo_tag, "$imageExtraTag": self.repo_additional_tag, "$service_name": self.app_name, "$time_out": self.time_out }) assert ret.status_code == 201, "创建Jenkins流水线项目失败" pipeline_id = ret.json()['uuid'] # get jenkins pipeline detail ret = self.jenkins_tool.get_pipeline_detail(pipeline_id) assert ret.status_code == 200, "获取流水线详情失败" contents = ret.json() script = re.search( r'("script":) "(.*)"(, "namespace": "[a-zA-Z0-9_-]*?", "created_by")', ret.text).group(2) logger.info("script: {}".format(script)) pipeline_name = contents['name'] # update jenkins pipeline ret = self.jenkins_tool.update_pipeline( pipeline_id, './test_data/jenkins/update_buildimage_updateservice_pipeline_svn.yaml', { "$pipeline_name": pipeline_name, "$display_name": self.pipeline_name, "$jenkins_integration_id": integration_id, "$pipeline_description": self.pipeline_description, "$pipeline_script": script, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$REG_URL": registry_endpoint, "$imageTag": self.repo_tag, "$imageExtraTag": self.repo_additional_tag, "$service_name": self.app_name, "$time_out": self.time_out }) assert ret.status_code == 204, "更新流水线操作失败" # get jenkins pipeline detail ret = self.jenkins_tool.get_pipeline_detail(pipeline_id) assert ret.status_code == 200, "获取流水线详情失败" assert ret.json( )['description'] == self.pipeline_description, "更新流水线失败" # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # get the service image tag ret = self.app_tool.get_newapp_detail(self.namespace, self.app_name) assert ret.status_code == 200, "获取应用的镜像版本失败" image_tag = "" for i in range(0, len(ret.json())): if ("image" in str(ret.json()[i])): image_tag = self.app_tool.get_value( ret.json()[i], 'kubernetes.spec.template.spec.containers.0.image').split( ":")[-1] assert image_tag == self.repo_tag, "流水线更新应用失败" # delete jenkins pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "删除Jenkins流水线项目操作失败" ret = self.jenkins_tool.get_pipeline_detail(pipeline_id) assert ret.status_code == 404, "流水线没有被成功删除掉" def test_jenkins_build_with_git(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建集成中心实例失败" integration_id = self.create_integration.json()['id'] # get template id template_id = self.jenkins_tool.get_sys_template_id( self.build_template_name, 'uuid') assert template_id, "获取模板失败" # create code credential self.jenkins_tool.create_credential( './test_data/jenkins/create_git_code_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.git_code_credential_name) assert ret, "创建git代码库凭证失败或获取凭证失败" # create pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_build_pipeline_git.yaml', { "$pipeline_name": self.git_buidl_pipeline, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$branch": self.branch, "$template_uuid": template_id }) assert ret.status_code == 201, "创建Jenkins流水线项目失败" pipeline_id = ret.json()['uuid'] # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目操作失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'status', 'RUNNING') assert ret, "流水线项目未处于运行状态" # 由于执行流水线后立即取消执行会导致取消执行失败,添加2秒的等待时间 time.sleep(2) # stop pipeline ret = self.jenkins_tool.pipeline_cancel(pipeline_id, history_id) assert ret.status_code == 204, "取消执行流水线操作失败" ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'ABORTED') assert ret, "流水线项目取消执行失败" # replay pipeline ret = self.jenkins_tool.pipeline_replay( './test_data/jenkins/pipeline_replay.yaml', { "$pipeline_uuid": pipeline_id, "$history_id": history_id }) assert ret.status_code == 200, "再次执行流水线操作失败" history_id = ret.json()['uuid'] ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # get pipeline logs ret = self.jenkins_tool.get_pipeline_log(history_id, pipeline_id) assert ret, "获取流水线日志失败" # delete pipeline history ret = self.jenkins_tool.delete_pipeline_history( history_id, pipeline_id) assert ret.status_code == 204, "删除流水线历史操作失败" ret = self.jenkins_tool.check_pipeline_history_exist( history_id, pipeline_id, 404) assert ret, "删除流水线历史失败" # delete jenkins pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "删除Jenkins流水线项目操作失败" ret = self.jenkins_tool.check_pipeline_exist(pipeline_id, 404) assert ret, "流水线没有被成功删除掉" def test_jenkins_build_with_svn_no_sonar(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建集成中心实例失败" integration_id = self.create_integration.json()['id'] # get template id template_id = self.jenkins_tool.get_sys_template_id( self.build_template_name, 'uuid') assert template_id, "获取模板失败" # get registry url registry_endpoint = self.app_tool.get_uuid_accord_name( self.app_tool.global_info.get("PRIVATE_REGISTRY"), {"name": self.app_tool.global_info.get("$REGISTRY")}, "endpoint") # get repo tag ret = self.image_tool.get_repo_tag(self.repo) assert ret.status_code == 200, "获取镜像版本失败" contents = ret.json()['results'] assert len(contents) > 0, "镜像版本为空" repo_tag = contents[0]['tag_name'] # create code credential self.jenkins_tool.create_credential( './test_data/jenkins/create_svn_code_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.code_credential_name) assert ret, "创建svn代码库凭证失败或获取凭证失败" # create registry credential self.jenkins_tool.create_credential( './test_data/jenkins/create_registry_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.registry_credential_name) assert ret, "创建镜像仓库凭证失败或获取凭证失败" # create jenkins pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_build_pipeline_no_sonar_svn.yaml', { "$pipeline_name": self.svn_build_pipeline_no_sonar, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$REG_URL": registry_endpoint, "$repo_tag": repo_tag, "$ci_commands": "ls" }) assert ret.status_code == 201, "创建Jenkins流水线项目失败" pipeline_id = ret.json()['uuid'] # get pipeline list ret = self.jenkins_tool.get_pipeline_list() assert ret.status_code == 200, "获取流水线项目列表失败" contents = ret.text assert pipeline_id in contents, "流水线项目列表中不包含该流水线项目" # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # get pipeline history list ret = self.jenkins_tool.get_pipeline_history_list(integration_id) assert ret.status_code == 200, "获取流水线运行历史列表失败" assert pipeline_id in ret.text, "流水线运行历史列表中不包含该流水线运行的历史记录" # delete pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "删除流水线项目操作失败" ret = self.jenkins_tool.check_pipeline_exist(pipeline_id, 404) assert ret, "删除流水线项目失败" def test_jenkins_update_service(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建集成中心实例失败" integration_id = self.create_integration.json()['id'] # get template id template_id = self.jenkins_tool.get_sys_template_id( self.updateservice_template_name, 'uuid') assert template_id, "获取模板失败" # get registry url registry_endpoint = self.app_tool.get_uuid_accord_name( self.app_tool.global_info.get("PRIVATE_REGISTRY"), {"name": self.app_tool.global_info.get("$REGISTRY")}, "endpoint") # get repo tag ret = self.image_tool.get_repo_tag(self.repo) assert ret.status_code == 200, "获取镜像版本失败" contents = ret.json()['results'] assert len(contents) > 0, "镜像版本为空" repo_tag = contents[0]['tag_name'] # create pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_update_service_pipeline.yaml', { "$pipeline_name": self.update_service_pipeline, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$service_name": self.app_name, "$REG_URL": registry_endpoint, "$imageTag": repo_tag, "$time_out": self.time_out }) assert ret.status_code == 201, "创建Jenkins流水线项目失败" pipeline_id = ret.json()['uuid'] # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # get the service info ret = self.app_tool.get_newapp_detail(self.namespace, self.app_name) assert ret.status_code == 200, "获取应用的详情失败" image_tag = "" for i in range(0, len(ret.json())): if ("image" in str(ret.json()[i])): image_tag = self.app_tool.get_value( ret.json()[i], 'kubernetes.spec.template.spec.containers.0.image').split( ":")[-1] assert image_tag == repo_tag, "流水线更新应用镜像版本失败" # delete pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "执行删除Jenkins流水线操作失败" ret = self.jenkins_tool.check_pipeline_exist(pipeline_id, 404) assert ret, "删除Jenkins流水线失败" def test_jenkins_build_with_svn_sonar(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # access sonar sonar_url = self.jenkins_tool.global_info.get("$SONAR_ENDPOINT") ret = self.jenkins_tool.access_service(sonar_url) assert ret, "访问sonar失败,请确认sonar是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建jenkins集成中心实例失败" integration_id = self.create_integration.json()['id'] assert self.create_sonar_integration.status_code == 201, "创建sonar集成中心实例失败" sonar_integration_id = self.create_sonar_integration.json()['id'] # create code credential self.jenkins_tool.create_credential( './test_data/jenkins/create_svn_code_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.code_credential_name) assert ret, "创建svn代码库凭证失败或获取凭证失败" # get template id template_id = self.jenkins_tool.get_sys_template_id( self.build_template_name, 'uuid') assert template_id, "获取模板失败" # get qualitygates ret = self.jenkins_tool.get_sonar_qualitygates(sonar_integration_id) assert ret.status_code == 200, "获取sonar扫描质量阈值失败" quality_id = self.jenkins_tool.get_uuid_accord_name( ret.json()['qualitygates'], {"name": self.qualitygates_name}, 'id') logger.info("qualitygates id: {}".format(quality_id)) # get languages ret = self.jenkins_tool.get_languages(sonar_integration_id) assert ret.status_code == 200, "获取开发语言失败" language = self.jenkins_tool.get_uuid_accord_name( ret.json()['languages'], {"name": self.language_name}, 'key') logger.info("language: {}".format(language)) # create pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_build_pipeline_with_sonar_svn.yaml', { "$pipeline_name": self.svn_build_pipeline_with_sonar, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$integrationSonarQubeID": sonar_integration_id, "$quality": str(quality_id), "$lang": language }) assert ret.status_code == 201, "创建Jenkins流水线失败" pipeline_id = ret.json()['uuid'] # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # get pipeline history detail ret = self.jenkins_tool.get_pipeline_history_detail( history_id, pipeline_id) assert ret.status_code == 200, "获取流水线运行历史详情失败" assert "SonarQube" in ret.text, "流水线运行历史中没有代码扫描的结果" # delete pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "执行删除Jenkins流水线操作失败" ret = self.jenkins_tool.check_pipeline_exist(pipeline_id, 404) assert ret, "删除Jenkins流水线失败" def test_jenkins_sync_registry(self): # if not self.get_publick_registry: # assert True, "no public registry, no need to run" # return # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # Verify that the integration instance was created successfully assert self.create_integration.status_code == 201, "创建jenkins集成中心实例失败" integration_id = self.create_integration.json()['id'] # create registry credential self.jenkins_tool.create_credential( './test_data/jenkins/create_registry_credential.yaml', {"$jenkins_integration_id": integration_id}) ret = self.jenkins_tool.get_credential(integration_id, self.registry_credential_name) assert ret, "创建镜像仓库凭证失败或获取镜像仓库凭证失败" # get template id template_id = self.jenkins_tool.get_sys_template_id( self.syncimage_template_name, 'uuid') assert template_id, "获取模板失败" # get source registry info # contents = self.image.split('/', 1) # source_reg_url = contents[0] # content = contents[1].split(":") # source_repo = content[0] # source_tag = content[1] # get dest registry url # reg_url = self.jenkins_tool.global_info.get("PRIVATE_REGISTRY")[0]['endpoint'] source_reg_url = self.app_tool.get_uuid_accord_name( self.app_tool.global_info.get("PRIVATE_REGISTRY"), {"name": self.app_tool.global_info.get("$REGISTRY")}, "endpoint") # get repo tag ret = self.image_tool.get_repo_tag(self.repo) assert ret.status_code == 200, "获取镜像版本失败" contents = ret.json()['results'] assert len(contents) > 0, "镜像版本为空" self.sync_repo_tag = contents[0]['tag_name'] # create source_repo ret = self.image_tool.create_repo('./test_data/image/create_repo.yaml', {"$REPO_IMAGE": self.target_repo}) assert ret.status_code == 201, "创建目标镜像仓库失败" # create pipeline ret = self.jenkins_tool.create_pipeline( './test_data/jenkins/create_sync_registry_pipeline.yaml', { "$pipeline_name": self.sync_registry_pipeline, "$jenkins_integration_id": integration_id, "$jenkins_integration_name": self.integration_name, "$template_uuid": template_id, "$source_reg_url": source_reg_url, "$source_tag": self.sync_repo_tag, "$reg_url": source_reg_url, "$target_repo": self.target_repo, "$tag": self.sync_repo_tag }) assert ret.status_code == 201, "创建Jenkins流水线项目失败" pipeline_id = ret.json()['uuid'] # execute pipeline ret = self.jenkins_tool.execute_pipeline( './test_data/jenkins/execute_pipeline.yaml', {"$pipeline_uuid": pipeline_id}) assert ret.status_code == 200, "执行流水线项目失败" history_id = ret.json()['uuid'] # get pipeline status ret = self.jenkins_tool.get_pipeline_status(history_id, pipeline_id, 'result', 'SUCCESS') assert ret, "流水线项目执行失败" # 加2秒的等待时间,以防镜像仓库中的数据还没有更新 time.sleep(2) # get repo tag ret = self.image_tool.get_repo_tag(self.target_repo) assert ret.status_code == 200, "获取镜像版本失败" logger.info("image tags list: {}".format(ret.text)) assert self.sync_repo_tag in ret.text, "同步镜像仓库失败" # delete image repo ret = self.image_tool.delete_repo(self.target_repo) assert ret.status_code == 204, "删除镜像版本操作失败" # delete pipeline ret = self.jenkins_tool.delete_pipeline(pipeline_id) assert ret.status_code == 204, "执行删除Jenkins流水线操作失败" ret = self.jenkins_tool.check_pipeline_exist(pipeline_id, 404) assert ret, "删除Jenkins流水线失败" @pytest.mark.BAT @pytest.mark.ace @pytest.mark.flaky(reruns=2, reruns_delay=3) def test_jenkins_template(self): # access jenkins ret = self.jenkins_tool.access_service(url=self.jenkins_url, auth=self.jenkins_auth) assert ret, "访问Jenkins失败, 请确认Jenkins是否正常" # get system template list ret = self.jenkins_tool.get_template_list() assert ret.status_code == 200, "获取模板列表失败" assert len(ret.json()['results']) > 0, "模板列表为空" # get template source ret = self.jenkins_tool.get_template_source_list() assert ret.status_code == 200, "获取模板仓库列表失败" content = ret.json()['result'] assert len(content) > 0, "模板仓库列表为空" template_source_id = self.jenkins_tool.get_uuid_accord_name( content, {"provider": "user"}, 'uuid') logger.info("template source id: {}".format(template_source_id)) if not template_source_id: assert True, "用户未添加模板仓库,不需要进行后面的同步模板仓库操作" return # refresh template source ret = self.jenkins_tool.refresh_template_source(template_source_id) assert ret.status_code == 204, "同步模板仓库操作失败" # get refresh status ret = self.jenkins_tool.get_refresh_template_source_status( template_source_id, 'last_job.status', 'SUCCESS') assert ret, "同步模板仓库执行失败" # get template source detail ret = self.jenkins_tool.get_template_source_detail(template_source_id) assert ret.status_code == 200, "获取模板详情失败" report = ret.json()['last_job']['report']['items'] # get user template list ret = self.jenkins_tool.get_template_list( template_resource_id=template_source_id, official=False) assert ret.status_code == 200, "获取用户定义的模板列表失败" if not report: assert len(ret.json()['results']) == 0, "同步模板仓库失败" else: add = self.jenkins_tool.get_uuid_accord_name( report, {"action": "ADD"}, "name") delete = self.jenkins_tool.get_uuid_accord_name( report, {"action": "DELETE"}, "name") skip = self.jenkins_tool.get_uuid_accord_name( report, {"action": "SKIP"}, "name") if add: assert add in ret.text, "同步模板仓库失败" if delete: assert delete not in ret.text, "同步模板仓库失败" if skip: assert add in ret.text, "同步模板仓库失败"
def __init__(self): super(SetUp, self).__init__() global global_app_name global_app_name = "alauda-global-app" self.common = { "$NAMESPACE": settings.ACCOUNT, "$PASSWORD": settings.PASSWORD, "$SVN_CREDENTIAL": settings.SVN_CREDENTIAL, "$SVN_PASSWORD": settings.SVN_PASSWORD, "$SVN_USERNAME": settings.SVN_USERNAME, "$SVN_REPO": settings.SVN_REPO, "$REGISTRY": settings.REGISTRY_NAME, "$REPO_NAME": settings.REPO_NAME, "$TARGET_REPO_NAME": settings.TARGET_REPO_NAME, "$SPACE_NAME": settings.SPACE_NAME, "$REGION_NAME": settings.REGION_NAME, "$REG_CREDENTIAL": settings.REGISTRY_CREDENTIAL, "$K8S_NAMESPACE": settings.K8S_NAMESPACE, "$IMAGE": settings.IMAGE, "$PROJECT_NAME": settings.PROJECT_NAME, "$JENKINS_ENDPOINT": settings.JENKINS_ENDPOINT, "$JENKINS_USER": settings.JENKINS_USER, "$JENKINS_TOKEN": settings.JENKINS_TOKEN, "$GIT_REPO": settings.GIT_REPO, "$GIT_CREDENTIAL": settings.GIT_CREDENTIAL, "$GIT_USERNAME": settings.GIT_USERNAME, "$GIT_PASSWORD": settings.GIT_PASSWORD, "$SONAR_ENDPOINT": settings.SONAR_ENDPOINT, "$SONAR_TOKEN": settings.SONAR_TOKEN, "$MIDDLEWARE_REGISTRY": settings.MIDDLEWARE_REGISTRY, "$GIT_URL": settings.GIT_URL, "$GIT_PATH": settings.GIT_PATH, "$GIT_BRANCH": settings.GIT_BRANCH, "$GIT_USER": settings.GIT_USER, "$GIT_PWD": settings.GIT_PWD, "$OAUTH2_CLIENTSECRET": settings.OAUTH2_CLIENTSECRET, "$OAUTH2_CLIENTID": settings.OAUTH2_CLIENTID, "$DOCKER_AUTH": settings.DOCKER_AUTH, "$DOCKER_USERNAME": settings.DOCKER_USERNAME, "$DOCKER_PASSWORD": settings.DOCKER_PASSWORD, "$DOCKER_EMAIL": settings.DOCKER_EMAIL, "$VM_USERNAME": settings.VM_USERNAME, "$VM_PASSWORD": settings.VM_PASSWORD } self.master_ip = "" if not settings.VM_IPS else settings.VM_IPS[0] self.input_file(self.common) self.cluster_client = Cluster() self.deploy_region() self.get_region_data() self.deploy_feature() self.get_registry_uuid() self.get_master_ips() self.get_slave_ips() self.input_file(self.common) self.namespace_client = Namespace() self.project_client = Project() self.image_client = Image() self.newapp = Newapplication() self.prepare() self.create_global_app() self.input_file(self.common)
class TestCatalogSuite(object): def setup_class(self): self.catalog = Catalog() self.middleware = Middleware() self.newapp = Newapplication() self.app_name = "e2e-mongodb-pri" self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.git_name = "alauda-git-repository" self.update_git_displayname = "update-git-repository" self.svn_name = "alauda-svn-repository" self.upadte_svn_displayname = "update-svn-repository" def teardown_class(self): logger.info("开始清除应用目录数据") self.newapp.delete_newapp(self.namespace, self.app_name) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.app_name), 404) if len(self.catalog.get_repository_list().json()) > 0: self.catalog.delete_repository( self.catalog.get_uuid_accord_name( self.catalog.get_repository_list().json()["results"], {"name": self.git_name}, "uuid")) if len(self.catalog.get_repository_list().json()) > 0: self.catalog.delete_repository( self.catalog.get_uuid_accord_name( self.catalog.get_repository_list().json()["results"], {"name": self.svn_name}, "uuid")) logger.info("清除完毕") @pytest.mark.repository_git def test_repository_git(self): ''' 添加应用目录模板仓库 :return: ''' result = {"flag": True} create_result = self.catalog.create_repository( './test_data/catalog/repository_git.json', {'$repository_name': self.git_name}) assert create_result.status_code == 201, "添加应用目录git模板仓库失败 {}".format( create_result.text) self.git_id = self.catalog.get_value(create_result.json(), 'uuid') logger.info("git模板仓库创建成功") ''' 获取模板仓库列表,列表中包含新建模板仓库 ''' repository_list = self.catalog.get_repository_list() result = self.catalog.update_result(result, repository_list.status_code == 200, "获取应用目录模板仓库列表失败") result = self.catalog.update_result( result, self.git_name in repository_list.text, "获取应用目录模板仓库列表失败:新建git模板仓库不在列表中") logger.info("列表中包含新建git模板仓库") ''' 获取模板仓库详细信息 ''' repository_status = self.catalog.get_repository_status( self.git_id, "SUCCESS") assert repository_status, "创建应用目录git模板仓库后,状态不是SUCCESS" logger.info("获取git模板仓库详情信息成功") ''' 更新模板仓库 ''' update_result = self.catalog.update_repository( self.git_id, './test_data/catalog/update_repository_git.json', {'$display_name': self.update_git_displayname}) assert update_result.status_code == 200, "更新应用目录git模板仓库失败 {}".format( update_result.text) result = self.catalog.update_result( result, self.update_git_displayname in update_result.text, "更新应用目录模板仓库失败:修改的显示名称与设置不一致") repository_status = self.catalog.get_repository_status( self.git_id, "SUCCESS") assert repository_status, "更新应用目录git模板仓库后,状态不是SUCCESS" logger.info("更新应用目录git模板仓库成功") ''' 同步模板仓库 ''' refresh_result = self.catalog.refresh_repository(self.git_id) assert refresh_result.status_code == 204, "应用目录同步模板仓库失败 {}".format( refresh_result.text) repository_status = self.catalog.get_repository_status( self.git_id, "SUCCESS") assert repository_status, "同步应用目录git模板仓库后,状态不是SUCCESS" logger.info("同步git模板仓库成功") assert result['flag'], result @pytest.mark.repository_svn def test_repository_svn(self): ''' 添加应用目录模板仓库 :return: ''' result = {"flag": True} create_result = self.catalog.create_repository( './test_data/catalog/repository_svn.json', {'$repository_name': self.svn_name}) assert create_result.status_code == 201, "添加应用目录svn模板仓库失败 {}".format( create_result.text) self.svn_id = self.catalog.get_value(create_result.json(), 'uuid') logger.info("模板仓库创建成功") ''' 获取模板仓库列表,列表中包含新建模板仓库 ''' repository_list = self.catalog.get_repository_list() result = self.catalog.update_result(result, repository_list.status_code == 200, "获取应用目录svn模板仓库列表失败") result = self.catalog.update_result( result, self.svn_name in repository_list.text, "获取应用目录svn模板仓库列表失败:新建模板仓库不在列表中") logger.info("列表中包含新建模板仓库") ''' 获取模板仓库详细信息 ''' repository_status = self.catalog.get_repository_status( self.svn_id, "SUCCESS") assert repository_status, "创建应用目录svn模板仓库后,状态不是SUCCESS" logger.info("获取详情信息成功") ''' 更新模板仓库 ''' update_result = self.catalog.update_repository( self.svn_id, './test_data/catalog/update_repository_svn.json', {'$display_name': self.upadte_svn_displayname}) assert update_result.status_code == 200, "更新应用目录svn模板仓库失败 {}".format( update_result.text) result = self.catalog.update_result( result, self.upadte_svn_displayname in update_result.text, "更新应用目录svn模板仓库失败:修改的显示名称与设置不一致") repository_status = self.catalog.get_repository_status( self.svn_id, "SUCCESS") assert repository_status, "更新应用目录svn模板仓库后,状态不是SUCCESS" logger.info("更新模板仓库成功") ''' 同步模板仓库 ''' refresh_result = self.catalog.refresh_repository(self.svn_id) assert refresh_result.status_code == 204, "应用目录同步模板仓库失败 {}".format( refresh_result.text) repository_status = self.catalog.get_repository_status( self.svn_id, "SUCCESS") assert repository_status, "同步应用目录模板仓库后,状态不是SUCCESS" logger.info("同步模板仓库成功") ''' 删除模板仓库 ''' delete_result = self.catalog.delete_repository(self.svn_id) result = self.catalog.update_result(result, delete_result.status_code == 200, "删除应用目录svn模板仓库失败") assert result['flag'], result logger.info("删除应用目录svn模板仓库成功") @pytest.mark.catalog_mongodb def nottest_catalog_mongodb(self): ''' 使用应用目录模板中的模板创建mongodb应用 :return: ''' result = {"flag": True} assert len(self.catalog.get_repository_list().json() ) > 0, "应用没有创建出git仓库,没法创建mongodb应用" if len(self.catalog.get_repository_list().json()) > 0: repository_id = self.catalog.get_uuid_accord_name( self.catalog.get_repository_list().json()["results"], {"name": self.git_name}, "uuid") templates_list = self.catalog.get_templates(repository_id) templates = self.catalog.get_value(templates_list.json(), 'results') template_id = "" for template in templates: template_name = template.get("name", "") if "mongodb" == template_name: template_id = template.get("uuid", "") break logger.info("获取应用目录mongodb模板的id:{}".format(template_id)) mongodb_info = self.middleware.get_template_info(template_id) version_id = self.catalog.get_value(mongodb_info.json(), "versions.0.uuid") logger.info("获取到的mongodb的version id:{}".format(version_id)) create_result = self.middleware.create_application( './test_data/catalog/catalog_mongodb.json', { "$catalog_app_name": self.app_name, "$template_id": template_id, "$version_id": version_id }) assert create_result.status_code == 201, "创建mongodb失败 {}".format( create_result.text) logger.info("应用目录创建mongodb成功") app_id = self.catalog.get_value(create_result.json(), "kubernetes.metadata.uid") logger.info("创建mongodb成功,id是:{}".format(app_id)) logger.info("创建mongodb成功,name是:{}".format(self.app_name)) app_status = self.middleware.get_application_status( self.namespace, self.app_name, "Running") assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format( self.app_name) logger.info("mongodb的应用状态为:Running") # 删除应用 delete_result = self.newapp.delete_newapp(self.namespace, self.app_name) assert delete_result.status_code == 204, "删除应用目录创建出的mongodb应用失败 {}".format( delete_result.text) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.app_name), 404) assert result['flag'], True
class TestNewApplicationSuite(object): def setup_class(self): self.newapp = Newapplication() self.k8s_namespace = self.newapp.global_info["$K8S_NAMESPACE"] self.newapp_name = 'alauda-newapp' self.configmap = Configmap() self.configmap_name = 'alauda-cmforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.appwithcm_name = 'alauda-appwithcm-{}'.format(self.newapp.region_name).replace('_', '-') self.scs = Scs() self.scs_name = 'alauda-scsforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.pvc = Pvc() self.pvc_name = 'alauda-pvcforapp-{}'.format(self.newapp.region_name).replace('_', '-') self.appwithpvc_name = 'alauda-appwithpvc-{}'.format(self.newapp.region_name).replace('_', '-') self.teardown_class(self) def teardown_class(self): self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name) self.newapp.delete_newapp(self.k8s_namespace, self.appwithcm_name) self.newapp.delete_newapp(self.k8s_namespace, self.appwithpvc_name) self.configmap.delete_configmap(self.k8s_namespace, self.configmap_name) self.pvc.delete_pvc(self.k8s_namespace, self.pvc_name) self.scs.delete_scs(self.scs_name) @pytest.mark.BAT @pytest.mark.ace def test_newapp(self): # if not self.newapp.is_weblab_open("USER_VIEW_ENABLED"): # return True, "用户视角未打开,不需要测试" """ 创建应用-获取拓扑图-获取容器组-获取yaml-获取日志-获取事件-获取k8s事件-exec-获取全部应用-获取命名空间下的应用-搜索应用- 更新应用-获取应用yaml-缩容-扩容-删除应用下的资源-获取应用详情-添加资源到应用-停止应用-启动应用-删除组件-删除应用 """ # 创建应用 result = {"flag": True} create_result = self.newapp.create_newapp('./test_data/newapp/appcore.json', {'$newapp_name': self.newapp_name}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_uuid = self.newapp.get_value(create_result.json(), '0.kubernetes.metadata.uid') # 获取应用状态 app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) # 获取拓扑图 topology_result = self.newapp.get_topology_in_namespace(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, topology_result.status_code == 200, "获取拓扑图失败") result = self.newapp.update_result(result, len(topology_result.json().get('referenced_by')) == 2, "拓扑图关联错误") # 获取容器组 pod_result = self.newapp.get_newapp_pods(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, pod_result.status_code == 200, "获取容器组失败") result = self.newapp.update_result(result, len(pod_result.json()) == 1, "容器组不是1 {}".format(len(pod_result.json()))) container_name = self.newapp.get_value(pod_result.json(), '0.kubernetes.metadata.name') # 获取yaml yaml_result = self.newapp.get_newapp_yaml(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, yaml_result.status_code == 200, "创建应用后,yaml失败") result = self.newapp.update_result(result, 'Application' in yaml_result.text, "创建应用后,yaml失败:不存在Application") result = self.newapp.update_result(result, 'Deployment' in yaml_result.text, "创建应用后,yaml失败:不存在Deployment") result = self.newapp.update_result(result, 'HorizontalPodAutoscaler' in yaml_result.text, "创建应用后,yaml失败:HorizontalPodAutoscaler") result = self.newapp.update_result(result, 'Service' in yaml_result.text, "创建应用后,yaml失败:不存在Service") # 获取日志 log_result = self.newapp.get_newapp_log(self.k8s_namespace, self.newapp_name, container_name) result = self.newapp.update_result(result, log_result.status_code == 200, "获取日志失败") result = self.newapp.update_result(result, len(log_result.json()['logs']) > 0, "获取日志为空") # 获取事件 event_result = self.newapp.get_newapp_event(app_uuid) result = self.newapp.update_result(result, event_result.status_code == 200, "获取事件失败") result = self.newapp.update_result(result, event_result.json().get('total_items') != 0, "获取事件为空") # 获取k8s事件 kevent_result = self.newapp.get_newapp_kevent(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, kevent_result.status_code == 200, "获取k8s事件失败") result = self.newapp.update_result(result, kevent_result.json().get('count') != 0, "获取k8s事件为空") # exec exec_result = self.newapp.exec_newapp(self.k8s_namespace, self.newapp_name, container_name) result = self.newapp.update_result(result, exec_result.status_code == 200, "exec失败") # 获取全部应用 list_result = self.newapp.get_all_newapp() result = self.newapp.update_result(result, list_result.status_code == 200, "获取应用列表失败") result = self.newapp.update_result(result, self.newapp_name in list_result.text, "获取应用列表失败:新建应用不在列表中") # 获取命名空间下的应用 namespace_result = self.newapp.get_newapp_in_namespace(self.k8s_namespace) result = self.newapp.update_result(result, namespace_result.status_code == 200, "获取命名空间下的应用列表失败") result = self.newapp.update_result(result, self.newapp_name in namespace_result.text, "获取命名空间下的应用列表失败:新建应用不在列表中") # 按名称搜索应用 search_result = self.newapp.search_newapp(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, search_result.status_code == 200, "按名称搜索应用失败") result = self.newapp.update_result(result, self.newapp_name in search_result.text, "按名称搜索应用失败:新建应用不在列表中") # 更新应用,个数为2,添加ClusterRole update_result = self.newapp.update_newapp(self.k8s_namespace, self.newapp_name, './test_data/newapp/update_appcore.json', {'$newapp_name': self.newapp_name}) assert update_result.status_code == 200, "更新应用失败 {}".format(update_result.text) self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Pending') app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "更新应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) # 获取应用yaml yaml_result = self.newapp.get_newapp_yaml(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, yaml_result.status_code == 200, "更新应用后,yaml失败") result = self.newapp.update_result(result, 'Application' in yaml_result.text, "更新应用后,yaml失败:不存在Application") result = self.newapp.update_result(result, 'Deployment' in yaml_result.text, "更新应用后,yaml失败:不存在Deployment") result = self.newapp.update_result(result, 'HorizontalPodAutoscaler' in yaml_result.text, "更新应用后,yaml失败:HorizontalPodAutoscaler") result = self.newapp.update_result(result, 'Service' in yaml_result.text, "更新应用后,yaml失败:不存在Service") result = self.newapp.update_result(result, 'ClusterRole' in yaml_result.text, "更新应用后,yaml失败:不存在ClusterRole") result = self.newapp.update_result(result, '15222222222' in yaml_result.text, "更新应用后,owners未更新") # 缩容 scale_down_result = self.newapp.scale_down_newapp(self.k8s_namespace, self.newapp_name) assert scale_down_result.status_code == 204, "缩容失败 {}".format(scale_down_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "缩容后,验证应用状态出错:app: {} is not running".format(self.newapp_name) flag = self.newapp.get_status(self.newapp.get_newapp_status_url(self.k8s_namespace, self.newapp_name), 'workloads.deployment-{}.desired'.format(self.newapp_name), 1) assert flag, "缩容后,预期个数不是1" # 扩容 scale_up_result = self.newapp.scale_up_newapp(self.k8s_namespace, self.newapp_name) assert scale_up_result.status_code == 204, "扩容失败 {}".format(scale_up_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "扩容后,验证应用状态出错:app: {} is not running".format(self.newapp_name) flag = self.newapp.get_status(self.newapp.get_newapp_status_url(self.k8s_namespace, self.newapp_name), 'workloads.deployment-{}.desired'.format(self.newapp_name), 2) assert flag, "扩容后,预期个数不是2" # 删除应用下的资源ClusterRole remove_result = self.newapp.remove_resource_newapp(self.k8s_namespace, self.newapp_name, './test_data/newapp/resource.json', {'$newapp_name': self.newapp_name}) assert remove_result.status_code == 204, "删除应用下的资源失败 {}".format(remove_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "删除应用下的资源后,验证应用状态出错:app: {} is not running".format(self.newapp_name) # 获取应用详情 detail_result = self.newapp.get_newapp_detail(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, detail_result.status_code == 200, "获取应用详情失败") result = self.newapp.update_result(result, 'Application' in detail_result.text, "删除应用下的资源详情失败:不存在Application") result = self.newapp.update_result(result, 'Deployment' in detail_result.text, "删除应用下的资源详情失败:不存在Deployment") result = self.newapp.update_result(result, 'HorizontalPodAutoscaler' in detail_result.text, "删除应用下的资源详情失败:不存在HorizontalPodAutoscaler") result = self.newapp.update_result(result, 'Service' in detail_result.text, "删除应用下的资源详情失败:存在Service") result = self.newapp.update_result(result, 'ClusterRole' not in detail_result.text, "删除应用下的资源详情失败:存在ClusterRole") # 添加资源ClusterRole到应用 add_result = self.newapp.add_resource_newapp(self.k8s_namespace, self.newapp_name, './test_data/newapp/resource.json', {'$newapp_name': self.newapp_name}) assert add_result.status_code == 204, "添加资源到应用失败 {}".format(remove_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "添加资源到应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) detail_result = self.newapp.get_newapp_detail(self.k8s_namespace, self.newapp_name) result = self.newapp.update_result(result, detail_result.status_code == 200, "获取应用详情失败") result = self.newapp.update_result(result, 'Application' in detail_result.text, "添加资源到应用详情失败:不存在Application") result = self.newapp.update_result(result, 'Deployment' in detail_result.text, "添加资源到应用详情失败:不存在Deployment") result = self.newapp.update_result(result, 'HorizontalPodAutoscaler' in detail_result.text, "删除应用下的资源详情失败:不存在HorizontalPodAutoscaler") result = self.newapp.update_result(result, 'Service' in detail_result.text, "添加资源到应用详情失败:不存在Service") result = self.newapp.update_result(result, 'ClusterRole' in detail_result.text, "添加资源到应用详情失败:不存在ClusterRole") # 停止应用 stop_result = self.newapp.stop_newapp(self.k8s_namespace, self.newapp_name) assert stop_result.status_code == 204, "停止应用失败 {}".format(stop_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Stopped') assert app_status, "停止应用后,验证应用状态出错:app: {} is running".format(self.newapp_name) # 开始应用 start_result = self.newapp.start_newapp(self.k8s_namespace, self.newapp_name) assert start_result.status_code == 204, "启动应用失败 {}".format(start_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Running') assert app_status, "启动应用后,验证应用状态出错:app: {} is not running".format(self.newapp_name) # 删除组件 deploy_result = self.newapp.delete_deployment(self.k8s_namespace, self.newapp_name) assert deploy_result.status_code == 204, "删除组件失败 {}".format(deploy_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.newapp_name, 'Empty') assert app_status, "删除组件后,验证应用状态出错:app: {} is not Empty".format(self.newapp_name) # 删除应用 delete_result = self.newapp.delete_newapp(self.k8s_namespace, self.newapp_name) assert delete_result.status_code == 204, "删除应用失败 {}".format(delete_result.text) delete_flag = self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.newapp_name), 404) assert delete_flag, "删除应用失败" assert result['flag'], result @pytest.mark.cm @pytest.mark.BAT def test_app_with_cm(self): """ 应用使用configmap测试:创建configmap-创建应用-删除应用-删除configmap :return: """ result = {"flag": True} # create configmap createconfigmap_result = self.configmap.create_configmap("./test_data/configmap/configmap.json", {"$cm_name": self.configmap_name, "$cm_key": self.configmap_name}) assert createconfigmap_result.status_code == 201, "创建cm出错:{}".format(createconfigmap_result.text) # create app with cm create_result = self.newapp.create_newapp_by_yaml(self.k8s_namespace, self.appwithcm_name, './test_data/newapp/daemonset_cm.yml', {'$newapp_name': self.appwithcm_name, "$cm_name": self.configmap_name, "$cm_key": self.configmap_name}) assert create_result.status_code == 201, "新版应用创建失败 {}".format(create_result.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.appwithcm_name, 'Running') assert app_status, "创建使用configmap的应用后,验证应用状态出错:app: {} is not running".format(self.appwithcm_name) daemonset_result = self.newapp.get_component(self.k8s_namespace, self.appwithcm_name, 'daemonsets') result = self.newapp.update_result(result, daemonset_result.status_code == 200, '获取daemonset详情失败') content = daemonset_result.json() v1 = self.newapp.get_value(content, 'kubernetes.spec.template.spec.volumes.0.configMap.name') result = self.newapp.update_result(result, v1 == self.configmap_name, '存储卷挂载configmap失败') v2 = self.newapp.get_value(content, 'kubernetes.spec.template.spec.volumes.1.configMap.name') result = self.newapp.update_result(result, v2 == self.configmap_name, '存储卷挂载configmap失败') e1 = self.newapp.get_value(content, 'kubernetes.spec.template.spec.containers.0.envFrom.0.configMapRef.name') result = self.newapp.update_result(result, e1 == self.configmap_name, '环境变量完整引用configmap失败') e2 = '' env = self.newapp.get_value(content, 'kubernetes.spec.template.spec.containers.0.env') for e in env: if e.get('name') == 'cm': e2 = self.newapp.get_value(e, 'valueFrom.configMapKeyRef.name') result = self.newapp.update_result(result, e2 == self.configmap_name, '环境变量引用configmap的key失败') self.newapp.delete_newapp(self.k8s_namespace, self.appwithcm_name) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.appwithcm_name), 404) self.configmap.delete_configmap(self.k8s_namespace, self.configmap_name) assert result['flag'], result @pytest.mark.pvc def test_app_with_pvc(self): """ 应用使用pvc测试:创建sc-创建pvc-创建应用-获取pvc拓扑图-删除应用-删除pvc-删除sc :return: """ result = {"flag": True} create_result = self.scs.create_scs("./test_data/scs/scs.yml", {"$scs_name": self.scs_name, "$is_default": "false", }) assert create_result.status_code == 201, "创建sc失败{}".format(create_result.text) # create pvc createpvc_result = self.pvc.create_pvc("./test_data/pvc/pvc.json", {"$pvc_name": self.pvc_name, "$pvc_mode": "ReadWriteOnce", "$scs_name": self.scs_name, "$size": "1"}) assert createpvc_result.status_code == 201, "创建pvc失败{}".format(createpvc_result.text) self.pvc.get_status(self.pvc.get_common_pvc_url(self.pvc.global_info["$K8S_NAMESPACE"], self.pvc_name), "status.phase", "Bound") # create app create_app = self.newapp.create_newapp_by_yaml(self.k8s_namespace, self.appwithpvc_name, './test_data/newapp/statefulset_pvc.yml', {"$newapp_name": self.appwithpvc_name, "$pvc_name": self.pvc_name}) assert create_app.status_code == 201, "创建app出错:{}".format(create_app.text) app_status = self.newapp.get_newapp_status(self.k8s_namespace, self.appwithpvc_name, 'Running') assert app_status, "创建使用pvc的应用后,验证应用状态出错:app: {} is not running".format(self.appwithpvc_name) # get statefulset detail statefulset_result = self.newapp.get_component(self.k8s_namespace, self.appwithpvc_name, 'statefulsets') result = self.newapp.update_result(result, statefulset_result.status_code == 200, '获取statefulset详情失败') claimName = self.newapp.get_value(statefulset_result.json(), "kubernetes.spec.template.spec.volumes.0.persistentVolumeClaim.claimName") result = self.newapp.update_result(result, claimName == self.pvc_name, "详情中挂载pvc失败") # get pvc topology pvc_topology = self.newapp.get_topology_in_namespace(self.k8s_namespace, self.pvc_name, 'PersistentVolumeClaim') result = self.newapp.update_result(result, pvc_topology.status_code == 200, "获取pvc拓扑图失败 {}".format(pvc_topology.status_code)) result = self.newapp.update_result(result, len(pvc_topology.json()['referenced_by']) == 1, "获取pvc拓扑图错误 {}".format(pvc_topology.text)) # delete self.newapp.delete_newapp(self.k8s_namespace, self.appwithpvc_name) self.newapp.check_exists(self.newapp.get_newapp_common_url(self.k8s_namespace, self.appwithpvc_name), 404) self.pvc.get_status( self.newapp.get_topology_in_namespace_url(self.k8s_namespace, self.pvc_name, 'PersistentVolumeClaim'), "referenced_by", []) time.sleep(60) self.pvc.delete_pvc(self.k8s_namespace, self.pvc_name) self.scs.delete_scs(self.scs_name) assert result['flag'], result
def setup_class(self): self.app_tool = Newapplication() self.jenkins_tool = Jenkins() self.integration_tool = Integrations() self.image_tool = Image() self.jenkins_auth = \ (self.jenkins_tool.global_info.get("$JENKINS_USER"), self.jenkins_tool.global_info.get("$JENKINS_TOKEN")) self.jenkins_url = self.jenkins_tool.global_info.get( "$JENKINS_ENDPOINT") self.pipeline_name = "alauda-jenkins-pipeline" self.git_buidl_pipeline = "alauda-jenkins-pipeline-git-build" self.svn_build_pipeline_no_sonar = "alauda-jenkins-pipeline-svn-build-no-sonar" self.svn_build_pipeline_with_sonar = "alauda-jenkins-pipeline-svn-build-with-sonar" self.update_service_pipeline = "alauda-jenkins-update-service-pipeline" self.sync_registry_pipeline = "alauda-jenkins-sync-registry-pipeline" self.code_credential_name = self.app_tool.global_info.get( '$SVN_CREDENTIAL') self.registry_credential_name = self.app_tool.global_info.get( '$REG_CREDENTIAL') self.git_code_credential_name = self.app_tool.global_info.get( '$GIT_CREDENTIAL') # self.get_publick_registry = self.app_tool.global_info.get('PUBLIC_REGISTRY') self.image = self.app_tool.global_info.get('$IMAGE') self.registry_name = self.app_tool.global_info.get("$REGISTRY") self.integration_name = "alauda-integration-instance-name-svn" self.sonar_integration_name = "alauda-sonar-integration-instance-name" self.namespace = self.app_tool.global_info["$K8S_NAMESPACE"] self.app_name = self.app_tool.global_info.get("$GLOBAL_APP_NAME") # self.app_id = self.app_tool.global_info.get("$GLOBAL_APP_ID") self.repo_tag = "alauda-e2e" self.repo_additional_tag = "alauda-e2e-additional" self.sync_repo_tag = "alauda-e2e-sync" self.branch = "master" self.template_name = "newalaudaBuildImageAndDeployService" self.build_template_name = "alaudaBuildImage" self.syncimage_template_name = "alaudaSyncImage" self.updateservice_template_name = "newalaudaDeployService" self.time_out = '300' self.repo = self.app_tool.global_info.get("$REPO_NAME") self.target_repo = self.app_tool.global_info.get("$TARGET_REPO_NAME") self.pipeline_description = "alauda jenkins pipeline" self.qualitygates_name = "SonarQube way" self.language_name = 'Java' self.teardown_class(self) # create jenkins integration instance self.create_integration = self.integration_tool.create_integration( './test_data/integration/ci_cd/create_integration.yaml', {"$INTEGRATION_NAME": self.integration_name}) # create sonar integration instance self.create_sonar_integration = self.integration_tool.create_integration( './test_data/integration/sonar/create_sonar_integration.yaml', {"$integration_name": self.sonar_integration_name})
def setup_class(self): self.middleware = Middleware() self.catalog = Catalog() self.newapp = Newapplication() self.name = "e2e-mongodb-pub" self.namespace = self.middleware.global_info["$K8S_NAMESPACE"]
class TestZookeeperSuite(object): def setup_class(self): self.newapp = Newapplication() self.middleware = Middleware() self.scs = Scs() self.namespace = self.middleware.global_info["$K8S_NAMESPACE"] self.zookeeper_name = "e2e-zookeeper-pub" self.kafka_name = "e2e-kafka-pub" scs_list_result = self.scs.list_scs() assert len( scs_list_result.json()) > 0, "没有存储类,不能创建zookeeper,创建zookeeper用例失败" self.scs_name = scs_list_result.json( )[0]["kubernetes"]["metadata"]["name"] logger.info("查出来的第一个scs {}".format(self.scs_name)) def teardown_class(self): logger.info("清除zookeeper应用资源") self.newapp.delete_newapp(self.namespace, self.kafka_name) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.kafka_name), 404) self.newapp.delete_newapp(self.namespace, self.zookeeper_name) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.zookeeper_name), 404) logger.info("清除完毕") @pytest.mark.middleware_zookeeper def test_zookeeper(self): result = {"flag": True} zookeeper_template_id = self.middleware.get_template_id("zookeeper") version_id = self.middleware.get_version_id(zookeeper_template_id) create_result = self.middleware.create_application( './test_data/middleware/zookeeper.json', { "$name": self.zookeeper_name, "$template_id": zookeeper_template_id, "$version_id": version_id, "$scs_name": self.scs_name }) assert create_result.status_code == 201, "中间件创建zookeeper应用失败 {}".format( create_result.text) logger.info("中间件创建zookeeper应用成功") app_status = self.middleware.get_application_status( self.namespace, self.zookeeper_name, "Running") assert app_status, "创建应用后,验证应用状态出错,app:{} is not Running".format( self.zookeeper_name) logger.info("中间件zookeeper应用为运行中") delete_result = self.newapp.delete_newapp(self.namespace, self.zookeeper_name) assert delete_result.status_code == 204, "删除zookeeper应用失败 {}".format( delete_result.text) self.newapp.check_exists( self.newapp.get_newapp_common_url(self.namespace, self.zookeeper_name), 404) logger.info("删除zookeeper应用成功") assert result["flag"], True
class TestAlbSuite(object): def setup_class(self): self.alb = Alb() self.alb2_name = 'nginx-{}'.format(self.alb.region_name).replace( '_', '-') self.k8s_namespace = self.alb.global_info["$K8S_NAMESPACE"] list_result = self.alb.list_alb() assert list_result.status_code == 200, "获取负载均衡列表失败 {}".format( list_result.text) if len(list_result.json()) == 0: return False, "集群负载均衡列表为空,请先部署alb2" self.alb_name = self.alb.get_value(list_result.json(), "-1.kubernetes.metadata.name") self.alb_namespace = self.alb.get_value( list_result.json(), "-1.kubernetes.metadata.namespace") self.tcpportname = "{}-999".format(self.alb_name) self.httpportname = "{}-777-http".format(self.alb_name) self.rulename = "rule-{}".format(self.httpportname) self.newapp = Newapplication() self.teardown_class(self) def teardown_class(self): self.alb.delete_rule(self.alb_namespace, self.rulename) self.alb.delete_frontend(self.alb_namespace, self.tcpportname) self.alb.delete_frontend(self.alb_namespace, self.httpportname) self.alb.delete_alb(self.k8s_namespace, self.alb2_name) def test_tcp(self): ''' 创建tcp端口-获取tcp端口详情-设置默认内部路由-获取应用访问地址-获取端口列表-删除tcp端口 :return: ''' crd_result = self.alb.get_alb_crd() assert crd_result.status_code == 200, "获取alb2的crd 失败:{}".format( crd_result.text) if len(crd_result.json()) == 0: return False, "集群不支持alb2,请先部署alb2" result = {"flag": True} # create tcp frontend tcp_result = self.alb.create_frontend( "./test_data/alb2/frontend-tcp.json", { "$alb_name": self.alb_name, "$alb-namespace": self.alb_namespace }) assert tcp_result.status_code == 201, "创建tcp监听端口失败:{}".format( tcp_result.text) # get frontend detail frontend_result = self.alb.get_detail_frontend(self.alb_namespace, self.tcpportname) result = self.alb.update_result(result, frontend_result.status_code == 200, frontend_result.text) version = self.alb.get_value(frontend_result.json(), 'kubernetes.metadata.resourceVersion') # update tcp frontend update_frontend_result = self.alb.update_frontend( self.alb_namespace, self.tcpportname, "./test_data/alb2/update-frontend-tcp.json", { "$alb_name": self.alb_name, "$alb-namespace": self.alb_namespace, "$resourceVersion": version }) assert update_frontend_result.status_code == 204, "设置tcp默认内部路由失败:{}".format( update_frontend_result.text) # get app address address_result = self.newapp.get_newapp_address( self.k8s_namespace, self.newapp.global_info['$GLOBAL_APP_NAME']) result = self.alb.update_result( result, self.tcpportname in address_result.text, "设置tcp默认内部路由,获取应用地址失败 {}".format(address_result.text)) # list frontend frontend_result = self.alb.list_frontend(self.alb_namespace, self.alb_name) result = self.alb.update_result(result, frontend_result.status_code == 200, frontend_result.text) result = self.alb.update_result(result, len(frontend_result.json()) > 0, frontend_result.text) # delete tcp frontend delete_result = self.alb.delete_frontend(self.alb_namespace, self.tcpportname) assert delete_result.status_code == 204, "删除tcp端口失败:{}".format( delete_result.text) assert self.alb.check_exists(self.alb.common_frontend_url( self.alb_namespace, self.tcpportname), 404, params={}) def test_http(self): ''' 创建http端口-创建规则-获取应用访问地址-获取规则详情-更新规则-获取规则列表-删除规则-删除http端口 :return: ''' crd_result = self.alb.get_alb_crd() assert crd_result.status_code == 200, "获取alb2的crd 失败:{}".format( crd_result.text) if len(crd_result.json()) == 0: return False, "集群不支持alb2,请先部署alb2" result = {"flag": True} # create http frontend http_result = self.alb.create_frontend( "./test_data/alb2/frontend-http.json", { "$alb_name": self.alb_name, "$alb-namespace": self.alb_namespace }) assert http_result.status_code == 201, "创建http监听端口失败:{}".format( http_result.text) # create rule create_rule_result = self.alb.create_rule( "./test_data/alb2/rule.json", { "$rule_name": self.rulename, "$alb_name": self.alb_name, "$alb-namespace": self.alb_namespace }) assert create_rule_result.status_code == 201, "创建规则失败:{}".format( create_rule_result.text) # get app address address_result = self.newapp.get_newapp_address( self.k8s_namespace, self.newapp.global_info['$GLOBAL_APP_NAME']) result = self.alb.update_result(result, address_result.status_code == 200, address_result.text) result = self.alb.update_result( result, self.rulename in address_result.text, "添加http规则后,获取应用地址失败 {}".format(address_result.text)) # detail rule detail_rule_result = self.alb.detail_rule(self.alb_namespace, self.rulename) assert detail_rule_result.status_code == 200, "获取规则详情失败 {}".format( detail_rule_result.text) version = self.alb.get_value(detail_rule_result.json(), 'kubernetes.metadata.resourceVersion') # update rule update_rule_result = self.alb.update_rule( self.alb_namespace, self.rulename, "./test_data/alb2/update-rule.json", { "$rule_name": self.rulename, "$alb_name": self.alb_name, "$alb-namespace": self.alb_namespace, "$resourceVersion": version }) assert update_rule_result.status_code == 204, "更新规则失败:{}".format( update_rule_result.text) # list rule list_rule_result = self.alb.list_rule(self.alb_namespace, self.alb_name, self.httpportname) result = self.alb.update_result(result, list_rule_result.status_code == 200, list_rule_result.text) result = self.alb.update_result(result, len(list_rule_result.json()) > 0, list_rule_result.text) # delete rule delete_rule_result = self.alb.delete_rule(self.alb_namespace, self.rulename) assert delete_rule_result.status_code == 204, "删除规则失败:{}".format( delete_rule_result.text) assert self.alb.check_exists(self.alb.common_rule_url( self.alb_namespace, self.rulename), 404, params={}) # delete http frontend delete_result = self.alb.delete_frontend(self.alb_namespace, self.httpportname) assert delete_result.status_code == 204, "删除http端口失败:{}".format( delete_result.text) assert self.alb.check_exists(self.alb.common_frontend_url( self.alb_namespace, self.httpportname), 404, params={}) assert result['flag'], result def test_alb2(self): ''' 创建负载均衡-负载均衡列表-更新域名后缀-获取负载均衡详情-删除负载均衡 :return: ''' crd_result = self.alb.get_alb_crd() assert crd_result.status_code == 200, "获取alb2的crd 失败:{}".format( crd_result.text) if len(crd_result.json()) == 0: return False, "集群不支持alb2,请先部署alb2" result = {"flag": True} # create alb masterip = self.alb.global_info['$MASTERIPS'].split(",")[0] createalb_result = self.alb.create_alb("./test_data/alb2/alb2.json", { "$alb_name": self.alb2_name, "$address": masterip }) assert createalb_result.status_code == 201, "创建负载均衡失败:{}".format( createalb_result.text) # list alb list_result = self.alb.list_alb() result = self.alb.update_result(result, list_result.status_code == 200, list_result.text) result = self.alb.update_result(result, self.alb2_name in list_result.text, "负载均衡列表:新建负载均衡不在列表中") # detail alb detail_result = self.alb.get_alb_detail(self.k8s_namespace, self.alb2_name) result = self.alb.update_result(result, detail_result.status_code == 200, detail_result.text) version = self.alb.get_value(detail_result.json(), 'kubernetes.metadata.resourceVersion') # update alb update_result = self.alb.update_alb( self.k8s_namespace, self.alb2_name, "./test_data/alb2/update-alb2.json", { "$alb_name": self.alb2_name, "$address": masterip, "$resourceVersion": version }) assert update_result.status_code == 204, "更新域名后缀失败:{}".format( update_result.text) # detail alb detail_result = self.alb.get_alb_detail(self.k8s_namespace, self.alb2_name) result = self.alb.update_result(result, detail_result.status_code == 200, detail_result.text) result = self.alb.update_result( result, self.alb.get_value(detail_result.json(), 'kubernetes.spec.address') == masterip, detail_result.text) result = self.alb.update_result( result, len( self.alb.get_value(detail_result.json(), 'kubernetes.spec.domains')) == 2, detail_result.text) # delete alb delete_result = self.alb.delete_alb(self.k8s_namespace, self.alb2_name) assert delete_result.status_code == 204, "删除负载均衡失败:{}".format( delete_result.text) assert self.alb.check_exists(self.alb.get_common_alb_url( self.k8s_namespace, self.alb2_name), 404, params={}) assert result['flag'], result
class SetUp(AlaudaRequest): """ 测试前先获取global信息已经新建一些测试的必须资源 """ def __init__(self): super(SetUp, self).__init__() global global_app_name global_app_name = "alauda-global-app" self.common = { "$NAMESPACE": settings.ACCOUNT, "$PASSWORD": settings.PASSWORD, "$SVN_CREDENTIAL": settings.SVN_CREDENTIAL, "$SVN_PASSWORD": settings.SVN_PASSWORD, "$SVN_USERNAME": settings.SVN_USERNAME, "$SVN_REPO": settings.SVN_REPO, "$REGISTRY": settings.REGISTRY_NAME, "$REPO_NAME": settings.REPO_NAME, "$TARGET_REPO_NAME": settings.TARGET_REPO_NAME, "$SPACE_NAME": settings.SPACE_NAME, "$REGION_NAME": settings.REGION_NAME, "$REG_CREDENTIAL": settings.REGISTRY_CREDENTIAL, "$K8S_NAMESPACE": settings.K8S_NAMESPACE, "$IMAGE": settings.IMAGE, "$PROJECT_NAME": settings.PROJECT_NAME, "$JENKINS_ENDPOINT": settings.JENKINS_ENDPOINT, "$JENKINS_USER": settings.JENKINS_USER, "$JENKINS_TOKEN": settings.JENKINS_TOKEN, "$GIT_REPO": settings.GIT_REPO, "$GIT_CREDENTIAL": settings.GIT_CREDENTIAL, "$GIT_USERNAME": settings.GIT_USERNAME, "$GIT_PASSWORD": settings.GIT_PASSWORD, "$SONAR_ENDPOINT": settings.SONAR_ENDPOINT, "$SONAR_TOKEN": settings.SONAR_TOKEN, "$MIDDLEWARE_REGISTRY": settings.MIDDLEWARE_REGISTRY, "$GIT_URL": settings.GIT_URL, "$GIT_PATH": settings.GIT_PATH, "$GIT_BRANCH": settings.GIT_BRANCH, "$GIT_USER": settings.GIT_USER, "$GIT_PWD": settings.GIT_PWD, "$OAUTH2_CLIENTSECRET": settings.OAUTH2_CLIENTSECRET, "$OAUTH2_CLIENTID": settings.OAUTH2_CLIENTID, "$DOCKER_AUTH": settings.DOCKER_AUTH, "$DOCKER_USERNAME": settings.DOCKER_USERNAME, "$DOCKER_PASSWORD": settings.DOCKER_PASSWORD, "$DOCKER_EMAIL": settings.DOCKER_EMAIL, "$VM_USERNAME": settings.VM_USERNAME, "$VM_PASSWORD": settings.VM_PASSWORD } self.master_ip = "" if not settings.VM_IPS else settings.VM_IPS[0] self.input_file(self.common) self.cluster_client = Cluster() self.deploy_region() self.get_region_data() self.deploy_feature() self.get_registry_uuid() self.get_master_ips() self.get_slave_ips() self.input_file(self.common) self.namespace_client = Namespace() self.project_client = Project() self.image_client = Image() self.newapp = Newapplication() self.prepare() self.create_global_app() self.input_file(self.common) def deploy_region(self): if self.cluster_client.get_region_info( settings.REGION_NAME).status_code == 200: return self.common.update({"CREATE_REGION": True}) if settings.ENV != "customer": ret_create = create_instance(2) assert ret_create["success"], ret_create["message"] global instances_id instances_id = ret_create['instances_id'] # instances_id = ['ins-2gecqljd', 'ins-bie1y8ax'] ret_get = get_instance(instances_id) assert ret_get["success"], ret_get["message"] private_ips = ret_get['private_ips'] public_ips = ret_get['public_ips'] elif settings.VM_IPS: private_ips = public_ips = settings.VM_IPS else: assert False, "客户环境没有集群且没有提供机器,无法测试" self.master_ip = public_ips[0] self.cluster_client.restart_sshd(self.master_ip) for public_ip in public_ips: self.cluster_client.excute_script("hostname node$RANDOM", public_ip) get_script = self.cluster_client.generate_install_cmd( "test_data/cluster/two_node_cluster_cmd.json", { "$cluster_name": settings.REGION_NAME, "$master_ip": private_ips[0], "$slave_ip": private_ips[1] }) assert get_script.status_code == 200, "获取创建集群脚本失败:{}".format( get_script.text) cmd = "export LANG=zh_CN.utf8;export LC_ALL=zh_CN.utf8;{}".format( get_script.json()["commands"]["install"]) ret_excute = self.cluster_client.excute_script(cmd, self.master_ip) assert "Install successfully!" in ret_excute[1], "执行脚本失败:{}".format( ret_excute[1]) is_exist = self.cluster_client.check_value_in_response( "v1/regions/{}".format(self.cluster_client.account), settings.REGION_NAME, params={}) register_region_log = self.cluster_client.excute_script( "docker logs region-setup", self.master_ip) assert is_exist, "添加集群超时,注册集群日志:{}".format(register_region_log) ret_list = self.cluster_client.get_region_list() assert ret_list.status_code == 200, "获取集群列表失败:{}".format(ret_list.text) region_name_list = self.cluster_client.get_value_list( ret_list.json(), "name") assert settings.REGION_NAME in region_name_list, "新建集群不在集群列表内" def deploy_feature(self): # 如果集群没部署了nevermore 再部署 ret_result = self.cluster_client.check_feature_status( settings.REGION_NAME, type="log") if not ret_result['success']: # 获取下namespace 不然安装不了nevermore 会出错default namespace找不到 self.send(method="GET", path="v2/kubernetes/clusters/{}/namespaces".format( self.region_id), params={}) sleep(5) self.cluster_client.check_exists( "v2/kubernetes/clusters/{}/namespaces/{}".format( self.region_id, "default"), 200, params={}) # 部署完第一次安装会出错,需要等待一段时间才可以正常安装,但是不确定具体时间 ret_log1 = self.cluster_client.install_nevermore( settings.REGION_NAME, "test_data/cluster/install_nevermore.json") sleep(1) ret_log2 = self.cluster_client.install_nevermore( settings.REGION_NAME, "test_data/cluster/install_nevermore.json") assert ret_log1.status_code == 200 or ret_log2.status_code == 200, "安装nevermore失败:{}, {}".format( ret_log1.text, ret_log2.text) # 如果集群没部署了registry 再部署 ret_result = self.cluster_client.check_feature_status( settings.REGION_NAME, type="registry") if not ret_result['success']: ret_registry = self.cluster_client.install_registry( settings.REGION_NAME, settings.REGISTRY_NAME, "test_data/cluster/install_registry.json") assert ret_registry.status_code == 200, "安装registry失败:{}".format( ret_registry.text) ret_result = self.cluster_client.check_feature_status( settings.REGION_NAME) assert ret_result['success'], "特性安装失败:{}".format(ret_result) @retry() def get_region_data(self): """ :return: 给self.region_data赋值集群信息 给self.region_id赋值集群ID """ response = self.send(method="GET", path="/v2/regions/{}/{}/".format( self.account, self.region_name), params={}) assert response.status_code == 200, response.json() self.region_data = response.json() self.region_id = self.region_data["id"] self.region_volume = self.network_modes = self.network_type = self.network_policy = "" if self.region_data.get("features").get("volume") is not None: self.region_volume = ",".join( self.region_data.get("features").get("volume").get("features")) if self.region_data.get('features').get('network_modes') is not None: self.network_modes = ",".join( self.region_data.get('features').get('network_modes').get( 'features')) if self.region_data.get('attr').get('kubernetes').get( 'cni') is not None: self.network_type = self.region_data.get('attr').get( 'kubernetes').get('cni').get('type') self.network_policy = self.region_data.get('attr').get( 'kubernetes').get('cni').get('network_policy') self.common.update({"$REGION_ID": self.region_id}) self.common.update({"$REGION_VOLUME": self.region_volume}) self.common.update({"NETWORK_MODES": self.network_modes}) self.common.update({"$NETWORK_TYPE": self.network_type}) self.common.update({"$NETWORK_POLICY": self.network_policy}) @retry() def get_load_balance_info(self): """ :return:获取haproxy的ip和name """ response = self.send( method='GET', path='/v1/load_balancers/{}?region_name={}'.format( self.account, self.region_name)) assert response.status_code == 200, response.text contents = response.json() assert len(contents) > 0, "get_load_balance_info is 空列表" for content in reversed(contents): if content['type'] == "nginx" or content["type"] == "haproxy": self.common.update({ "$HAPROXY_NAME": content['name'], "$HAPROXY_IP": content['address'] }) break @retry() def get_registry_uuid(self): response = self.send(method='get', path="/v1/registries/{}/".format(self.account)) assert response.status_code == 200, response.text private = [] contents = response.json() for index, content in enumerate(contents): private_dict = {} if content['name'] == settings.REGISTRY_NAME: private_dict['name'] = content['name'] private_dict['uuid'] = content['uuid'] private_dict['endpoint'] = content['endpoint'] private.append(private_dict) self.common.update({"PRIVATE_REGISTRY": private}) @retry() def get_slave_ips(self): response = self.send(method='GET', path='/v2/regions/{}/{}/nodes'.format( self.account, self.region_name), params={}) assert response.status_code == 200, response.text contents = response.json() slaveips = [] for content in contents.get("items"): if "node-role.kubernetes.io/node" in Common.get_value( content, "metadata.labels"): slaveips.append( Common.get_uuid_accord_name( self, Common.get_value(content, "status.addresses"), {'type': 'InternalIP'}, "address")) self.common.update({"$SLAVEIPS": ','.join(slaveips)}) @retry() def get_master_ips(self): response = self.send(method='GET', path='/v2/regions/{}/{}/nodes'.format( self.account, self.region_name), params={}) assert response.status_code == 200, response.text contents = response.json() masterips = [] for content in contents.get("items"): if "node-role.kubernetes.io/master" in Common.get_value( content, "metadata.labels"): masterips.append( Common.get_uuid_accord_name( self, Common.get_value(content, "status.addresses"), {'type': 'InternalIP'}, "address")) self.common.update({"$MASTERIPS": ','.join(masterips)}) def input_file(self, content): """ 将global数据写入临时文件 :param content: str :return: """ file_path = self.global_info_path.split('/')[-2] if not os.path.exists(file_path): os.makedirs(file_path) with open(self.global_info_path, 'w+') as write: write.write(json.dumps(content, indent=2)) def prepare(self): # create project self.project_client.delete_project(settings.PROJECT_NAME) response = self.project_client.get_project(settings.PROJECT_NAME) if response.status_code != 200: response = self.project_client.create_project( './test_data/project/project.yml', {"$project": settings.PROJECT_NAME}) assert response.status_code == 201, "prepare data failed: create project failed {}".format( response.text) self.common.update({ "CREATE_PROJECT": True, "$PROJECT_UUID": response.json()['uuid'] }) else: self.common.update({"$PROJECT_UUID": response.json()['uuid']}) self.namespace_client.delete_general_namespaces(settings.K8S_NAMESPACE) self.namespace_client.check_exists( self.namespace_client.get_namespace_url(settings.K8S_NAMESPACE), 404) response = self.namespace_client.get_namespaces(settings.K8S_NAMESPACE) if response.status_code != 200: response = self.namespace_client.create_general_namespaces( './test_data/namespace/newnamespace.yml', {'$K8S_NAMESPACE': settings.K8S_NAMESPACE}) assert response.status_code == 201, "prepare data failed: create namespace failed {}".format( response.text) self.common.update({"CREATE_NAMESPACE": True}) response = self.namespace_client.get_namespaces(settings.K8S_NAMESPACE) assert response.status_code == 200, "prepare data failed: get namespace detail failed {}".format( response.text) namespace_uuid = response.json()["kubernetes"]["metadata"]["uid"] self.common.update({"$K8S_NS_UUID": namespace_uuid}) # delete repo self.image_client.delete_repo(settings.REPO_NAME) self.image_client.delete_repo(settings.REPO_NAME, reg_project_name=settings.REPO_PROJECT) self.image_client.delete_reg_project(settings.REPO_PROJECT) # create Repo create_repo_ret = self.image_client.create_repo( './test_data/image/create_repo.yaml', {"$REPO_IMAGE": settings.REPO_NAME}) assert create_repo_ret.status_code == 201, "创建目标镜像仓库失败" create_reg_ret = self.image_client.create_reg_project( './test_data/image/create_reg_project.yaml', {"$REG_PROJECT_NAME": settings.REPO_PROJECT}) assert create_reg_ret.status_code == 201, "创建镜像项目操作失败" create_repo_ret = self.image_client.create_repo( './test_data/image/create_repo.yaml', {"$REPO_IMAGE": settings.REPO_NAME}, reg_project_name=settings.REPO_PROJECT) assert create_repo_ret.status_code == 201, "创建项目下目标镜像仓库失败" if self.master_ip: # login private registry cmd = "docker login {} -u {} -p {}".format( self.image_client.get_value(self.common, "PRIVATE_REGISTRY.0.endpoint"), settings.ACCOUNT, settings.PASSWORD) ret_excute = self.image_client.excute_script(cmd, self.master_ip) assert "Login Succeeded" in ret_excute[1], "执行脚本失败:{}".format( ret_excute[1]) # push share repo dest_repo = self.image_client.get_value( self.common, "PRIVATE_REGISTRY.0.endpoint") + '/' + settings.REPO_NAME cmd = "docker pull {source_image};docker tag {source_image} {dest_image};docker push {dest_image}:latest".format( source_image=settings.IMAGE, dest_image=dest_repo) ret_excute = self.image_client.excute_script(cmd, self.master_ip) assert 0 == ret_excute[0], "执行脚本失败:{}".format(ret_excute[1]) # push project repo dest_repo = self.image_client.get_value( self.common, "PRIVATE_REGISTRY.0.endpoint" ) + '/' + settings.REPO_PROJECT + '/' + settings.REPO_NAME cmd = "docker pull {source_image};docker tag {source_image} {dest_image};docker push {dest_image}:latest".format( source_image=settings.IMAGE, dest_image=dest_repo) ret_excute = self.image_client.excute_script(cmd, self.master_ip) assert 0 == ret_excute[0], "执行脚本失败:{}".format(ret_excute[1]) else: assert False, "没有获取到集群主机登录方式" def create_global_app(self): self.newapp.delete_newapp(settings.K8S_NAMESPACE, global_app_name) self.newapp.check_exists( self.newapp.get_newapp_common_url(settings.K8S_NAMESPACE, global_app_name), 404) create_result = self.newapp.create_newapp( './test_data/newapp/newapp.json', {'$newapp_name': global_app_name}) assert create_result.status_code == 201, "新版应用创建失败 {}".format( create_result.text) app_status = self.newapp.get_newapp_status(settings.K8S_NAMESPACE, global_app_name, 'Running') assert app_status, "创建应用后,验证应用状态出错:app: {} is not running".format( global_app_name) app_uuid = self.newapp.get_value(create_result.json(), '0.kubernetes.metadata.uid') self.common.update({"$GLOBAL_APP_NAME": global_app_name}) self.common.update({"$GLOBAL_APP_ID": app_uuid})