From 7aec4d9f2116156a38d7738cabfbdfb5eb6ca79f Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Mon, 2 Apr 2018 19:34:44 +0800 Subject: [PATCH] Add UT and remove useless code. --- src/common/job/client_test.go | 56 ++++++++++++++++++++ src/common/job/test/server.go | 98 +++++++++++++++++++++++++++++++++++ src/common/job/test/test.log | 1 + src/ui/api/repository.go | 1 - src/ui/utils/utils.go | 29 ----------- 5 files changed, 155 insertions(+), 30 deletions(-) create mode 100644 src/common/job/client_test.go create mode 100644 src/common/job/test/server.go create mode 100644 src/common/job/test/test.log diff --git a/src/common/job/client_test.go b/src/common/job/client_test.go new file mode 100644 index 000000000..e2b0936b9 --- /dev/null +++ b/src/common/job/client_test.go @@ -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) +} diff --git a/src/common/job/test/server.go b/src/common/job/test/server.go new file mode 100644 index 000000000..ee1dd689f --- /dev/null +++ b/src/common/job/test/server.go @@ -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) +} diff --git a/src/common/job/test/test.log b/src/common/job/test/test.log new file mode 100644 index 000000000..2cd30ff03 --- /dev/null +++ b/src/common/job/test/test.log @@ -0,0 +1 @@ +The content in this file is for mocking the get log api. diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index cff3f9444..af6277a4d 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -821,7 +821,6 @@ func (ra *RepositoryAPI) ScanImage() { return } err = uiutils.TriggerImageScan(repoName, tag) - //TODO better check existence if err != nil { log.Errorf("Error while calling job service to trigger image scan: %v", err) ra.HandleInternalServerError("Failed to scan image, please check log for details") diff --git a/src/ui/utils/utils.go b/src/ui/utils/utils.go index ad49194ff..7557f2fb1 100644 --- a/src/ui/utils/utils.go +++ b/src/ui/utils/utils.go @@ -16,43 +16,14 @@ package utils import ( - "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/registry" "github.com/vmware/harbor/src/common/utils/registry/auth" "github.com/vmware/harbor/src/ui/config" "github.com/vmware/harbor/src/ui/service/token" - "io" "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 // access the internal registry func NewRepositoryClientForUI(username, repository string) (*registry.Repository, error) {