-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Kevin/eng 2022 investigate tpl values templates (#187)
* 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
1 parent
ff12875
commit d765f7b
Showing
8 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |