Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for helm utils #987

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions pkg/helm/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package helm

import (
"os"
"reflect"
"testing"
)

func Test_Load(t *testing.T) {
tests := []struct {
name string
content string
want ValuesMap
err bool
}{
{
name: "valid yaml file",
content: `
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should also be a test for when image: is at the top level of the file for things like inlets-operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated content with a top level image key.

replicaCount: 1
image: demo-operator
nginx:
image: nginx
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""`,
want: ValuesMap{
"replicaCount": 1,
"image": "demo-operator",
"nginx": ValuesMap{
"image": "nginx",
"pullPolicy": "IfNotPresent",
},
"imagePullSecrets": []interface{}{},
"nameOverride": "",
"fullnameOverride": "",
},
},
{
name: "empty yaml file",
content: ``,
want: ValuesMap{},
},
{
name: "invalid yaml file",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's invalid about this YAML file, perhaps the test case name could say?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replicaCount: 1
image:
repository: nginx

not properly indented.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like properly indented YAML to me?

But it sounds like you've come up with a great name for the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't valid yaml will look like below? There is no proper indentation.

replicaCount: 1
  image:
repository: nginx

content: `
replicaCount: 1
image:
repository: nginx`,
err: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
file, err := os.CreateTemp("", "app-helm-*.yaml")
if err != nil {
t.Fatalf("failed to create yaml file for load test: %v", err)
}
// defer os.Remove(file.Name())

_, err = file.WriteString(tc.content)
if err != nil {
t.Fatalf("failed to write yaml file for load test: %v", err)
}

err = file.Close()
if err != nil {
t.Fatalf("failed to close yaml file for load test: %v", err)
}

got, err := Load(file.Name())
if !tc.err && err != nil {
t.Fatalf("failed to parse yaml file full path for load test: %v", err)
}

if !tc.err && !reflect.DeepEqual(tc.want, got) {
t.Fatalf("fwant: %q\n but got: %q", tc.want, got)
}
})
}
}

func Test_ReplaceValuesInHelmValuesFile(t *testing.T) {
tests := []struct {
name string
fileContent string
values map[string]string
want string
}{
{
name: "replace successfully",
fileContent: `
replicaCount: "1"
nginx:
image: NGINX_IMG
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""`,
values: map[string]string{
"NGINX_IMG": "nginx",
},
want: `
replicaCount: "1"
nginx:
image: nginx
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""`,
},
{
name: "replace values not found",
fileContent: `
replicaCount: "1"
nginx:
image: NGINX_IMG
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""`,
values: map[string]string{
"TEST_IMG": "test",
},
want: `
replicaCount: "1"
nginx:
image: NGINX_IMG
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""`,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
file, err := os.CreateTemp("", "app-helm-*.yaml")
if err != nil {
t.Fatalf("failed to create yaml file for load test: %v", err)
}
defer os.Remove(file.Name())

_, err = file.WriteString(tc.fileContent)
if err != nil {
t.Fatalf("failed to write yaml file for load test: %v", err)
}

err = file.Close()
if err != nil {
t.Fatalf("failed to close yaml file for load test: %v", err)
}

got, err := ReplaceValuesInHelmValuesFile(tc.values, file.Name())
if err != nil {
t.Fatalf("failed to replace values in yaml file: %v", err)
}

if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("fwant: %q\n but got: %q", tc.want, got)
}
})
}
}

func Test_FilterImagesUptoDepth(t *testing.T) {
tests := []struct {
name string
values ValuesMap
depth int
want map[string]string
}{
{
name: "fetch images upto level 1",
values: ValuesMap{
"nginx": ValuesMap{
"image": "nginx",
"pullPolicy": "IfNotPresent",
},
},
depth: 1,
want: map[string]string{
"nginx": "nginx",
},
},
{
name: "fetch images upto level 2",
values: ValuesMap{
"deployment1": ValuesMap{
"container1": ValuesMap{
"image": "nginx",
"pullPolicy": "IfNotPresent",
},
"container2": ValuesMap{
"image": "deamonjob",
"pullPolicy": "IfNotPresent",
},
},
},
depth: 2,
want: map[string]string{
"nginx": "nginx",
"deamonjob": "deamonjob",
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := FilterImagesUptoDepth(tc.values, tc.depth)
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("fwant: %q\n but got: %q", tc.want, got)
}
})
}
}