Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed Nov 18, 2023
1 parent 4bc082c commit 006c6e7
Show file tree
Hide file tree
Showing 12 changed files with 498 additions and 341 deletions.
153 changes: 153 additions & 0 deletions pkg/charts/__snapshots__/testspec_test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
['ApplyDynamicFields should replace specified fields 1']
SnapShot = """
[
{
\"apiVersion\": \"apps/v1\",
\"kind\": \"Deployment\",
\"metadata\": {
\"labels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/managed-by\": \"Helm\",
\"app.kubernetes.io/name\": \"app1\",
\"app.kubernetes.io/version\": \"###DYNAMIC_FIELD###\",
\"helm.sh/chart\": \"app1-0.1.0\"
},
\"name\": \"chartsnap-app1\"
},
\"spec\": {
\"replicas\": 1,
\"selector\": {
\"matchLabels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/name\": \"app1\"
}
},
\"template\": {
\"metadata\": {
\"labels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/managed-by\": \"Helm\",
\"app.kubernetes.io/name\": \"app1\",
\"app.kubernetes.io/version\": \"1.16.0\",
\"helm.sh/chart\": \"app1-0.1.0\"
}
},
\"spec\": {
\"containers\": [
{
\"image\": \"nginx:1.16.0\",
\"imagePullPolicy\": \"IfNotPresent\",
\"livenessProbe\": {
\"httpGet\": {
\"path\": \"/\",
\"port\": \"http\"
}
},
\"name\": \"app1\",
\"ports\": [
{
\"containerPort\": 80,
\"name\": \"http\",
\"protocol\": \"TCP\"
}
],
\"readinessProbe\": {
\"httpGet\": {
\"path\": \"/\",
\"port\": \"http\"
}
},
\"resources\": {},
\"securityContext\": {}
}
],
\"securityContext\": {},
\"serviceAccountName\": \"chartsnap-app1\"
}
}
}
},
{
\"apiVersion\": \"v1\",
\"kind\": \"Pod\",
\"metadata\": {
\"annotations\": {
\"helm.sh/hook\": \"test\"
},
\"labels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/managed-by\": \"Helm\",
\"app.kubernetes.io/name\": \"app1\",
\"app.kubernetes.io/version\": \"1.16.0\",
\"helm.sh/chart\": \"app1-0.1.0\"
},
\"name\": \"###DYNAMIC_FIELD###\"
},
\"spec\": {
\"containers\": [
{
\"args\": [
\"chartsnap-app1:80\"
],
\"command\": [
\"wget\"
],
\"image\": \"busybox\",
\"name\": \"wget\"
}
],
\"restartPolicy\": \"Never\"
}
},
{
\"apiVersion\": \"v1\",
\"kind\": \"Service\",
\"metadata\": {
\"labels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/managed-by\": \"Helm\",
\"app.kubernetes.io/name\": \"app1\",
\"app.kubernetes.io/version\": \"1.16.0\",
\"helm.sh/chart\": \"app1-0.1.0\"
},
\"name\": \"chartsnap-app1\"
},
\"spec\": {
\"ports\": [
{
\"name\": \"http\",
\"port\": 80,
\"protocol\": \"TCP\",
\"targetPort\": \"http\"
},
{
\"name\": \"https\",
\"port\": 443,
\"protocol\": \"TCP\",
\"targetPort\": \"###DYNAMIC_FIELD###\"
}
],
\"selector\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/name\": \"app1\"
},
\"type\": \"ClusterIP\"
}
},
{
\"apiVersion\": \"v1\",
\"automountServiceAccountToken\": true,
\"kind\": \"ServiceAccount\",
\"metadata\": {
\"labels\": {
\"app.kubernetes.io/instance\": \"chartsnap\",
\"app.kubernetes.io/managed-by\": \"Helm\",
\"app.kubernetes.io/name\": \"app1\",
\"app.kubernetes.io/version\": \"1.16.0\",
\"helm.sh/chart\": \"app1-0.1.0\"
},
\"name\": \"chartsnap-app1\"
}
}
]
"""
7 changes: 6 additions & 1 deletion pkg/charts/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type HelmTemplateCmdOptions struct {
AdditionalArgs []string
}

