2018-08-01 00:38:23 +02:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import sys
|
2018-10-31 06:13:31 +01:00
|
|
|
|
2019-09-03 09:52:41 +02:00
|
|
|
sys.path.insert(0, os.environ["SWAGGER_CLIENT_PATH"])
|
2020-02-25 03:40:29 +01:00
|
|
|
import v2_swagger_client
|
2018-08-01 00:38:23 +02:00
|
|
|
from swagger_client.rest import ApiException
|
|
|
|
import swagger_client.models
|
|
|
|
from pprint import pprint
|
|
|
|
|
2018-11-20 07:24:13 +01:00
|
|
|
admin_user = "admin"
|
|
|
|
admin_pwd = "Harbor12345"
|
|
|
|
|
2018-10-31 06:13:31 +01:00
|
|
|
harbor_server = os.environ["HARBOR_HOST"]
|
2018-11-20 07:24:13 +01:00
|
|
|
#CLIENT=dict(endpoint="https://"+harbor_server+"/api")
|
2020-02-07 08:04:25 +01:00
|
|
|
ADMIN_CLIENT=dict(endpoint = os.environ.get("HARBOR_HOST_SCHEMA", "https")+ "://"+harbor_server+"/api/v2.0", username = admin_user, password = admin_pwd)
|
2020-03-31 06:35:03 +02:00
|
|
|
CHART_API_CLIENT=dict(endpoint = os.environ.get("HARBOR_HOST_SCHEMA", "https")+ "://"+harbor_server+"/api", username = admin_user, password = admin_pwd)
|
2018-11-01 11:26:04 +01:00
|
|
|
USER_ROLE=dict(admin=0,normal=1)
|
2020-02-20 16:20:34 +01:00
|
|
|
TEARDOWN = os.environ.get('TEARDOWN', 'true').lower() in ('true', 'yes')
|
2020-06-04 12:17:26 +02:00
|
|
|
notary_url = os.environ.get('NOTARY_URL', 'https://'+harbor_server+':4443')
|
2018-10-31 06:13:31 +01:00
|
|
|
|
2018-08-01 00:38:23 +02:00
|
|
|
def GetProductApi(username, password, harbor_server= os.environ["HARBOR_HOST"]):
|
2019-05-06 05:34:11 +02:00
|
|
|
|
2018-08-01 00:38:23 +02:00
|
|
|
cfg = swagger_client.Configuration()
|
2020-02-07 08:04:25 +01:00
|
|
|
cfg.host = "https://"+harbor_server+"/api/v2.0"
|
2018-08-01 00:38:23 +02:00
|
|
|
cfg.username = username
|
|
|
|
cfg.password = password
|
|
|
|
cfg.verify_ssl = False
|
|
|
|
cfg.debug = True
|
|
|
|
api_client = swagger_client.ApiClient(cfg)
|
|
|
|
api_instance = swagger_client.ProductsApi(api_client)
|
2018-11-29 11:27:53 +01:00
|
|
|
return api_instance
|
2020-02-25 03:40:29 +01:00
|
|
|
|
|
|
|
def GetRepositoryApi(username, password, harbor_server= os.environ["HARBOR_HOST"]):
|
|
|
|
|
|
|
|
cfg = v2_swagger_client.Configuration()
|
|
|
|
cfg.host = "https://"+harbor_server+"/api/v2.0"
|
|
|
|
cfg.username = username
|
|
|
|
cfg.password = password
|
|
|
|
cfg.verify_ssl = False
|
|
|
|
cfg.debug = True
|
|
|
|
api_client = v2_swagger_client.ApiClient(cfg)
|
|
|
|
api_instance = v2_swagger_client.RepositoryApi(api_client)
|
|
|
|
return api_instance
|
|
|
|
|
2018-11-29 11:27:53 +01:00
|
|
|
class TestResult(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.num_errors = 0
|
|
|
|
self.error_message = []
|
|
|
|
def add_test_result(self, error_message):
|
|
|
|
self.num_errors = self.num_errors + 1
|
|
|
|
self.error_message.append(error_message)
|
|
|
|
def get_final_result(self):
|
|
|
|
if self.num_errors > 0:
|
|
|
|
for each_err_msg in self.error_message:
|
2020-07-30 10:04:14 +02:00
|
|
|
print("Error message:", each_err_msg)
|
2018-11-29 11:27:53 +01:00
|
|
|
raise Exception(r"Test case failed with {} errors.".format(self.num_errors))
|
2020-03-12 04:50:30 +01:00
|
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def created_user(password):
|
|
|
|
from library.user import User
|
|
|
|
|
|
|
|
api = User()
|
|
|
|
|
|
|
|
user_id, user_name = api.create_user(user_password=password, **ADMIN_CLIENT)
|
|
|
|
try:
|
|
|
|
yield (user_id, user_name)
|
|
|
|
finally:
|
|
|
|
if TEARDOWN:
|
|
|
|
api.delete_user(user_id, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def created_project(name=None, metadata=None, user_id=None, member_role_id=None):
|
|
|
|
from library.project import Project
|
|
|
|
|
|
|
|
api = Project()
|
|
|
|
|
|
|
|
project_id, project_name = api.create_project(name=None, metadata=None, **ADMIN_CLIENT)
|
|
|
|
if user_id:
|
|
|
|
api.add_project_members(project_id, user_id, member_role_id=member_role_id, **ADMIN_CLIENT)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield (project_id, project_name)
|
|
|
|
finally:
|
|
|
|
if TEARDOWN:
|
|
|
|
api.delete_project(project_id, **ADMIN_CLIENT)
|