Skip to content

Commit

Permalink
Merge f618046 into b29faa4
Browse files Browse the repository at this point in the history
  • Loading branch information
danfengliu committed Aug 9, 2020
2 parents b29faa4 + f618046 commit e6a8661
Show file tree
Hide file tree
Showing 27 changed files with 123 additions and 168 deletions.
2 changes: 0 additions & 2 deletions tests/apitests/python/library/Harbor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
import site
reload(site)
import project
import label
import registry
Expand Down
1 change: 0 additions & 1 deletion tests/apitests/python/library/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def get_charts(self, repository, expect_status_code = 200, **kwargs):

def chart_should_exist(self, repository, chart_name, **kwargs):
charts_data = self.get_charts(repository, **kwargs)
print "charts_data:", charts_data
for chart in charts_data:
if chart.name == chart_name:
return True
Expand Down
38 changes: 23 additions & 15 deletions tests/apitests/python/library/docker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def docker_login(self, registry, username, password, expected_error_message = No
expected_error_message = None
try:
self.DCLIENT.login(registry = registry, username=username, password=password)
except docker.errors.APIError, err:
except docker.errors.APIError as err:
if expected_error_message is not None:
print "docker login error:", str(err)
print( "docker login error:", str(err))
if str(err).lower().find(expected_error_message.lower()) < 0:
raise Exception(r"Docker login: Return message {} is not as expected {}".format(str(err), expected_error_message))
else:
Expand All @@ -37,19 +37,20 @@ def docker_image_pull(self, image, tag = None, expected_error_message = None):
caught_err = False
ret = ""
try:
ret = base._get_string_from_unicode(self.DCLIENT.pull(r'{}:{}'.format(image, _tag)))
except Exception, err:
self.DCLIENT.pull(r'{}:{}'.format(image, _tag))
return ret
except Exception as err:
caught_err = True
if expected_error_message is not None:
print "docker image pull error:", str(err)
print( "docker image pull error:", str(err))
if str(err).lower().find(expected_error_message.lower()) < 0:
raise Exception(r"Pull image: Return message {} is not as expected {}".format(str(err), expected_error_message))
else:
raise Exception(r" Docker pull image {} failed, error is [{}]".format (image, err.message))
if caught_err == False:
if expected_error_message is not None:
if str(ret).lower().find(expected_error_message.lower()) < 0:
raise Exception(r" Failed to catch error [{}] when pull image {}".format (expected_error_message, image))
raise Exception(r" Failed to catch error [{}] when pull image {}, return message: {}".format (expected_error_message, image, str(ret)))
else:
if str(ret).lower().find("error".lower()) >= 0:
raise Exception(r" It's was not suppose to catch error when pull image {}, return message is [{}]".format (image, ret))
Expand All @@ -61,7 +62,7 @@ def docker_image_tag(self, image, harbor_registry, tag = None):
try:
self.DCLIENT.tag(image, harbor_registry, _tag, force=True)
return harbor_registry, _tag
except docker.errors.APIError, e:
except docker.errors.APIError as e:
raise Exception(r" Docker tag image {} failed, error is [{}]".format (image, e.message))

def docker_image_push(self, harbor_registry, tag, expected_error_message = None):
Expand All @@ -70,22 +71,25 @@ def docker_image_push(self, harbor_registry, tag, expected_error_message = None)
if expected_error_message is "":
expected_error_message = None
try:
ret = base._get_string_from_unicode(self.DCLIENT.push(harbor_registry, tag, stream=True))
except Exception, err:
self.DCLIENT.push(harbor_registry, tag)
return ret
except Exception as err:
caught_err = True
if expected_error_message is not None:
print "docker image push error:", str(err)
print( "docker image push error:", str(err))
if str(err).lower().find(expected_error_message.lower()) < 0:
raise Exception(r"Push image: Return message {} is not as expected {}".format(str(err), expected_error_message))
else:
raise Exception(r" Docker push image {} failed, error is [{}]".format (harbor_registry, err.message))
if caught_err == False:
if expected_error_message is not None:
if str(ret).lower().find(expected_error_message.lower()) < 0:
raise Exception(r" Failed to catch error [{}] when push image {}".format (expected_error_message, harbor_registry))
raise Exception(r" Failed to catch error [{}] when push image {}, return message: {}".
format (expected_error_message, harbor_registry, str(ret)))
else:
if str(ret).lower().find("errorDetail".lower()) >= 0:
raise Exception(r" It's was not suppose to catch error when push image {}, return message is [{}]".format (harbor_registry, ret))
raise Exception(r" It's was not suppose to catch error when push image {}, return message is [{}]".
format (harbor_registry, ret))

def docker_image_build(self, harbor_registry, tags=None, size=1, expected_error_message = None):
caught_err = False
Expand All @@ -111,18 +115,22 @@ def docker_image_build(self, harbor_registry, tags=None, size=1, expected_error_
print("build image %s with size %d" % (repo, size))
self.DCLIENT.remove_image(repo)
self.DCLIENT.remove_container(c)
except Exception, err:
self.DCLIENT.pull(repo)
image = self.DCLIENT2.images.get(repo)
return repo, image.id
except Exception as err:
caught_err = True
if expected_error_message is not None:
print "docker image build error:", str(err)
print( "docker image build error:", str(err))
if str(err).lower().find(expected_error_message.lower()) < 0:
raise Exception(r"Push image: Return message {} is not as expected {}".format(str(err), expected_error_message))
else:
raise Exception(r" Docker build image {} failed, error is [{}]".format (harbor_registry, err.message))
if caught_err == False:
if expected_error_message is not None:
if str(ret).lower().find(expected_error_message.lower()) < 0:
raise Exception(r" Failed to catch error [{}] when push image {}".format (expected_error_message, harbor_registry))
raise Exception(r" Failed to catch error [{}] when build image {}, return message: {}".
format (expected_error_message, harbor_registry, str(ret)))
else:
if str(ret).lower().find("errorDetail".lower()) >= 0:
raise Exception(r" It's was not suppose to catch error when push image {}, return message is [{}]".format (harbor_registry, ret))
7 changes: 5 additions & 2 deletions tests/apitests/python/library/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ def projects_should_exist(self, params, expected_count = None, expected_project_

def check_project_name_exist(self, name=None, **kwargs):
client = self._get_client(**kwargs)
_, status_code, _ = client.projects_head_with_http_info(name)
try:
_, status_code, _ = client.projects_head_with_http_info(name)
except ApiException as e:
status_code = -1
return {
200: True,
404: False,
}.get(status_code,'error')
}.get(status_code,False)

def get_project(self, project_id, expect_status_code = 200, expect_response_body = None, **kwargs):
client = self._get_client(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tests/apitests/python/library/replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def check_replication_rule_should_exist(self, check_rule_id, expect_rule_name, e
if str(rule_data.name) != str(expect_rule_name):
raise Exception(r"Check replication rule failed, expect <{}> actual <{}>.".format(expect_rule_name, str(rule_data.name)))
else:
print r"Check Replication rule passed, rule name <{}>.".format(str(rule_data.name))
print(r"Check Replication rule passed, rule name <{}>.".format(str(rule_data.name)))
#get_trigger = str(rule_data.trigger.kind)
#if expect_trigger is not None and get_trigger == str(expect_trigger):
# print r"Check Replication rule trigger passed, trigger name <{}>.".format(get_trigger)
Expand Down
7 changes: 4 additions & 3 deletions tests/apitests/python/library/sign.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import subprocess
from testutils import notary_url

def sign_image(registry_ip, project_name, image, tag):
try:
ret = subprocess.check_output(["./tests/apitests/python/sign_image.sh", registry_ip, project_name, image, tag], shell=False)
print "sign_image return: ", ret
except subprocess.CalledProcessError, exc:
ret = subprocess.check_output(["./tests/apitests/python/sign_image.sh", registry_ip, project_name, image, tag, notary_url], shell=False)
print("sign_image return: ", ret)
except subprocess.CalledProcessError as exc:
raise Exception("Failed to sign image error is {} {}.".format(exc.returncode, exc.output))

2 changes: 0 additions & 2 deletions tests/apitests/python/library/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def get_user(self, user_id, **kwargs):
client = self._get_client(**kwargs)
data, status_code, _ = client.users_user_id_get_with_http_info(user_id)
base._assert_status_code(200, status_code)
print "data in lib:", data
return data


Expand Down Expand Up @@ -80,7 +79,6 @@ def update_user_profile(self, user_id, email=None, realname=None, comment=None,
def update_user_role_as_sysadmin(self, user_id, IsAdmin, **kwargs):
client = self._get_client(**kwargs)
has_admin_role = swagger_client.HasAdminRole(IsAdmin)
print "has_admin_role:", has_admin_role
_, status_code, _ = client.users_user_id_sysadmin_put_with_http_info(user_id, has_admin_role)
base._assert_status_code(200, status_code)
return user_id
3 changes: 2 additions & 1 deletion tests/apitests/python/sign_image.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/sh
IP=$1
NOTARY_URL=$5
PASSHRASE='Harbor12345'

echo $IP

export DOCKER_CONTENT_TRUST=1
export DOCKER_CONTENT_TRUST_SERVER=https://$IP:4443
export DOCKER_CONTENT_TRUST_SERVER=$NOTARY_URL

export NOTARY_ROOT_PASSPHRASE=$PASSHRASE
export NOTARY_TARGETS_PASSPHRASE=$PASSHRASE
Expand Down
8 changes: 2 additions & 6 deletions tests/apitests/python/test_add_member_to_private_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
class TestProjects(unittest.TestCase):
"""UserGroup unit test stubs"""
def setUp(self):
project = Project()
self.project= project

user = User()
self.user= user

self.project = Project()
self.user= User()

def tearDown(self):
pass
Expand Down
18 changes: 5 additions & 13 deletions tests/apitests/python/test_add_replication_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@
class TestProjects(unittest.TestCase):
@classmethod
def setUp(self):
project = Project()
self.project= project

user = User()
self.user= user

replication = Replication()
self.replication= replication

registry = Registry()
self.registry= registry
self.project = Project()
self.user = User()
self.replication = Replication()
self.registry = Registry()

@classmethod
def tearDown(self):
print "Case completed"
print("Case completed")

@unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
def test_ClearData(self):
Expand Down Expand Up @@ -76,7 +69,6 @@ def testAddReplicationRule(self):

#3. Create a new registry
TestProjects.registry_id, _ = self.registry.create_registry("https://" + harbor_server,**ADMIN_CLIENT)
print "TestProjects.registry_id:", TestProjects.registry_id

#4. Create a new rule for this registry;
TestProjects.rule_id, rule_name = self.replication.create_replication_policy(dest_registry=swagger_client.Registry(id=int(TestProjects.registry_id)), **ADMIN_CLIENT)
Expand Down
18 changes: 6 additions & 12 deletions tests/apitests/python/test_add_sys_label_to_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@
class TestProjects(unittest.TestCase):
@classmethod
def setUp(self):
project = Project()
self.project= project

user = User()
self.user= user

repo = Repository()
self.repo= repo

label = Label()
self.label= label
self.project = Project()
self.user = User()
self.artifact = Artifact()
self.repo = Repository()
self.label = Label()

@classmethod
def tearDown(self):
print "Case completed"
print("Case completed")

@unittest.skipIf(TEARDOWN == False, "Test data won't be erased.")
def test_ClearData(self):
Expand Down

0 comments on commit e6a8661

Please sign in to comment.