Refactor loseup test
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
		| @@ -143,7 +143,7 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) error { | |||||||
| // | // | ||||||
| // Upon success, the file handle to the loop device is returned. | // Upon success, the file handle to the loop device is returned. | ||||||
| func setupLoop(backingFile string, param LoopParams) (string, error) { | func setupLoop(backingFile string, param LoopParams) (string, error) { | ||||||
| 	for retry := 1; retry < 200; retry++ { | 	for retry := 1; retry < 100; retry++ { | ||||||
| 		num, err := getFreeLoopDev() | 		num, err := getFreeLoopDev() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
|   | |||||||
| @@ -26,85 +26,89 @@ import ( | |||||||
| 	"github.com/containerd/continuity/testutil" | 	"github.com/containerd/continuity/testutil" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestSetupLoop(t *testing.T) { | var randomData = []byte("randomdata") | ||||||
| 	testutil.RequiresRoot(t) |  | ||||||
| 	const randomdata = "randomdata" |  | ||||||
|  |  | ||||||
| 	/* Non-existing loop */ | func createTempFile(t *testing.T) string { | ||||||
| 	backingFile := "setup-loop-test-no-such-file" | 	t.Helper() | ||||||
| 	_, err := setupLoop(backingFile, LoopParams{}) |  | ||||||
| 	if err == nil { |  | ||||||
| 		t.Fatalf("setupLoop with non-existing file should fail") |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	f, err := ioutil.TempFile("", "losetup") | 	f, err := ioutil.TempFile("", "losetup") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  | 	defer f.Close() | ||||||
|  |  | ||||||
| 	if err = f.Truncate(512); err != nil { | 	if err = f.Truncate(512); err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	backingFile = f.Name() |  | ||||||
| 	f.Close() | 	return f.Name() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestNonExistingLoop(t *testing.T) { | ||||||
|  | 	testutil.RequiresRoot(t) | ||||||
|  |  | ||||||
|  | 	backingFile := "setup-loop-test-no-such-file" | ||||||
|  | 	_, err := setupLoop(backingFile, LoopParams{}) | ||||||
|  | 	if err == nil { | ||||||
|  | 		t.Fatalf("setupLoop with non-existing file should fail") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestRoLoop(t *testing.T) { | ||||||
|  | 	testutil.RequiresRoot(t) | ||||||
|  |  | ||||||
|  | 	backingFile := createTempFile(t) | ||||||
| 	defer func() { | 	defer func() { | ||||||
| 		if err := os.Remove(backingFile); err != nil { | 		if err := os.Remove(backingFile); err != nil { | ||||||
| 			t.Fatal(err) | 			t.Fatal(err) | ||||||
| 		} | 		} | ||||||
| 	}() | 	}() | ||||||
|  |  | ||||||
| 	/* RO loop */ |  | ||||||
| 	path, err := setupLoop(backingFile, LoopParams{Readonly: true, Autoclear: true}) | 	path, err := setupLoop(backingFile, LoopParams{Readonly: true, Autoclear: true}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	ff, err := os.OpenFile(path, os.O_RDWR, 0) |  | ||||||
| 	if err != nil { | 	if err := ioutil.WriteFile(path, randomData, os.ModePerm); err == nil { | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	if _, err = ff.Write([]byte(randomdata)); err == nil { |  | ||||||
| 		t.Fatalf("writing to readonly loop device should fail") | 		t.Fatalf("writing to readonly loop device should fail") | ||||||
| 	} | 	} | ||||||
| 	if err = ff.Close(); err != nil { | } | ||||||
|  |  | ||||||
|  | func TestRwLoop(t *testing.T) { | ||||||
|  | 	testutil.RequiresRoot(t) | ||||||
|  |  | ||||||
|  | 	backingFile := createTempFile(t) | ||||||
|  | 	defer func() { | ||||||
|  | 		if err := os.Remove(backingFile); err != nil { | ||||||
|  | 			t.Fatal(err) | ||||||
|  | 		} | ||||||
|  | 	}() | ||||||
|  |  | ||||||
|  | 	path, err := setupLoop(backingFile, LoopParams{Autoclear: true}) | ||||||
|  | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/* RW loop */ | 	if err := ioutil.WriteFile(path, randomData, os.ModePerm); err != nil { | ||||||
| 	path, err = setupLoop(backingFile, LoopParams{Autoclear: true}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	ff, err = os.OpenFile(path, os.O_RDWR, 0) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	if _, err = ff.Write([]byte(randomdata)); err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	if err = ff.Close(); err != nil { |  | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestAttachDetachLoopDevice(t *testing.T) { | func TestAttachDetachLoopDevice(t *testing.T) { | ||||||
| 	testutil.RequiresRoot(t) | 	testutil.RequiresRoot(t) | ||||||
| 	f, err := ioutil.TempFile("", "losetup") |  | ||||||
| 	if err != nil { | 	path := createTempFile(t) | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	if err = f.Truncate(512); err != nil { |  | ||||||
| 		t.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	f.Close() |  | ||||||
| 	defer func() { | 	defer func() { | ||||||
| 		if err := os.Remove(f.Name()); err != nil { | 		if err := os.Remove(path); err != nil { | ||||||
| 			t.Fatal(err) | 			t.Fatal(err) | ||||||
| 		} | 		} | ||||||
| 	}() | 	}() | ||||||
|  |  | ||||||
| 	dev, err := AttachLoopDevice(f.Name()) | 	dev, err := AttachLoopDevice(path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if err = DetachLoopDevice(dev); err != nil { | 	if err = DetachLoopDevice(dev); err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Maksym Pavlenko
					Maksym Pavlenko