add test file for systeminfo.go (#1288)

LGTM
This commit is contained in:
wemeya 2017-01-06 15:41:07 +08:00 committed by yhua123
parent f27f2cf84e
commit ce0a44010d
5 changed files with 124 additions and 0 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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()
}

View File

@ -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"`
}

1
tests/ca.crt Normal file
View File

@ -0,0 +1 @@
test for ca.crt.