Skip to content

Commit bd78686

Browse files
authored
feat(executor): add executor logic from pkg-executor (#220)
* feat(executor): add logic from pkg-executor * chore: clean go dependencies
1 parent fceece1 commit bd78686

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+11284
-16
lines changed

api/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
"github.com/gin-gonic/gin"
1212

13-
"github.com/go-vela/pkg-executor/executor"
1413
"github.com/go-vela/types"
1514
"github.com/go-vela/types/library"
15+
"github.com/go-vela/worker/executor"
1616
exec "github.com/go-vela/worker/router/middleware/executor"
1717
)
1818

cmd/vela-worker/exec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"context"
99
"time"
1010

11-
"github.com/go-vela/pkg-executor/executor"
1211
"github.com/go-vela/pkg-runtime/runtime"
12+
"github.com/go-vela/worker/executor"
1313
"github.com/go-vela/worker/version"
1414

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

4646
// setup the executor
4747
//
48-
// https://godoc.org/github.com/go-vela/pkg-executor/executor#New
48+
// https://godoc.org/github.com/go-vela/worker/executor#New
4949
_executor, err := executor.New(&executor.Setup{
5050
Driver: w.Config.Executor.Driver,
5151
Client: w.VelaClient,

cmd/vela-worker/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package main
77
import (
88
"time"
99

10-
"github.com/go-vela/pkg-executor/executor"
1110
"github.com/go-vela/pkg-queue/queue"
1211
"github.com/go-vela/pkg-runtime/runtime"
12+
"github.com/go-vela/worker/executor"
1313

1414
"github.com/urfave/cli/v2"
1515
)

cmd/vela-worker/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
"github.com/gin-gonic/gin"
1212

13-
"github.com/go-vela/pkg-executor/executor"
1413
"github.com/go-vela/pkg-queue/queue"
1514
"github.com/go-vela/pkg-runtime/runtime"
15+
"github.com/go-vela/worker/executor"
1616

1717
"github.com/sirupsen/logrus"
1818

cmd/vela-worker/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"net/url"
99
"time"
1010

11-
"github.com/go-vela/pkg-executor/executor"
1211
"github.com/go-vela/pkg-queue/queue"
1312
"github.com/go-vela/pkg-runtime/runtime"
1413
"github.com/go-vela/sdk-go/vela"
14+
"github.com/go-vela/worker/executor"
1515
)
1616

1717
type (

executor/context.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2021 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 executor
6+
7+
import (
8+
"context"
9+
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// key defines the key type for storing
14+
// the executor Engine in the context.
15+
const key = "executor"
16+
17+
// FromContext retrieves the executor Engine from the context.Context.
18+
func FromContext(c context.Context) Engine {
19+
// get executor value from context.Context
20+
v := c.Value(key)
21+
if v == nil {
22+
return nil
23+
}
24+
25+
// cast executor value to expected Engine type
26+
e, ok := v.(Engine)
27+
if !ok {
28+
return nil
29+
}
30+
31+
return e
32+
}
33+
34+
// FromGinContext retrieves the executor Engine from the gin.Context.
35+
func FromGinContext(c *gin.Context) Engine {
36+
// get executor value from gin.Context
37+
//
38+
// https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc#Context.Get
39+
v, ok := c.Get(key)
40+
if !ok {
41+
return nil
42+
}
43+
44+
// cast executor value to expected Engine type
45+
e, ok := v.(Engine)
46+
if !ok {
47+
return nil
48+
}
49+
50+
return e
51+
}
52+
53+
// WithContext inserts the executor Engine into the context.Context.
54+
func WithContext(c context.Context, e Engine) context.Context {
55+
// set the executor Engine in the context.Context
56+
//
57+
// nolint: golint,staticcheck // ignore using string with context value
58+
return context.WithValue(c, key, e)
59+
}
60+
61+
// WithGinContext inserts the executor Engine into the gin.Context.
62+
func WithGinContext(c *gin.Context, e Engine) {
63+
// set the executor Engine in the gin.Context
64+
//
65+
// https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc#Context.Set
66+
c.Set(key, e)
67+
}

executor/context_test.go

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// Copyright (c) 2021 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 executor
6+
7+
import (
8+
"context"
9+
"net/http/httptest"
10+
"reflect"
11+
"testing"
12+
13+
"github.com/gin-gonic/gin"
14+
15+
"github.com/go-vela/mock/server"
16+
17+
"github.com/go-vela/worker/executor/linux"
18+
19+
"github.com/go-vela/pkg-runtime/runtime/docker"
20+
21+
"github.com/go-vela/sdk-go/vela"
22+
)
23+
24+
func TestExecutor_FromContext(t *testing.T) {
25+
// setup types
26+
gin.SetMode(gin.TestMode)
27+
28+
s := httptest.NewServer(server.FakeHandler())
29+
30+
_client, err := vela.NewClient(s.URL, "", nil)
31+
if err != nil {
32+
t.Errorf("unable to create Vela API client: %v", err)
33+
}
34+
35+
_runtime, err := docker.NewMock()
36+
if err != nil {
37+
t.Errorf("unable to create runtime engine: %v", err)
38+
}
39+
40+
_engine, err := linux.New(
41+
linux.WithBuild(_build),
42+
linux.WithPipeline(_pipeline),
43+
linux.WithRepo(_repo),
44+
linux.WithRuntime(_runtime),
45+
linux.WithUser(_user),
46+
linux.WithVelaClient(_client),
47+
)
48+
if err != nil {
49+
t.Errorf("unable to create linux engine: %v", err)
50+
}
51+
52+
// setup tests
53+
tests := []struct {
54+
context context.Context
55+
want Engine
56+
}{
57+
{
58+
// nolint: golint,staticcheck // ignore using string with context value
59+
context: context.WithValue(context.Background(), key, _engine),
60+
want: _engine,
61+
},
62+
{
63+
context: context.Background(),
64+
want: nil,
65+
},
66+
{
67+
// nolint: golint,staticcheck // ignore using string with context value
68+
context: context.WithValue(context.Background(), key, "foo"),
69+
want: nil,
70+
},
71+
}
72+
73+
// run tests
74+
for _, test := range tests {
75+
got := FromContext(test.context)
76+
77+
if !reflect.DeepEqual(got, test.want) {
78+
t.Errorf("FromContext is %v, want %v", got, test.want)
79+
}
80+
}
81+
}
82+
83+
func TestExecutor_FromGinContext(t *testing.T) {
84+
// setup types
85+
gin.SetMode(gin.TestMode)
86+
87+
s := httptest.NewServer(server.FakeHandler())
88+
89+
_client, err := vela.NewClient(s.URL, "", nil)
90+
if err != nil {
91+
t.Errorf("unable to create Vela API client: %v", err)
92+
}
93+
94+
_runtime, err := docker.NewMock()
95+
if err != nil {
96+
t.Errorf("unable to create runtime engine: %v", err)
97+
}
98+
99+
_engine, err := linux.New(
100+
linux.WithBuild(_build),
101+
linux.WithPipeline(_pipeline),
102+
linux.WithRepo(_repo),
103+
linux.WithRuntime(_runtime),
104+
linux.WithUser(_user),
105+
linux.WithVelaClient(_client),
106+
)
107+
if err != nil {
108+
t.Errorf("unable to create linux engine: %v", err)
109+
}
110+
111+
// setup tests
112+
tests := []struct {
113+
context *gin.Context
114+
value interface{}
115+
want Engine
116+
}{
117+
{
118+
context: new(gin.Context),
119+
value: _engine,
120+
want: _engine,
121+
},
122+
{
123+
context: new(gin.Context),
124+
value: nil,
125+
want: nil,
126+
},
127+
{
128+
context: new(gin.Context),
129+
value: "foo",
130+
want: nil,
131+
},
132+
}
133+
134+
// run tests
135+
for _, test := range tests {
136+
if test.value != nil {
137+
test.context.Set(key, test.value)
138+
}
139+
140+
got := FromGinContext(test.context)
141+
142+
if !reflect.DeepEqual(got, test.want) {
143+
t.Errorf("FromGinContext is %v, want %v", got, test.want)
144+
}
145+
}
146+
}
147+
148+
func TestExecutor_WithContext(t *testing.T) {
149+
// setup types
150+
gin.SetMode(gin.TestMode)
151+
152+
s := httptest.NewServer(server.FakeHandler())
153+
154+
_client, err := vela.NewClient(s.URL, "", nil)
155+
if err != nil {
156+
t.Errorf("unable to create Vela API client: %v", err)
157+
}
158+
159+
_runtime, err := docker.NewMock()
160+
if err != nil {
161+
t.Errorf("unable to create runtime engine: %v", err)
162+
}
163+
164+
_engine, err := linux.New(
165+
linux.WithBuild(_build),
166+
linux.WithPipeline(_pipeline),
167+
linux.WithRepo(_repo),
168+
linux.WithRuntime(_runtime),
169+
linux.WithUser(_user),
170+
linux.WithVelaClient(_client),
171+
)
172+
if err != nil {
173+
t.Errorf("unable to create linux engine: %v", err)
174+
}
175+
176+
// nolint: golint,staticcheck // ignore using string with context value
177+
want := context.WithValue(context.Background(), key, _engine)
178+
179+
// run test
180+
got := WithContext(context.Background(), _engine)
181+
182+
if !reflect.DeepEqual(got, want) {
183+
t.Errorf("WithContext is %v, want %v", got, want)
184+
}
185+
}
186+
187+
func TestExecutor_WithGinContext(t *testing.T) {
188+
// setup types
189+
gin.SetMode(gin.TestMode)
190+
191+
s := httptest.NewServer(server.FakeHandler())
192+
193+
_client, err := vela.NewClient(s.URL, "", nil)
194+
if err != nil {
195+
t.Errorf("unable to create Vela API client: %v", err)
196+
}
197+
198+
_runtime, err := docker.NewMock()
199+
if err != nil {
200+
t.Errorf("unable to create runtime engine: %v", err)
201+
}
202+
203+
_engine, err := linux.New(
204+
linux.WithBuild(_build),
205+
linux.WithPipeline(_pipeline),
206+
linux.WithRepo(_repo),
207+
linux.WithRuntime(_runtime),
208+
linux.WithUser(_user),
209+
linux.WithVelaClient(_client),
210+
)
211+
if err != nil {
212+
t.Errorf("unable to create linux engine: %v", err)
213+
}
214+
215+
want := new(gin.Context)
216+
want.Set(key, _engine)
217+
218+
// run test
219+
got := new(gin.Context)
220+
WithGinContext(got, _engine)
221+
222+
if !reflect.DeepEqual(got, want) {
223+
t.Errorf("WithGinContext is %v, want %v", got, want)
224+
}
225+
}

executor/doc.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2021 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 executor provides the ability for Vela to
6+
// integrate with different supported operating
7+
// systems.
8+
//
9+
// Usage:
10+
//
11+
// import "github.com/go-vela/worker/executor"
12+
package executor

0 commit comments

Comments
 (0)