-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
170 lines (162 loc) · 3.37 KB
/
main_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
)
func TestGetNamespaceAndName(t *testing.T) {
tests := map[string]struct {
inputs []string
namespace, name string
ok bool
}{
"namespace and name is separated": {
inputs: []string{"namespace", "name"},
namespace: "namespace",
name: "name",
ok: true,
},
"namespace and name is concatenated": {
inputs: []string{"namespace/name"},
namespace: "namespace",
name: "name",
ok: true,
},
"only name is specified": {
inputs: []string{"name"},
namespace: "",
name: "name",
ok: true,
},
"inputs are nil": {
inputs: nil,
ok: false,
},
"separator is too many": {
inputs: []string{"namespace/name/hello"},
ok: false,
},
"inputs are too many": {
inputs: []string{"namespace", "name", "hello"},
ok: false,
},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
namespace, name, ok := getNamespaceAndName(tt.inputs)
if namespace != tt.namespace {
t.Errorf(`namespace expected "%s", but got "%s"`, tt.namespace, namespace)
}
if name != tt.name {
t.Errorf(`name expected "%s", but got "%s"`, tt.name, name)
}
if ok != tt.ok {
t.Errorf(`ok expected %v, but got %v`, tt.ok, ok)
}
})
}
}
func TestIsCronJobGA(t *testing.T) {
tests := map[string]struct {
input *version.Info
expect bool
}{
"1.20": {
input: &version.Info{
Major: "1",
Minor: "20",
},
expect: false,
},
"1.21": {
input: &version.Info{
Major: "1",
Minor: "21",
},
expect: true,
},
"1.19+": {
input: &version.Info{
Major: "1",
Minor: "19+",
},
expect: false,
},
"1.21+": {
input: &version.Info{
Major: "1",
Minor: "21+",
},
expect: true,
},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
got := isCronJobGA(tt.input)
if got != tt.expect {
t.Errorf("isCronJobGA expect %t, got %t", tt.expect, got)
}
})
}
}
func TestJobToYaml(t *testing.T) {
tests := map[string]struct {
job *batchv1.Job
expect []byte
}{
"should ownereReferences to commented out": {
job: &batchv1.Job{
TypeMeta: metav1.TypeMeta{
APIVersion: "batch/v1",
Kind: "Job",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "batch/v1",
BlockOwnerDeletion: toPtr(true),
Kind: "CronJob",
Name: "test",
UID: "",
},
},
},
},
expect: []byte(`apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: test
namespace: default
# ownerReferences:
# - apiVersion: batch/v1
# blockOwnerDeletion: true
# kind: CronJob
# name: test
# uid: ""
spec:
template:
metadata:
creationTimestamp: null
spec:
containers: null
status: {}
`),
},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
got, err := jobToYaml(tt.job)
if err != nil {
t.Fatalf("jobToYaml got error: %v", err)
}
if diff := cmp.Diff(tt.expect, got); diff != "" {
t.Errorf("jobToYaml result diff (-expect, +got)\n%s", diff)
}
})
}
}