From 771f1f6704985613bc0d0ad7e3b49238c13e45b9 Mon Sep 17 00:00:00 2001 From: Xeckt Date: Fri, 21 Jun 2024 15:28:31 +0100 Subject: [PATCH] update tests for new changes Moved new dhcptestcontent from the createtempfile func to a constant on the daemon_tests.go so that createtempfile has a clearer usage with less code --- sztp-agent/pkg/secureagent/daemon_test.go | 47 +++++++++++------------ sztp-agent/pkg/secureagent/utils_test.go | 32 +++++++-------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/sztp-agent/pkg/secureagent/daemon_test.go b/sztp-agent/pkg/secureagent/daemon_test.go index 1efd3144..d76db7d3 100644 --- a/sztp-agent/pkg/secureagent/daemon_test.go +++ b/sztp-agent/pkg/secureagent/daemon_test.go @@ -14,10 +14,26 @@ import ( "testing" ) +const DHCPTestContent = `lease { + interface "eth0"; + fixed-address 10.127.127.100; + filename "grubx64.efi"; + option subnet-mask 255.255.255.0; + option sztp-redirect-urls "http://mymock/test"; + option dhcp-lease-time 600; + option tftp-server-name "w.x.y.z"; + option bootfile-name "test.cfg"; + option dhcp-message-type 5; + option dhcp-server-identifier 10.127.127.2; + renew 1 2022/08/15 19:16:40; + rebind 1 2022/08/15 19:20:50; + expire 1 2022/08/15 19:22:05; +}` + //nolint:funlen func TestAgent_getBootstrapURL(t *testing.T) { dhcpTestFileOK := "/tmp/test.dhcp" - createTempTestFile(dhcpTestFileOK, "", true) + createTempTestFile(dhcpTestFileOK, DHCPTestContent, true) type fields struct { BootstrapURL string @@ -92,31 +108,12 @@ func createTempTestFile(file string, content string, _ bool) { // nolint:gosec f, err := os.Create(file) if err != nil { - log.Fatal(err) + log.Println(err) } - mydhcpresponse := `lease { - interface "eth0"; - fixed-address 10.127.127.100; - filename "grubx64.efi"; - option subnet-mask 255.255.255.0; - option sztp-redirect-urls "http://mymock/test"; - option dhcp-lease-time 600; - option tftp-server-name "w.x.y.z"; - option bootfile-name "test.cfg"; - option dhcp-message-type 5; - option dhcp-server-identifier 10.127.127.2; - renew 1 2022/08/15 19:16:40; - rebind 1 2022/08/15 19:20:50; - expire 1 2022/08/15 19:22:05; -}` - - if content != "" { - mydhcpresponse = content - } - _, err2 := f.WriteString(mydhcpresponse) - - if err2 != nil { - log.Fatal(err2) + defer f.Close() + _, err = f.WriteString(content) + if err != nil { + log.Println(err) } } diff --git a/sztp-agent/pkg/secureagent/utils_test.go b/sztp-agent/pkg/secureagent/utils_test.go index 64cc3852..d8d3ad04 100644 --- a/sztp-agent/pkg/secureagent/utils_test.go +++ b/sztp-agent/pkg/secureagent/utils_test.go @@ -22,13 +22,11 @@ func TestAgent_doTLSRequest(t *testing.T) { InputJSONContent string DhcpLeaseFile string } - tests := []struct { + var tests []struct { name string fields fields want *BootstrapServerPostOutput wantErr bool - }{ - // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -96,7 +94,7 @@ func Test_extractfromLine(t *testing.T) { func Test_linesInFileContains(t *testing.T) { dhcpTestFileOK := "/tmp/test.dhcp" - createTempTestFile(dhcpTestFileOK, "", true) + createTempTestFile(dhcpTestFileOK, DHCPTestContent, true) type args struct { file string substr string @@ -127,8 +125,9 @@ func Test_linesInFileContains(t *testing.T) { func Test_readSSHHostKeyPublicFiles(t *testing.T) { type args struct { - file string - line string + file string + line string + keyType string } tests := []struct { name string @@ -138,26 +137,29 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) { { name: "Test OK line in files no comment", args: args{ - file: "/tmp/test.pub", - line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", + file: "/tmp/test.pub", + line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", + keyType: "ssh-ed25519", }, - want: []publicKey{{Algorithm: "ssh-ed25519", KeyData: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, }, { name: "Test OK line in files with comment", args: args{ - file: "/tmp/test.pub", - line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment", + file: "/tmp/test.pub", + line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment", + keyType: "ssh-ed25519", }, - want: []publicKey{{Algorithm: "ssh-ed25519", KeyData: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, }, { name: "Test too many parts in file", args: args{ - file: "/tmp/test.pub", - line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment error", + file: "/tmp/test.pub", + line: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment error", + keyType: "ssh-ed25519", }, - want: []publicKey{{Algorithm: "ssh-ed25519", KeyData: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, }, { name: "Test not enough parts in file",