diff --git a/mock/worker/build.go b/mock/worker/build.go new file mode 100644 index 00000000..d8e05fc9 --- /dev/null +++ b/mock/worker/build.go @@ -0,0 +1,86 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + "github.com/go-vela/types" + "github.com/go-vela/types/library" +) + +const ( + // BuildResp represents a JSON return for a single build. + BuildResp = `{ + "id": 1, + "repo_id": 1, + "number": 1, + "parent": 1, + "event": "push", + "status": "created", + "error": "", + "enqueued": 1563474077, + "created": 1563474076, + "started": 1563474077, + "finished": 0, + "deploy": "", + "clone": "https://github.com/github/octocat.git", + "source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163", + "title": "push received from https://github.com/github/octocat", + "message": "First commit...", + "commit": "48afb5bdc41ad69bf22588491333f7cf71135163", + "sender": "OctoKitty", + "author": "OctoKitty", + "email": "octokitty@github.com", + "link": "https://vela.example.company.com/github/octocat/1", + "branch": "master", + "ref": "refs/heads/master", + "base_ref": "", + "host": "example.company.com", + "runtime": "docker", + "distribution": "linux" +}` +) + +// getBuild has a param :build returns mock JSON for a http GET. +func getBuild(c *gin.Context) { + b := c.Param("build") + + if strings.EqualFold(b, "0") { + msg := fmt.Sprintf("Build %s does not exist", b) + + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) + + return + } + + data := []byte(BuildResp) + + var body library.Build + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} + +// cancelBuild has a param :build returns mock JSON for a http DELETE. +// +// Pass "0" to :build to test receiving a http 404 response. +func cancelBuild(c *gin.Context) { + b := c.Param("build") + + if strings.EqualFold(b, "0") { + msg := fmt.Sprintf("Build %s does not exist", b) + + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) + + return + } + + c.JSON(http.StatusOK, BuildResp) +} diff --git a/mock/worker/doc.go b/mock/worker/doc.go new file mode 100644 index 00000000..281bb581 --- /dev/null +++ b/mock/worker/doc.go @@ -0,0 +1,10 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// Package worker provides a mock for using the Worker API. +// +// Usage: +// +// import "github.com/go-vela/worker/mock/worker" +package worker diff --git a/mock/worker/executor.go b/mock/worker/executor.go new file mode 100644 index 00000000..f9af3c47 --- /dev/null +++ b/mock/worker/executor.go @@ -0,0 +1,63 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + "github.com/go-vela/types" + "github.com/go-vela/types/library" +) + +const ( + // ExecutorResp represents a JSON return for a single worker. + ExecutorResp = ` + { + "id": 1, + "host": "worker_1", + "runtime": "docker", + "distribution": "linux", + "build": ` + BuildResp + `, + "pipeline": ` + PipelineResp + `, + "repo": ` + RepoResp + ` + }` + + // ExecutorsResp represents a JSON return for one to many workers. + ExecutorsResp = `[ ` + ExecutorResp + `,` + ExecutorResp + `]` +) + +// getExecutors returns mock JSON for a http GET. +func getExecutors(c *gin.Context) { + data := []byte(ExecutorsResp) + + var body []library.Executor + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} + +// getExecutor has a param :executor returns mock JSON for a http GET. +func getExecutor(c *gin.Context) { + w := c.Param("executor") + + if strings.EqualFold(w, "0") { + msg := fmt.Sprintf("Executor %s does not exist", w) + + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) + + return + } + + data := []byte(ExecutorResp) + + var body library.Executor + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} diff --git a/mock/worker/pipeline.go b/mock/worker/pipeline.go new file mode 100644 index 00000000..9184615d --- /dev/null +++ b/mock/worker/pipeline.go @@ -0,0 +1,60 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/go-vela/types/library" + + "github.com/gin-gonic/gin" + "github.com/go-vela/types" +) + +const ( + // PipelineResp represents a JSON return for a single pipeline. + PipelineResp = `{ + "id": 1, + "repo_id": 1, + "commit": "48afb5bdc41ad69bf22588491333f7cf71135163", + "flavor": "", + "platform": "", + "ref": "refs/heads/master", + "type": "yaml", + "version": "1", + "external_secrets": false, + "internal_secrets": false, + "services": false, + "stages": false, + "steps": true, + "templates": false, + "data": "LS0tCnZlcnNpb246ICIxIgoKc3RlcHM6CiAgLSBuYW1lOiBlY2hvCiAgICBpbWFnZTogYWxwaW5lOmxhdGVzdAogICAgY29tbWFuZHM6IFtlY2hvIGZvb10=" +}` +) + +// getPipeline has a param :pipeline returns mock YAML for a http GET. +// +// Pass "0" to :pipeline to test receiving a http 404 response. +func getPipeline(c *gin.Context) { + p := c.Param("pipeline") + + if strings.EqualFold(p, "0") { + msg := fmt.Sprintf("Pipeline %s does not exist", p) + + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) + + return + } + + data := []byte(PipelineResp) + + var body library.Pipeline + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} diff --git a/mock/worker/repo.go b/mock/worker/repo.go new file mode 100644 index 00000000..da6d1d47 --- /dev/null +++ b/mock/worker/repo.go @@ -0,0 +1,62 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + "github.com/go-vela/types" + "github.com/go-vela/types/library" +) + +const ( + // RepoResp represents a JSON return for a single repo. + RepoResp = `{ + "id": 1, + "user_id": 1, + "org": "github", + "name": "octocat", + "full_name": "github/octocat", + "link": "https://github.com/github/octocat", + "clone": "https://github.com/github/octocat", + "branch": "master", + "build_limit": 10, + "timeout": 60, + "visibility": "public", + "private": false, + "trusted": true, + "active": true, + "allow_pr": false, + "allow_push": true, + "allow_deploy": false, + "allow_tag": false +}` +) + +// getRepo has a param :repo returns mock JSON for a http GET. +// +// Pass "not-found" to :repo to test receiving a http 404 response. +func getRepo(c *gin.Context) { + r := c.Param("repo") + + if strings.Contains(r, "not-found") { + msg := fmt.Sprintf("Repo %s does not exist", r) + + c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg}) + + return + } + + data := []byte(RepoResp) + + var body library.Repo + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} diff --git a/mock/worker/server.go b/mock/worker/server.go new file mode 100644 index 00000000..a3a687b6 --- /dev/null +++ b/mock/worker/server.go @@ -0,0 +1,35 @@ +// Copyright (c) 2022 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package worker + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// FakeHandler returns an http.Handler that is capable of handling +// Vela API requests and returning mock responses. +func FakeHandler() http.Handler { + gin.SetMode(gin.TestMode) + + e := gin.New() + + // mock endpoints for executor calls + e.GET("/api/v1/executors", getExecutors) + e.GET("/api/v1/executors/:executor", getExecutor) + + // mock endpoints for build calls + e.GET("/api/v1/executors/:executor/build", getBuild) + e.DELETE("/api/v1/executors/:executor/build/cancel", cancelBuild) + + // mock endpoints for pipeline calls + e.GET("/api/v1/executors/:executor/pipeline", getPipeline) + + // mock endpoints for repo calls + e.GET("/api/v1/executors/:executor/repo", getRepo) + + return e +}