mirror of
https://github.com/goharbor/harbor.git
synced 2024-10-01 23:07:39 +02:00
Add UT and remove useless code.
This commit is contained in:
parent
e4952b7a63
commit
7aec4d9f21
56
src/common/job/client_test.go
Normal file
56
src/common/job/client_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package job
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/vmware/harbor/src/common/job/models"
|
||||||
|
"github.com/vmware/harbor/src/common/job/test"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
testClient Client
|
||||||
|
)
|
||||||
|
|
||||||
|
const ID = "u-1234-5678-9012"
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
mockServer := test.NewJobServiceServer()
|
||||||
|
defer mockServer.Close()
|
||||||
|
testClient = NewDefaultClient(mockServer.URL, "")
|
||||||
|
rc := m.Run()
|
||||||
|
if rc != 0 {
|
||||||
|
os.Exit(rc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubmitJob(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
d := &models.JobData{
|
||||||
|
Name: "replication",
|
||||||
|
Metadata: nil,
|
||||||
|
}
|
||||||
|
uuid, err := testClient.SubmitJob(d)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(ID, uuid)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetJobLog(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
_, err1 := testClient.GetJobLog("non")
|
||||||
|
assert.NotNil(err1)
|
||||||
|
|
||||||
|
b2, err2 := testClient.GetJobLog(ID)
|
||||||
|
assert.Nil(err2)
|
||||||
|
text := string(b2)
|
||||||
|
assert.Contains(text, "The content in this file is for mocking the get log api.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostAction(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
err := testClient.PostAction(ID, "fff")
|
||||||
|
assert.NotNil(err)
|
||||||
|
err2 := testClient.PostAction(ID, "stop")
|
||||||
|
assert.Nil(err2)
|
||||||
|
}
|
98
src/common/job/test/server.go
Normal file
98
src/common/job/test/server.go
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/vmware/harbor/src/common/job/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
jobUUID = "u-1234-5678-9012"
|
||||||
|
jobsPrefix = "/api/v1/jobs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func currPath() string {
|
||||||
|
_, f, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
panic("Failed to get current directory")
|
||||||
|
}
|
||||||
|
return path.Dir(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJobServiceServer
|
||||||
|
func NewJobServiceServer() *httptest.Server {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc(fmt.Sprintf("%s/%s/log", jobsPrefix, jobUUID),
|
||||||
|
func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
if req.Method != http.MethodGet {
|
||||||
|
rw.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rw.Header().Add("Content-Type", "text/plain")
|
||||||
|
rw.WriteHeader(http.StatusOK)
|
||||||
|
f := path.Join(currPath(), "test.log")
|
||||||
|
b, _ := ioutil.ReadFile(f)
|
||||||
|
_, err := rw.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
mux.HandleFunc(fmt.Sprintf("%s/%s", jobsPrefix, jobUUID),
|
||||||
|
func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
if req.Method != http.MethodPost {
|
||||||
|
rw.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
action := models.JobActionRequest{}
|
||||||
|
if err := json.Unmarshal(data, &action); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if strings.ToLower(action.Action) != "stop" && strings.ToLower(action.Action) != "cancel" && strings.ToLower(action.Action) != "retry" {
|
||||||
|
rw.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rw.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
})
|
||||||
|
mux.HandleFunc(fmt.Sprintf("%s", jobsPrefix),
|
||||||
|
func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
if req.Method == http.MethodPost {
|
||||||
|
data, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
jobReq := models.JobRequest{}
|
||||||
|
json.Unmarshal(data, &jobReq)
|
||||||
|
if jobReq.Job.Name == "replication" {
|
||||||
|
respData := models.JobStats{
|
||||||
|
Stats: &models.JobStatData{
|
||||||
|
JobID: jobUUID,
|
||||||
|
Status: "Pending",
|
||||||
|
RunAt: time.Now().Unix(),
|
||||||
|
IsUnique: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(respData)
|
||||||
|
rw.WriteHeader(http.StatusAccepted)
|
||||||
|
if _, err := rw.Write(b); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return httptest.NewServer(mux)
|
||||||
|
}
|
1
src/common/job/test/test.log
Normal file
1
src/common/job/test/test.log
Normal file
@ -0,0 +1 @@
|
|||||||
|
The content in this file is for mocking the get log api.
|
@ -821,7 +821,6 @@ func (ra *RepositoryAPI) ScanImage() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = uiutils.TriggerImageScan(repoName, tag)
|
err = uiutils.TriggerImageScan(repoName, tag)
|
||||||
//TODO better check existence
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while calling job service to trigger image scan: %v", err)
|
log.Errorf("Error while calling job service to trigger image scan: %v", err)
|
||||||
ra.HandleInternalServerError("Failed to scan image, please check log for details")
|
ra.HandleInternalServerError("Failed to scan image, please check log for details")
|
||||||
|
@ -16,43 +16,14 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/vmware/harbor/src/common/models"
|
|
||||||
"github.com/vmware/harbor/src/common/utils/registry"
|
"github.com/vmware/harbor/src/common/utils/registry"
|
||||||
"github.com/vmware/harbor/src/common/utils/registry/auth"
|
"github.com/vmware/harbor/src/common/utils/registry/auth"
|
||||||
"github.com/vmware/harbor/src/ui/config"
|
"github.com/vmware/harbor/src/ui/config"
|
||||||
"github.com/vmware/harbor/src/ui/service/token"
|
"github.com/vmware/harbor/src/ui/service/token"
|
||||||
|
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RequestAsUI is a shortcut to make a request attach UI secret and send the request.
|
|
||||||
// Do not use this when you want to handle the response
|
|
||||||
func RequestAsUI(method, url string, body io.Reader, h ResponseHandler) error {
|
|
||||||
req, err := http.NewRequest(method, url, body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
AddUISecret(req)
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return h.Handle(resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
//AddUISecret add secret cookie to a request
|
|
||||||
func AddUISecret(req *http.Request) {
|
|
||||||
if req != nil {
|
|
||||||
req.AddCookie(&http.Cookie{
|
|
||||||
Name: models.UISecretCookie,
|
|
||||||
Value: config.UISecret(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRepositoryClientForUI creates a repository client that can only be used to
|
// NewRepositoryClientForUI creates a repository client that can only be used to
|
||||||
// access the internal registry
|
// access the internal registry
|
||||||
func NewRepositoryClientForUI(username, repository string) (*registry.Repository, error) {
|
func NewRepositoryClientForUI(username, repository string) (*registry.Repository, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user