From 06f4c29a1deeaf6884c88c01f5f6cf03795c5292 Mon Sep 17 00:00:00 2001 From: Alejandro Tjaarda <alexdecb@yahoo.es> Date: Fri, 27 Sep 2024 19:01:04 +0000 Subject: [PATCH] switch: deleted no used init tests --- src/switch/cmd/l2sm-init/main_test.go | 98 ------------------------- src/switch/cmd/l2sm-vxlans/main_test.go | 52 ------------- 2 files changed, 150 deletions(-) delete mode 100644 src/switch/cmd/l2sm-init/main_test.go delete mode 100644 src/switch/cmd/l2sm-vxlans/main_test.go diff --git a/src/switch/cmd/l2sm-init/main_test.go b/src/switch/cmd/l2sm-init/main_test.go deleted file mode 100644 index bd26d7f..0000000 --- a/src/switch/cmd/l2sm-init/main_test.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "errors" - "flag" - "fmt" - "os" - "os/exec" - "testing" - - "l2sm.local/ovs-switch/pkg/ovs" -) - -// Mock implementation of ovs.Bridge for testing purposes -type MockBridge struct { - Name string - Controller string - Protocol string - DatapathId string -} - -func (b *MockBridge) AddPort(port string) error { - return nil -} - -func (b *MockBridge) String() string { - return fmt.Sprintf("MockBridge{Name: %s, Controller: %s, Protocol: %s, DatapathId: %s}", b.Name, b.Controller, b.Protocol, b.DatapathId) -} - -// Override ovs.NewBridge for testing -var NewBridge = func(b ovs.Bridge) (ovs.Bridge, error) { - return ovs.Bridge{ - Name: b.Name, - Controller: b.Controller, - Protocol: b.Protocol, - DatapathId: b.DatapathId, - }, nil -} - -func TestTakeArguments(t *testing.T) { - // Backup original command line arguments - oldArgs := os.Args - defer func() { os.Args = oldArgs }() - - tests := []struct { - args []string - expectedErr error - }{ - {[]string{"cmd", "-n_veths", "5", "-controller_ip", "192.168.1.1", "-switch_name", "switch1"}, nil}, - {[]string{"cmd", "-n_veths", "5", "-controller_ip", ""}, errors.New("controller IP is not defined")}, - } - - for _, test := range tests { - os.Args = test.args - flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) - - _, _, _, err := takeArguments() - if err != nil && err.Error() != test.expectedErr.Error() { - t.Errorf("Expected error: %v, got: %v", test.expectedErr, err) - } - } -} - -func TestInitializeSwitch(t *testing.T) { - // Mock exec.Command for testing - oldExecCommand := exec.Command - defer func() { exec.Command = oldExecCommand }() - - exec.Command = func(name string, arg ...string) *exec.Cmd { - cmd := oldExecCommand("echo", "192.168.1.1") - return cmd - } - - tests := []struct { - switchName string - controllerIP string - expectedErr error - }{ - {"switch1", "192.168.1.1", nil}, - {"switch1", "invalid-ip", nil}, - } - - for _, test := range tests { - bridge, err := initializeSwitch(test.switchName, test.controllerIP) - if err != nil && err.Error() != test.expectedErr.Error() { - t.Errorf("Expected error: %v, got: %v", test.expectedErr, err) - } - if bridge == nil { - t.Errorf("Expected bridge to be initialized, got nil") - } - } -} - -func TestGenerateDatapathID(t *testing.T) { - - datapathID := generateDatapathID("pod") - fmt.Println(datapathID) -} diff --git a/src/switch/cmd/l2sm-vxlans/main_test.go b/src/switch/cmd/l2sm-vxlans/main_test.go deleted file mode 100644 index 47737f7..0000000 --- a/src/switch/cmd/l2sm-vxlans/main_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package main - -import ( - "encoding/json" - "testing" - - "l2sm.local/ovs-switch/pkg/ovs" -) - -func TestCreateTopology(t *testing.T) { - bridge := ovs.FromName("brtun") - - config := `{ - "Nodes": [ - {"name": "netma-test-2", "nodeIP": "10.244.1.8"}, - {"name": "netma-test-3", "nodeIP": "10.244.2.10"}, - {"name": "netma-test-1", "nodeIP": "10.244.0.4"} - ], - "Links": [ - {"endpointA": "netma-test-2", "endpointB": "netma-test-3"}, - {"endpointA": "netma-test-2", "endpointB": "netma-test-1"}, - {"endpointA": "netma-test-3", "endpointB": "netma-test-1"} - ] - }` - - var topology Topology - err := json.Unmarshal([]byte(config), &topology) - if err != nil { - t.Fatalf("Error unmarshalling config: %v", err) - } - - nodeName := "netma-test-1" - err = createTopology(bridge, topology, nodeName) - if err != nil { - t.Fatalf("Error creating topology: %v", err) - } - - expectedCommands := []string{ - "ovs-vsctl add-port brtun vxlan1 -- set interface vxlan1 type=vxlan options:key=flow options:remote_ip=10.244.1.8 options:local_ip=10.244.0.4 options:dst_port=7000", - "ovs-vsctl add-port brtun vxlan2 -- set interface vxlan2 type=vxlan options:key=flow options:remote_ip=10.244.2.10 options:local_ip=10.244.0.4 options:dst_port=7000", - } - - if len(bridge.Commands) != len(expectedCommands) { - t.Fatalf("Expected %d commands, got %d", len(expectedCommands), len(bridge.Commands)) - } - - for i, command := range expectedCommands { - if bridge.Commands[i] != command { - t.Errorf("Expected command %q, got %q", command, bridge.Commands[i]) - } - } -} -- GitLab