Skip to content

Commit

Permalink
add unitest
Browse files Browse the repository at this point in the history
Signed-off-by: Raziel Cohen <rcohen@armosec.io>
  • Loading branch information
Raziel Cohen committed May 1, 2023
1 parent e93ea11 commit c9a7d26
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pkg/config/v1/config_data_mock_times.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config

import (
"path"
"sniffer/pkg/utils"
"time"
)

type ConfigDataTimesMock struct {
}

func CreateTimesMockConfigData() *ConfigDataTimesMock {
return &ConfigDataTimesMock{}
}

func (c *ConfigDataTimesMock) IsFalcoEbpfEngine() bool {
return true
}

func (c *ConfigDataTimesMock) GetFalcoSyscallFilter() []string {
return []string{"open", "openat", "execve", "execveat"}
}

func (c *ConfigDataTimesMock) GetFalcoKernelObjPath() string {
return path.Join(utils.CurrentDir(), "..", "testdata", "mock_falco_ebpf_engine", "kernel_obj_mock.o")
}

func (c *ConfigDataTimesMock) GetEbpfEngineLoaderPath() string {
return path.Join(utils.CurrentDir(), "..", "testdata", "mock_falco_ebpf_engine", "userspace_app_mock")
}

func (c *ConfigDataTimesMock) GetUpdateDataPeriod() time.Duration {
return time.Duration(5) * time.Second
}

func (c *ConfigDataTimesMock) GetSniffingMaxTimes() time.Duration {
return time.Duration(10) * time.Second
}

func (c *ConfigDataTimesMock) IsRelevantCVEServiceEnabled() bool {
return true
}

func (c *ConfigDataTimesMock) GetNodeName() string {
return "minikube"
}

func (c *ConfigDataTimesMock) GetClusterName() string {
return "test"
}

func (c *ConfigDataTimesMock) SetNodeName() {
}

func (c *ConfigDataTimesMock) SetNamespace() {
}

func (c *ConfigDataTimesMock) SetContainerName() {
}

func (c *ConfigDataTimesMock) GetNamespace() string {
return "Namespace"
}

func (c *ConfigDataTimesMock) GetContainerName() string {
return "ContName"
}

func (c *ConfigDataTimesMock) SetBackgroundContextURL() {
}

func (c *ConfigDataTimesMock) GetBackgroundContextURL() string {
return "URLcontext"
}

func (c *ConfigDataTimesMock) GetAccountID() string {
return "AccountID"
}
60 changes: 60 additions & 0 deletions pkg/conthandler/container_main_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"sniffer/pkg/config"
configV1 "sniffer/pkg/config/v1"
conthadlerV1 "sniffer/pkg/conthandler/v1"
conthandlerV1 "sniffer/pkg/conthandler/v1"
accumulator "sniffer/pkg/event_data_storage"
"sniffer/pkg/sbom"
"sniffer/pkg/storageclient"
"sniffer/pkg/utils"
"testing"
Expand Down Expand Up @@ -69,3 +71,61 @@ func TestContMainHandler(t *testing.T) {
t.Fatalf("handleNewContainerEvent failed with error %v", err)
}
}



func TestContMainHandlerStopMonitorAfterXMinutes(t *testing.T) {
configPath := path.Join(utils.CurrentDir(), "..", "..", "configuration", "ConfigurationFile.json")
t.Setenv(config.ConfigEnvVar, configPath)

cfg := config.GetConfigurationConfigContext()
configData, err := cfg.GetConfigurationReader()
if err != nil {
t.Fatalf("GetConfigurationReader failed with err %v", err)
}
err = cfg.ParseConfiguration(configV1.CreateTimesMockConfigData(), configData)
if err != nil {
t.Fatalf("ParseConfiguration failed with err %v", err)
}

cacheAccumulatorErrorChan := make(chan error)
acc := accumulator.GetAccumulator()
err = acc.StartAccumulator(cacheAccumulatorErrorChan)
if err != nil {
t.Fatalf("StartAccumulator failed with err %v", err)
}

contHandler, err := CreateContainerHandler(nil, storageclient.CreateSBOMStorageHttpClientMock())
if err != nil {
t.Fatalf("CreateContainerHandler failed with err %v", err)
}
RedisInstanceID := instanceidhandler.InstanceID{}
RedisInstanceID.SetAPIVersion("apps/v1")
RedisInstanceID.SetNamespace("any")
RedisInstanceID.SetKind("deployment")
RedisInstanceID.SetName("redis")
RedisInstanceID.SetContainerName("redis")
contEvent := conthandlerV1.CreateNewContainerEvent(RedisImageID, RedisContainerIDContHandler, RedisWLID, RedisPodName, &RedisInstanceID, conthadlerV1.ContainerRunning)

newWatchedContainer := watchedContainerData{
containerAggregator: CreateAggregator(getShortContainerID(contEvent.GetContainerID())),
snifferTicker: createTicker(),
event: *contEvent,
sbomClient: sbom.CreateSBOMStorageClient(contHandler.storageClient, contEvent.GetK8SWorkloadID(), contEvent.GetImageID(), contEvent.GetInstanceID()),
syncChannel: map[string]chan error{
StepGetSBOM: make(chan error, 10),
StepEventAggregator: make(chan error, 10),
},
}
contHandler.watchedContainers.Store(contEvent.GetContainerID(), newWatchedContainer)
now := time.Now()
contHandler.startRelevancyProcess(*contEvent)
stopTime := time.Now()
elapsedTime := stopTime.Sub(now)
if elapsedTime.Minutes() < config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes() {
t.Fatalf("elapsedTime is too little, should be %f < %f", elapsedTime.Minutes(), config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes())
}
if elapsedTime.Minutes() > (config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes() + float64(time.Minute)) {
t.Fatalf("elapsedTime is too High, should be %f > %f", elapsedTime.Minutes(), config.GetConfigurationConfigContext().GetSniffingMaxTimes().Minutes())
}
}

0 comments on commit c9a7d26

Please sign in to comment.