Skip to content

Commit

Permalink
feat(executor): add executor logic from pkg-executor (#220)
Browse files Browse the repository at this point in the history
* feat(executor): add logic from pkg-executor

* chore: clean go dependencies
  • Loading branch information
jbrockopp authored Oct 26, 2021
1 parent fceece1 commit bd78686
Show file tree
Hide file tree
Showing 88 changed files with 11,284 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"github.com/gin-gonic/gin"

"github.com/go-vela/pkg-executor/executor"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
"github.com/go-vela/worker/executor"
exec "github.com/go-vela/worker/router/middleware/executor"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"context"
"time"

"github.com/go-vela/pkg-executor/executor"
"github.com/go-vela/pkg-runtime/runtime"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/version"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (w *Worker) exec(index int) error {

// setup the executor
//
// https://godoc.org/github.com/go-vela/pkg-executor/executor#New
// https://godoc.org/github.com/go-vela/worker/executor#New
_executor, err := executor.New(&executor.Setup{
Driver: w.Config.Executor.Driver,
Client: w.VelaClient,
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-worker/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package main
import (
"time"

"github.com/go-vela/pkg-executor/executor"
"github.com/go-vela/pkg-queue/queue"
"github.com/go-vela/pkg-runtime/runtime"
"github.com/go-vela/worker/executor"

"github.com/urfave/cli/v2"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-worker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"github.com/gin-gonic/gin"

"github.com/go-vela/pkg-executor/executor"
"github.com/go-vela/pkg-queue/queue"
"github.com/go-vela/pkg-runtime/runtime"
"github.com/go-vela/worker/executor"

"github.com/sirupsen/logrus"

Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"net/url"
"time"

"github.com/go-vela/pkg-executor/executor"
"github.com/go-vela/pkg-queue/queue"
"github.com/go-vela/pkg-runtime/runtime"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/worker/executor"
)

type (
Expand Down
67 changes: 67 additions & 0 deletions executor/context.go
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)
}
225 changes: 225 additions & 0 deletions executor/context_test.go
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)
}
}
12 changes: 12 additions & 0 deletions executor/doc.go
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
Loading

0 comments on commit bd78686

Please sign in to comment.