func (o *HelmTemplateCmdOptions) Execute(ctx context.Context) ([]byte, error) {
func (o *HelmTemplateCmdOptions) Args() []string {
args := []string{
"template", o.ReleaseName, o.Chart,
}
Expand All @@ -29,6 +29,11 @@ func (o *HelmTemplateCmdOptions) Execute(ctx context.Context) ([]byte, error) {
if len(o.AdditionalArgs) > 0 {
args = append(args, o.AdditionalArgs...)
}
return args
}

func (o *HelmTemplateCmdOptions) Execute(ctx context.Context) ([]byte, error) {
args := o.Args()
Log().DebugContext(ctx, "executing 'helm template' command", "args", args, "additionalArgs", o.AdditionalArgs)

// helm template should not be executed in debug mode because YAML parser fails.
Expand Down
46 changes: 29 additions & 17 deletions pkg/charts/helm_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package charts

import (
"context"
"reflect"
"testing"
)

func TestHelmTemplateCmdOptions_Execute(t *testing.T) {
func TestHelmTemplateCmdOptions_Args(t *testing.T) {
type fields struct {
HelmPath string
ReleaseName string
Expand All @@ -15,17 +14,34 @@ func TestHelmTemplateCmdOptions_Execute(t *testing.T) {
ValuesFile string
AdditionalArgs []string
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
want []byte
wantErr bool
name string
fields fields
want []string
}{
// TODO: Add test cases.
{
name: "default",
fields: fields{
HelmPath: "helm",
ReleaseName: "chartsnap",
Namespace: "default",
Chart: "charts/app1/",
ValuesFile: "charts/app1/test/test.values.yaml",
},
want: []string{"template", "chartsnap", "charts/app1/", "--namespace=default", "--values=charts/app1/test/test.values.yaml"},
},
{
name: "additional args",
fields: fields{
HelmPath: "helm",
ReleaseName: "chartsnap",
Namespace: "xxx",
Chart: "postgres",
ValuesFile: "postgres.values.yaml",
AdditionalArgs: []string{"--repo", "https://charts.bitnami.com/bitnami", "--skip-tests"},
},
want: []string{"template", "chartsnap", "postgres", "--namespace=xxx", "--values=postgres.values.yaml", "--repo", "https://charts.bitnami.com/bitnami", "--skip-tests"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -37,13 +53,9 @@ func TestHelmTemplateCmdOptions_Execute(t *testing.T) {
ValuesFile: tt.fields.ValuesFile,
AdditionalArgs: tt.fields.AdditionalArgs,
}
got, err := o.Execute(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("HelmTemplateCmdOptions.Execute() error = %v, wantErr %v", err, tt.wantErr)
return
}
got := o.Args()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("HelmTemplateCmdOptions.Execute() = %v, want %v", got, tt.want)
t.Errorf("HelmTemplateCmdOptions.Args() = %v, want %v", got, tt.want)
}
})
}
Expand Down
17 changes: 3 additions & 14 deletions pkg/charts/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,10 @@ func Snap(ctx context.Context, o ChartSnapOptions) (match bool, failureMessage s
return match, "", fmt.Errorf("failed to load helm output: %w: out='%s'", err, string(out))
}

for _, v := range sv.TestSpec.DynamicFields {
for i, obj := range manifests {
if v.APIVersion == obj.GetAPIVersion() &&
v.Kind == obj.GetKind() &&
v.Name == obj.GetName() {
for _, p := range v.JSONPath {
newObj, err := unstructured.Replace(manifests[i], p, "###DYNAMIC_FIELD###")
if err != nil {
return match, "", fmt.Errorf("failed to replace json path: %w", err)
}
manifests[i] = *newObj
}
}
}
if err := sv.TestSpec.ApplyFixedValue(manifests); err != nil {
return match, "", fmt.Errorf("failed to replace json path: %w", err)
}

snap.SetLogger(Log())
s := snap.UnstructuredSnapShotMatcher(
o.SnapshotFile,
Expand Down
104 changes: 104 additions & 0 deletions pkg/charts/testdata/testspec_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: app1
app.kubernetes.io/version: 1.16.0
helm.sh/chart: app1-0.1.0
name: chartsnap-app1
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/name: app1
template:
metadata:
labels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: app1
app.kubernetes.io/version: 1.16.0
helm.sh/chart: app1-0.1.0
spec:
containers:
- image: nginx:1.16.0
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: http
name: app1
ports:
- containerPort: 80
name: http
protocol: TCP
readinessProbe:
httpGet:
path: /
port: http
resources: {}
securityContext: {}
securityContext: {}
serviceAccountName: chartsnap-app1
---
apiVersion: v1
kind: Pod
metadata:
annotations:
helm.sh/hook: test
labels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: app1
app.kubernetes.io/version: 1.16.0
helm.sh/chart: app1-0.1.0
name: chartsnap-app1-test-connection
spec:
containers:
- args:
- chartsnap-app1:80
command:
- wget
image: busybox
name: wget
restartPolicy: Never
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: app1
app.kubernetes.io/version: 1.16.0
helm.sh/chart: app1-0.1.0
name: chartsnap-app1
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/name: app1
type: ClusterIP
---
apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/instance: chartsnap
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: app1
app.kubernetes.io/version: 1.16.0
helm.sh/chart: app1-0.1.0
name: chartsnap-app1
Loading

0 comments on commit 006c6e7

Please sign in to comment.