Skip to content

Commit 29694e5

Browse files
committed
Add test for secretfactory.
1 parent a99f415 commit 29694e5

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package configmapandsecret
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"sigs.k8s.io/kustomize/pkg/fs"
24+
"sigs.k8s.io/kustomize/pkg/types"
25+
)
26+
27+
func TestMakeSecretNoCommands(t *testing.T) {
28+
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
29+
args := types.SecretArgs{
30+
GeneratorArgs: types.GeneratorArgs{Name: "apple"},
31+
Type: "Opaque",
32+
CommandSources: types.CommandSources{
33+
Commands: nil,
34+
EnvCommand: "",
35+
}}
36+
s, err := factory.MakeSecret(&args, nil)
37+
if err != nil {
38+
t.Fatalf("unexpected error: %v", err)
39+
}
40+
if s.ObjectMeta.Name != "apple" {
41+
t.Fatalf("unexpected name: %v", s.ObjectMeta.Name)
42+
}
43+
if len(s.Data) > 0 || len(s.StringData) > 0 {
44+
t.Fatalf("unexpected data: %v", s)
45+
}
46+
}
47+
48+
func TestMakeSecretNoCommandsBadDir(t *testing.T) {
49+
factory := NewSecretFactory(fs.MakeFakeFS(), "/does/not/exist")
50+
args := types.SecretArgs{
51+
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
52+
Type: "Opaque",
53+
CommandSources: types.CommandSources{
54+
Commands: nil,
55+
EnvCommand: "",
56+
}}
57+
_, err := factory.MakeSecret(&args, nil)
58+
if err == nil {
59+
t.Fatalf("expected error: %v", err)
60+
}
61+
if !strings.Contains(err.Error(), "not a directory") {
62+
t.Fatalf("unexpected error: %v", err)
63+
}
64+
}
65+
66+
func TestMakeSecretEmptyCommandMap(t *testing.T) {
67+
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
68+
args := types.SecretArgs{
69+
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
70+
Type: "Opaque",
71+
CommandSources: types.CommandSources{
72+
// TODO try: map[string]string{"commandName": "bogusCommand bogusArg"},
73+
Commands: nil,
74+
EnvCommand: "echo beans",
75+
}}
76+
s, err := factory.MakeSecret(&args, nil)
77+
if err != nil {
78+
t.Fatalf("unexpected error: %v", err)
79+
}
80+
if s == nil {
81+
t.Fatalf("nil result")
82+
}
83+
v, ok := s.Data["beans"]
84+
if !ok {
85+
t.Fatalf("expected beans")
86+
}
87+
if len(v) > 0 {
88+
t.Fatalf("unexpected data")
89+
}
90+
}
91+
92+
func TestMakeSecretWithCommandMap(t *testing.T) {
93+
factory := NewSecretFactory(fs.MakeFakeFS(), "/")
94+
args := types.SecretArgs{
95+
GeneratorArgs: types.GeneratorArgs{Name: "envConfigMap"},
96+
Type: "Opaque",
97+
CommandSources: types.CommandSources{
98+
Commands: map[string]string{"commandName": "echo beans"},
99+
}}
100+
s, err := factory.MakeSecret(&args, nil)
101+
if err != nil {
102+
t.Fatalf("unexpected error: %v", err)
103+
}
104+
if s == nil {
105+
t.Fatalf("nil result")
106+
}
107+
v, ok := s.Data["commandName"]
108+
if !ok {
109+
t.Fatalf("expected something for commandName")
110+
}
111+
if string(v) != "beans\n" {
112+
t.Fatalf("unexpected data: %s", string(v))
113+
}
114+
}

0 commit comments

Comments
 (0)