@@ -18,83 +18,170 @@ import (
18
18
"testing"
19
19
20
20
"github.com/golang/mock/gomock"
21
+ "github.com/stretchr/testify/assert"
22
+ corev1 "k8s.io/api/core/v1"
23
+ "k8s.io/apimachinery/pkg/api/resource"
21
24
22
25
"github.com/erda-project/erda/apistructs"
23
- "github.com/erda-project/erda/internal/tools/orchestrator/scheduler/executor/plugins/k8s/addon/sourcecov/ mock"
26
+ "github.com/erda-project/erda/internal/tools/orchestrator/scheduler/executor/plugins/k8s/addon/mock"
24
27
"github.com/erda-project/erda/pkg/http/httpclient"
25
28
)
26
29
27
- type k8s struct {}
30
+ var sg = & apistructs.ServiceGroup {
31
+ Dice : apistructs.Dice {
32
+ ID : "mock-canal" ,
33
+ Labels : map [string ]string {
34
+ "USE_OPERATOR" : "canal" ,
35
+ },
36
+ Services : []apistructs.Service {
37
+ {
38
+ Name : "canal" ,
39
+ Resources : apistructs.Resources {
40
+ Cpu : 3 ,
41
+ Mem : 3072 ,
42
+ },
43
+ Scale : 2 ,
44
+ Env : map [string ]string {
45
+ apistructs .DiceWorkspaceEnvKey : apistructs .WORKSPACE_DEV ,
46
+ "CANAL_DESTINATION" : "example" ,
47
+ "canal.instance.master.address" : "mock-mysql.svc.cluster.local:3306" ,
48
+ "canal.instance.dbUsername" : "erda" ,
49
+ "canal.instance.dbPassword" : "password" ,
50
+ },
51
+ },
52
+ },
53
+ },
54
+ }
55
+
56
+ var sgCanalAdmin = & apistructs.ServiceGroup {
57
+ Dice : apistructs.Dice {
58
+ ID : "mock-canal" ,
59
+ Labels : map [string ]string {
60
+ "USE_OPERATOR" : "canal" ,
61
+ },
62
+ Services : []apistructs.Service {
63
+ {
64
+ Name : "canal" ,
65
+ Resources : apistructs.Resources {
66
+ Cpu : 1 ,
67
+ Mem : 2048 ,
68
+ },
69
+ Scale : 2 ,
70
+ Env : map [string ]string {
71
+ apistructs .DiceWorkspaceEnvKey : apistructs .WORKSPACE_DEV ,
72
+ "canal.admin.manager" : "127.0.0.1:8089" ,
73
+ "spring.datasource.address" : "mock-mysql.svc.cluster.local:3306" ,
74
+ "spring.datasource.username" : "erda" ,
75
+ "spring.datasource.password" : "" ,
76
+ },
77
+ },
78
+ },
79
+ },
80
+ }
28
81
29
- func (k8s ) GetK8SAddr () string {
30
- return ""
82
+ var mockResourceRequirements = corev1.ResourceRequirements {
83
+ Limits : corev1.ResourceList {
84
+ corev1 .ResourceCPU : resource .MustParse ("3" ),
85
+ corev1 .ResourceMemory : resource .MustParse ("3072Mi" ),
86
+ },
87
+ Requests : corev1.ResourceList {
88
+ corev1 .ResourceCPU : resource .MustParse ("3" ),
89
+ corev1 .ResourceMemory : resource .MustParse ("3072Mi" ),
90
+ },
91
+ }
92
+
93
+ var mockAdminResourceRequirements = corev1.ResourceRequirements {
94
+ Limits : corev1.ResourceList {
95
+ corev1 .ResourceCPU : resource .MustParse ("1" ),
96
+ corev1 .ResourceMemory : resource .MustParse ("2048Mi" ),
97
+ },
98
+ Requests : corev1.ResourceList {
99
+ corev1 .ResourceCPU : resource .MustParse ("1" ),
100
+ corev1 .ResourceMemory : resource .MustParse ("2048Mi" ),
101
+ },
31
102
}
32
103
33
104
func TestCanalOperator (t * testing.T ) {
34
105
ctrl := gomock .NewController (t )
35
106
defer ctrl .Finish ()
36
107
37
- ns := mock .NewMockNamespaceUtil (ctrl )
108
+ // Create mock
109
+ namespaceUtil := mock .NewMockNamespaceUtil (ctrl )
110
+ overCommitUtil := mock .NewMockOverCommitUtil (ctrl )
111
+ overCommitUtil .EXPECT ().ResourceOverCommit (apistructs .DevWorkspace , sg .Services [0 ].Resources ).
112
+ Return (mockResourceRequirements , nil ).AnyTimes ()
113
+ overCommitUtil .EXPECT ().ResourceOverCommit (apistructs .DevWorkspace , sgCanalAdmin .Services [0 ].Resources ).
114
+ Return (mockAdminResourceRequirements , nil ).AnyTimes ()
115
+
116
+ k8sUtil := mock .NewMockK8SUtil (ctrl )
117
+ k8sUtil .EXPECT ().GetK8SAddr ().Return ("mock-k8s-addr" ).AnyTimes ()
118
+
119
+ mo := New (k8sUtil , namespaceUtil , overCommitUtil , nil , nil , httpclient .New ())
120
+
121
+ t .Run ("Test Name and NamespacedName" , func (t * testing.T ) {
122
+ assert .NotPanics (t , func () { mo .Name (sg ) })
123
+ assert .NotPanics (t , func () { mo .NamespacedName (sg ) })
124
+ })
125
+
126
+ t .Run ("Test IsSupported" , func (t * testing.T ) {
127
+ assert .NotPanics (t , func () { mo .IsSupported () })
128
+ })
129
+
130
+ t .Run ("Test Validate" , func (t * testing.T ) {
131
+ assert .NotPanics (t , func () { mo .Validate (sg ) })
132
+ })
38
133
39
- mo := New (new (k8s ), ns , nil , nil , httpclient .New ())
40
- sg := new (apistructs.ServiceGroup )
41
- sg .Services = append (sg .Services , apistructs.Service {
42
- Name : "canal" ,
134
+ t .Run ("Test Convert" , func (t * testing.T ) {
135
+ _ , err := mo .Convert (sg )
136
+ assert .NoError (t , err )
137
+ })
138
+
139
+ t .Run ("Test CRUD Operations" , func (t * testing.T ) {
140
+ assert .NotPanics (t , func () { mo .Create (sg ) })
141
+ assert .NotPanics (t , func () { mo .Inspect (sg ) })
142
+ assert .NotPanics (t , func () { mo .Update (sg ) })
143
+ assert .NotPanics (t , func () { mo .Remove (sg ) })
43
144
})
44
- sg .ID = "abcdefghigklmn"
45
- mo .Name (sg )
46
- mo .NamespacedName (sg )
47
- mo .IsSupported ()
48
- mo .Validate (sg )
49
- sg .Labels = make (map [string ]string )
50
- sg .Labels ["USE_OPERATOR" ] = "canal"
51
- mo .Validate (sg )
52
- sg .Services [0 ].Env = make (map [string ]string )
53
- mo .Validate (sg )
54
- sg .Services [0 ].Env ["CANAL_DESTINATION" ] = "b"
55
- sg .Services [0 ].Env ["canal.instance.master.address" ] = "1"
56
- sg .Services [0 ].Env ["canal.instance.master.address" ] = "1"
57
- sg .Services [0 ].Env ["canal.instance.dbUsername" ] = "2"
58
- sg .Services [0 ].Env ["canal.instance.dbPassword" ] = "3"
59
- mo .Validate (sg )
60
- mo .Convert (sg )
61
- mo .Create (sg )
62
- mo .Inspect (sg )
63
- mo .Update (sg )
64
- mo .Remove (sg )
65
145
}
66
146
67
- func TestCanalOperator2 (t * testing.T ) {
147
+ func TestCanalOperatorAdmin (t * testing.T ) {
68
148
ctrl := gomock .NewController (t )
69
149
defer ctrl .Finish ()
70
150
71
- ns := mock .NewMockNamespaceUtil (ctrl )
151
+ // Create mock
152
+ namespaceUtil := mock .NewMockNamespaceUtil (ctrl )
153
+ overCommitUtil := mock .NewMockOverCommitUtil (ctrl )
154
+ overCommitUtil .EXPECT ().ResourceOverCommit (apistructs .DevWorkspace , sg .Services [0 ].Resources ).
155
+ Return (mockResourceRequirements , nil ).AnyTimes ()
156
+ overCommitUtil .EXPECT ().ResourceOverCommit (apistructs .DevWorkspace , sgCanalAdmin .Services [0 ].Resources ).
157
+ Return (mockAdminResourceRequirements , nil ).AnyTimes ()
158
+
159
+ k8sUtil := mock .NewMockK8SUtil (ctrl )
160
+ k8sUtil .EXPECT ().GetK8SAddr ().Return ("mock-k8s-addr" ).AnyTimes ()
161
+
162
+ mo := New (k8sUtil , namespaceUtil , overCommitUtil , nil , nil , httpclient .New ())
163
+
164
+ t .Run ("Test Name and NamespacedName" , func (t * testing.T ) {
165
+ assert .NotPanics (t , func () { mo .Name (sg ) })
166
+ assert .NotPanics (t , func () { mo .NamespacedName (sg ) })
167
+ })
168
+
169
+ t .Run ("Test IsSupported" , func (t * testing.T ) {
170
+ assert .NotPanics (t , func () { mo .IsSupported () })
171
+ })
172
+
173
+ t .Run ("Test Validate" , func (t * testing.T ) {
174
+ assert .NotPanics (t , func () { mo .Validate (sg ) })
175
+ })
176
+
177
+ t .Run ("Test Convert" , func (t * testing.T ) {
178
+ assert .NotPanics (t , func () { mo .Convert (sg ) })
179
+ })
72
180
73
- mo := New (new (k8s ), ns , nil , nil , httpclient .New ())
74
- sg := new (apistructs.ServiceGroup )
75
- sg .Services = append (sg .Services , apistructs.Service {
76
- Name : "canal" ,
181
+ t .Run ("Test CRUD Operations" , func (t * testing.T ) {
182
+ assert .NotPanics (t , func () { mo .Create (sg ) })
183
+ assert .NotPanics (t , func () { mo .Inspect (sg ) })
184
+ assert .NotPanics (t , func () { mo .Update (sg ) })
185
+ assert .NotPanics (t , func () { mo .Remove (sg ) })
77
186
})
78
- sg .ID = "abcdefghigklmn"
79
- mo .Name (sg )
80
- mo .NamespacedName (sg )
81
- mo .IsSupported ()
82
- mo .Validate (sg )
83
- sg .Labels = make (map [string ]string )
84
- sg .Labels ["USE_OPERATOR" ] = "canal"
85
- mo .Validate (sg )
86
- sg .Services [0 ].Env = make (map [string ]string )
87
- mo .Validate (sg )
88
- sg .Services [0 ].Env ["CANAL_DESTINATION" ] = "b"
89
- sg .Services [0 ].Env ["canal.admin.manager" ] = "127.0.0.1:8089"
90
- sg .Services [0 ].Env ["spring.datasource.address" ] = "1"
91
- sg .Services [0 ].Env ["spring.datasource.address" ] = "1"
92
- sg .Services [0 ].Env ["spring.datasource.username" ] = "2"
93
- sg .Services [0 ].Env ["spring.datasource.password" ] = "3"
94
- mo .Validate (sg )
95
- mo .Convert (sg )
96
- mo .Create (sg )
97
- mo .Inspect (sg )
98
- mo .Update (sg )
99
- mo .Remove (sg )
100
187
}
0 commit comments