Skip to content

Commit e0ee4e7

Browse files
Merge pull request #32 from kubescape/dev-branch
adding unitests
2 parents 5665b8b + cf8adea commit e0ee4e7

File tree

7 files changed

+352
-1
lines changed

7 files changed

+352
-1
lines changed

internal/validator/validator_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package validator
22

33
import (
4+
"os"
5+
"path"
6+
"sniffer/pkg/config"
7+
v1 "sniffer/pkg/config/v1"
8+
"sniffer/pkg/utils"
49
"testing"
510
)
611

@@ -45,3 +50,28 @@ func TestInt8ToStr(t *testing.T) {
4550
t.Errorf("int8ToStr(%v) = %v; want %v", input4, output4, expected4)
4651
}
4752
}
53+
54+
func TestCheckPrerequisites(t *testing.T) {
55+
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
56+
err := os.Setenv(config.ConfigEnvVar, configPath)
57+
if err != nil {
58+
t.Fatalf("failed to set env %s with err %v", config.ConfigEnvVar, err)
59+
}
60+
61+
config := config.GetConfigurationConfigContext()
62+
configData, err := config.GetConfigurationReader()
63+
if err != nil {
64+
t.Errorf("GetConfigurationReader failed with err %v", err)
65+
}
66+
err = config.ParseConfiguration(v1.CreateConfigData(), configData)
67+
if err != nil {
68+
t.Fatalf("ParseConfiguration failed with err %v", err)
69+
}
70+
71+
minKernelVersion = "0.1"
72+
err = CheckPrerequisites()
73+
if err != nil {
74+
t.Fatalf("checkNodePrerequisites failed with err %v", err)
75+
}
76+
77+
}

pkg/config/v1/config_data_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,68 @@ func TestConfigData_SetNodeName(t *testing.T) {
140140
}
141141
}
142142

