-
Notifications
You must be signed in to change notification settings - Fork 20
/
yaml_test.go
98 lines (95 loc) · 1.89 KB
/
yaml_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"strings"
"testing"
)
func Test_getYamlInfo(t *testing.T) {
type args struct {
yamlContent string
}
tests := []struct {
name string
args args
wantKind string
wantName string
wantNamespace string
wantErr bool
wantErrMsg string
}{
{
name: "Error on empty file",
args: args{
yamlContent: "",
},
wantKind: "",
wantName: "",
wantErr: true,
wantErrMsg: "yaml file with kind missing",
},
{
name: "Error on non-yaml file",
args: args{
yamlContent: "Non-yaml file....?",
},
wantErr: true,
wantErrMsg: "Could not unmarshal",
},
{
name: "Error on missing kind",
args: args{
yamlContent: `
apiVersion: v1
metadata:
name: simple
`,
},
wantErr: true,
wantErrMsg: "yaml file with kind missing",
},
{
name: "Error on missing name",
args: args{
yamlContent: `kind: Deployment`,
},
wantErr: true,
wantErrMsg: "yaml file with name missing",
},
{
name: "Can handle simple file",
args: args{
yamlContent: `
apiVersion: v1
kind: Pod
metadata:
name: simple
namespace: foo
`,
},
wantName: "simple",
wantNamespace: "foo",
wantKind: "Pod",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getYamlInfo(tt.args.yamlContent)
if (err != nil) != tt.wantErr {
t.Errorf("getYamlInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && !strings.HasPrefix(err.Error(), tt.wantErrMsg) {
t.Errorf("getYamlInfo() error = %v, wantErr %v", err.Error(), tt.wantErrMsg)
return
}
if tt.wantErr {
return
}
if got.Kind != tt.wantKind {
t.Errorf("getYamlInfo() got = %v, want %v", got.Kind, tt.wantKind)
}
if got.Metadata.Name != tt.wantName {
t.Errorf("getYamlInfo() got = %v, want %v", got.Metadata.Name, tt.wantName)
}
})
}
}