Skip to content

thin kubernetes agent to execute deployments of plural services

Notifications You must be signed in to change notification settings

pluralsh/deployment-operator

Repository files navigation

Deployment Operator

Testing Local Changes to Dependencies

Using Go Work

I'm updating the polly for the .tpl template rendering
Clone the polly repo locally
Create a Go Workspace In the repo that has polly as a dependency

cd ~/git/plrl
git clone git@github.com:pluralsh/polly.git
cd deployment-operator 
go work init .

This creates a go workspace file named go.work in the deployment-operator repo
Tell go to use the locally cloned version of the polly repo

go use ../polly

My go.work file now looks like this

// ./go.work

go 1.22.2

use (
	.
	../polly
)

Now the Go Workspace settings will allow me to use the local version of the polly source code when compiling and testing

Unit Tests

Pre Reqs

Ensure that the cluster in your current kube context is reachable

Helm tests will run against this cluster
You can test with:

kubectl cluster-info

Install dependencies with make

make tools

Setup Environment

Set the KUBEBUILDER_ASSETS directory

# Mac
export KUBEBUILDER_ASSETS=${GOBIN}/k8s/1.28.3-darwin-arm64

# Linux
export KUBEBUILDER_ASSETS=${GOBIN}/k8s/1.28.3-linux-amd64

Running Unit Tests

make test

Adding Tests

Reference the Ginkgo Getting Started to see the expected structure

Install the Ginkgo CLI

go install github.com/onsi/ginkgo/v2/ginkgo

The Test Suites for several Packages are already Generated in the Deployment-Operator Repo

If creating a new package or testing a package that doesn't already have a test suite

cd pkg/package/that/needs/suite
ginkgo bootstrap

Generate A Basic test

I'm creating a test for ./pkg/manifests/template/tpl.go

cd ./pkg/manifests/template
ginkgo generate tpl

example output

Generating ginkgo test for Tpl in:
  tpl_test.go

It generates: ./pkg/manifests/template/tpl_test.go

# ./pkg/manifests/template/tpl_test.go
package template_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"

	"github.com/pluralsh/deployment-operator/pkg/manifests/template"
)

var _ = Describe("Tpl", func() {

})

From here you can begin adding specs (test) to your generated file

# ./pkg/manifests/template/tpl_test.go
var _ = Describe("Tpl", func() {

	Context("Example Test", func() {
		It("Should always Pass", func() {
			Expect(1).To(Equal(1))
		})
	})

    Context("Test Should Fail for example output", func() {
		It("Should always fail", func() {
			Expect(1).To(Equal(2))
		})
	})

})

Run the Suite with your New Test

I'm doing this here just for an example and to check that my tests are bing added to the Suite

make test
# ... other output

[GIN-debug] GET    /version                  --> github.com/pluralsh/deployment-operator/pkg/manifests/template.init.func1.1 (3 handlers)
Running Suite: Controller Suite - /Users/kjj/git/plrl/deployment-operator/pkg/manifests/template
================================================================================================
Random Seed: 1715288079

Will run 6 of 6 specs
# Warning: 'bases' is deprecated. Please use 'resources' instead. Run 'kustomize edit fix' to update your Kustomization automatically.
# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead. Run 'kustomize edit fix' to update your Kustomization automatically.
••
------------------------------
• [FAILED] [0.000 seconds]
Tpl Test Should Fail for example output [It] Should always Fail
/Users/kjj/git/plrl/deployment-operator/pkg/manifests/template/tpl_test.go:17

  [FAILED] Expected
      <int>: 1
  to equal
      <int>: 2
  In [It] at: /Users/kjj/git/plrl/deployment-operator/pkg/manifests/template/tpl_test.go:18 @ 05/09/24 16:54:41.29
------------------------------
2024/05/09 16:54:41 render helm templates: enable dependency update= false dependencies= 0
Found unknown types unknown resource types: apiextensions.k8s.io/v1/CustomResourceDefinition,apiextensions.k8s.io/v1/CustomResourceDefinition, ignoring for now2024/05/09 16:54:41 Server exiting
•••

Summarizing 1 Failure:
  [FAIL] Tpl Test Should Fail for example output [It] Should always Fail
  /Users/kjj/git/plrl/deployment-operator/pkg/manifests/template/tpl_test.go:18

Ran 6 of 6 Specs in 2.810 seconds
FAIL! -- 5 Passed | 1 Failed | 0 Pending | 0 Skipped
--- FAIL: TestControllers (2.81s)
FAIL
FAIL    github.com/pluralsh/deployment-operator/pkg/manifests/template  3.421s
FAIL
make: *** [test] Error 1