Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mocks to cli #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .mk/dev.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
##@ dev helpers

.PHONY: flows-mock
flows-mock: build ## Run flow capture using mocks
./build/network-observability-cli get-flows --mock true
tput reset

.PHONY: packets-mock
packets-mock: build ## Run packet capture using mocks
./build/network-observability-cli get-packets --mock true
tput reset
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,5 @@ endif
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

include .mk/dev.mk
include .mk/shortcuts.mk
1 change: 1 addition & 0 deletions cmd/flow_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func runFlowCaptureOnAddr(port int, filename string) {
log.Fatal(err)
}
log.Trace("Started collector")
collectorStarted = true

go func() {
<-utils.ExitChannel()
Expand Down
66 changes: 66 additions & 0 deletions cmd/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"

"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/write/grpc"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/write/grpc/genericmap"
"google.golang.org/protobuf/types/known/anypb"
)

func sendMock(ch chan struct{}, client genericmap.CollectorClient) {

gm := config.GenericMap{
"DstAddr": fmt.Sprintf("10.0.0.%d", rand.Intn(255)),
"DstPort": rand.Intn(9999),
"Proto": rand.Intn(255),
"SrcAddr": fmt.Sprintf("10.0.0.%d", rand.Intn(255)),
"SrcPort": rand.Intn(9999),
"SrcK8S_Name": fmt.Sprintf("Pod-%d", rand.Intn(100)),
"SrcK8S_Namespace": fmt.Sprintf("Namespace-%d", rand.Intn(100)),
"SrcK8S_Type": "Pod",
"TimeFlowEndMs": time.Now().UnixMilli(),
}
value, err := json.Marshal(gm)
if err != nil {
log.Error(err)
return
}
_, err = client.Send(context.Background(), &genericmap.Flow{
GenericMap: &anypb.Any{
Value: value,
},
})
if err != nil {
log.Error(err)
return
}

time.Sleep(10 * time.Millisecond)
ch <- struct{}{}
}

func MockForever() {
wait := make(chan struct{})

for !collectorStarted {
log.Info("Waiting for collector to start...")
time.Sleep(1 * time.Second)
}

for _, port := range ports {
cc, err := grpc.ConnectClient("127.0.0.1", port)
if err != nil {
log.Fatal(err)
}
for {
go sendMock(wait, cc.Client())
<-wait
}
}
}
1 change: 1 addition & 0 deletions cmd/packet_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func runPacketCaptureOnAddr(port int, filename string) {
log.Fatal(err)
}
log.Trace("Started collector")
collectorStarted = true

go func() {
<-utils.ExitChannel()
Expand Down
22 changes: 15 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ var (
},
}

captureType = "Flow"
outputBuffer *bytes.Buffer
captureStarted = false
stopReceived = false
keyboardError = ""
captureType = "Flow"
outputBuffer *bytes.Buffer
collectorStarted = false
captureStarted = false
stopReceived = false
useMocks = false
keyboardError = ""
)

// Execute executes the root command.
Expand All @@ -67,13 +69,14 @@ func Execute() error {

// func main() {
func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(onInit)
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "info", "Log level")
rootCmd.PersistentFlags().IntSliceVarP(&ports, "ports", "", []int{9999}, "TCP ports to listen")
rootCmd.PersistentFlags().StringSliceVarP(&nodes, "nodes", "", []string{""}, "Node names per port (optionnal)")
rootCmd.PersistentFlags().StringVarP(&filter, "filter", "", "", "Filter(s)")
rootCmd.PersistentFlags().DurationVarP(&maxTime, "maxtime", "", 5*time.Minute, "Maximum capture time")
rootCmd.PersistentFlags().Int64VarP(&maxBytes, "maxbytes", "", 50000000, "Maximum capture bytes")
rootCmd.PersistentFlags().BoolVarP(&useMocks, "mock", "", false, "Use mock")

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
Expand All @@ -92,7 +95,7 @@ func init() {
rootCmd.AddCommand(pktCmd)
}

func initConfig() {
func onInit() {
lvl, _ := logrus.ParseLevel(logLevel)
log.SetLevel(lvl)

Expand All @@ -102,6 +105,11 @@ func initConfig() {

log.Infof("Running network-observability-cli\nLog level: %s\nFilter(s): %s", logLevel, filter)
showKernelVersion()

if useMocks {
log.Info("Using mocks...")
go MockForever()
}
}

func showKernelVersion() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.34.2
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading