kubelatte-ce
Форк от sbertech/kubelatte-ce
179 строк · 3.5 Кб
1package utils
2
3import (
4"encoding/json"
5"errors"
6"fmt"
7"github.com/stretchr/testify/assert"
8"gitverse.ru/synapse/kubelatte/pkg/api/v1alpha1"
9"gitverse.ru/synapse/kubelatte/pkg/util/types"
10metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11"testing"
12)
13
14func TestSelector(t *testing.T) {
15data, _ := json.Marshal(&types.NamespaceSelector{
16LabelSelector: metav1.LabelSelector{
17MatchExpressions: []metav1.LabelSelectorRequirement{
18{
19Key: "test",
20Operator: "In",
21Values: []string{"aaaa"},
22},
23},
24},
25Include: []string{"incExample"},
26Exclude: []string{"exExample"},
27})
28
29fmt.Println(string(data))
30}
31
32func Test_removeCondition(t *testing.T) {
33type args struct {
34conditions []v1alpha1.Condition
35ct v1alpha1.ConditionType
36}
37tests := []struct {
38name string
39args args
40want []v1alpha1.Condition
41}{
42{
43name: "with removable conditions",
44args: args{
45conditions: []v1alpha1.Condition{
46{
47Type: "RemoveIt",
48},
49},
50ct: "RemoveIt",
51},
52want: []v1alpha1.Condition{},
53},
54{
55name: "without removable conditions",
56args: args{
57conditions: []v1alpha1.Condition{
58{
59Type: "DontRemoveIt",
60},
61},
62ct: "RemoveIt",
63},
64want: []v1alpha1.Condition{
65{
66Type: "DontRemoveIt",
67},
68},
69},
70}
71for _, tt := range tests {
72t.Run(tt.name, func(t *testing.T) {
73assert.Equalf(t, tt.want, RemoveCondition(tt.args.conditions, tt.args.ct), "removeCondition(%v, %v)", tt.args.conditions, tt.args.ct)
74})
75}
76}
77
78func TestGetOwnerRef(t *testing.T) {
79type args struct {
80obj map[string]interface{}
81}
82tests := []struct {
83name string
84args args
85want *metav1.OwnerReference
86wantErr error
87}{
88{
89name: "without metadata",
90args: args{
91obj: map[string]interface{}{
92"spec": "str",
93},
94},
95want: nil,
96wantErr: fmt.Errorf("GetOwnerRef failed. Metadata not found"),
97},
98{
99name: "without name",
100args: args{
101obj: map[string]interface{}{
102"metadata": map[string]interface{}{
103"test": "test",
104},
105},
106},
107want: nil,
108wantErr: fmt.Errorf("GetOwnerRef failed. Name not found"),
109},
110{
111name: "without uid",
112args: args{
113obj: map[string]interface{}{
114"metadata": map[string]interface{}{
115"name": "test",
116},
117},
118},
119want: nil,
120wantErr: fmt.Errorf("GetOwnerRef failed. UID not found"),
121},
122{
123name: "without apiVersion",
124args: args{
125obj: map[string]interface{}{
126"metadata": map[string]interface{}{
127"name": "test",
128"uid": "uid",
129},
130},
131},
132want: nil,
133wantErr: fmt.Errorf("GetOwnerRef failed. ApiVersion not found"),
134},
135{
136name: "without kind",
137args: args{
138obj: map[string]interface{}{
139"apiVersion": "test",
140"metadata": map[string]interface{}{
141"name": "test",
142"uid": "uid",
143},
144},
145},
146want: nil,
147wantErr: fmt.Errorf("GetOwnerRef failed. Kind not found"),
148},
149{
150name: "ok",
151args: args{
152obj: map[string]interface{}{
153"kind": "test",
154"apiVersion": "test",
155"metadata": map[string]interface{}{
156"name": "test",
157"uid": "uid",
158},
159},
160},
161want: &metav1.OwnerReference{
162APIVersion: "test",
163Kind: "test",
164Name: "test",
165UID: "uid",
166},
167wantErr: nil,
168},
169}
170for _, tt := range tests {
171t.Run(tt.name, func(t *testing.T) {
172got, err := GetOwnerRef(tt.args.obj)
173if !errors.Is(err, tt.wantErr) {
174return
175}
176assert.Equalf(t, tt.want, got, "GetOwnerRef(%v)", tt.args.obj)
177})
178}
179}
180