From 72394b93f29a2bc72f5b26efb94a4920629fdb95 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 9 May 2022 17:05:03 -0500 Subject: [PATCH 1/2] tests: make runtime more testable via runtime.New() --- cmd/vela-worker/exec.go | 1 + runtime/setup.go | 42 +++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cmd/vela-worker/exec.go b/cmd/vela-worker/exec.go index c5093f3f..dc2e3602 100644 --- a/cmd/vela-worker/exec.go +++ b/cmd/vela-worker/exec.go @@ -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, diff --git a/runtime/setup.go b/runtime/setup.go index 3a88c75a..7207f39c 100644 --- a/runtime/setup.go +++ b/runtime/setup.go @@ -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 @@ -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 @@ -45,14 +49,23 @@ 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 @@ -60,17 +73,26 @@ func (s *Setup) Docker() (Engine, error) { 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 From eeb4e89efa17e8f2336dc02e0bd370a9cbc81286 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 9 May 2022 17:22:31 -0500 Subject: [PATCH 2/2] tests: add mocked runtime tests --- runtime/setup_test.go | 58 +++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/runtime/setup_test.go b/runtime/setup_test.go index 8b87fd2b..2484fe2d 100644 --- a/runtime/setup_test.go +++ b/runtime/setup_test.go @@ -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) + } + }) } }