Skip to content

Commit

Permalink
Add test for isValidResolvConf (#10302)
Browse files Browse the repository at this point in the history
 

Signed-off-by: Francisco <francisco.moral@suse.com>
  • Loading branch information
fmoral2 authored Jun 7, 2024
1 parent 88c20d8 commit 6d6cf86
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/agent/config/config_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"os"
"testing"
)

func Test_isValidResolvConf(t *testing.T) {
tests := []struct {
name string
fileContent string
expectedResult bool
}{
{name: "Valid ResolvConf", fileContent: "nameserver 8.8.8.8\nnameserver 2001:4860:4860::8888\n", expectedResult: true},
{name: "Invalid ResolvConf", fileContent: "nameserver 999.999.999.999\nnameserver not.an.ip\n", expectedResult: false},
{name: "Wrong Nameserver", fileContent: "search example.com\n", expectedResult: false},
{name: "One valid nameserver", fileContent: "test test.com\nnameserver 8.8.8.8", expectedResult: true},
{name: "Non GlobalUnicast", fileContent: "nameserver ::1\nnameserver 169.254.0.1\nnameserver fe80::1\n", expectedResult: false},
{name: "Empty File", fileContent: "", expectedResult: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "resolv.conf")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.WriteString(tt.fileContent); err != nil {
t.Errorf("error writing to file: %v with content: %v", tmpfile.Name(), tt.fileContent)
}

res := isValidResolvConf(tmpfile.Name())
if res != tt.expectedResult {
t.Errorf("isValidResolvConf(%s) = %v; want %v", tt.name, res, tt.expectedResult)
}
})
}
}

0 comments on commit 6d6cf86

Please sign in to comment.