-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for
isValidResolvConf
(#10302)
Signed-off-by: Francisco <francisco.moral@suse.com> (cherry picked from commit 043b1ea) Signed-off-by: Francisco <francisco.moral@suse.com>
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |