Skip to content

Commit

Permalink
fix: Kevin/eng 2022 investigate tpl values templates (#187)
Browse files Browse the repository at this point in the history
* Adding Example tests in helm_tests.go
I also added a tpl_test.go which was not picked up by the suite
not sure whwere the filenames/dirs for testing are defined

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* readme

* tests working, but failing for .tpl that uses

* test working as expected
TODO: use values file and add tests for `include` directive

* update tpl test

* test tamplate file

* add expect err not to happen for _helpers.tpl

* go work readme

* go work ignore

* testing added include tpl function

* update renderTpl and tpl tests to test for  directive

* update renderTpl and tpl tests to test for  directive

* update include .tpl and tests

* add a test file that should not be included when rendering .tpl files

* update test templates

* update indenting

* update go mod to use polly@370e32355cbc492749a9ed08dbfbc93389c8cc08

* test tamplate file

* update the include directive to do a lookup of the named template as opposed to including the template file

* update polly template version in go mod
  • Loading branch information
seemywingz authored May 17, 2024
1 parent ff12875 commit d765f7b
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# IDE
.idea/
.vscode/
.DS_store

# Go Workspace Files
go.work
go.work.sum

# Binaries for programs and plugins
bin/
Expand All @@ -19,4 +24,4 @@ bin/
# Dependency directories (remove the comment below to include it)
# vendor/

notes.md
notes.md
168 changes: 168 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,169 @@
# Deployment Operator

# Testing Local Changes to Dependencies
## Using [Go Work](https://go.dev/doc/tutorial/workspaces)
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
```sh
cd ~/git/plrl
git clone [email protected]: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
```sh
go use ../polly
```
My _go.work_ file now looks like this
```go
// ./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:
```sh
kubectl cluster-info
```

### Install dependencies with make
```sh
make tools
```
### Setup Environment
Set the `KUBEBUILDER_ASSETS` directory
```sh
# 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
```sh
make test
```

## Adding Tests
Reference the [Ginkgo Getting Started](https://onsi.github.io/ginkgo/#getting-started) to see the expected structure
### Install the Ginkgo CLI
```sh
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
```sh
cd pkg/package/that/needs/suite
ginkgo bootstrap
```

### Generate A Basic test
I'm creating a test for ./pkg/manifests/template/tpl.go
```sh
cd ./pkg/manifests/template
ginkgo generate tpl
```
example output
```sh
Generating ginkgo test for Tpl in:
tpl_test.go
```
It generates: `./pkg/manifests/template/tpl_test.go`
```sh
# ./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
```sh
# ./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
```sh
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
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/pluralsh/console-client-go v0.5.6
github.com/pluralsh/controller-reconcile-helper v0.0.4
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
github.com/pluralsh/polly v0.1.9
github.com/pluralsh/polly v0.1.10
github.com/samber/lo v1.39.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxD
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
github.com/pluralsh/polly v0.1.9 h1:x968ohGfOtX/YwNbZBPCvRPFhQglvYZ33KLf4Yt/5q0=
github.com/pluralsh/polly v0.1.9/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
github.com/pluralsh/polly v0.1.10 h1:8Or7SPy7OCbQiAU2Fu7vMUsyZ88WgF66Wwjx9aVus9c=
github.com/pluralsh/polly v0.1.10/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
Expand Down
2 changes: 1 addition & 1 deletion pkg/manifests/template/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ var _ = Describe("Helm template", func() {
Expect(err).NotTo(HaveOccurred())
Expect(len(resp)).To(Equal(10))
})

})

})
68 changes: 68 additions & 0 deletions pkg/manifests/template/tpl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package template

import (
"fmt"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
console "github.com/pluralsh/console-client-go"
)

var _ = Describe(".tpl Template Rendering", func() {
var svc *console.GetServiceDeploymentForAgent_ServiceDeployment

BeforeEach(func() {
// Setup the mock service deployment each time
svc = mockServiceDeployment()
})

Describe("Render .tpl with valid data", func() {
templateFile := "_simpleConfigMap.tpl"
It(fmt.Sprintf("should render %s correctly", templateFile), func() {
tplFile := filepath.Join("..", "..", "..", "test", "tpl", templateFile)
tplData, err := os.ReadFile(tplFile)
Expect(err).NotTo(HaveOccurred())

rendered, err := renderTpl(tplData, svc)
fmt.Println("ℹ️ rendered template:", templateFile)
fmt.Println(string(rendered))
Expect(err).NotTo(HaveOccurred())
Expect(string(rendered)).To(ContainSubstring("name: test-config-configmap"))
Expect(string(rendered)).To(ContainSubstring("version: \"v1\""))
})
})

Describe("Render template with include", func() {
templateFile := "_templateWithInclude.tpl"
It(fmt.Sprintf("should render %s correctly", templateFile), func() {
tplFile := filepath.Join("..", "..", "..", "test", "tpl", templateFile)
tplData, err := os.ReadFile(tplFile)
Expect(err).NotTo(HaveOccurred())

rendered, err := renderTpl(tplData, svc)
fmt.Println("ℹ️ rendered template:", templateFile)
fmt.Println(string(rendered))
Expect(err).NotTo(HaveOccurred())
Expect(string(rendered)).To(ContainSubstring("name: test-config-main"))
Expect(string(rendered)).To(ContainSubstring("more-data: test-config-included"))
Expect(string(rendered)).To(ContainSubstring("version: \"v1\""))
})
})
})

func mockServiceDeployment() *console.GetServiceDeploymentForAgent_ServiceDeployment {
return &console.GetServiceDeploymentForAgent_ServiceDeployment{
Namespace: "default",
Name: "test-service",
Cluster: &console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster{
ID: "123",
Name: "test-cluster",
},
Configuration: []*console.GetServiceDeploymentForAgent_ServiceDeployment_Configuration{
{Name: "name", Value: "test-config"},
{Name: "version", Value: "v1"},
},
}
}
11 changes: 11 additions & 0 deletions test/tpl/_simpleConfigMap.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Configuration.name }}-configmap
labels:
foo: "true"
data:
myvalue: "Hello World"
{{- range $key, $val := .Configuration }}
{{ $key }}: {{ $val | quote }}
{{- end }}
13 changes: 13 additions & 0 deletions test/tpl/_templateWithInclude.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# test/tpl/_templateWithInclude.tpl
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Configuration.name }}-main
data:
included:
{{ include "more-data" . | indent 4 }}

{{- define "more-data" -}}
version: {{ .Configuration.version | quote }}
more-data: {{ .Configuration.name }}-included
{{- end }}

0 comments on commit d765f7b

Please sign in to comment.