1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package configmapandsecret
18
19import (
20	"reflect"
21	"testing"
22
23	corev1 "k8s.io/api/core/v1"
24	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25	"sigs.k8s.io/kustomize/pkg/fs"
26	"sigs.k8s.io/kustomize/pkg/loader"
27	"sigs.k8s.io/kustomize/pkg/types"
28)
29
30func makeEnvConfigMap(name string) *corev1.ConfigMap {
31	return &corev1.ConfigMap{
32		TypeMeta: metav1.TypeMeta{
33			APIVersion: "v1",
34			Kind:       "ConfigMap",
35		},
36		ObjectMeta: metav1.ObjectMeta{
37			Name: name,
38		},
39		Data: map[string]string{
40			"DB_USERNAME": "admin",
41			"DB_PASSWORD": "somepw",
42		},
43	}
44}
45
46func makeFileConfigMap(name string) *corev1.ConfigMap {
47	return &corev1.ConfigMap{
48		TypeMeta: metav1.TypeMeta{
49			APIVersion: "v1",
50			Kind:       "ConfigMap",
51		},
52		ObjectMeta: metav1.ObjectMeta{
53			Name: name,
54		},
55		Data: map[string]string{
56			"app-init.ini": `FOO=bar
57BAR=baz
58`,
59		},
60		BinaryData: map[string][]byte{
61			"app.bin": {0xff, 0xfd},
62		},
63	}
64}
65
66func makeLiteralConfigMap(name string) *corev1.ConfigMap {
67	cm := &corev1.ConfigMap{
68		TypeMeta: metav1.TypeMeta{
69			APIVersion: "v1",
70			Kind:       "ConfigMap",
71		},
72		ObjectMeta: metav1.ObjectMeta{
73			Name: name,
74		},
75		Data: map[string]string{
76			"a": "x",
77			"b": "y",
78			"c": "Hello World",
79			"d": "true",
80		},
81	}
82	cm.SetLabels(map[string]string{"foo": "bar"})
83	return cm
84}
85
86func TestConstructConfigMap(t *testing.T) {
87	type testCase struct {
88		description string
89		input       types.ConfigMapArgs
90		options     *types.GeneratorOptions
91		expected    *corev1.ConfigMap
92	}
93
94	testCases := []testCase{
95		{
96			description: "construct config map from env",
97			input: types.ConfigMapArgs{
98				GeneratorArgs: types.GeneratorArgs{
99					Name: "envConfigMap",
100					DataSources: types.DataSources{
101						EnvSource: "configmap/app.env",
102					},
103				},
104			},
105			options:  nil,
106			expected: makeEnvConfigMap("envConfigMap"),
107		},
108		{
109			description: "construct config map from file",
110			input: types.ConfigMapArgs{
111				GeneratorArgs: types.GeneratorArgs{
112					Name: "fileConfigMap",
113					DataSources: types.DataSources{
114						FileSources: []string{"configmap/app-init.ini", "configmap/app.bin"},
115					},
116				},
117			},
118			options:  nil,
119			expected: makeFileConfigMap("fileConfigMap"),
120		},
121		{
122			description: "construct config map from literal",
123			input: types.ConfigMapArgs{
124				GeneratorArgs: types.GeneratorArgs{
125					Name: "literalConfigMap",
126					DataSources: types.DataSources{
127						LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
128					},
129				},
130			},
131			options: &types.GeneratorOptions{
132				Labels: map[string]string{
133					"foo": "bar",
134				},
135			},
136			expected: makeLiteralConfigMap("literalConfigMap"),
137		},
138	}
139
140	fSys := fs.MakeFakeFS()
141	fSys.WriteFile("/configmap/app.env", []byte("DB_USERNAME=admin\nDB_PASSWORD=somepw\n"))
142	fSys.WriteFile("/configmap/app-init.ini", []byte("FOO=bar\nBAR=baz\n"))
143	fSys.WriteFile("/configmap/app.bin", []byte{0xff, 0xfd})
144	f := NewConfigMapFactory(loader.NewFileLoaderAtRoot(fSys))
145	for _, tc := range testCases {
146		cm, err := f.MakeConfigMap(&tc.input, tc.options)
147		if err != nil {
148			t.Fatalf("unexpected error: %v", err)
149		}
150		if !reflect.DeepEqual(*cm, *tc.expected) {
151			t.Fatalf("in testcase: %q updated:\n%#v\ndoesn't match expected:\n%#v\n", tc.description, *cm, tc.expected)
152		}
153	}
154}
155