Skip to content

Commit 8ec88e6

Browse files
authored
feat: add unit tests for helm and kustomize templates (#101)
* add unit tests for helm and kustomize * goimport
1 parent 9aaee96 commit 8ec88e6

File tree

9 files changed

+205
-1
lines changed

9 files changed

+205
-1
lines changed

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/Masterminds/sprig/v3 v3.2.3
77
github.com/evanphx/json-patch v5.6.0+incompatible
88
github.com/fluxcd/flagger v1.35.0
9+
github.com/gin-gonic/gin v1.7.7
910
github.com/gofrs/flock v0.8.1
1011
github.com/onsi/ginkgo/v2 v2.12.1
1112
github.com/onsi/gomega v1.27.10
@@ -65,13 +66,17 @@ require (
6566
github.com/fatih/color v1.13.0 // indirect
6667
github.com/fsnotify/fsnotify v1.6.0 // indirect
6768
github.com/fvbommel/sortorder v1.0.1 // indirect
69+
github.com/gin-contrib/sse v0.1.0 // indirect
6870
github.com/go-errors/errors v1.4.2 // indirect
6971
github.com/go-gorp/gorp/v3 v3.0.5 // indirect
7072
github.com/go-logr/logr v1.3.0 // indirect
7173
github.com/go-logr/zapr v1.3.0 // indirect
7274
github.com/go-openapi/jsonpointer v0.19.6 // indirect
7375
github.com/go-openapi/jsonreference v0.20.2 // indirect
7476
github.com/go-openapi/swag v0.22.3 // indirect
77+
github.com/go-playground/locales v0.13.0 // indirect
78+
github.com/go-playground/universal-translator v0.17.0 // indirect
79+
github.com/go-playground/validator/v10 v10.4.1 // indirect
7580
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
7681
github.com/gobwas/glob v0.2.3 // indirect
7782
github.com/gogo/protobuf v1.3.2 // indirect
@@ -104,6 +109,7 @@ require (
104109
github.com/klauspost/compress v1.11.13 // indirect
105110
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
106111
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
112+
github.com/leodido/go-urn v1.2.0 // indirect
107113
github.com/lib/pq v1.10.7 // indirect
108114
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
109115
github.com/magiconair/properties v1.8.7 // indirect
@@ -149,6 +155,7 @@ require (
149155
github.com/spf13/viper v1.15.0 // indirect
150156
github.com/spyzhov/ajson v0.7.2 // indirect
151157
github.com/subosito/gotenv v1.4.2 // indirect
158+
github.com/ugorji/go/codec v1.1.7 // indirect
152159
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
153160
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
154161
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect

go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
200200
github.com/fvbommel/sortorder v1.0.1 h1:dSnXLt4mJYH25uDDGa3biZNQsozaUWDSWeKJ0qqFfzE=
201201
github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
202202
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
203+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
204+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
205+
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
206+
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
203207
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
204208
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
205209
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -221,6 +225,14 @@ github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2Kv
221225
github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k=
222226
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
223227
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
228+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
229+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
230+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
231+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
232+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
233+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
234+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
235+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
224236
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
225237
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
226238
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -403,6 +415,7 @@ github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2E
403415
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
404416
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
405417
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
418+
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
406419
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
407420
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
408421
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
@@ -440,6 +453,8 @@ github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq
440453
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
441454
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
442455
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
456+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
457+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
443458
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
444459
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
445460
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
@@ -684,6 +699,9 @@ github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8
684699
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
685700
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
686701
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
702+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
703+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
704+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
687705
github.com/vektah/gqlparser/v2 v2.5.1 h1:ZGu+bquAY23jsxDRcYpWjttRZrUz07LbiY77gUOHcr4=
688706
github.com/vektah/gqlparser/v2 v2.5.1/go.mod h1:mPgqFBu/woKTVYWyNk8cO3kh4S/f4aRFZrvOnp3hmCs=
689707
github.com/vektra/mockery/v2 v2.39.0 h1:AUyq9PQ/VkjrqmKedxoulg83G7uouk4o7hBs1uUMeog=
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package template
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
"path/filepath"
8+
"time"
9+
10+
"github.com/gin-gonic/gin"
11+
. "github.com/onsi/ginkgo/v2"
12+
. "github.com/onsi/gomega"
13+
console "github.com/pluralsh/console-client-go"
14+
)
15+
16+
var _ = Describe("Helm template", func() {
17+
18+
dir := filepath.Join("..", "..", "..", "charts", "deployment-operator")
19+
svc := &console.ServiceDeploymentExtended{
20+
Namespace: "default",
21+
Name: "test",
22+
}
23+
24+
r := gin.Default()
25+
r.GET("/version", func(c *gin.Context) {
26+
c.JSON(http.StatusOK, gin.H{
27+
"major": "1",
28+
"minor": "21",
29+
})
30+
})
31+
32+
srv := &http.Server{
33+
Addr: ":8080",
34+
Handler: r,
35+
}
36+
37+
BeforeEach(func() {
38+
// Initializing the server in a goroutine so that
39+
// it won't block the graceful shutdown handling below
40+
go func() {
41+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
42+
Expect(err).NotTo(HaveOccurred())
43+
}
44+
}()
45+
})
46+
AfterEach(func() {
47+
48+
// The context is used to inform the server it has 5 seconds to finish
49+
// the request it is currently handling
50+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
51+
defer cancel()
52+
if err := srv.Shutdown(ctx); err != nil {
53+
log.Fatal("Server forced to shutdown: ", err)
54+
}
55+
56+
log.Println("Server exiting")
57+
})
58+
59+
Context("Render helm template", func() {
60+
It("should successfully render the helm template", func() {
61+
resp, err := NewHelm(dir).Render(svc, utilFactory)
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(len(resp)).To(Equal(6))
64+
})
65+
66+
})
67+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package template
2+
3+
import (
4+
"path/filepath"
5+
"sort"
6+
"strings"
7+
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
console "github.com/pluralsh/console-client-go"
11+
)
12+
13+
var _ = Describe("Kustomize template", func() {
14+
15+
dir := filepath.Join("..", "..", "..", "test", "kustomize", "overlays")
16+
svc := &console.ServiceDeploymentExtended{
17+
Namespace: "default",
18+
Kustomize: &console.KustomizeFragment{
19+
Path: "",
20+
},
21+
}
22+
Context("Render kustomize template", func() {
23+
It("should successfully render the dev template", func() {
24+
svc.Kustomize.Path = "dev"
25+
resp, err := NewKustomize(dir).Render(svc, utilFactory)
26+
Expect(err).NotTo(HaveOccurred())
27+
Expect(len(resp)).To(Equal(3))
28+
sort.Slice(resp, func(i, j int) bool {
29+
return resp[i].GetKind() < resp[j].GetKind()
30+
})
31+
Expect(resp[0].GetKind()).To(Equal("ConfigMap"))
32+
Expect(strings.HasPrefix(resp[0].GetName(), "app-config")).Should(BeTrue())
33+
Expect(resp[1].GetKind()).To(Equal("Deployment"))
34+
Expect(resp[2].GetKind()).To(Equal("Secret"))
35+
Expect(strings.HasPrefix(resp[2].GetName(), "credentials")).Should(BeTrue())
36+
})
37+
38+
})
39+
})

pkg/manifests/template/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package template
1818

1919
import (
2020
"fmt"
21-
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
2221
"path/filepath"
2322
"runtime"
2423
"testing"
2524

25+
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
26+
2627
. "github.com/onsi/ginkgo/v2"
2728
. "github.com/onsi/gomega"
2829
"k8s.io/client-go/kubernetes/scheme"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: my-app
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: my-app
10+
template:
11+
metadata:
12+
labels:
13+
app: my-app
14+
spec:
15+
containers:
16+
- name: my-app
17+
image: alpine:3.10
18+
tty: true
19+
stdin: true
20+
env:
21+
- name: foo
22+
value: bar
23+
resources:
24+
limits:
25+
memory: "64Mi"
26+
cpu: "100m"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
- deployment.yaml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: my-app
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: my-app
10+
env:
11+
- name: foo
12+
value: we-are-in-dev
13+
envFrom:
14+
- configMapRef:
15+
name: app-config
16+
- secretRef:
17+
name: credentials
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
bases:
5+
- ../../base
6+
7+
namespace: my-app-dev
8+
9+
nameSuffix: -dev
10+
11+
patchesStrategicMerge:
12+
- deployment_env.yaml
13+
14+
15+
configMapGenerator:
16+
- name: app-config
17+
literals:
18+
- username=demo-user
19+
20+
secretGenerator:
21+
- name: credentials
22+
type: Opaque
23+
literals:
24+
- password=demo

0 commit comments

Comments
 (0)