Skip to content

Commit 0111fc0

Browse files
authored
test: add API mock for Vela Worker (#338)
1 parent bf6e5b8 commit 0111fc0

File tree

6 files changed

+316
-0
lines changed

6 files changed

+316
-0
lines changed

mock/worker/build.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package worker
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"strings"
12+
13+
"github.com/gin-gonic/gin"
14+
"github.com/go-vela/types"
15+
"github.com/go-vela/types/library"
16+
)
17+
18+
const (
19+
// BuildResp represents a JSON return for a single build.
20+
BuildResp = `{
21+
"id": 1,
22+
"repo_id": 1,
23+
"number": 1,
24+
"parent": 1,
25+
"event": "push",
26+
"status": "created",
27+
"error": "",
28+
"enqueued": 1563474077,
29+
"created": 1563474076,
30+
"started": 1563474077,
31+
"finished": 0,
32+
"deploy": "",
33+
"clone": "https://github.com/github/octocat.git",
34+
"source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163",
35+
"title": "push received from https://github.com/github/octocat",
36+
"message": "First commit...",
37+
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
38+
"sender": "OctoKitty",
39+
"author": "OctoKitty",
40+
"email": "[email protected]",
41+
"link": "https://vela.example.company.com/github/octocat/1",
42+
"branch": "master",
43+
"ref": "refs/heads/master",
44+
"base_ref": "",
45+
"host": "example.company.com",
46+
"runtime": "docker",
47+
"distribution": "linux"
48+
}`
49+
)
50+
51+
// getBuild has a param :build returns mock JSON for a http GET.
52+
func getBuild(c *gin.Context) {
53+
b := c.Param("build")
54+
55+
if strings.EqualFold(b, "0") {
56+
msg := fmt.Sprintf("Build %s does not exist", b)
57+
58+
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
59+
60+
return
61+
}
62+
63+
data := []byte(BuildResp)
64+
65+
var body library.Build
66+
_ = json.Unmarshal(data, &body)
67+
68+
c.JSON(http.StatusOK, body)
69+
}
70+
71+
// cancelBuild has a param :build returns mock JSON for a http DELETE.
72+
//
73+
// Pass "0" to :build to test receiving a http 404 response.
74+
func cancelBuild(c *gin.Context) {
75+
b := c.Param("build")
76+
77+
if strings.EqualFold(b, "0") {
78+
msg := fmt.Sprintf("Build %s does not exist", b)
79+
80+
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
81+
82+
return
83+
}
84+
85+
c.JSON(http.StatusOK, BuildResp)
86+
}

mock/worker/doc.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
// Package worker provides a mock for using the Worker API.
6+
//
7+
// Usage:
8+
//
9+
// import "github.com/go-vela/worker/mock/worker"
10+
package worker

mock/worker/executor.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package worker
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"strings"
12+
13+
"github.com/gin-gonic/gin"
14+
"github.com/go-vela/types"
15+
"github.com/go-vela/types/library"
16+
)
17+
18+
const (
19+
// ExecutorResp represents a JSON return for a single worker.
20+
ExecutorResp = `
21+
{
22+
"id": 1,
23+
"host": "worker_1",
24+
"runtime": "docker",
25+
"distribution": "linux",
26+
"build": ` + BuildResp + `,
27+
"pipeline": ` + PipelineResp + `,
28+
"repo": ` + RepoResp + `
29+
}`
30+
31+
// ExecutorsResp represents a JSON return for one to many workers.
32+
ExecutorsResp = `[ ` + ExecutorResp + `,` + ExecutorResp + `]`
33+
)
34+
35+
// getExecutors returns mock JSON for a http GET.
36+
func getExecutors(c *gin.Context) {
37+
data := []byte(ExecutorsResp)
38+
39+
var body []library.Executor
40+
_ = json.Unmarshal(data, &body)
41+
42+
c.JSON(http.StatusOK, body)
43+
}
44+
45+
// getExecutor has a param :executor returns mock JSON for a http GET.
46+
func getExecutor(c *gin.Context) {
47+
w := c.Param("executor")
48+
49+
if strings.EqualFold(w, "0") {
50+
msg := fmt.Sprintf("Executor %s does not exist", w)
51+
52+
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
53+
54+
return
55+
}
56+
57+
data := []byte(ExecutorResp)
58+
59+
var body library.Executor
60+
_ = json.Unmarshal(data, &body)
61+
62+
c.JSON(http.StatusOK, body)
63+
}

