Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add API mock for Vela Worker #338

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions mock/worker/build.go
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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)
}
10 changes: 10 additions & 0 deletions mock/worker/doc.go
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions mock/worker/executor.go
Original file line number Diff line number Diff line change
@@ -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)
}
60 changes: 60 additions & 0 deletions mock/worker/pipeline.go
Original file line number Diff line number Diff line change
@@ -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)
}
62 changes: 62 additions & 0 deletions mock/worker/repo.go
Original file line number Diff line number Diff line change
@@ -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)
}
35 changes: 35 additions & 0 deletions mock/worker/server.go
Original file line number Diff line number Diff line change
@@ -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
}