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

Allow creating mocks via runtime.New() #340

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions cmd/vela-worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (w *Worker) exec(index int) error {
// https://pkg.go.dev/github.com/go-vela/worker/runtime?tab=doc#New
w.Runtime, err = runtime.New(&runtime.Setup{
Logger: logger,
Mock: w.Config.Mock,
Driver: w.Config.Runtime.Driver,
ConfigFile: w.Config.Runtime.ConfigFile,
HostVolumes: w.Config.Runtime.HostVolumes,
Expand Down
42 changes: 32 additions & 10 deletions runtime/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-vela/types/constants"

"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
)

// Setup represents the configuration necessary for
Expand All @@ -24,6 +25,9 @@ type Setup struct {

// Runtime Configuration

// Mock should only be true for tests.
Mock bool

// specifies the driver to use for the runtime client
Driver string
// specifies the path to a configuration file to use for the runtime client
Expand All @@ -45,32 +49,50 @@ type Setup struct {
func (s *Setup) Docker() (Engine, error) {
logrus.Trace("creating docker runtime client from setup")

// create new Docker runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/docker?tab=doc#New
return docker.New(
opts := []docker.ClientOpt{
docker.WithHostVolumes(s.HostVolumes),
docker.WithPrivilegedImages(s.PrivilegedImages),
docker.WithLogger(s.Logger),
)
}

if s.Mock {
// create new mock Docker runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/docker?tab=doc#NewMock
return docker.NewMock(opts...)
}

// create new Docker runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/docker?tab=doc#New
return docker.New(opts...)
}

// Kubernetes creates and returns a Vela engine capable of
// integrating with a Kubernetes runtime environment.
func (s *Setup) Kubernetes() (Engine, error) {
logrus.Trace("creating kubernetes runtime client from setup")

// create new Kubernetes runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/kubernetes?tab=doc#New
return kubernetes.New(
opts := []kubernetes.ClientOpt{
kubernetes.WithConfigFile(s.ConfigFile),
kubernetes.WithHostVolumes(s.HostVolumes),
kubernetes.WithNamespace(s.Namespace),
kubernetes.WithPodsTemplate(s.PodsTemplateName, s.PodsTemplateFile),
kubernetes.WithPrivilegedImages(s.PrivilegedImages),
kubernetes.WithLogger(s.Logger),
)
}

if s.Mock {
// create new mock Kubernetes runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/kubernetes?tab=doc#NewMock
return kubernetes.NewMock(&v1.Pod{}, opts...)
}

// create new Kubernetes runtime engine
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime/kubernetes?tab=doc#New
return kubernetes.New(opts...)
}

// Validate verifies the necessary fields for the
Expand Down
58 changes: 42 additions & 16 deletions runtime/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,56 @@ import (
)

func TestRuntime_Setup_Docker(t *testing.T) {
// setup types
_setup := &Setup{
Driver: constants.DriverDocker,
tests := []struct {
name string
mock bool
}{
{name: "standard", mock: false},
{name: "mocked", mock: true},
}

// run test
_, err := _setup.Docker()
if err != nil {
t.Errorf("Docker returned err: %v", err)
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// setup types
_setup := &Setup{
Mock: test.mock,
Driver: constants.DriverDocker,
}

_, err := _setup.Docker()
if err != nil {
t.Errorf("Docker returned err: %v", err)
}
})
}
}

func TestRuntime_Setup_Kubernetes(t *testing.T) {
// setup types
_setup := &Setup{
Driver: constants.DriverKubernetes,
ConfigFile: "testdata/config",
Namespace: "docker",
tests := []struct {
name string
mock bool
}{
{name: "standard", mock: false},
{name: "mocked", mock: true},
}

// run test
_, err := _setup.Kubernetes()
if err != nil {
t.Errorf("Kubernetes returned err: %v", err)
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// setup types
_setup := &Setup{
Mock: test.mock,
Driver: constants.DriverKubernetes,
ConfigFile: "testdata/config",
Namespace: "docker",
}

_, err := _setup.Kubernetes()
if err != nil {
t.Errorf("Kubernetes returned err: %v", err)
}
})
}
}

Expand Down