mock/worker/pipeline.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package worker
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"strings"
12+
13+
"github.com/go-vela/types/library"
14+
15+
"github.com/gin-gonic/gin"
16+
"github.com/go-vela/types"
17+
)
18+
19+
const (
20+
// PipelineResp represents a JSON return for a single pipeline.
21+
PipelineResp = `{
22+
"id": 1,
23+
"repo_id": 1,
24+
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
25+
"flavor": "",
26+
"platform": "",
27+
"ref": "refs/heads/master",
28+
"type": "yaml",
29+
"version": "1",
30+
"external_secrets": false,
31+
"internal_secrets": false,
32+
"services": false,
33+
"stages": false,
34+
"steps": true,
35+
"templates": false,
36+
"data": "LS0tCnZlcnNpb246ICIxIgoKc3RlcHM6CiAgLSBuYW1lOiBlY2hvCiAgICBpbWFnZTogYWxwaW5lOmxhdGVzdAogICAgY29tbWFuZHM6IFtlY2hvIGZvb10="
37+
}`
38+
)
39+
40+
// getPipeline has a param :pipeline returns mock YAML for a http GET.
41+
//
42+
// Pass "0" to :pipeline to test receiving a http 404 response.
43+
func getPipeline(c *gin.Context) {
44+
p := c.Param("pipeline")
45+
46+
if strings.EqualFold(p, "0") {
47+
msg := fmt.Sprintf("Pipeline %s does not exist", p)
48+
49+
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
50+
51+
return
52+
}
53+
54+
data := []byte(PipelineResp)
55+
56+
var body library.Pipeline
57+
_ = json.Unmarshal(data, &body)
58+
59+
c.JSON(http.StatusOK, body)
60+
}

mock/worker/repo.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package worker
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"strings"
12+
13+
"github.com/gin-gonic/gin"
14+
"github.com/go-vela/types"
15+
"github.com/go-vela/types/library"
16+
)
17+
18+
const (
19+
// RepoResp represents a JSON return for a single repo.
20+
RepoResp = `{
21+
"id": 1,
22+
"user_id": 1,
23+
"org": "github",
24+
"name": "octocat",
25+
"full_name": "github/octocat",
26+
"link": "https://github.com/github/octocat",
27+
"clone": "https://github.com/github/octocat",
28+
"branch": "master",
29+
"build_limit": 10,
30+
"timeout": 60,
31+
"visibility": "public",
32+
"private": false,
33+
"trusted": true,
34+
"active": true,
35+
"allow_pr": false,
36+
"allow_push": true,
37+
"allow_deploy": false,
38+
"allow_tag": false
39+
}`
40+
)
41+
42+
// getRepo has a param :repo returns mock JSON for a http GET.
43+
//
44+
// Pass "not-found" to :repo to test receiving a http 404 response.
45+
func getRepo(c *gin.Context) {
46+
r := c.Param("repo")
47+
48+
if strings.Contains(r, "not-found") {
49+
msg := fmt.Sprintf("Repo %s does not exist", r)
50+
51+
c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})
52+
53+
return
54+
}
55+
56+
data := []byte(RepoResp)
57+
58+
var body library.Repo
59+
_ = json.Unmarshal(data, &body)
60+
61+
c.JSON(http.StatusOK, body)
62+
}

mock/worker/server.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package worker
6+
7+
import (
8+
"net/http"
9+
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// FakeHandler returns an http.Handler that is capable of handling
14+
// Vela API requests and returning mock responses.
15+
func FakeHandler() http.Handler {
16+
gin.SetMode(gin.TestMode)
17+
18+
e := gin.New()
19+
20+
// mock endpoints for executor calls
21+
e.GET("/api/v1/executors", getExecutors)
22+
e.GET("/api/v1/executors/:executor", getExecutor)
23+
24+
// mock endpoints for build calls
25+
e.GET("/api/v1/executors/:executor/build", getBuild)
26+
e.DELETE("/api/v1/executors/:executor/build/cancel", cancelBuild)
27+
28+
// mock endpoints for pipeline calls
29+
e.GET("/api/v1/executors/:executor/pipeline", getPipeline)
30+
31+
// mock endpoints for repo calls
32+
e.GET("/api/v1/executors/:executor/repo", getRepo)
33+
34+
return e
35+
}

0 commit comments

Comments
 (0)