-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(executor): add executor logic from pkg-executor (#220)
* feat(executor): add logic from pkg-executor * chore: clean go dependencies
- Loading branch information
Showing
88 changed files
with
11,284 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2021 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package executor | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// key defines the key type for storing | ||
// the executor Engine in the context. | ||
const key = "executor" | ||
|
||
// FromContext retrieves the executor Engine from the context.Context. | ||
func FromContext(c context.Context) Engine { | ||
// get executor value from context.Context | ||
v := c.Value(key) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
// cast executor value to expected Engine type | ||
e, ok := v.(Engine) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return e | ||
} | ||
|
||
// FromGinContext retrieves the executor Engine from the gin.Context. | ||
func FromGinContext(c *gin.Context) Engine { | ||
// get executor value from gin.Context | ||
// | ||
// https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc#Context.Get | ||
v, ok := c.Get(key) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
// cast executor value to expected Engine type | ||
e, ok := v.(Engine) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return e | ||
} | ||
|
||
// WithContext inserts the executor Engine into the context.Context. | ||
func WithContext(c context.Context, e Engine) context.Context { | ||
// set the executor Engine in the context.Context | ||
// | ||
// nolint: golint,staticcheck // ignore using string with context value | ||
return context.WithValue(c, key, e) | ||
} | ||
|
||
// WithGinContext inserts the executor Engine into the gin.Context. | ||
func WithGinContext(c *gin.Context, e Engine) { | ||
// set the executor Engine in the gin.Context | ||
// | ||
// https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc#Context.Set | ||
c.Set(key, e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
// Copyright (c) 2021 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package executor | ||
|
||
import ( | ||
"context" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/go-vela/mock/server" | ||
|
||
"github.com/go-vela/worker/executor/linux" | ||
|
||
"github.com/go-vela/pkg-runtime/runtime/docker" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
) | ||
|
||
func TestExecutor_FromContext(t *testing.T) { | ||
// setup types | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
_client, err := vela.NewClient(s.URL, "", nil) | ||
if err != nil { | ||
t.Errorf("unable to create Vela API client: %v", err) | ||
} | ||
|
||
_runtime, err := docker.NewMock() | ||
if err != nil { | ||
t.Errorf("unable to create runtime engine: %v", err) | ||
} | ||
|
||
_engine, err := linux.New( | ||
linux.WithBuild(_build), | ||
linux.WithPipeline(_pipeline), | ||
linux.WithRepo(_repo), | ||
linux.WithRuntime(_runtime), | ||
linux.WithUser(_user), | ||
linux.WithVelaClient(_client), | ||
) | ||
if err != nil { | ||
t.Errorf("unable to create linux engine: %v", err) | ||
} | ||
|
||
// setup tests | ||
tests := []struct { | ||
context context.Context | ||
want Engine | ||
}{ | ||
{ | ||
// nolint: golint,staticcheck // ignore using string with context value | ||
context: context.WithValue(context.Background(), key, _engine), | ||
want: _engine, | ||
}, | ||
{ | ||
context: context.Background(), | ||
want: nil, | ||
}, | ||
{ | ||
// nolint: golint,staticcheck // ignore using string with context value | ||
context: context.WithValue(context.Background(), key, "foo"), | ||
want: nil, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
got := FromContext(test.context) | ||
|
||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("FromContext is %v, want %v", got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestExecutor_FromGinContext(t *testing.T) { | ||
// setup types | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
_client, err := vela.NewClient(s.URL, "", nil) | ||
if err != nil { | ||
t.Errorf("unable to create Vela API client: %v", err) | ||
} | ||
|
||
_runtime, err := docker.NewMock() | ||
if err != nil { | ||
t.Errorf("unable to create runtime engine: %v", err) | ||
} | ||
|
||
_engine, err := linux.New( | ||
linux.WithBuild(_build), | ||
linux.WithPipeline(_pipeline), | ||
linux.WithRepo(_repo), | ||
linux.WithRuntime(_runtime), | ||
linux.WithUser(_user), | ||
linux.WithVelaClient(_client), | ||
) | ||
if err != nil { | ||
t.Errorf("unable to create linux engine: %v", err) | ||
} | ||
|
||
// setup tests | ||
tests := []struct { | ||
context *gin.Context | ||
value interface{} | ||
want Engine | ||
}{ | ||
{ | ||
context: new(gin.Context), | ||
value: _engine, | ||
want: _engine, | ||
}, | ||
{ | ||
context: new(gin.Context), | ||
value: nil, | ||
want: nil, | ||
}, | ||
{ | ||
context: new(gin.Context), | ||
value: "foo", | ||
want: nil, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
if test.value != nil { | ||
test.context.Set(key, test.value) | ||
} | ||
|
||
got := FromGinContext(test.context) | ||
|
||
if !reflect.DeepEqual(got, test.want) { | ||
t.Errorf("FromGinContext is %v, want %v", got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestExecutor_WithContext(t *testing.T) { | ||
// setup types | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
_client, err := vela.NewClient(s.URL, "", nil) | ||
if err != nil { | ||
t.Errorf("unable to create Vela API client: %v", err) | ||
} | ||
|
||
_runtime, err := docker.NewMock() | ||
if err != nil { | ||
t.Errorf("unable to create runtime engine: %v", err) | ||
} | ||
|
||
_engine, err := linux.New( | ||
linux.WithBuild(_build), | ||
linux.WithPipeline(_pipeline), | ||
linux.WithRepo(_repo), | ||
linux.WithRuntime(_runtime), | ||
linux.WithUser(_user), | ||
linux.WithVelaClient(_client), | ||
) | ||
if err != nil { | ||
t.Errorf("unable to create linux engine: %v", err) | ||
} | ||
|
||
// nolint: golint,staticcheck // ignore using string with context value | ||
want := context.WithValue(context.Background(), key, _engine) | ||
|
||
// run test | ||
got := WithContext(context.Background(), _engine) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("WithContext is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestExecutor_WithGinContext(t *testing.T) { | ||
// setup types | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
_client, err := vela.NewClient(s.URL, "", nil) | ||
if err != nil { | ||
t.Errorf("unable to create Vela API client: %v", err) | ||
} | ||
|
||
_runtime, err := docker.NewMock() | ||
if err != nil { | ||
t.Errorf("unable to create runtime engine: %v", err) | ||
} | ||
|
||
_engine, err := linux.New( | ||
linux.WithBuild(_build), | ||
linux.WithPipeline(_pipeline), | ||
linux.WithRepo(_repo), | ||
linux.WithRuntime(_runtime), | ||
linux.WithUser(_user), | ||
linux.WithVelaClient(_client), | ||
) | ||
if err != nil { | ||
t.Errorf("unable to create linux engine: %v", err) | ||
} | ||
|
||
want := new(gin.Context) | ||
want.Set(key, _engine) | ||
|
||
// run test | ||
got := new(gin.Context) | ||
WithGinContext(got, _engine) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("WithGinContext is %v, want %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2021 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
// Package executor provides the ability for Vela to | ||
// integrate with different supported operating | ||
// systems. | ||
// | ||
// Usage: | ||
// | ||
// import "github.com/go-vela/worker/executor" | ||
package executor |
Oops, something went wrong.