diff --git a/.travis.yml b/.travis.yml index e463cfc70..a80c95942 100644 --- a/.travis.yml +++ b/.travis.yml @@ -73,6 +73,8 @@ before_script: - sudo sqlite3 /registry.db < make/common/db/registry_sqlite.sql script: + - sudo mkdir -p /harbor_storage/ca_download + - sudo mv ./tests/ca.crt /harbor_storage/ca_download - sudo service mysql stop - sudo ./tests/testprepare.sh - docker-compose -f ./make/docker-compose.test.yml up -d diff --git a/src/ui/api/harborapi_test.go b/src/ui/api/harborapi_test.go index 749927438..4fd57a1c3 100644 --- a/src/ui/api/harborapi_test.go +++ b/src/ui/api/harborapi_test.go @@ -88,6 +88,8 @@ func init() { beego.Router("/api/policies/replication", &RepPolicyAPI{}, "get:List") beego.Router("/api/policies/replication", &RepPolicyAPI{}, "post:Post;delete:Delete") beego.Router("/api/policies/replication/:id([0-9]+)/enablement", &RepPolicyAPI{}, "put:UpdateEnablement") + beego.Router("/api/systeminfo/volumes", &SystemInfoAPI{}, "get:GetVolumeInfo") + beego.Router("/api/systeminfo/getcert", &SystemInfoAPI{}, "get:GetCert") _ = updateInitPassword(1, "Harbor12345") @@ -872,3 +874,26 @@ func updateInitPassword(userID int, password string) error { } return nil } + +//Get system volume info +func (a testapi) VolumeInfoGet(authInfo usrInfo) (int, apilib.SystemInfo, error) { + _sling := sling.New().Get(a.basePath) + path := "/api/systeminfo/volumes" + _sling = _sling.Path(path) + httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo) + var successPayLoad apilib.SystemInfo + if 200 == httpStatusCode && nil == err { + err = json.Unmarshal(body, &successPayLoad) + } + + return httpStatusCode, successPayLoad, err +} + +//Get system cert +func (a testapi) CertGet(authInfo usrInfo) (int, []byte, error) { + _sling := sling.New().Get(a.basePath) + path := "/api/systeminfo/getcert" + _sling = _sling.Path(path) + httpStatusCode, body, err := request(_sling, jsonAcceptHeader, authInfo) + return httpStatusCode, body, err +} diff --git a/src/ui/api/systeminfo_test.go b/src/ui/api/systeminfo_test.go new file mode 100644 index 000000000..933e47640 --- /dev/null +++ b/src/ui/api/systeminfo_test.go @@ -0,0 +1,64 @@ +package api + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetVolumeInfo(t *testing.T) { + fmt.Println("Testing Get Volume Info") + assert := assert.New(t) + apiTest := newHarborAPI() + + //case 1: get volume info without admin role + CommonAddUser() + code, _, err := apiTest.VolumeInfoGet(*testUser) + if err != nil { + t.Error("Error occured while get system volume info") + t.Log(err) + } else { + assert.Equal(403, code, "Get system volume info should be 403") + } + //case 2: get volume info with admin role + code, info, err := apiTest.VolumeInfoGet(*admin) + if err != nil { + t.Error("Error occured while get system volume info") + t.Log(err) + } else { + assert.Equal(200, code, "Get system volume info should be 200") + if info.HarborStorage.Total <= 0 { + assert.Equal(1, info.HarborStorage.Total, "Total storage of system should be larger than 0") + } + if info.HarborStorage.Free <= 0 { + assert.Equal(1, info.HarborStorage.Free, "Free storage of system should be larger than 0") + } + } + +} + +func TestGetCert(t *testing.T) { + fmt.Println("Testing Get Cert") + assert := assert.New(t) + apiTest := newHarborAPI() + + //case 1: get cert without admin role + code, _, err := apiTest.CertGet(*testUser) + if err != nil { + t.Error("Error occured while get system cert") + t.Log(err) + } else { + assert.Equal(403, code, "Get system cert should be 403") + } + //case 2: get cert with admin role + code, content, err := apiTest.CertGet(*admin) + if err != nil { + t.Error("Error occured while get system cert") + t.Log(err) + } else { + assert.Equal(200, code, "Get system cert should be 200") + assert.Equal("test for ca.crt.\n", string(content), "Get system cert content should be equal") + + } + CommonDelUser() +} diff --git a/tests/apitests/apilib/system_info.go b/tests/apitests/apilib/system_info.go new file mode 100644 index 000000000..927fa0354 --- /dev/null +++ b/tests/apitests/apilib/system_info.go @@ -0,0 +1,32 @@ +/* + * Harbor API + * + * These APIs provide services for manipulating Harbor project. + * + * OpenAPI spec version: 0.3.0 + * + * Generated by: https://github.com/swagger-api/swagger-codegen.git + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package apilib + +type SystemInfo struct { + HarborStorage Storage `json:"storage"` +} + +type Storage struct { + Total uint64 `json:"total"` + Free uint64 `json:"free"` +} diff --git a/tests/ca.crt b/tests/ca.crt new file mode 100644 index 000000000..ed71ba717 --- /dev/null +++ b/tests/ca.crt @@ -0,0 +1 @@ +test for ca.crt.