@@ -2,6 +2,7 @@ package adapters_test
2
2
3
3
import (
4
4
"context"
5
+ "strings"
5
6
"testing"
6
7
7
8
"cloud.google.com/go/compute/apiv1/computepb"
@@ -25,10 +26,13 @@ func TestComputeNodeGroup(t *testing.T) {
25
26
projectID := "test-project-id"
26
27
zone := "us-central1-a"
27
28
29
+ testTemplateUrl := "https://www.googleapis.com/compute/v1/projects/test-project/regions/northamerica-northeast1/nodeTemplates/node-template-1"
30
+ testTemplateUrl2 := "https://www.googleapis.com/compute/v1/projects/test-project/regions/northamerica-northeast1/nodeTemplates/node-template-2"
31
+
28
32
t .Run ("Get" , func (t * testing.T ) {
29
33
wrapper := adapters .NewComputeNodeGroup (mockClient , projectID , zone )
30
34
31
- mockClient .EXPECT ().Get (ctx , gomock .Any ()).Return (createComputeNodeGroup ("test-node-group" , computepb .NodeGroup_READY ), nil )
35
+ mockClient .EXPECT ().Get (ctx , gomock .Any ()).Return (createComputeNodeGroup ("test-node-group" , testTemplateUrl , computepb .NodeGroup_READY ), nil )
32
36
33
37
adapter := sources .WrapperToAdapter (wrapper )
34
38
@@ -90,7 +94,7 @@ func TestComputeNodeGroup(t *testing.T) {
90
94
wrapper := adapters .NewComputeNodeGroup (mockClient , projectID , zone )
91
95
adapter := sources .WrapperToAdapter (wrapper )
92
96
93
- mockClient .EXPECT ().Get (ctx , gomock .Any ()).Return (createComputeNodeGroup ("test-ng" , tc .input ), nil )
97
+ mockClient .EXPECT ().Get (ctx , gomock .Any ()).Return (createComputeNodeGroup ("test-ng" , "test-temp" , tc .input ), nil )
94
98
95
99
sdpItem , qErr := adapter .Get (ctx , wrapper .Scopes ()[0 ], "test-node-group" , true )
96
100
if qErr != nil {
@@ -112,8 +116,8 @@ func TestComputeNodeGroup(t *testing.T) {
112
116
mockComputeIterator := mocks .NewMockComputeNodeGroupIterator (ctrl )
113
117
114
118
// add mock implementation here
115
- mockComputeIterator .EXPECT ().Next ().Return (createComputeNodeGroup ("test-node-group-1" , computepb .NodeGroup_READY ), nil )
116
- mockComputeIterator .EXPECT ().Next ().Return (createComputeNodeGroup ("test-node-group-2" , computepb .NodeGroup_READY ), nil )
119
+ mockComputeIterator .EXPECT ().Next ().Return (createComputeNodeGroup ("test-node-group-1" , testTemplateUrl , computepb .NodeGroup_READY ), nil )
120
+ mockComputeIterator .EXPECT ().Next ().Return (createComputeNodeGroup ("test-node-group-2" , testTemplateUrl2 , computepb .NodeGroup_READY ), nil )
117
121
mockComputeIterator .EXPECT ().Next ().Return (nil , iterator .Done )
118
122
119
123
// Mock the List method
@@ -133,17 +137,80 @@ func TestComputeNodeGroup(t *testing.T) {
133
137
t .Fatalf ("Expected no validation error, got: %v" , item .Validate ())
134
138
}
135
139
136
- if item .GetLinkedItemQueries ()[0 ].GetQuery ().GetQuery () != "node-template-1" {
137
- t .Fatalf ("Expected node-template-1 as query, got: %s" , item .GetTags ()["env" ])
140
+ query := item .GetLinkedItemQueries ()[0 ].GetQuery ().GetQuery ()
141
+ if ! strings .Contains (query , "node-template" ) {
142
+ t .Fatalf ("Expected node-template in query, got: %s" , query )
143
+ }
144
+ }
145
+ })
146
+
147
+ t .Run ("Search" , func (t * testing.T ) {
148
+ wrapper := adapters .NewComputeNodeGroup (mockClient , projectID , zone )
149
+ adapter := sources .WrapperToAdapter (wrapper )
150
+
151
+ filterBy := testTemplateUrl
152
+
153
+ // Mock the List method
154
+ mockClient .EXPECT ().List (ctx , gomock .Any ()).DoAndReturn (func (ctx context.Context , req * computepb.ListNodeGroupsRequest , opts ... any ) * mocks.MockComputeNodeGroupIterator {
155
+ fullList := []* computepb.NodeGroup {
156
+ createComputeNodeGroup ("test-node-group-1" , testTemplateUrl , computepb .NodeGroup_READY ),
157
+ createComputeNodeGroup ("test-node-group-2" , testTemplateUrl2 , computepb .NodeGroup_READY ),
158
+ createComputeNodeGroup ("test-node-group-3" , testTemplateUrl , computepb .NodeGroup_READY ),
159
+ createComputeNodeGroup ("test-node-group-4" , testTemplateUrl , computepb .NodeGroup_READY ),
160
+ }
161
+
162
+ expectedFilter := "nodeTemplate = " + filterBy
163
+ if req .GetFilter () != expectedFilter {
164
+ t .Fatalf ("Expected filter to be %s, got: %s" , expectedFilter , req .GetFilter ())
165
+ }
166
+
167
+ mockComputeIterator := mocks .NewMockComputeNodeGroupIterator (ctrl )
168
+ for _ , nodeGroup := range fullList {
169
+ if nodeGroup .GetNodeTemplate () == filterBy {
170
+ mockComputeIterator .EXPECT ().Next ().Return (nodeGroup , nil )
171
+ }
172
+ }
173
+
174
+ mockComputeIterator .EXPECT ().Next ().Return (nil , iterator .Done )
175
+
176
+ return mockComputeIterator
177
+ })
178
+
179
+ // [SPEC] Search filters by the node template URL. It will list and filter out
180
+ // any node groups that are not using the given URL.
181
+
182
+ sdpItems , err := adapter .Search (ctx , wrapper .Scopes ()[0 ], testTemplateUrl , true )
183
+ if err != nil {
184
+ t .Fatalf ("Expected no error, got: %v" , err )
185
+ }
186
+
187
+ // 1 of 4 are filtered out.
188
+ if len (sdpItems ) != 3 {
189
+ t .Fatalf ("Expected 3 items, got: %d" , len (sdpItems ))
190
+ }
191
+
192
+ for _ , item := range sdpItems {
193
+ if item .Validate () != nil {
194
+ t .Fatalf ("Expected no validation error, got: %v" , item .Validate ())
195
+ }
196
+
197
+ attributes := item .GetAttributes ()
198
+ nodeTemplate , err := attributes .Get ("node_template" )
199
+ if err != nil {
200
+ t .Fatalf ("Failed to get node_template attribute: %v" , err )
201
+ }
202
+
203
+ if nodeTemplate != testTemplateUrl {
204
+ t .Fatalf ("Expected node_template to be %s, got: %s" , testTemplateUrl , nodeTemplate )
138
205
}
139
206
}
140
207
})
141
208
}
142
209
143
- func createComputeNodeGroup (name string , status computepb.NodeGroup_Status ) * computepb.NodeGroup {
210
+ func createComputeNodeGroup (name , templateUrl string , status computepb.NodeGroup_Status ) * computepb.NodeGroup {
144
211
return & computepb.NodeGroup {
145
212
Name : ptr .To (name ),
146
- NodeTemplate : ptr .To ("https://www.googleapis.com/compute/v1/projects/test-project/regions/northamerica-northeast1/nodeTemplates/node-template-1" ),
213
+ NodeTemplate : ptr .To (templateUrl ),
147
214
Status : ptr .To (status .String ()),
148
215
}
149
216
}
0 commit comments