|
| 1 | +package pod |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/arttor/helmify/pkg/helmify" |
| 7 | + "github.com/arttor/helmify/pkg/metadata" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + |
| 11 | + "github.com/arttor/helmify/internal" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + strDeployment = ` |
| 17 | +apiVersion: apps/v1 |
| 18 | +kind: Deployment |
| 19 | +metadata: |
| 20 | + name: nginx-deployment |
| 21 | + labels: |
| 22 | + app: nginx |
| 23 | +spec: |
| 24 | + replicas: 3 |
| 25 | + selector: |
| 26 | + matchLabels: |
| 27 | + app: nginx |
| 28 | + template: |
| 29 | + metadata: |
| 30 | + labels: |
| 31 | + app: nginx |
| 32 | + spec: |
| 33 | + containers: |
| 34 | + - name: nginx |
| 35 | + image: nginx:1.14.2 |
| 36 | + args: |
| 37 | + - --test |
| 38 | + - --arg |
| 39 | + ports: |
| 40 | + - containerPort: 80 |
| 41 | +` |
| 42 | + |
| 43 | + strDeploymentWithNoArgs = ` |
| 44 | +apiVersion: apps/v1 |
| 45 | +kind: Deployment |
| 46 | +metadata: |
| 47 | + name: nginx-deployment |
| 48 | + labels: |
| 49 | + app: nginx |
| 50 | +spec: |
| 51 | + replicas: 3 |
| 52 | + selector: |
| 53 | + matchLabels: |
| 54 | + app: nginx |
| 55 | + template: |
| 56 | + metadata: |
| 57 | + labels: |
| 58 | + app: nginx |
| 59 | + spec: |
| 60 | + containers: |
| 61 | + - name: nginx |
| 62 | + image: nginx:1.14.2 |
| 63 | + ports: |
| 64 | + - containerPort: 80 |
| 65 | +` |
| 66 | +) |
| 67 | + |
| 68 | +func Test_pod_Process(t *testing.T) { |
| 69 | + t.Run("deployment with args", func(t *testing.T) { |
| 70 | + var deploy appsv1.Deployment |
| 71 | + obj := internal.GenerateObj(strDeployment) |
| 72 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy) |
| 73 | + specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec) |
| 74 | + assert.NoError(t, err) |
| 75 | + |
| 76 | + assert.Equal(t, map[string]interface{}{ |
| 77 | + "containers": []interface{}{ |
| 78 | + map[string]interface{}{ |
| 79 | + "args": "{{- toYaml .Values.nginx.nginx.args | nindent 8 }}", |
| 80 | + "env": []interface{}{ |
| 81 | + map[string]interface{}{ |
| 82 | + "name": "KUBERNETES_CLUSTER_DOMAIN", |
| 83 | + "value": "{{ quote .Values.kubernetesClusterDomain }}", |
| 84 | + }, |
| 85 | + }, |
| 86 | + "image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}", |
| 87 | + "name": "nginx", "ports": []interface{}{ |
| 88 | + map[string]interface{}{ |
| 89 | + "containerPort": int64(80), |
| 90 | + }, |
| 91 | + }, |
| 92 | + "resources": map[string]interface{}{}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, specMap) |
| 96 | + |
| 97 | + assert.Equal(t, helmify.Values{ |
| 98 | + "nginx": map[string]interface{}{ |
| 99 | + "nginx": map[string]interface{}{ |
| 100 | + "image": map[string]interface{}{ |
| 101 | + "repository": "nginx", |
| 102 | + "tag": "1.14.2", |
| 103 | + }, |
| 104 | + "args": []interface{}{ |
| 105 | + "--test", |
| 106 | + "--arg", |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, tmpl) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("deployment with no args", func(t *testing.T) { |
| 114 | + var deploy appsv1.Deployment |
| 115 | + obj := internal.GenerateObj(strDeploymentWithNoArgs) |
| 116 | + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy) |
| 117 | + specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec) |
| 118 | + assert.NoError(t, err) |
| 119 | + |
| 120 | + assert.Equal(t, map[string]interface{}{ |
| 121 | + "containers": []interface{}{ |
| 122 | + map[string]interface{}{ |
| 123 | + "env": []interface{}{ |
| 124 | + map[string]interface{}{ |
| 125 | + "name": "KUBERNETES_CLUSTER_DOMAIN", |
| 126 | + "value": "{{ quote .Values.kubernetesClusterDomain }}", |
| 127 | + }, |
| 128 | + }, |
| 129 | + "image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}", |
| 130 | + "name": "nginx", "ports": []interface{}{ |
| 131 | + map[string]interface{}{ |
| 132 | + "containerPort": int64(80), |
| 133 | + }, |
| 134 | + }, |
| 135 | + "resources": map[string]interface{}{}, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, specMap) |
| 139 | + |
| 140 | + assert.Equal(t, helmify.Values{ |
| 141 | + "nginx": map[string]interface{}{ |
| 142 | + "nginx": map[string]interface{}{ |
| 143 | + "image": map[string]interface{}{ |
| 144 | + "repository": "nginx", |
| 145 | + "tag": "1.14.2", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, tmpl) |
| 150 | + }) |
| 151 | + |
| 152 | +} |
0 commit comments