Commit 5edd408d authored by wangyan's avatar wangyan
Browse files

Add scripts to verify Harbor DB migration

parent 43774df6
*** settings ***
Library JSONLibrary
Resource ../../resources/Util.robot
*** Keywords ***
#for jsonpath refer to http://goessner.net/articles/JsonPath/ or https://nottyo.github.io/robotframework-jsonlibrary/JSONLibrary.html
${json}= Load Json From File testdata.json
Verify User
@{user}= Get Value From Json ${json} $.users..name
#verify user exist
Sign In Harbor ${HARBOR_URL} %{HARBOR_ADMIN} %{HARBOR_PASSWORD}
[Arguments] ${json}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
Switch To User Tag
:For ${user} In @{user}
\ Page Should Contain ${user}
@{user}= Get Value From Json ${json} $.users..name
:FOR ${user} IN @{user}
\ Page Should Contain ${user}
Logout Harbor
#verify user can login
:For ${user} In @{user}
\ Sign In Harbor ${HARBOR_URL} ${user} %{HARBOR_PASSWORD}
@{user}= Get Value From Json ${json} $.users..name
:FOR ${user} IN @{user}
\ Sign In Harbor ${HARBOR_URL} ${user} ${HARBOR_PASSWORD}
\ Logout Harbor
Close Browser
Verify Project
[Arguments] ${json}
@{project}= Get Value From Json ${json} $.projects.[*].name
Sign In Harbor ${HARBOR_URL} %{HARBOR_ADMIN} %{HARBOR_PASSWORD}
:For ${project} In @{project}
\ Page Should Contain ${project}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
:FOR ${project} IN @{project}
\ Page Should Contain ${project}
Close Browser
Verify Image Tag
[Arguments] ${json}
@{project}= Get Value From Json ${json} $.projects.[*].name
Sign In Harbor ${HARBOR_URL} %{HARBOR_ADMIN} %{HARBOR_PASSWORD}
:For ${project} In @{projects}
\ Go Into Project ${project}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
:FOR ${project} IN @{project}
\ Go Into Project ${project}
\ @{repo}= Get Value From Json ${json} $.projects[?(@name=${project})]..repo..name
\ @{tag}= Get Value From Json ${json} $.projects[?(@name=${project})]..repo..tag
\ :For ${repo} In @{repo}
\ \ Go Into Repo ${repo}
\ \ :For ${tag} In @{tag}
\ \ \ Page Should Contain ${tag}
\ \ \ Back To Projects
\ Loop Image Repo @{repo}
\ Back To Projects
Close Browser
Loop Image Repo
[Arguments] @{repo}
:For ${repo} In @{repo}
\ Page Should Contain ${repo}
Verify Member Exist
[Arguments] ${json}
@{project}= Get Value From Json ${json} $.projects.[*].name
Sign In Harbor ${HARBOR_URL} %{HARBOR_ADMIN} %{HAROBR_PASSWORD}
:For ${project} In @{project}
\ Go Into Project ${projet}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
:For ${project} In @{project}
\ Go Into Project ${project}
\ Switch To Member
\ @{members}= Get Value From Json ${json} $.projects[?(@name=${project})].member..name
\ :For ${member} In @{members}
\ \ Page Should Contain ${member}
\ Loop Member @{members}
\ Back To Projects
Close Browser
Loop Member
[Arguments] @{members}
:For ${member} In @{members}
\ Page Should Contain ${member}
Verify User System Admin Role
[Arguments] ${json}
@{user}= Get Value From Json ${json} $.admin..name
:For ${user} in @{user}
\ Sign In Harbor ${HARBOR_URL} ${user} %{HARBOR_PASSWORD}
Init Chrome Driver
:FOR ${user} IN @{user}
\ Sign In Harbor ${HARBOR_URL} ${user} ${HARBOR_PASSWORD}
\ Page Should Contain Administration
\ Logout Harbor
Close Browser
Verify System Label
[Arguments] ${json}
@{label}= Get Value From Json ${json} $..syslabel..name
Sign In Harbor ${HAROBR_URL} %{HAROBR_ADMIN} %{HAROBR_PASSWORD}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
Switch To Configure
Switch To System Labels
:For ${label} In @{label}
\ Page Should Contain ${label}
:For ${label} In @{label}
\ Page Should Contain ${label}
Close Browser
Verify Project Label
[Arguments] ${json}
@{project}= Get Value From Json ${json} $.peoject.[*].name
Sign In Harbor ${HAROBR_URL} %{HAROBR_ADMIN} %{HAROBR_PASSWORD}
:For ${project} In @{project}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
:For ${project} In @{project}
\ Go Into Project ${project}
\ Switch To Project Label
\ @{projectlabel}= Get Value From Json ${json} $.projects[?(@.name=${project})]..labels..name
\ :For ${label} In @{projectlabel}
\ \ Page Should Contain ${projectlabel}
\ :For ${label} In @{label}
\ \ Page Should Contain ${projectlabel}
\ Back To Projects
Close Browser
Verify Endpoint
[Arguments] ${json}
@{endpoint}= Get Value From Json ${json} $.endpoint..name
Sign In Harbor ${HAROBR_URL} %{HAROBR_ADMIN} %{HAROBR_PASSWORD}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
Switch To Registries
:For ${endpoint} In @{endpoint}
\ Page Should Contain ${endpoint}
:For ${endpoint} In @{endpoint}
\ Page Should Contain ${endpoint}
Close Browser
Verify Replicationrule
[Arguments] ${json}
@{replicationrule}= Get Value From Json ${json} $.replicationrule..name
Sign In Harbor ${HAROBR_URL} %{HAROBR_ADMIN} %{HAROBR_PASSWORD}
Switch To System Replication
:For ${replicationrule} In @{replicationrule}
\ Page Should Contain ${replicationrule}
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
Switch To Replication Manage
:For ${replicationrule} IN @{replicationrule}
\ Page Should Contain ${replicationrule}
Close Browser
Verify Project Setting
[Arguments] ${json}
@{projects}= Get Value From Json ${json} $.projects.[*].name
:For ${project} In @{projects}
\ ${public}= Get Value From Json ${json} $.projects[?(@.name=${projectname})].accesslevel
\ ${contenttrust}= Get Value From Json ${json} $.projects[?(@.name=${projectname})]..enable_content_trust
\ ${preventrunning}= Get Value From Json ${json} $.projects[?(@.name=${projectname})]..prevent_vulnerable_images_from_running
\ ${scanonpush}= Get Value From Json ${json} $.projects[?(@.name=${projectname})]..automatically_scan_images_on_push
\ Sign In Harbor ${HAROBR_URL} %{HAROBR_ADMIN} %{HAROBR_PASSWORD}
:For ${project} In @{Projects}
\ ${public}= Get Value From Json ${json} $.projects[?(@.name=${project})].accesslevel
\ ${contenttrust}= Get Value From Json ${json} $.projects[?(@.name=${project})]..enable_content_trust
\ ${preventrunning}= Get Value From Json ${json} $.projects[?(@.name=${project})]..prevent_vulnerable_images_from_running
\ ${scanonpush}= Get Value From Json ${json} $.projects[?(@.name=${project})]..automatically_scan_images_on_push
\ Init Chrome Driver
\ Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
\ Go Into Project ${project}
\ Goto Project Config
\ Run Keyword If ${public} == "public"
\ Checkbox Should Be Checked //clr-checkbox[@name='public']//label
\ Else
\ Checkbox Should Not Be Checked //clr-checkbox[@name='public']//label
\ Run Keyword If ${contenttrust} == "true"
\ Checkbox Should Be Checked //clr-checkbox[@name='content-trust']//label
\ Else
\ Checkbox Should Not Be Checked //clr-checkbox[@name='content-trust']//label
\ Run Keyword If ${preventrunning} == "true"
\ Checkbox Should Be Checked //clr-checkbox[@name='prevent-vulenrability-image']//label
\ #verify level?page should not contain disabled element
\ Else
\ Checkbox Should Not Be Checked //clr-checkbox[@name='prevent-vulenrability-image']//label
\ #Page Should Contain a disabled element
\ Run Keyword If ${scanonpush} == "true"
\ Checkbox Should Be Checked //clr-checkbox[@name='scan-image-on-push']//label
\ Else
\ Checkbox Should Not Be Checked //clr-checkbox[@name='scan-image-on-push']//label
\ Back To Projects
\ Run Keyword If ${public} == "public" Checkbox Should Be Checked //clr-checkbox[@name='public']//label
\ Run Keyword If ${contenttrust} == "true" Checkbox Should Be Checked //clr-checkbox[@name='content-trust']//label
\ Run Keyword If ${contenttrust} == "false" Checkbox Should Not Be Checked //clr-checkbox[@name='content-trust']//label
\ Run Keyword If ${preventrunning} == "true" Checkbox Should Be Checked //clr-checkbox[@name='prevent-vulenrability-image']//label
\ Run Keyword If ${preventrunning} == "false" Checkbox Should Not Be Checked //clr-checkbox[@name='prevent-vulenrability-image']//label
\ Run Keyword If ${scanonpush} == "true" Checkbox Should Be Checked //clr-checkbox[@name='scan-image-on-push']//label
\ Run Keyword If ${scanonpush} == "true" Checkbox Should Not Be Checked //clr-checkbox[@name='scan-image-on-push']//label
\ Close Browser
Verify System Setting
${authtype}= Get Value From Json ${json} $.configuration.authmode
${creation}= Get Value From Json ${json} $.configuration..projectcreation
${selfreg}= Get Value From Json ${json} $.configuration..selfreg
${emailserver}= Get Value From Json ${json} $.configuration..emailserver
${emailport}= Get Value From Json ${json} $.configuration..emailport
${emailuser}= Get Value From Json ${json} $.configuration..emailuser
${emailfrom}= Get Value From Json ${json} $.configuration..emailfrom
${token}= Get Value From Json ${json} $.configuration..token
${scanschedule}= Get Value From Json ${json} $.configuration..scanall
Sign In Harbor ${HARBOR_URL} %{HARBOR_ADMIN} %{HARBOR_PASSWORD}
[Arguments] ${json}
@{authtype}= Get Value From Json ${json} $.configuration.authmode
@{creation}= Get Value From Json ${json} $.configuration..projectcreation
@{selfreg}= Get Value From Json ${json} $.configuration..selfreg
@{emailserver}= Get Value From Json ${json} $.configuration..emailserver
@{emailport}= Get Value From Json ${json} $.configuration..emailport
@{emailuser}= Get Value From Json ${json} $.configuration..emailuser
@{emailfrom}= Get Value From Json ${json} $.configuration..emailfrom
@{token}= Get Value From Json ${json} $.configuration..token
@{scanschedule}= Get Value From Json ${json} $.configuration..scanall
Init Chrome Driver
Sign In Harbor ${HARBOR_URL} ${HARBOR_ADMIN} ${HARBOR_PASSWORD}
Switch To Configure
Page Should Contain ${authtype}
Run Keyword If ${selfreg} == "True"
Checkbox Should Be Checked //clr-checkbox[@id='selfReg']//label
Else
Checkbox Should Not Be Checked //clr-checkbox[@id='selfReg']//label
Page Should Contain ${creation}
Page Should Contain @{authtype}[0]
Run Keyword If @{selfreg}[0] == "True" Checkbox Should Be Checked //clr-checkbox[@id='selfReg']//label
Run Keyword If @{selfreg}[0] == "False" Checkbox Should Not Be Checked //clr-checkbox[@id='selfReg']//label
Page Should Contain @{creation}[0]
Switch To Email
Page Should Contain ${emailserver}
Page Should Contain ${emailport}
Page Should Contain ${emailuser}
Page Should Contain ${emailfrom}
Textfield Value Should Be xpath=//*[@id="mailServer"] @{emailserver}[0]
Textfield Value Should Be xpath=//*[@id="emailPort"] @{emailport}[0]
Textfield Value Should Be xpath=//*[@id="emailUsername"] @{emailuser}[0]
Textfield Value Should Be xpath=//*[@id="emailFrom"] @{emailfrom}[0]
Switch To System Settings
Page Should Contain ${token}
Go To Vulnerability Config
Page Should Contain ${scanschedule}
Token Must Be Match @{token}[0]
Go To Vulnerability Config
Page Should Contain None
Close Browser
......@@ -21,6 +21,7 @@ Library Process
Library SSHLibrary 1 minute
Library DateTime
Library Selenium2Library 10 10
Library JSONLibrary
Resource Nimbus-Util.robot
Resource Vsphere-Util.robot
Resource VCH-Util.robot
......@@ -47,6 +48,7 @@ Resource Harbor-Pages/ToolKit.robot
Resource Harbor-Pages/ToolKit_Elements.robot
Resource Harbor-Pages/Vulnerability.robot
Resource Harbor-Pages/LDAP-Mode.robot
Resource Harbor-Pages/Verify.robot
Resource Docker-Util.robot
Resource Admiral-Util.robot
Resource OVA-Util.robot
......
*** Settings ***
Documentation Harbor BATs
Resource ../../resources/Util.robot
Suite Setup Nightly Test Setup ${ip} ${SSH_PWD} ${HARBOR_PASSWORD} ${ip1}
Suite Teardown Collect Nightly Logs ${ip} ${SSH_PWD} ${ip1}
Default Tags Nightly
*** Variables ***
${HARBOR_URL} https://${ip}
${SSH_USER} root
${HARBOR_ADMIN} admin
*** test case ***
Verify Data
#get version from extenal argument
Convert To Float ${version}
Run Keyword If ${version}==1.1
Run Keywords Verify User Verify Project Verify Member Exist Verify Image Tag Verify Endpoint Verify Replicationrule
Else If ${version}==1.2
Run Keywords Verify User Verify Project Verify Member Exist Verify Image Tag Verify Endpoint Verify Replicationrule
Else If ${version}==1.3
Run Keywords Verify User Verify Project Verify Member Exist Verify Image Tag Verify Endpoint Verify Replicationrule Verify System Setting Verify Project Setting
Else If ${version}==1.4
Run Keywords Verify User Verify Project Verify Member Exist Verify Image Tag Verify Endpoint Verify Replicationrule Verify System Setting Verify Project Setting
Else If ${version}==1.5
Run Keywords Verify User Verify Project Verify Member Exist Verify Image Tag Verify Endpoint Verify Replicationrule Verify System Setting Verify Project Setting Verify Project Label Verify Syslabel
Else
Log To Consle "Version Not Supported"
{
"users":[
{
"name":"user1"
},
{
"name":"user2"
},
{
"name":"user3"
},
{
"name":"user4"
},
{
"name":"user5"
}
],
"admin":[
{
"name":"user1",
"has_admin":true
},
{
"name":"user2",
"has_admin":true
}
],
"endpoint":[
{
"url":"http://url",
"name":"endpoint1",
"user":"endpointuser",
"pass":"endpointpass"
}
],
"replicationrule":[
{
"project":"project1",
"endpoint":"endpoint1",
"trigger":"Manual",
"rulename":"rulename"
},
{
"project":"library",
"endpoint":"endpoint1",
"trigger":"Manual",
"rulename":"rulelibrary"
}
],
"configuration":{
"authmode":"Database",
"projectcreation":"everyone",
"selfreg":false,
"emailsetting":{
"emailserver":"emailserver",
"emailport":"2220",
"emailuser":"emailuser",
"emailfrom":"emailfrom",
"emailssl":false,
"verifycert":false
},
"token":"40",
"readonly":false,
"scanall":{
"type":"none",
"param":{
"daily":0
}
},
"syslabel":[
{
"name":"label1"
},
{
"name":"label2"
},
{
"name":"label3"
},
{
"name":"label4"
},
{
"name":"label5"
},
{
"name":"label6"
}
]
},
"projects":[
{
"name":"project1",
"accesslevel":"public",
"repocounts":2,
"repo":[
{
"name":"busybox",
"tag":"latest",
"signed":"False"
},
{
"name":"alpine",
"tag":"latest",
"signed":"True"
}
],
"member":[
{
"name":"user1",
"role":1
},
{
"name":"user2",
"role":2
},
{
"name":"user3",
"role":3
}
],
"replications":{
"rulename":"ruleproject1",
"endpointname":"endpoint1",
"trigger":"Manual"
},
"labels":[
{
"name":"proj1label1"
},
{
"name":"proj1label2"
},
{
"name":"proj1label3"
}
],
"logs":[
{
"name":"admin",
"operation":"create"
}
],
"configuration":{
"enable_content_trust":"true",
"automatically_scan_images_on_push":"true",
"prevent_vulnerable_images_from_running":"true",
"prevent_vlunerable_images_from_running_severity":"High"
}
},
{
"name":"project2",
"accesslevel":"public",
"repocounts":2,
"repo":[
{
"name":"busybox",
"tag":"latest",
"signed":"False"
},
{
"name":"alpine",
"tag":"latest",
"signed":"True"
}
],
"member":[
{
"name":"user1",
"role":1
},
{
"name":"user2",
"role":2
},
{
"name":"user3",
"role":3
}
],
"replications":{
"rulename":"rulename1",
"endpointname":"endpoint1",
"trigger":"Manual"
},
"labels":[
{
"name":"proj2label1"
},
{
"name":"proj2label2"
},
{
"name":"proj2label3"
}
],
"logs":[
{
"name":"admin",
"operation":"create"
}
],
"configuration":{
"enable_content_trust":"True",
"automatically_scan_images_on_push":"True",
"prevent_vulnerable_images_from_running":"True",
"prevent_vlunerable_images_from_running_severity":"High"
}
}
]
}
\ No newline at end of file
import os
import json
import argparse
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
parser = argparse.ArgumentParser(description='The script to generate data for harbor v1.4.0')
parser.add_argument('--endpoint', '-e', dest='endpoint', required=True, help='The endpoint to harbor')
args = parser.parse_args()
url = "https://"+args.endpoint+"/api/"
print url
class HarborAPI:
def create_project(self, project_name):
r = requests.post(url+"projects", auth=("admin", "Harbor12345"), json={"project_name": ""+project_name+"", "metadata": {"public": "true"}}, verify=False)
print(r.status_code)
def create_user(self, username):
payload = {"username":username, "email":username+"@vmware.com", "password":"Harbor12345", "realname":username, "commment":"string"}
r = requests.post(url+"users", auth=("admin", "Harbor12345"), json=payload, verify=False)
print(r.status_code)
def set_user_admin(self, user):
r = requests.get(url+"users?username="+user+"", auth=("admin", "Harbor12345"), verify=False)
userid = str(r.json()[0]['user_id'])
r = requests.put(url+"users/"+userid+"/sysadmin", auth=("admin", "Harbor12345"), json={"has_admin_role": 1}, verify=False)
print(r.status_code)
def add_member(self, project, user, role):
r = requests.get(url+"projects?name="+project+"", auth=("admin", "Harbor12345"), verify=False)
projectid = str(r.json()[0]['project_id'])
payload = {"roles": [role], "username":""+user+""}
r = requests.post(url+"projects/"+projectid+"/members", auth=("admin", "Harbor12345"), json=payload, verify=False)
print(r.status_code)
def add_endpoint(self, endpointurl, endpointname, username, password, insecure):
payload = {"endpoint": ""+endpointurl+"", "name": ""+endpointname+"", "username": ""+username+"", "password": ""+password+"", "insecure": insecure}
r = requests.post(url+"targets", auth=("admin", "Harbor12345"), json=payload, verify=False)
print(r.status_code)
def add_replication_rule(self, project, target, trigger, rulename):
r = requests.get(url+"projects?name="+project+"", auth=("admin", "Harbor12345"), verify=False)
projectid = r.json()[0]['project_id']
r = requests.get(url+"targets?name="+target+"", auth=("admin", "Harbor12345"), verify=False)
targetid = r.json()[0]['id']
payload = {"name": ""+rulename+"", "description": "string", "projects": [{"project_id": projectid,}], "targets": [{"id": targetid,}], "trigger": {"kind": ""+trigger+"", "schedule_param": {"type": "weekly", "weekday": 1, "offtime": 0}}}
r = requests.post(url+"policies/replication", auth=("admin", "Harbor12345"), json=payload, verify=False)
print(r.status_code)
def update_project_setting(self, project, contenttrust, preventrunning, preventseverity, scanonpush):
r = requests.get(url+"projects?name="+project+"", auth=("admin", "Harbor12345"), verify=False)
projectid = str(r.json()[0]['project_id'])
payload = {
"project_name": ""+project+"",
"metadata": {
"public": "True",
"enable_content_trust":