@@ -7,12 +7,14 @@ import (
7
7
"github.com/stretchr/testify/assert"
8
8
"github.com/stretchr/testify/require"
9
9
v1 "k8s.io/api/core/v1"
10
+ "k8s.io/apimachinery/pkg/api/errors"
10
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
12
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12
13
"k8s.io/apimachinery/pkg/runtime"
13
14
"k8s.io/apimachinery/pkg/runtime/schema"
14
15
dynamicFake "k8s.io/client-go/dynamic/fake"
15
16
kubernetesFake "k8s.io/client-go/kubernetes/fake"
17
+ "k8s.io/utils/ptr"
16
18
)
17
19
18
20
var TestAuthentications = serviceAuthentication {
@@ -29,15 +31,15 @@ var TestAuthentications = serviceAuthentication{
29
31
port : 80 ,
30
32
protocol : "TCP" ,
31
33
applicationLayer : "sql" ,
32
- authenticated : true ,
34
+ authenticated : ptr . To ( true ) ,
33
35
sessionLayer : "tcp" ,
34
36
presentationLayer : "http" ,
35
37
},
36
38
{
37
39
port : 443 ,
38
40
protocol : "TCP" ,
39
41
applicationLayer : "kafka" ,
40
- authenticated : true ,
42
+ authenticated : ptr . To ( true ) ,
41
43
sessionLayer : "tcp" ,
42
44
presentationLayer : "http" ,
43
45
},
@@ -111,6 +113,7 @@ func Test_translate(t *testing.T) {
111
113
func TestDiscoveryServiceHandler (t * testing.T ) {
112
114
//write a component test that creates fake client and test the service discovery and see if it creates a crd
113
115
//and if it deletes the crd
116
+ //IMPORTANT: fake cilent doesnt have an Apply option like the real client so we need to create the crd and check if it exists -it will blog errors but will pass
114
117
testCases := []struct {
115
118
name string
116
119
services []runtime.Object
@@ -163,15 +166,15 @@ func TestDiscoveryServiceHandler(t *testing.T) {
163
166
"metadata" : map [string ]interface {}{"name" : "service1" , "namespace" : "test1" },
164
167
"spec" : map [string ]interface {}{"clusterIP" : "" ,
165
168
"ports" : []interface {}{
166
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
167
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
169
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
170
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
168
171
}}}},
169
172
{Object : map [string ]interface {}{"apiVersion" : "kubescape.io/v1" , "kind" : "ServiceScanResult" ,
170
173
"metadata" : map [string ]interface {}{"name" : "service2" , "namespace" : "test2" },
171
174
"spec" : map [string ]interface {}{"clusterIP" : "" ,
172
175
"ports" : []interface {}{
173
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
174
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
176
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
177
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
175
178
}}}},
176
179
},
177
180
},
@@ -207,8 +210,8 @@ func TestDiscoveryServiceHandler(t *testing.T) {
207
210
"metadata" : map [string ]interface {}{"name" : "service1" , "namespace" : "test1" },
208
211
"spec" : map [string ]interface {}{"clusterIP" : "" ,
209
212
"ports" : []interface {}{
210
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
211
- map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
213
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" },
214
+ map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (443 ), "presentationLayer" : "" , "protocol" : "UDP" , "sessionLayer" : "" },
212
215
}}}},
213
216
},
214
217
},
@@ -261,7 +264,7 @@ func TestDiscoveryServiceHandler(t *testing.T) {
261
264
want : []unstructured.Unstructured {
262
265
{Object : map [string ]interface {}{"apiVersion" : "kubescape.io/v1" , "kind" : "ServiceScanResult" ,
263
266
"metadata" : map [string ]interface {}{"name" : "service1" , "namespace" : "test1" },
264
- "spec" : map [string ]interface {}{"clusterIP" : "" , "ports" : []interface {}{map [string ]interface {}{"applicationLayer" : "" , "authenticated" : false , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" }}}}},
267
+ "spec" : map [string ]interface {}{"clusterIP" : "" , "ports" : []interface {}{map [string ]interface {}{"applicationLayer" : "" , "authenticated" : nil , "port" : int64 (80 ), "presentationLayer" : "" , "protocol" : "TCP" , "sessionLayer" : "" }}}}},
265
268
},
266
269
delete : []runtime.Object {
267
270
& v1.Service {
@@ -301,12 +304,12 @@ func TestDiscoveryServiceHandler(t *testing.T) {
301
304
for i := 0 ; i < 10 ; i ++ {
302
305
services , _ := serviceExtractor (ctx , regClient )
303
306
for _ , service := range services {
304
- existObj , _ := dynamicClient .Resource (ServiceScanSchema ).Namespace (service .metadata .namespace ).Get (ctx , service .metadata .name , metav1.GetOptions {})
305
- if existObj == nil {
306
- obj , _ := service .unstructured ()
307
- _ , err := dynamicClient .Resource (ServiceScanSchema ).Namespace (service .metadata .namespace ).Create (ctx , obj , metav1.CreateOptions {})
307
+ obj , _ := service .unstructured ()
308
+ _ , err := dynamicClient .Resource (ServiceScanSchema ).Namespace (service .metadata .namespace ).Create (ctx , obj , metav1.CreateOptions {})
309
+ if ! errors .IsAlreadyExists (err ) {
308
310
require .NoError (t , err )
309
311
}
312
+
310
313
}
311
314
312
315
err := discoveryService (context .Background (), regClient , dynamicClient )
@@ -315,7 +318,9 @@ func TestDiscoveryServiceHandler(t *testing.T) {
315
318
crds , _ = dynamicClient .Resource (ServiceScanSchema ).List (ctx , metav1.ListOptions {})
316
319
if tc .delete != nil {
317
320
for _ , delService := range tc .delete {
318
- _ = regClient .CoreV1 ().Services (delService .(* v1.Service ).Namespace ).Delete (ctx , delService .(* v1.Service ).Name , metav1.DeleteOptions {})
321
+ err = regClient .CoreV1 ().Services (delService .(* v1.Service ).Namespace ).Delete (ctx , delService .(* v1.Service ).Name , metav1.DeleteOptions {})
322
+ require .NoError (t , err )
323
+ tc .delete = nil
319
324
}
320
325
}
321
326
}
0 commit comments