2020-03-28 04:29:28 +01:00
from __future__ import absolute_import
import unittest
2020-11-04 03:13:12 +01:00
from testutils import harbor_server , suppress_urllib3_warning
2020-03-28 04:29:28 +01:00
from testutils import admin_user
from testutils import admin_pwd
from testutils import TEARDOWN
from testutils import ADMIN_CLIENT
from library . system import System
from library . project import Project
from library . user import User
from library . repository import Repository
2020-12-04 11:28:29 +01:00
from library . repository import push_self_build_image_to_project
2020-03-28 04:29:28 +01:00
from library . artifact import Artifact
from library . scanner import Scanner
2020-08-07 08:56:18 +02:00
from library . docker_api import list_image_tags
from library . docker_api import list_repositories
2020-03-28 04:29:28 +01:00
import os
import library . base
import json
class TestProjects ( unittest . TestCase ) :
2020-11-04 03:13:12 +01:00
@suppress_urllib3_warning
2020-03-28 04:29:28 +01:00
def setUp ( self ) :
self . system = System ( )
self . project = Project ( )
self . user = User ( )
self . artifact = Artifact ( )
self . repo = Repository ( )
self . repo_name = " hello-world "
@unittest.skipIf ( TEARDOWN == False , " Test data won ' t be erased. " )
2020-11-04 03:13:12 +01:00
def tearDown ( self ) :
2020-03-28 04:29:28 +01:00
#1. Delete Alice's repository and Luca's repository;
self . repo . delete_repoitory ( TestProjects . project_Alice_name , TestProjects . repo_a . split ( ' / ' ) [ 1 ] , * * ADMIN_CLIENT )
self . repo . delete_repoitory ( TestProjects . project_Alice_name , TestProjects . repo_b . split ( ' / ' ) [ 1 ] , * * ADMIN_CLIENT )
self . repo . delete_repoitory ( TestProjects . project_Alice_name , TestProjects . repo_c . split ( ' / ' ) [ 1 ] , * * ADMIN_CLIENT )
#2. Delete Alice's project and Luca's project;
self . project . delete_project ( TestProjects . project_Alice_id , * * ADMIN_CLIENT )
#3. Delete user Alice and Luca.
self . user . delete_user ( TestProjects . user_Alice_id , * * ADMIN_CLIENT )
def testRegistryAPI ( self ) :
"""
Test case :
Catalog API to list all repositories by system admin
Test step and expected result : G
1. Create user Alice ;
2. Create 1 new private projects project_Alice ;
3. Push 3 images to project_Alice and Add 3 tags to the 3 rd image
4. Call the image_list_tag API
5. Call the catalog API using admin account without pagination , can get all 3 images
5.1 Call the catalog API using admin account with pagination n = 1 , page = 2 , twice to get all 3 images .
5.2 Call the catalog API using Alice account , no repos should be found .
Tear down :
1. Delete Alice ' s repository;
2. Delete Alice ' s project;
3. Delete user Alice .
"""
url = ADMIN_CLIENT [ " endpoint " ]
user_common_password = " Aa123456 "
#1. Create user Alice and Luca;
TestProjects . user_Alice_id , user_Alice_name = self . user . create_user ( user_password = user_common_password , * * ADMIN_CLIENT )
USER_ALICE_CLIENT = dict ( endpoint = url , username = user_Alice_name , password = user_common_password )
#2. Create 2 new private projects project_Alice and project_Luca;
TestProjects . project_Alice_id , TestProjects . project_Alice_name = self . project . create_project ( metadata = { " public " : " false " } , * * USER_ALICE_CLIENT )
#3. Push 3 images to project_Alice and Add 3 tags to the 3rd image.
src_tag = " latest "
image_a = " busybox "
2020-12-04 11:28:29 +01:00
TestProjects . repo_a , tag_a = push_self_build_image_to_project ( TestProjects . project_Alice_name , harbor_server , user_Alice_name , user_common_password , image_a , src_tag )
2020-03-28 04:29:28 +01:00
image_b = " alpine "
2020-12-04 11:28:29 +01:00
TestProjects . repo_b , tag_b = push_self_build_image_to_project ( TestProjects . project_Alice_name , harbor_server , user_Alice_name , user_common_password , image_b , src_tag )
2020-03-28 04:29:28 +01:00
image_c = " hello-world "
2020-12-04 11:28:29 +01:00
TestProjects . repo_c , tag_c = push_self_build_image_to_project ( TestProjects . project_Alice_name , harbor_server , user_Alice_name , user_common_password , image_c , src_tag )
2020-03-28 04:29:28 +01:00
create_tags = [ " 1.0 " , " 2.0 " , " 3.0 " ]
for tag in create_tags :
self . artifact . create_tag ( TestProjects . project_Alice_name , self . repo_name , tag_c , tag , * * USER_ALICE_CLIENT )
#4. Call the image_list_tags API
2020-08-07 08:56:18 +02:00
tags = list_image_tags ( harbor_server , TestProjects . repo_c , user_Alice_name , user_common_password )
for tag in create_tags :
2020-03-28 04:29:28 +01:00
self . assertTrue ( tags . count ( tag ) > 0 , " Expect tag: %s is not listed " % tag )
2020-08-07 08:56:18 +02:00
page_tags = list_image_tags ( harbor_server , TestProjects . repo_c , user_Alice_name , user_common_password , len ( tags ) / 2 + 1 )
page_tags + = list_image_tags ( harbor_server , TestProjects . repo_c , user_Alice_name , user_common_password , len ( tags ) / 2 + 1 , tags [ int ( len ( tags ) / 2 ) ] )
for tag in create_tags :
2020-03-28 04:29:28 +01:00
self . assertTrue ( page_tags . count ( tag ) > 0 , " Expect tag: %s is not listed by the pagination query " % tag )
#5. Call the catalog API;
2020-08-07 08:56:18 +02:00
repos = list_repositories ( harbor_server , admin_user , admin_pwd )
self . assertTrue ( repos . count ( TestProjects . repo_a ) > 0 and repos . count ( TestProjects . repo_b ) > 0 and repos . count ( TestProjects . repo_c ) > 0 , " Expected repo not found " )
2020-03-28 04:29:28 +01:00
for repo in [ TestProjects . repo_a , TestProjects . repo_b , TestProjects . repo_c ] :
self . assertTrue ( repos . count ( repo ) > 0 , " Expected repo: %s is not listed " % repo )
2020-08-07 08:56:18 +02:00
page_repos = list_repositories ( harbor_server , admin_user , admin_pwd , len ( repos ) / 2 + 1 )
page_repos + = list_repositories ( harbor_server , admin_user , admin_pwd , len ( repos ) / 2 + 1 , repos [ int ( len ( repos ) / 2 ) ] )
2020-03-28 04:29:28 +01:00
for repo in [ TestProjects . repo_a , TestProjects . repo_b , TestProjects . repo_c ] :
self . assertTrue ( page_repos . count ( repo ) > 0 , " Expected repo: %s is not listed by the pagination query " % repo )
2020-08-07 08:56:18 +02:00
null_repos = list_repositories ( harbor_server , user_Alice_name , user_common_password )
2020-03-28 04:29:28 +01:00
self . assertEqual ( null_repos , " " )
if __name__ == ' __main__ ' :
unittest . main ( )