kubelatte-ce

Форк
2
Форк от sbertech/kubelatte-ce
/
utils_test.go 
179 строк · 3.5 Кб
1
package utils
2

3
import (
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"
10
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
	"testing"
12
)
13

14
func TestSelector(t *testing.T) {
15
	data, _ := json.Marshal(&types.NamespaceSelector{
16
		LabelSelector: metav1.LabelSelector{
17
			MatchExpressions: []metav1.LabelSelectorRequirement{
18
				{
19
					Key:      "test",
20
					Operator: "In",
21
					Values:   []string{"aaaa"},
22
				},
23
			},
24
		},
25
		Include: []string{"incExample"},
26
		Exclude: []string{"exExample"},
27
	})
28

29
	fmt.Println(string(data))
30
}
31

32
func Test_removeCondition(t *testing.T) {
33
	type args struct {
34
		conditions []v1alpha1.Condition
35
		ct         v1alpha1.ConditionType
36
	}
37
	tests := []struct {
38
		name string
39
		args args
40
		want []v1alpha1.Condition
41
	}{
42
		{
43
			name: "with removable conditions",
44
			args: args{
45
				conditions: []v1alpha1.Condition{
46
					{
47
						Type: "RemoveIt",
48
					},
49
				},
50
				ct: "RemoveIt",
51
			},
52
			want: []v1alpha1.Condition{},
53
		},
54
		{
55
			name: "without removable conditions",
56
			args: args{
57
				conditions: []v1alpha1.Condition{
58
					{
59
						Type: "DontRemoveIt",
60
					},
61
				},
62
				ct: "RemoveIt",
63
			},
64
			want: []v1alpha1.Condition{
65
				{
66
					Type: "DontRemoveIt",
67
				},
68
			},
69
		},
70
	}
71
	for _, tt := range tests {
72
		t.Run(tt.name, func(t *testing.T) {
73
			assert.Equalf(t, tt.want, RemoveCondition(tt.args.conditions, tt.args.ct), "removeCondition(%v, %v)", tt.args.conditions, tt.args.ct)
74
		})
75
	}
76
}
77

78
func TestGetOwnerRef(t *testing.T) {
79
	type args struct {
80
		obj map[string]interface{}
81
	}
82
	tests := []struct {
83
		name    string
84
		args    args
85
		want    *metav1.OwnerReference
86
		wantErr error
87
	}{
88
		{
89
			name: "without metadata",
90
			args: args{
91
				obj: map[string]interface{}{
92
					"spec": "str",
93
				},
94
			},
95
			want:    nil,
96
			wantErr: fmt.Errorf("GetOwnerRef failed. Metadata not found"),
97
		},
98
		{
99
			name: "without name",
100
			args: args{
101
				obj: map[string]interface{}{
102
					"metadata": map[string]interface{}{
103
						"test": "test",
104
					},
105
				},
106
			},
107
			want:    nil,
108
			wantErr: fmt.Errorf("GetOwnerRef failed. Name not found"),
109
		},
110
		{
111
			name: "without uid",
112
			args: args{
113
				obj: map[string]interface{}{
114
					"metadata": map[string]interface{}{
115
						"name": "test",
116
					},
117
				},
118
			},
119
			want:    nil,
120
			wantErr: fmt.Errorf("GetOwnerRef failed. UID not found"),
121
		},
122
		{
123
			name: "without apiVersion",
124
			args: args{
125
				obj: map[string]interface{}{
126
					"metadata": map[string]interface{}{
127
						"name": "test",
128
						"uid":  "uid",
129
					},
130
				},
131
			},
132
			want:    nil,
133
			wantErr: fmt.Errorf("GetOwnerRef failed. ApiVersion not found"),
134
		},
135
		{
136
			name: "without kind",
137
			args: args{
138
				obj: map[string]interface{}{
139
					"apiVersion": "test",
140
					"metadata": map[string]interface{}{
141
						"name": "test",
142
						"uid":  "uid",
143
					},
144
				},
145
			},
146
			want:    nil,
147
			wantErr: fmt.Errorf("GetOwnerRef failed. Kind not found"),
148
		},
149
		{
150
			name: "ok",
151
			args: args{
152
				obj: map[string]interface{}{
153
					"kind":       "test",
154
					"apiVersion": "test",
155
					"metadata": map[string]interface{}{
156
						"name": "test",
157
						"uid":  "uid",
158
					},
159
				},
160
			},
161
			want: &metav1.OwnerReference{
162
				APIVersion: "test",
163
				Kind:       "test",
164
				Name:       "test",
165
				UID:        "uid",
166
			},
167
			wantErr: nil,
168
		},
169
	}
170
	for _, tt := range tests {
171
		t.Run(tt.name, func(t *testing.T) {
172
			got, err := GetOwnerRef(tt.args.obj)
173
			if !errors.Is(err, tt.wantErr) {
174
				return
175
			}
176
			assert.Equalf(t, tt.want, got, "GetOwnerRef(%v)", tt.args.obj)
177
		})
178
	}
179
}
180

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.