From 681d152e3c5cd28eeba6c07712583e07fbbdd4e3 Mon Sep 17 00:00:00 2001 From: baoyinghai_yewu Date: Mon, 25 Nov 2024 11:11:40 +0800 Subject: [PATCH] feat: add unit test for printer Signed-off-by: baoyinghai_yewu --- pkg/command/share/printer.go | 10 +- pkg/command/share/printer_test.go | 1697 +++++++++++++++++++++++++++++ pkg/utils/file_test.go | 134 +++ 3 files changed, 1838 insertions(+), 3 deletions(-) create mode 100644 pkg/command/share/printer_test.go create mode 100644 pkg/utils/file_test.go diff --git a/pkg/command/share/printer.go b/pkg/command/share/printer.go index 408847b0..f6a9ef50 100644 --- a/pkg/command/share/printer.go +++ b/pkg/command/share/printer.go @@ -2,6 +2,7 @@ package share import ( "fmt" + "io" "os" "strconv" @@ -19,13 +20,16 @@ type PrintCheckData struct { } func PrintResult(resultData []*PrintCheckData) { - table := tablewriter.NewWriter(os.Stdout) + PrintResultWithWriter(resultData, os.Stdout) +} +func PrintResultWithWriter(resultData []*PrintCheckData, writer io.Writer) { + table := tablewriter.NewWriter(writer) table.SetHeader([]string{"S/N", "SRC_NODE_NAME", "DST_NODE_NAME", "TARGETP", "RESULT"}) - tableException := tablewriter.NewWriter(os.Stdout) + tableException := tablewriter.NewWriter(writer) tableException.SetHeader([]string{"S/N", "SRC_NODE_NAME", "DST_NODE_NAME", "TARGET", "RESULT", "LOG"}) - tableFailed := tablewriter.NewWriter(os.Stdout) + tableFailed := tablewriter.NewWriter(writer) tableFailed.SetHeader([]string{"S/N", "SRC_NODE_NAME", "DST_NODE_NAME", "TARGET", "RESULT", "LOG"}) resumeData := []*PrintCheckData{} diff --git a/pkg/command/share/printer_test.go b/pkg/command/share/printer_test.go new file mode 100644 index 00000000..8c5eb2a0 --- /dev/null +++ b/pkg/command/share/printer_test.go @@ -0,0 +1,1697 @@ +package share + +import ( + "bytes" + "testing" + + command "github.com/kosmos.io/netdoctor/pkg/command/share/remote-command" +) + +func TestPrintResult(t *testing.T) { + + tests := []struct { + checkData []*PrintCheckData + containsString string + }{ + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + }, + containsString: command.PrintStatus(command.CommandSuccessed), + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: command.PrintStatus(command.CommandFailed), + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.8.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "Node14", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: command.PrintStatus(command.ExecError), + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.3.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "target ip", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "target ip", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.11.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.7.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.11.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "192.168.4.1", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "NodeTarget", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "NodeTarget", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.100.3", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.100.1", + }, + }, + containsString: "192.168.100.3", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "255.255.2555.255", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "255.255.2555.0", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + }, + containsString: "255.255.2555.255", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Node14", + TargetIP: "192.168.8.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "NodeTarget", + DstNodeName: "Node16", + TargetIP: "255.255.2555.0", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + }, + containsString: "255.255.2555.0", + }, + { + checkData: []*PrintCheckData{ + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node1", + DstNodeName: "Node2", + TargetIP: "192.168.1.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node3", + DstNodeName: "Node4", + TargetIP: "192.168.2.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node5", + DstNodeName: "Node6", + TargetIP: "192.168.3.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node7", + DstNodeName: "Node8", + TargetIP: "192.168.4.1", + }, + { + Result: command.Result{ + Status: command.ExecError, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node9", + DstNodeName: "Node10", + TargetIP: "192.168.5.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node15", + DstNodeName: "Node16", + TargetIP: "192.168.9.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node17", + DstNodeName: "Node18", + TargetIP: "192.168.10.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node21", + DstNodeName: "Node20", + TargetIP: "192.168.11.1", + }, + { + Result: command.Result{ + Status: command.CommandSuccessed, + ResultStr: "Command executed successfully", + }, + SrcNodeName: "Node22", + DstNodeName: "Node23", + TargetIP: "192.168.12.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node11", + DstNodeName: "Node12", + TargetIP: "192.168.7.1", + }, + { + Result: command.Result{ + Status: command.CommandFailed, + ResultStr: "Command failed to execute", + }, + SrcNodeName: "Node13", + DstNodeName: "Nodetarget", + TargetIP: "192.168.8.1", + }, + }, + containsString: "Nodetarget", + }, + } + + for _, test := range tests { + var outputBuffer bytes.Buffer + + PrintResultWithWriter(test.checkData, &outputBuffer) + output := outputBuffer.String() + + if !contains(output, test.containsString) { + t.Errorf("Expected output to contain '%s', got: %s", test.containsString, output) + } + } + +} + +func contains(output, substr string) bool { + return bytes.Contains([]byte(output), []byte(substr)) +} diff --git a/pkg/utils/file_test.go b/pkg/utils/file_test.go new file mode 100644 index 00000000..b184f629 --- /dev/null +++ b/pkg/utils/file_test.go @@ -0,0 +1,134 @@ +package utils + +import ( + "testing" +) + +func TestWrite(t *testing.T) { + type testStruct struct { + Test string + } + + testCases := []struct { + name string + datas testStruct + filePath string + wantErr bool + }{ + {"success", testStruct{"test"}, "test.json", false}, + {"fail", testStruct{"test"}, "/no_permission_dir/test.json", true}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := Write(tc.datas, tc.filePath) + if (err != nil) != tc.wantErr { + t.Errorf("Write() error = %v, wantErr %v", err, tc.wantErr) + } + }) + } +} + +func TestRead(t *testing.T) { + type testStruct struct { + Test string + } + + testCases := []struct { + name string + datas *testStruct + filePath string + wantErr bool + }{ + {"success", &testStruct{}, "test.json", false}, + {"fail", &testStruct{}, "/no_exist_file.json", true}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := Read(tc.datas, tc.filePath) + if (err != nil) != tc.wantErr { + t.Errorf("Read() error = %v, wantErr %v", err, tc.wantErr) + } + }) + } +} + +func TestWriteResume(t *testing.T) { + type testStruct struct { + Test string + } + + testCases := []struct { + name string + datas testStruct + }{ + {"success", testStruct{"test"}}, + {"fail", testStruct{"test"}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := WriteResume(tc.datas) + if err != nil { + t.Errorf("WriteResume() error = %v", err) + } + }) + } +} + +func TestReadResume(t *testing.T) { + type testStruct struct { + Test string + } + + testCases := []struct { + name string + datas *testStruct + }{ + {"success", &testStruct{}}, + {"fail", &testStruct{}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := ReadResume(tc.datas) + if err != nil { + t.Errorf("ReadResume() error = %v", err) + } + }) + } +} + +func TestWriteOpt(t *testing.T) { + t.Run("Test with valid data", func(t *testing.T) { + data := struct { + Name string `json:"name,omitempty"` + }{"hhhhh"} + err := WriteOpt(data) + if err != nil { + t.Errorf("Expected nil, got %v", err) + } + }) +} + +func TestReadOpt(t *testing.T) { + + type TestOptions struct { + Name string `json:"name,omitempty"` + } + + t.Run("Test with valid data", func(t *testing.T) { + data := TestOptions{"hhhhh"} + err := WriteOpt(data) + if err != nil { + t.Errorf("Expected nil, got %v", err) + } + + fromConfig := &TestOptions{} + err = ReadOpt(fromConfig) + if err != nil { + t.Errorf("Expected nil, got %v", err) + } + }) +}