-
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.
feat: add unit tests for helm and kustomize templates (#101)
* add unit tests for helm and kustomize * goimport
- Loading branch information
Showing
9 changed files
with
205 additions
and
1 deletion.
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
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,67 @@ | ||
package template | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
console "github.com/pluralsh/console-client-go" | ||
) | ||
|
||
var _ = Describe("Helm template", func() { | ||
|
||
dir := filepath.Join("..", "..", "..", "charts", "deployment-operator") | ||
svc := &console.ServiceDeploymentExtended{ | ||
Namespace: "default", | ||
Name: "test", | ||
} | ||
|
||
r := gin.Default() | ||
r.GET("/version", func(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"major": "1", | ||
"minor": "21", | ||
}) | ||
}) | ||
|
||
srv := &http.Server{ | ||
Addr: ":8080", | ||
Handler: r, | ||
} | ||
|
||
BeforeEach(func() { | ||
// Initializing the server in a goroutine so that | ||
// it won't block the graceful shutdown handling below | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}() | ||
}) | ||
AfterEach(func() { | ||
|
||
// The context is used to inform the server it has 5 seconds to finish | ||
// the request it is currently handling | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatal("Server forced to shutdown: ", err) | ||
} | ||
|
||
log.Println("Server exiting") | ||
}) | ||
|
||
Context("Render helm template", func() { | ||
It("should successfully render the helm template", func() { | ||
resp, err := NewHelm(dir).Render(svc, utilFactory) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(resp)).To(Equal(6)) | ||
}) | ||
|
||
}) | ||
}) |
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,39 @@ | ||
package template | ||
|
||
import ( | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
console "github.com/pluralsh/console-client-go" | ||
) | ||
|
||
var _ = Describe("Kustomize template", func() { | ||
|
||
dir := filepath.Join("..", "..", "..", "test", "kustomize", "overlays") | ||
svc := &console.ServiceDeploymentExtended{ | ||
Namespace: "default", | ||
Kustomize: &console.KustomizeFragment{ | ||
Path: "", | ||
}, | ||
} | ||
Context("Render kustomize template", func() { | ||
It("should successfully render the dev template", func() { | ||
svc.Kustomize.Path = "dev" | ||
resp, err := NewKustomize(dir).Render(svc, utilFactory) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(resp)).To(Equal(3)) | ||
sort.Slice(resp, func(i, j int) bool { | ||
return resp[i].GetKind() < resp[j].GetKind() | ||
}) | ||
Expect(resp[0].GetKind()).To(Equal("ConfigMap")) | ||
Expect(strings.HasPrefix(resp[0].GetName(), "app-config")).Should(BeTrue()) | ||
Expect(resp[1].GetKind()).To(Equal("Deployment")) | ||
Expect(resp[2].GetKind()).To(Equal("Secret")) | ||
Expect(strings.HasPrefix(resp[2].GetName(), "credentials")).Should(BeTrue()) | ||
}) | ||
|
||
}) | ||
}) |
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,26 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: my-app | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: my-app | ||
template: | ||
metadata: | ||
labels: | ||
app: my-app | ||
spec: | ||
containers: | ||
- name: my-app | ||
image: alpine:3.10 | ||
tty: true | ||
stdin: true | ||
env: | ||
- name: foo | ||
value: bar | ||
resources: | ||
limits: | ||
memory: "64Mi" | ||
cpu: "100m" |
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,5 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
resources: | ||
- deployment.yaml |
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,17 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: my-app | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: my-app | ||
env: | ||
- name: foo | ||
value: we-are-in-dev | ||
envFrom: | ||
- configMapRef: | ||
name: app-config | ||
- secretRef: | ||
name: credentials |
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,24 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
bases: | ||
- ../../base | ||
|
||
namespace: my-app-dev | ||
|
||
nameSuffix: -dev | ||
|
||
patchesStrategicMerge: | ||
- deployment_env.yaml | ||
|
||
|
||
configMapGenerator: | ||
- name: app-config | ||
literals: | ||
- username=demo-user | ||
|
||
secretGenerator: | ||
- name: credentials | ||
type: Opaque | ||
literals: | ||
- password=demo |