Skip to content
Snippets Groups Projects
Commit 06f4c29a authored by Alejandro Tjaarda's avatar Alejandro Tjaarda
Browse files

switch: deleted no used init tests

parent 04d369c6
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
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)
}
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])
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment