Browse Source

Merge commit 'e26544ddf550c7fcaf550af88eac5163fa8f2ee2' into HEAD

feature/restructure-pipelines
Jenkins 3 years ago
parent
commit
15ca2a91f1
  1. 1
      go.mod
  2. 2
      go.sum
  3. 17
      main.go
  4. 37
      main_test.go
  5. 16
      pipeline.go
  6. 3
      pipeline_test.go
  7. 1
      pipelines/example.json

1
go.mod

@ -5,6 +5,7 @@ go 1.17
require github.com/gorilla/mux v1.8.0
require (
github.com/bxcodec/faker/v3 v3.7.0 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/hexops/valast v1.4.1 // indirect

2
go.sum

@ -1,4 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/bxcodec/faker/v3 v3.7.0 h1:qWAFFwcyVS0ukF0UoJju1wBLO0cuPQ7JdVBPggM8kNo=
github.com/bxcodec/faker/v3 v3.7.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=

17
main.go

@ -15,16 +15,31 @@ type Metadata struct {
Version string
}
func PipelineHandle(pipeline IPipeline, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pipeline)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Metadata{Name, Version})
}
func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) {
for _, pipeline := range pipelines {
r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
PipelineHandle(pipeline, w, r)
})
}
}
func main() {
LoadPipelines()
pipelines := LoadPipelines()
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler)
RegisterPipelineRoutes(r, pipelines)
http.ListenAndServe(":8000", r)
}

37
main_test.go

@ -1,10 +1,14 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/bxcodec/faker/v3"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
@ -19,3 +23,36 @@ func TestIndexRoute(t *testing.T) {
assert.NotNil(t, responseRecorder.Body, "Response should contain body")
})
}
func TestEndpointRoute(t *testing.T) {
data := Pipeline{}
err := faker.FakeData(&data)
if err != nil {
fmt.Println(err)
}
t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter()
RegisterPipelineRoutes(router, []IPipeline{data})
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, responseRecorder.Code, 200)
body, _ := json.Marshal(data)
assert.JSONEq(t, string(body), responseRecorder.Body.String())
})
t.Run("Unregistered pipelines return 404", func(t *testing.T) {
router := mux.NewRouter()
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, responseRecorder.Code, 404)
})
}

16
pipeline.go

@ -21,14 +21,16 @@ type PipelineType int
type IPipeline interface {
GetName() string
GetSlug() string
GetType() PipelineType
GetSteps() []Step
}
type Pipeline struct {
Name string `json:"name"`
Type PipelineType `json:"type"`
RemoveMetadata bool `json:"remove_metadata"`
Name string `json:"name" faker:"name"`
Slug string `json:"slug" faker:"word"`
Type PipelineType `json:"type" faker:"-"`
RemoveMetadata bool `json:"remove_metadata" faker:"-"`
Steps []Step `json:"steps"`
}
@ -36,6 +38,10 @@ func (p Pipeline) GetName() string {
return p.Name
}
func (p Pipeline) GetSlug() string {
return p.Slug
}
func (p Pipeline) GetType() PipelineType {
return p.Type
}
@ -62,8 +68,8 @@ const (
)
type Step struct {
Name string
Type StepType
Name string `faker:"name"`
Type StepType `faker:"-"`
}
func (s Step) Translate() (IExecutableStep, error) {

3
pipeline_test.go

@ -1,8 +1,9 @@
package main
import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestImagePipelineDeserialization(t *testing.T) {

1
pipelines/example.json

@ -1,5 +1,6 @@
{
"name": "example pipeline",
"slug": "example",
"type": 0,
"removeMetadata": false,
"steps": [

Loading…
Cancel
Save