143+
func TestConfigData_SetNamespace(t *testing.T) {
144+
expectedName := "namespace-1"
145+
os.Setenv(NamespaceEnvVar, expectedName)
146+
c := &ConfigData{}
147+
c.SetNamespace()
148+
if c.Namespace != expectedName {
149+
t.Errorf("SetNamespace() failed to set the namespace to %s, got %s instead", expectedName, c.Namespace)
150+
}
151+
}
152+
153+
func TestConfigData_SetContainerName(t *testing.T) {
154+
expectedName := "cont-1"
155+
os.Setenv(ContainerNameEnvVar, expectedName)
156+
c := &ConfigData{}
157+
c.SetContainerName()
158+
if c.ContainerName != expectedName {
159+
t.Errorf("SetContainerName() failed to set the container name to %s, got %s instead", expectedName, c.ContainerName)
160+
}
161+
}
162+
163+
func TestConfigData_SetBackgroundContextURL(t *testing.T) {
164+
expectedName := "URL-1"
165+
os.Setenv("OTEL_COLLECTOR_SVC", expectedName)
166+
c := &ConfigData{}
167+
c.SetBackgroundContextURL()
168+
if c.telemetryURL != expectedName {
169+
t.Errorf("SetBackgroundContextURL() failed to set the background context name to %s, got %s instead", expectedName, c.telemetryURL)
170+
}
171+
}
172+
173+
func TestConfigData_GetNamespace(t *testing.T) {
174+
expectedName := ""
175+
c := &ConfigData{}
176+
if c.GetNamespace() != expectedName {
177+
t.Errorf("GetNamespace() failed to get the namespace to %s, got %s instead", expectedName, c.Namespace)
178+
}
179+
}
180+
181+
func TestConfigData_GetContainerName(t *testing.T) {
182+
expectedName := ""
183+
c := &ConfigData{}
184+
if c.GetContainerName() != expectedName {
185+
t.Errorf("GetContainerName() failed to set the container name to %s, got %s instead", expectedName, c.NodeData.Name)
186+
}
187+
}
188+
189+
func TestConfigData_GetBackgroundContextURL(t *testing.T) {
190+
expectedName := ""
191+
c := &ConfigData{}
192+
if c.GetBackgroundContextURL() != expectedName {
193+
t.Errorf("GetBackgroundContextURL() failed to get the background context name to %s, got %s instead", expectedName, c.NodeData.Name)
194+
}
195+
}
196+
197+
func TestConfigData_GetAccountID(t *testing.T) {
198+
expectedName := ""
199+
c := &ConfigData{}
200+
if c.GetAccountID() != expectedName {
201+
t.Errorf("GetAccountID() failed to get the account ID name to %s, got %s instead", expectedName, c.NodeData.Name)
202+
}
203+
}
204+
143205
// check is slices are equal
144206
func equalStringSlices(a, b []string) bool {
145207
if len(a) != len(b) {

pkg/conthandler/container_watcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (client *k8sFakeClient) GetApiVersion(workload any) string {
2727
}
2828

2929
func (client *k8sFakeClient) GetResourceVersion(workload any) string {
30-
return "1234"
30+
return "1234wat"
3131
}
3232

3333
func (client *k8sFakeClient) CalculateWorkloadParentRecursive(workload any) (string, string, error) {

pkg/ebpfeng/falco_sniffer_engine_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package ebpfeng
22

33
import (
4+
"os"
5+
"path"
6+
"sniffer/pkg/config"
7+
v1 "sniffer/pkg/config/v1"
8+
"sniffer/pkg/utils"
9+
"strings"
410
"testing"
511
)
612

@@ -29,6 +35,48 @@ func TestConvertStringTimeToTimeOBJ(t *testing.T) {
2935
t.Fatalf("timestamp convert nanosecond is wrong")
3036
}
3137

38+
timestamp = "2w023-02-14T14:30:06.863996608+0000"
39+
_, err = convertStringTimeToTimeOBJ(timestamp)
40+
if err == nil {
41+
t.Fatalf("timestamp convert should fail")
42+
}
43+
44+
timestamp = "2023-0w2-14T14:30:06.863996608+0000"
45+
_, err = convertStringTimeToTimeOBJ(timestamp)
46+
if err == nil {
47+
t.Fatalf("timestamp convert should fail")
48+
}
49+
50+
timestamp = "2023-02-1w4T14:30:06.863996608+0000"
51+
_, err = convertStringTimeToTimeOBJ(timestamp)
52+
if err == nil {
53+
t.Fatalf("timestamp convert should fail")
54+
}
55+
56+
timestamp = "2023-02-14T1w4:30:06.863996608+0000"
57+
_, err = convertStringTimeToTimeOBJ(timestamp)
58+
if err == nil {
59+
t.Fatalf("timestamp convert should fail")
60+
}
61+
62+
timestamp = "2023-02-14T14:3w0:06.863996608+0000"
63+
_, err = convertStringTimeToTimeOBJ(timestamp)
64+
if err == nil {
65+
t.Fatalf("timestamp convert should fail")
66+
}
67+
68+
timestamp = "2023-02-14T14:30:0w6.863996608+0000"
69+
_, err = convertStringTimeToTimeOBJ(timestamp)
70+
if err == nil {
71+
t.Fatalf("timestamp convert should fail")
72+
}
73+
74+
timestamp = "2023-02-14T14:30:06.86w3996608+0000"
75+
_, err = convertStringTimeToTimeOBJ(timestamp)
76+
if err == nil {
77+
t.Fatalf("timestamp convert should fail")
78+
}
79+
3280
}
3381

3482
func TestParseFalcoEvent(t *testing.T) {
@@ -46,4 +94,132 @@ func TestParseFalcoEvent(t *testing.T) {
4694
if ev.GetEventSyscallArgs() != "TYPE=openat(fd: <f>/var/lib/kubelet/pods, dirfd: AT_FDCWD, name: /var/lib/kubelet/pods, flags: O_RDONLY|O_CLOEXEC, mode: 0, dev: 802, ino: 6456368)" {
4795
t.Fatalf("ev.GetEventContainerID() failed")
4896
}
97+
98+
line = "drop event occured"
99+
ev, err = parseLine(line)
100+
if err != nil {
101+
t.Fatalf("drop event: parseLine failed with err %v", err)
102+
}
103+
104+
if !strings.Contains(ev.GetEventCMD(), "drop event occurred") {
105+
t.Fatalf("drop event should contain line: %s in cmd", line)
106+
}
107+
108+
line = "2023-02-14T14:30:06.863996608+0000]::[0002f88945ec]::"
109+
_, err = parseLine(line)
110+
if err == nil {
111+
t.Fatalf("parse line should fail")
112+
}
113+
114+
line = "2023-02-14T14:]::[0002f88945ec]::[CAT=FILE]::[PPID=3006]::[PID=4525]::[TYPE=openat(fd: <f>/var/lib/kubelet/pods, dirfd: AT_FDCWD, name: /var/lib/kubelet/pods, flags: O_RDONLY|O_CLOEXEC, mode: 0, dev: 802, ino: 6456368)]::[EXE=/var/lib/minikube/binaries/v1.24.3/kubelet]::[CMD="
115+
_, err = parseLine(line)
116+
if err == nil {
117+
t.Fatalf("parse line should fail")
118+
}
119+
120+
}
121+
122+
func TestCreateSyscallFilterString(t *testing.T) {
123+
expectedFilterString := "evt.type=execve or evt.type=open"
124+
filterString := createSyscallFilterString([]string{"execve", "open"})
125+
126+
if filterString != expectedFilterString {
127+
t.Fatalf("filterString:%s should be equal to expectedFilterString:%s", filterString, expectedFilterString)
128+
}
129+
130+
}
131+
132+
func TestCreateFalcoEbpfEngine(t *testing.T) {
133+
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
134+
err := os.Setenv(config.ConfigEnvVar, configPath)
135+
if err != nil {
136+
t.Fatalf("failed to set env %s with err %v", config.ConfigEnvVar, err)
137+
}
138+
139+
config := config.GetConfigurationConfigContext()
140+
configData, err := config.GetConfigurationReader()
141+
if err != nil {
142+
t.Errorf("GetConfigurationReader failed with err %v", err)
143+
}
144+
err = config.ParseConfiguration(v1.CreateConfigData(), configData)
145+
if err != nil {
146+
t.Fatalf("ParseConfiguration failed with err %v", err)
147+
}
148+
149+
engine := CreateFalcoEbpfEngine([]string{"123", "456"}, false, false, "")
150+
if engine.containerID != "" || engine.includeHost != false || engine.sniffMainThreadOnly != false {
151+
t.Fatalf("CreateFalcoEbpfEngine fail to create as expected")
152+
}
153+
}
154+
155+
func TestEbpfEngineCMDWithParams(t *testing.T) {
156+
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
157+
err := os.Setenv(config.ConfigEnvVar, configPath)
158+
if err != nil {
159+
t.Fatalf("failed to set env %s with err %v", config.ConfigEnvVar, err)
160+
}
161+
162+
config := config.GetConfigurationConfigContext()
163+
configData, err := config.GetConfigurationReader()
164+
if err != nil {
165+
t.Errorf("GetConfigurationReader failed with err %v", err)
166+
}
167+
err = config.ParseConfiguration(v1.CreateConfigData(), configData)
168+
if err != nil {
169+
t.Fatalf("ParseConfiguration failed with err %v", err)
170+
}
171+
172+
engine := CreateFalcoEbpfEngine([]string{"123", "456"}, false, false, "")
173+
if engine.containerID != "" || engine.includeHost != false || engine.sniffMainThreadOnly != false {
174+
t.Fatalf("CreateFalcoEbpfEngine fail to create as expected")
175+
}
176+
177+
cmd := engine.ebpfEngineCMDWithParams()
178+
if cmd[0] != "-f" || cmd[1] != "evt.type=123 or evt.type=456" || cmd[2] != "-e" {
179+
t.Fatalf("ebpfEngineCMDWithParams is note with the right values %v", cmd)
180+
}
181+
182+
engine2 := CreateFalcoEbpfEngine([]string{"123", "456"}, true, true, "123")
183+
if engine2.containerID != "123" || engine2.includeHost != true || engine2.sniffMainThreadOnly != true {
184+
t.Fatalf("CreateFalcoEbpfEngine fail to create as expected")
185+
}
186+
187+
cmd2 := engine2.ebpfEngineCMDWithParams()
188+
if cmd2[0] != "-f" || cmd2[1] != "evt.type=123 or evt.type=456" || cmd2[2] != "-o" || cmd2[3] != "-m" || cmd2[4] != "-c" || cmd2[5] != "123" || cmd2[6] != "-e" {
189+
t.Fatalf("ebpfEngineCMDWithParams is note with the right values %v", cmd)
190+
}
191+
192+
}
193+
194+
func TestStartEbpfEngine(t *testing.T) {
195+
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
196+
err := os.Setenv(config.ConfigEnvVar, configPath)
197+
if err != nil {
198+
t.Fatalf("failed to set env ConfigEnvVar with err %v", err)
199+
}
200+
201+
cfg := config.GetConfigurationConfigContext()
202+
configData, err := cfg.GetConfigurationReader()
203+
if err != nil {
204+
t.Fatalf("GetConfigurationReader failed with err %v", err)
205+
}
206+
err = cfg.ParseConfiguration(v1.CreateFalcoMockConfigData(), configData)
207+
if err != nil {
208+
t.Fatalf("ParseConfiguration failed with err %v", err)
209+
}
210+
211+
engine := CreateFalcoEbpfEngine([]string{"123", "456"}, false, false, "")
212+
if engine.containerID != "" || engine.includeHost != false || engine.sniffMainThreadOnly != false {
213+
t.Fatalf("CreateFalcoEbpfEngine fail to create as expected")
214+
}
215+
216+
cmd := engine.ebpfEngineCMDWithParams()
217+
if cmd[0] != "-f" || cmd[1] != "evt.type=123 or evt.type=456" || cmd[2] != "-e" {
218+
t.Fatalf("ebpfEngineCMDWithParams is note with the right values %v", cmd)
219+
}
220+
221+
err = engine.StartEbpfEngine()
222+
if err != nil {
223+
t.Fatalf("StartEbpfEngine failed with err %v", err)
224+
}
49225
}

pkg/sbom/sbom_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,22 @@ func TestStoreFilterSBOM(t *testing.T) {
5151
}
5252

5353
}
54+
55+
func TestStoreFilterSBOMFailure(t *testing.T) {
56+
SBOMClient := CreateSBOMStorageClient(storageclient.CreateStorageHttpClientFailureMock(), NGINX_WLID)
57+
err := SBOMClient.GetSBOM(storageclient.NGINX)
58+
if err != nil {
59+
t.Fatalf("fail to get sbom")
60+
}
61+
err = SBOMClient.FilterSBOM(map[string]bool{
62+
"/usr/share/adduser/adduser.conf": true,
63+
})
64+
if err != nil {
65+
t.Fatalf("fail to filter sbom")
66+
}
67+
err = SBOMClient.StoreFilterSBOM("anyInstanceID")
68+
if err == nil {
69+
t.Fatalf("StoreFilterSBOM should fail")
70+
}
71+
72+
}

pkg/storageclient/storage_client_mock.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package storageclient
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
"path"
78
"sniffer/pkg/utils"
@@ -13,6 +14,10 @@ type StorageHttpClientMock struct {
1314
nginxSBOMSpdxBytes *spdxv1beta1.SBOMSPDXv2p3
1415
}
1516

17+
type StorageHttpClientFailureMock struct {
18+
nginxSBOMSpdxBytes *spdxv1beta1.SBOMSPDXv2p3
19+
}
20+
1621
const (
1722
NGINX = "6a59f1cbb8d28ac484176d52c473494859a512ddba3ea62a547258cf16c9b3ae"
1823
)
@@ -46,3 +51,39 @@ func (sc *StorageHttpClientMock) PutData(key string, data any) error {
4651
func (sc *StorageHttpClientMock) PostData(key string, data any) error {
4752
return nil
4853
}
54+
55+
func CreateStorageHttpClientFailureMock() *StorageHttpClientFailureMock {
56+
var data spdxv1beta1.SBOMSPDXv2p3
57+
nginxSBOMPath := path.Join(utils.CurrentDir(), "testdata", "nginx-spdx-format-mock.json")
58+
bytes, err := os.ReadFile(nginxSBOMPath)
59+
if err != nil {
60+
return nil
61+
}
62+
err = json.Unmarshal(bytes, &data)
63+
if err != nil {
64+
return nil
65+
}
66+
67+
return &StorageHttpClientFailureMock{
68+
nginxSBOMSpdxBytes: &data,
69+
}
70+
}
71+
72+
func (sc *StorageHttpClientFailureMock) GetData(key string) (any, error) {
73+
if key == NGINX {
74+
return sc.nginxSBOMSpdxBytes, nil
75+
}
76+
return nil, nil
77+
}
78+
79+
func (sc *StorageHttpClientFailureMock) PutData(key string, data any) error {
80+
return fmt.Errorf("any")
81+
}
82+
83+
func (sc *StorageHttpClientFailureMock) PostData(key string, data any) error {
84+
return fmt.Errorf("error already exist")
85+
}
86+
87+
func (sc *StorageHttpClientFailureMock) IsAlreadyExist(err error) bool {
88+
return true
89+
}

0 commit comments

Comments
 (0)