Skip to content

Commit

Permalink
update tests for new changes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Xeckt committed Jun 21, 2024
1 parent 4b7a8aa commit 771f1f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
47 changes: 22 additions & 25 deletions sztp-agent/pkg/secureagent/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
32 changes: 17 additions & 15 deletions sztp-agent/pkg/secureagent/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down

0 comments on commit 771f1f6

Please sign in to comment.