1/*
2Copyright 2019 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 add
18
19import (
20	"testing"
21
22	"sigs.k8s.io/kustomize/pkg/fs"
23	"sigs.k8s.io/kustomize/pkg/types"
24)
25
26func TestNewCmdAddSecretIsNotNil(t *testing.T) {
27	if newCmdAddSecret(fs.MakeFakeFS(), nil) == nil {
28		t.Fatal("newCmdAddSecret shouldn't be nil")
29	}
30}
31
32func TestMakeSecretArgs(t *testing.T) {
33	secretName := "test-secret-name"
34
35	kustomization := &types.Kustomization{
36		NamePrefix: "test-name-prefix",
37	}
38
39	secretType := "Opaque"
40
41	if len(kustomization.SecretGenerator) != 0 {
42		t.Fatal("Initial kustomization should not have any secrets")
43	}
44	args := makeSecretArgs(kustomization, secretName, secretType)
45
46	if args == nil {
47		t.Fatalf("args should always be non-nil")
48	}
49
50	if len(kustomization.SecretGenerator) != 1 {
51		t.Fatalf("Kustomization should have newly created secret")
52	}
53
54	if &kustomization.SecretGenerator[len(kustomization.SecretGenerator)-1] != args {
55		t.Fatalf("Pointer address for newly inserted secret generator should be same")
56	}
57
58	args2 := makeSecretArgs(kustomization, secretName, secretType)
59
60	if args2 != args {
61		t.Fatalf("should have returned an existing args with name: %v", secretName)
62	}
63
64	if len(kustomization.SecretGenerator) != 1 {
65		t.Fatalf("Should not insert secret for an existing name: %v", secretName)
66	}
67}
68
69func TestMergeFlagsIntoSecretArgs_LiteralSources(t *testing.T) {
70	ds := &types.DataSources{}
71
72	err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{LiteralSources: []string{"k1=v1"}})
73	if err != nil {
74		t.Fatalf("Merge initial literal source should not return error")
75	}
76
77	if len(ds.LiteralSources) != 1 {
78		t.Fatalf("Initial literal source should have been added")
79	}
80
81	err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{LiteralSources: []string{"k2=v2"}})
82	if err != nil {
83		t.Fatalf("Merge second literal source should not return error")
84	}
85
86	if len(ds.LiteralSources) != 2 {
87		t.Fatalf("Second literal source should have been added")
88	}
89}
90
91func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) {
92	ds := &types.DataSources{}
93
94	err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{FileSources: []string{"file1"}})
95	if err != nil {
96		t.Fatalf("Merge initial file source should not return error")
97	}
98
99	if len(ds.FileSources) != 1 {
100		t.Fatalf("Initial file source should have been added")
101	}
102
103	err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{FileSources: []string{"file2"}})
104	if err != nil {
105		t.Fatalf("Merge second file source should not return error")
106	}
107
108	if len(ds.FileSources) != 2 {
109		t.Fatalf("Second file source should have been added")
110	}
111}
112
113func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) {
114	envFileName := "env1"
115	envFileName2 := "env2"
116	ds := &types.DataSources{}
117
118	err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{EnvFileSource: envFileName})
119	if err != nil {
120		t.Fatalf("Merge initial env source should not return error")
121	}
122
123	if ds.EnvSource != envFileName {
124		t.Fatalf("Initial env source filename should have been added")
125	}
126
127	err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{EnvFileSource: envFileName2})
128	if err == nil {
129		t.Fatalf("Updating env source should return an error")
130	}
131}
132