Skip to content

Commit

Permalink
Mock KubeData
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Sep 22, 2022
1 parent 0afae26 commit f1e8240
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/flowlogs-pipeline/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"

"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline"
"github.com/stretchr/testify/require"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/transform/kubernetes"
)

func TestTheMain(t *testing.T) {
Expand All @@ -43,6 +45,11 @@ func TestTheMain(t *testing.T) {
}

func TestPipelineConfigSetup(t *testing.T) {
// Kube init mock
kdata := new(kubernetes.KubeDataMock)
kdata.On("InitFromConfig", "").Return(nil)
kubernetes.Data = kdata

js := `{
"PipeLine": "[{\"name\":\"grpc\"},{\"follows\":\"grpc\",\"name\":\"enrich\"},{\"follows\":\"enrich\",\"name\":\"loki\"},{\"follows\":\"enrich\",\"name\":\"prometheus\"}]",
"Parameters": "[{\"ingest\":{\"grpc\":{\"port\":2055},\"type\":\"grpc\"},\"name\":\"grpc\"},{\"name\":\"enrich\",\"transform\":{\"network\":{\"rules\":[{\"input\":\"SrcAddr\",\"output\":\"SrcK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstAddr\",\"output\":\"DstK8S\",\"type\":\"add_kubernetes\"},{\"input\":\"DstPort\",\"output\":\"Service\",\"parameters\":\"Proto\",\"type\":\"add_service\"},{\"input\":\"SrcAddr\",\"output\":\"SrcSubnet\",\"parameters\":\"/16\",\"type\":\"add_subnet\"}]},\"type\":\"network\"}},{\"name\":\"loki\",\"write\":{\"loki\":{\"batchSize\":102400,\"batchWait\":\"1s\",\"clientConfig\":{\"follow_redirects\":false,\"proxy_url\":null,\"tls_config\":{\"insecure_skip_verify\":false}},\"labels\":[\"SrcK8S_Namespace\",\"SrcK8S_OwnerName\",\"DstK8S_Namespace\",\"DstK8S_OwnerName\",\"FlowDirection\"],\"maxBackoff\":\"5m0s\",\"maxRetries\":10,\"minBackoff\":\"1s\",\"staticLabels\":{\"app\":\"netobserv-flowcollector\"},\"tenantID\":\"netobserv\",\"timeout\":\"10s\",\"timestampLabel\":\"TimeFlowEndMs\",\"timestampScale\":\"1ms\",\"url\":\"http://loki.netobserv.svc:3100/\"},\"type\":\"loki\"}},{\"encode\":{\"prom\":{\"metrics\":[{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\",\"SrcK8S_Namespace\"],\"name\":\"bandwidth_per_network_service_per_namespace\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"SrcSubnet\"],\"name\":\"bandwidth_per_source_subnet\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"filter\":{\"key\":\"\",\"value\":\"\"},\"labels\":[\"Service\"],\"name\":\"network_service_total\",\"type\":\"counter\",\"valueKey\":\"\"}],\"port\":9102,\"prefix\":\"netobserv_\"},\"type\":\"prom\"},\"name\":\"prometheus\"}]",
Expand Down
13 changes: 13 additions & 0 deletions pkg/pipeline/transform/kubernetes/kubernetes-mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kubernetes

import "github.com/stretchr/testify/mock"

type KubeDataMock struct {
mock.Mock
kubeDataInterface
}

func (o *KubeDataMock) InitFromConfig(kubeConfigPath string) error {
args := o.Called(kubeConfigPath)
return args.Error(0)
}
8 changes: 7 additions & 1 deletion pkg/pipeline/transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var Data KubeData
var Data kubeDataInterface = &KubeData{}

const (
kubeConfigEnvVariable = "KUBECONFIG"
Expand All @@ -47,7 +47,13 @@ const (
typeService = "Service"
)

type kubeDataInterface interface {
GetInfo(string) (*Info, error)
InitFromConfig(string) error
}

type KubeData struct {
kubeDataInterface
ipInformers map[string]cache.SharedIndexInformer
replicaSetInformer cache.SharedIndexInformer
stopChan chan struct{}
Expand Down

0 comments on commit f1e8240

Please sign in to comment.