-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'UT_Coverage' of https://github.com/dell/karavi-resiliency…
… into UT_Coverage
- Loading branch information
Showing
3 changed files
with
286 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,147 @@ | ||
// File: linuxLoopBackDevice_test.go | ||
//go:build test || linux | ||
// +build test linux | ||
|
||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Mock exec.Command for testing purposes | ||
var ( | ||
execCommandBackup = execCommand | ||
) | ||
|
||
func testHelperProcessSuccess(*testing.T) { | ||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
return | ||
} | ||
cmdArgs := strings.Join(os.Args[3:], " ") | ||
switch { | ||
case strings.Contains(cmdArgs, "/usr/sbin/losetup -a"): | ||
fmt.Fprint(os.Stdout, "/dev/loop1: 0 /dev/sda") | ||
case strings.Contains(cmdArgs, "grep"): | ||
textBytes, _ := ioutil.ReadAll(os.Stdin) | ||
if bytes.Contains(textBytes, []byte("mypv")) { | ||
fmt.Fprint(os.Stdout, "/dev/loop1") | ||
} | ||
case strings.Contains(cmdArgs, "/usr/sbin/losetup -d"): | ||
fmt.Fprint(os.Stdout, "deleted\n") | ||
default: | ||
fmt.Fprint(os.Stderr, "unexpected command") | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func testHelperProcessFailure(*testing.T) { | ||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
return | ||
} | ||
cmdArgs := strings.Join(os.Args[3:], " ") | ||
switch { | ||
case strings.Contains(cmdArgs, "/usr/sbin/losetup -a"): | ||
fmt.Fprint(os.Stdout, "") | ||
case strings.Contains(cmdArgs, "grep"): | ||
textBytes, _ := ioutil.ReadAll(os.Stdin) | ||
if bytes.Contains(textBytes, []byte("mypv")) { | ||
fmt.Fprint(os.Stderr, "error identifying loopback device") | ||
os.Exit(1) | ||
} | ||
case strings.Contains(cmdArgs, "/usr/sbin/losetup -d"): | ||
fmt.Fprint(os.Stderr, "failed to delete loopback device") | ||
os.Exit(1) | ||
default: | ||
fmt.Fprint(os.Stderr, "unexpected command") | ||
os.Exit(1) | ||
} | ||
os.Exit(1) | ||
} | ||
|
||
func TestHelperProcessSuccess(*testing.T) { | ||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
return | ||
} | ||
testHelperProcessSuccess(nil) | ||
} | ||
|
||
func TestHelperProcessFailure(*testing.T) { | ||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
return | ||
} | ||
testHelperProcessFailure(nil) | ||
} | ||
|
||
func TestGetLoopBackDevice_Success(t *testing.T) { | ||
execCommand = func(name string, arg ...string) *exec.Cmd { | ||
cs := []string{"-test.run=TestHelperProcessSuccess", "--", name} | ||
cs = append(cs, arg...) | ||
cmd := exec.Command(os.Args[0], cs...) | ||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | ||
return cmd | ||
} | ||
defer func() { execCommand = execCommandBackup }() | ||
|
||
_, err := GetLoopBackDevice("mypv") | ||
if err != nil { | ||
t.Fatalf("GetLoopBackDevice() failed unexpectedly: %v", err) | ||
} | ||
} | ||
|
||
func TestGetLoopBackDevice_Failure(t *testing.T) { | ||
execCommand = func(name string, arg ...string) *exec.Cmd { | ||
cs := []string{"-test.run=TestHelperProcessFailure", "--", name} | ||
cs = append(cs, arg...) | ||
cmd := exec.Command(os.Args[0], cs...) | ||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | ||
return cmd | ||
} | ||
defer func() { execCommand = execCommandBackup }() | ||
|
||
_, err := GetLoopBackDevice("mypv") | ||
if err == nil { | ||
t.Fatal("expected error, got nil") | ||
} | ||
} | ||
|
||
func TestDeleteLoopBackDevice_Success(t *testing.T) { | ||
execCommand = func(name string, arg ...string) *exec.Cmd { | ||
cs := []string{"-test.run=TestHelperProcessSuccess", "--", name} | ||
cs = append(cs, arg...) | ||
cmd := exec.Command(os.Args[0], cs...) | ||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | ||
return cmd | ||
} | ||
defer func() { execCommand = execCommandBackup }() | ||
|
||
out, err := DeleteLoopBackDevice("/dev/loop1") | ||
if err != nil { | ||
t.Fatalf("DeleteLoopBackDevice() failed unexpectedly: %v", err) | ||
} | ||
expectedOutput := "deleted\n" | ||
if string(out) != expectedOutput { | ||
t.Fatalf("expected %s, got %s", expectedOutput, string(out)) | ||
} | ||
} | ||
|
||
func TestDeleteLoopBackDevice_Failure(t *testing.T) { | ||
execCommand = func(name string, arg ...string) *exec.Cmd { | ||
cs := []string{"-test.run=TestHelperProcessFailure", "--", name} | ||
cs = append(cs, arg...) | ||
cmd := exec.Command(os.Args[0], cs...) | ||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | ||
return cmd | ||
} | ||
defer func() { execCommand = execCommandBackup }() | ||
|
||
_, err := DeleteLoopBackDevice("/dev/loop1") | ||
if err == nil { | ||
t.Fatal("expected error, got nil") | ||
} | ||
} |
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,70 @@ | ||
// File: linuxUnmount_test.go | ||
//go:build test || linux | ||
// +build test linux | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"testing" | ||
) | ||
|
||
// setupTestFile creates a test file and returns its name and a cleanup function | ||
func setupTestFile(t *testing.T) (string, func()) { | ||
t.Helper() // Marks this function as a test helper | ||
|
||
tempFile, err := os.CreateTemp("", "testdevice") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp file: %v", err) | ||
} | ||
tempFileName := tempFile.Name() | ||
tempFile.Close() | ||
|
||
// Cleanup function to remove the test file | ||
cleanup := func() { | ||
os.Remove(tempFileName) | ||
} | ||
|
||
return tempFileName, cleanup | ||
} | ||
|
||
// TestUnmount verifies Unmount wrapper | ||
func TestUnmount(t *testing.T) { | ||
tempFile, cleanup := setupTestFile(t) | ||
defer cleanup() | ||
|
||
// Attempt to unmount the temporary file | ||
err := Unmount(tempFile, 0) | ||
if err != nil { | ||
t.Log("Unmount failed with a known invalid argument for simply using a file as device-like") | ||
} else { | ||
t.Fatalf("Unmount expected to fail on regular file. Possible invalid test argument.") | ||
} | ||
} | ||
|
||
// TestCreat verifies Creat wrapper | ||
func TestCreat(t *testing.T) { | ||
tempDir := os.TempDir() | ||
testFilePath := tempDir + "/testcreatefile.txt" | ||
|
||
// Ensure the file does not already exist | ||
os.Remove(testFilePath) | ||
defer os.Remove(testFilePath) | ||
|
||
// Use syscall.Creat as per syscall requirement | ||
fd, err := Creat(testFilePath, 0) | ||
if err != nil { | ||
t.Fatalf("Creat() failed: %v", err) | ||
} | ||
defer syscall.Close(fd) // Clean up file descriptor after the test | ||
|
||
if fd < 0 { | ||
t.Fatalf("Creat() returned invalid file descriptor: %d", fd) | ||
} | ||
|
||
// Verify that the file has been created | ||
if _, err := os.Stat(testFilePath); os.IsNotExist(err) { | ||
t.Fatalf("File %s was not created", testFilePath) | ||
} | ||
} |
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,69 @@ | ||
//go:build test || windows | ||
// +build test windows | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func setupTempFile() (string, error) { | ||
// Create a temporary file to simulate a device | ||
tempFile, err := os.CreateTemp("", "testdevice") | ||
if err != nil { | ||
return "", err | ||
} | ||
tempFileName := tempFile.Name() | ||
tempFile.Close() | ||
return tempFileName, nil | ||
} | ||
|
||
func teardownTempFile(fileName string) { | ||
if _, err := os.Stat(fileName); !os.IsNotExist(err) { | ||
// Clean up by removing the file | ||
os.Remove(fileName) | ||
} | ||
} | ||
|
||
// Ensure the test runs only on Windows (or with the test build tag) | ||
func TestUnmount(t *testing.T) { | ||
tempFileName, err := setupTempFile() | ||
if err != nil { | ||
t.Fatalf("Setup failed: %v", err) | ||
} | ||
defer teardownTempFile(tempFileName) | ||
|
||
// Attempt to unmount the simulated device (temporary file) | ||
if err := Unmount(tempFileName, 0); err != nil { | ||
t.Fatalf("Unmount() failed: %v", err) | ||
} | ||
|
||
// Verify that the file has been removed | ||
if _, err := os.Stat(tempFileName); !os.IsNotExist(err) { | ||
t.Fatalf("File %s was not removed", tempFileName) | ||
} | ||
} | ||
|
||
// Ensure the test runs only on Windows (or with the test build tag) | ||
func TestCreat(t *testing.T) { | ||
tempDir := os.TempDir() | ||
testFilePath := tempDir + "/testcreatefile.txt" | ||
|
||
// Ensure the file does not already exist | ||
os.Remove(testFilePath) | ||
defer os.Remove(testFilePath) | ||
|
||
// Attempt to create the file | ||
fd, err := Creat(testFilePath, 0) | ||
if err != nil { | ||
t.Fatalf("Creat() failed: %v", err) | ||
} else if fd < 0 { | ||
t.Fatalf("Creat() returned invalid file descriptor: %d", fd) | ||
} | ||
|
||
// Verify that the file has been created | ||
if _, err := os.Stat(testFilePath); os.IsNotExist(err) { | ||
t.Fatalf("File %s was not created", testFilePath) | ||
} | ||
} |