Skip to content

Commit

Permalink
refactor: move kubernetes runtime mock code to separate file (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Oct 26, 2022
1 parent a83e90a commit 86c6fa4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 76 deletions.
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ coverage:
# disable the changes status report
changes: off

# This section provides the configuration that tells
# codecov to ignore files matching these files or patterns.
#
# https://docs.codecov.io/docs/codecovyml-reference#section-ignore
ignore:
# this is only for tests, so reporting coverage is misleading.
- runtime/kubernetes/mock.go

# This section provides the configuration for the
# parsers codecov uses for the coverage report.
#
Expand Down
76 changes: 0 additions & 76 deletions runtime/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ package kubernetes

import (
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

velav1alpha1 "github.com/go-vela/worker/runtime/kubernetes/apis/vela/v1alpha1"
velaK8sClient "github.com/go-vela/worker/runtime/kubernetes/generated/clientset/versioned"
fakeVelaK8sClient "github.com/go-vela/worker/runtime/kubernetes/generated/clientset/versioned/fake"
)

type config struct {
Expand Down Expand Up @@ -131,75 +127,3 @@ func New(opts ...ClientOpt) (*client, error) {

return c, nil
}

// NewMock returns an Engine implementation that
// integrates with a Kubernetes runtime.
//
// This function is intended for running tests only.
//
//nolint:revive // ignore returning unexported client
func NewMock(_pod *v1.Pod, opts ...ClientOpt) (*client, error) {
// create new Kubernetes client
c := new(client)

// create new fields
c.config = new(config)
c.Pod = new(v1.Pod)

c.containersLookup = map[string]int{}
for i, ctn := range _pod.Spec.Containers {
c.containersLookup[ctn.Name] = i
}

// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#StandardLogger
logger := logrus.StandardLogger()

// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#NewEntry
c.Logger = logrus.NewEntry(logger)

// set the Kubernetes namespace in the runtime client
c.config.Namespace = "test"

// set the Kubernetes pod in the runtime client
c.Pod = _pod.DeepCopy()
c.Pod.SetResourceVersion("0")

// apply all provided configuration options
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}

// set the Kubernetes fake client in the runtime client
//
// https://pkg.go.dev/k8s.io/client-go/kubernetes/fake?tab=doc#NewSimpleClientset
c.Kubernetes = fake.NewSimpleClientset(c.Pod)

// set the VelaKubernetes fake client in the runtime client
c.VelaKubernetes = fakeVelaK8sClient.NewSimpleClientset(
&velav1alpha1.PipelinePodsTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.config.Namespace,
Name: "mock-pipeline-pods-template",
},
},
)

// set the PodTracker (normally populated in SetupBuild)
tracker, err := mockPodTracker(c.Logger, c.Kubernetes, c.Pod)
if err != nil {
return c, err
}

c.PodTracker = tracker

// The test is responsible for calling c.PodTracker.Start() if needed

return c, nil
}
87 changes: 87 additions & 0 deletions runtime/kubernetes/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package kubernetes

import (
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

velav1alpha1 "github.com/go-vela/worker/runtime/kubernetes/apis/vela/v1alpha1"
fakeVelaK8sClient "github.com/go-vela/worker/runtime/kubernetes/generated/clientset/versioned/fake"
)

// NewMock returns an Engine implementation that
// integrates with a Kubernetes runtime.
//
// This function is intended for running tests only.
//
//nolint:revive // ignore returning unexported client
func NewMock(_pod *v1.Pod, opts ...ClientOpt) (*client, error) {
// create new Kubernetes client
c := new(client)

// create new fields
c.config = new(config)
c.Pod = new(v1.Pod)

c.containersLookup = map[string]int{}
for i, ctn := range _pod.Spec.Containers {
c.containersLookup[ctn.Name] = i
}

// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#StandardLogger
logger := logrus.StandardLogger()

// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#NewEntry
c.Logger = logrus.NewEntry(logger)

// set the Kubernetes namespace in the runtime client
c.config.Namespace = "test"

// set the Kubernetes pod in the runtime client
c.Pod = _pod.DeepCopy()
c.Pod.SetResourceVersion("0")

// apply all provided configuration options
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}

// set the Kubernetes fake client in the runtime client
//
// https://pkg.go.dev/k8s.io/client-go/kubernetes/fake?tab=doc#NewSimpleClientset
c.Kubernetes = fake.NewSimpleClientset(c.Pod)

// set the VelaKubernetes fake client in the runtime client
c.VelaKubernetes = fakeVelaK8sClient.NewSimpleClientset(
&velav1alpha1.PipelinePodsTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.config.Namespace,
Name: "mock-pipeline-pods-template",
},
},
)

// set the PodTracker (normally populated in SetupBuild)
tracker, err := mockPodTracker(c.Logger, c.Kubernetes, c.Pod)
if err != nil {
return c, err
}

c.PodTracker = tracker

// The test is responsible for calling c.PodTracker.Start() if needed

return c, nil
}

0 comments on commit 86c6fa4

Please sign in to comment.