Exemplo n.º 1
0
func TestCleaningUpErrors(t *testing.T) {
	t.Parallel()

	targetPidFile := filepath.FromSlash("/this/place/does/not/exists")

	cfg := getCfg(config.System{Pidfile: targetPidFile})

	if err := CleanupEnv(cfg); err == nil {
		t.Errorf("There was not an error for missing pidfile")
	}

	wrongPidFile, err := ioutil.TempFile("", "wrong_pid_in_file_test")

	if err != nil {
		t.Fatalf("Failed to create a temporray file: %s", err)
	}

	defer func() {
		testutils.ShouldntFail(t, os.Remove(wrongPidFile.Name()))
	}()

	fmt.Fprintf(wrongPidFile, "%d", os.Getpid()+1)
	testutils.ShouldntFail(t, wrongPidFile.Close())

	cfg.System = config.System{
		Pidfile: wrongPidFile.Name(),
	}

	if err := CleanupEnv(cfg); err == nil {
		t.Error("There was not an error with pidfile with different process id")
	}

}
Exemplo n.º 2
0
func TestSkipReaderCloseWithPipe(t *testing.T) {
	t.Parallel()
	var input = []byte{'a', 'b', 'c', 'd'}
	var output = []byte{'b', 'c', 'd'}
	r, w := io.Pipe()
	src, err := SkipReadCloser(r, 1)
	if err != nil {
		t.Fatal("unexpected error", err)
	}
	go func() {
		if _, err := w.Write(input); err != nil {
			t.Fatalf("Unexpected Write error: %s", err)
		}
		testutils.ShouldntFail(t, w.Close())
	}()
	defer func() {
		testutils.ShouldntFail(t, src.Close())
	}()

	var result bytes.Buffer
	if _, err := result.ReadFrom(iotest.OneByteReader(src)); err != nil {
		t.Fatalf("Unexpected ReadFrom error: %s", err)
	}
	if result.String() != string(output) {
		t.Fatalf("Expected to skipread [%s] read [%s]", output, result.String())
	}
}
Exemplo n.º 3
0
func TestChangingTheUserWihtNobody(t *testing.T) {
	t.Parallel()

	//!TODO: find out if this test is possible at all.
	// If not, delete it from here.
	t.Skip("Setting tye UID and GID is not supported for some reason")

	nobody, err := user.Lookup("nobody")

	if err != nil {
		if _, ok := err.(user.UnknownUserError); ok {
			t.Skip("This system does not have the nobody user." +
				" Skipping the test since it requires it")
		} else {
			t.Errorf("Error getting the nobody user: %s", err)
		}
	}

	tempDir, cleanup := testutils.GetTestFolder(t)
	defer cleanup()

	targetPidFile := filepath.Join(tempDir, "pidfile")

	cfg := getCfg(config.System{
		User:    nobody.Name,
		Pidfile: targetPidFile,
	})

	err = SetupEnv(cfg)
	if err != nil {
		t.Errorf("There was an error when setting gid and uit to %s's. %s",
			nobody.Name, err)
	}

	currentEuid := os.Geteuid()
	uidOfNobody, err := strconv.Atoi(nobody.Uid)

	if err != nil {
		t.Errorf("Error converting UID [%s] to int: %s", nobody.Uid, err)
	}

	if uidOfNobody != currentEuid {
		t.Errorf("The current user id was not set to nobody's. "+
			"Expected %d but it was %d",
			uidOfNobody, currentEuid)
	}

	currentEgid := os.Getegid()
	gidOfNobody, err := strconv.Atoi(nobody.Gid)

	if err != nil {
		t.Errorf("Error converting GID [%s] to int: %s", nobody.Gid, err)
	}

	if gidOfNobody != currentEgid {
		t.Errorf("The current group id was not set to nobody's. "+
			"Expected %d but it was %d", gidOfNobody, currentEgid)
	}
	testutils.ShouldntFail(t, os.Remove(targetPidFile))
}
Exemplo n.º 4
0
func TestMockStorageOperations(t *testing.T) {
	t.Parallel()
	s := NewStorage(10)

	saveMetadata(t, s, obj1)
	saveMetadata(t, s, obj2)

	idx := &types.ObjectIndex{ObjID: obj2.ID, Part: 13}
	savePart(t, s, idx, "loremipsum2")

	passed := false
	testutils.ShouldntFail(t, s.Iterate(func(obj *types.ObjectMetadata, parts ...*types.ObjectIndex) bool {
		if passed {
			t.Fatal("Expected iteration to stop after the first result")
		}
		passed = true
		return false
	}))
	testutils.ShouldntFail(t, s.Discard(obj1.ID))
	if len(s.Objects) != 1 {
		t.Errorf("Expected only 1 remaining object but there are %d", len(s.Objects))
	}

	testutils.ShouldntFail(t, s.Iterate(func(obj *types.ObjectMetadata, parts ...*types.ObjectIndex) bool {
		if obj != obj2 {
			t.Error("Expected to receive obj2's pointer")
		}
		for _, part := range parts {
			if part.Part == idx.Part {
				return false
			}
		}
		t.Errorf("Expected part %s to be present", idx)

		return false
	}))

	testutils.ShouldntFail(t, s.DiscardPart(idx), s.Discard(obj2.ID))

	testutils.ShouldntFail(t, s.Iterate(func(obj *types.ObjectMetadata, parts ...*types.ObjectIndex) bool {
		t.Error("Expected never to be called")
		return false
	}))
}
Exemplo n.º 5
0
func TestProperEnvironmentCreation(t *testing.T) {
	t.Parallel()
	tempDir, cleanup := testutils.GetTestFolder(t)
	defer cleanup()

	tempFile := filepath.Join(tempDir, "test_pid_file.pid")
	currentUser, err := user.Current()

	if err != nil {
		t.Fatal("Was not able to find the current user")
	}

	cfg := getCfg(config.System{
		User:    currentUser.Username,
		Workdir: tempDir,
		Pidfile: tempFile,
	})

	if err := SetupEnv(cfg); err != nil {
		t.Fatalf("Error on creating environment. %s", err)
	}

	wd, err := os.Getwd()

	if err != nil {
		t.Errorf("Error getting current directory. %s", err)
	}

	if tempDir != wd {
		t.Errorf("SetupEnv did not change the current directory. %s", err)
	}

	pidfh, err := os.Open(tempFile)
	if err != nil {
		t.Fatalf("Was not able to open the created pid file. %s", err)
	}

	scanner := bufio.NewScanner(pidfh)
	if !scanner.Scan() {
		t.Fatal("Pidfile was empty.")
	}

	pidInFile, err := strconv.Atoi(strings.Trim(scanner.Text(), "\n"))
	if err != nil {
		t.Fatalf("Was not able to convert pid to int from the pidfile. %s", err)
	}

	progPid := os.Getpid()
	if pidInFile != progPid {
		t.Error("Pidfile in the file was different than the one expected")
	}

	testutils.ShouldntFail(t, os.Remove(tempFile))
}
Exemplo n.º 6
0
func storageWithObjects(t *testing.T, objs ...*types.ObjectID) types.Storage {
	var st = mock.NewStorage(10)
	for _, obj := range objs {
		testutils.ShouldntFail(t,
			st.SaveMetadata(&types.ObjectMetadata{ID: obj}),
			st.SavePart(
				&types.ObjectIndex{ObjID: obj, Part: 2},
				bytes.NewReader([]byte("test bytes"))),
			st.SavePart(
				&types.ObjectIndex{ObjID: obj, Part: 4},
				bytes.NewReader([]byte("more bytes"))),
		)
	}
	return st
}
Exemplo n.º 7
0
func TestSkipReaderClose(t *testing.T) {
	t.Parallel()
	hw := ioutil.NopCloser(bytes.NewBufferString("Hello, World!"))
	src := SkipReadCloser(hw, 5)
	defer func() {
		testutils.ShouldntFail(t, src.Close())
	}()
	var result bytes.Buffer
	if _, err := result.ReadFrom(src); err != nil {
		t.Fatalf("Unexpected ReadFrom error: %s", err)
	}
	expected := ", World!"
	if result.String() != expected {
		t.Fatalf("Expected to skipread [%s] read [%s]", expected, result.String())
	}
}
Exemplo n.º 8
0
func TestWhenPidFileCreationFails(t *testing.T) {
	t.Parallel()

	targetPidFile := filepath.FromSlash("/this/place/does/not/exists")

	cfg := getCfg(config.System{Pidfile: targetPidFile})
	err := SetupEnv(cfg)

	if err == nil {
		t.Errorf("There was no error with pidfile `%s`", targetPidFile)

		// Remove the file in the off chance it has been created
		// for some reason
		testutils.ShouldntFail(t, os.Remove(targetPidFile))
	}

	if pathErr, ok := err.(*os.PathError); !ok || pathErr.Op != "open" {
		t.Errorf("Error was for creating the file. Not for writing in it: `%s`", err)
	}
}
Exemplo n.º 9
0
func TestIterationErrors(t *testing.T) {
	t.Parallel()
	d, _, cleanup := getTestDiskStorage(t, 10)
	defer cleanup()

	callback, _ := getCallback(t, iterResMap{})

	saveMetadata(t, d, obj1)
	saveMetadata(t, d, obj3)

	testutils.ShouldntFail(t,
		os.Rename(d.getObjectMetadataPath(obj1.ID), d.getObjectMetadataPath(obj3.ID)),
		d.Iterate(callback),
		ioutil.WriteFile(d.getObjectMetadataPath(obj3.ID), []byte("wrong json!"), d.filePermissions),
		d.Iterate(callback),
		os.RemoveAll(d.getObjectIDPath(obj3.ID)),
		d.Iterate(callback),
		os.RemoveAll(d.getObjectIDPath(obj1.ID)),
		d.Iterate(callback),
	)
}
Exemplo n.º 10
0
func TestFileExistsFunction(t *testing.T) {
	t.Parallel()
	tmpDir, cleanup := testutils.GetTestFolder(t)
	defer cleanup()

	if exists := FileExists(tmpDir); exists {
		t.Errorf("Expected false when calling FileExists with directory: %s", tmpDir)
	}

	tmpFile, err := ioutil.TempFile(tmpDir, "functest")

	if err != nil {
		t.Fatalf("Creating a temporary file filed: %s", err)
	}

	defer func() {
		testutils.ShouldntFail(t, tmpFile.Close(), os.Remove(tmpFile.Name()))
	}()

	if exists := FileExists(tmpFile.Name()); !exists {
		t.Errorf("Expected true when calling FileEists with a file %s", tmpFile.Name())
	}
}
Exemplo n.º 11
0
func TestCleaningUpSuccesful(t *testing.T) {
	t.Parallel()
	testPidFile, err := ioutil.TempFile("", "pidfile")

	if err != nil {
		t.Fatalf("Failed to create a temporray file: %s", err)
	}

	defer func() {
		if err := os.Remove(testPidFile.Name()); err == nil {
			t.Fatalf("Expected error but received none")
		}
	}()

	fmt.Fprintf(testPidFile, "%d", os.Getpid())
	testutils.ShouldntFail(t, testPidFile.Close())

	cfg := getCfg(config.System{Pidfile: testPidFile.Name()})

	if err := CleanupEnv(cfg); err != nil {
		t.Error("Error cleaning up the pidfile")
	}
}
func TestDiskReload(t *testing.T) {
	t.Parallel()
	tempDir, cleanup := testutils.GetTestFolder(t)
	defer cleanup()

	app, err := New(types.AppVersion{}, getConfigGetter(tempDir))
	if err != nil {
		t.Fatalf("Could not create an application: %s", err)
	}

	stor, err := disk.New(app.cfg.CacheZones["default"], mock.NewLogger())
	if err != nil {
		t.Fatalf("Could not initialize a storage: %s", err)
	}

	objIDNew := types.NewObjectID("key", "new")
	objIDOld := types.NewObjectID("key", "old")
	testutils.ShouldntFail(t,
		stor.SaveMetadata(&types.ObjectMetadata{ID: objIDNew, ExpiresAt: time.Now().Unix() + 600}),
		stor.SaveMetadata(&types.ObjectMetadata{ID: objIDOld, ExpiresAt: time.Now().Unix() - 600}),
		stor.SavePart(&types.ObjectIndex{ObjID: objIDNew, Part: 0}, strings.NewReader("test1-1")),
		stor.SavePart(&types.ObjectIndex{ObjID: objIDNew, Part: 1}, strings.NewReader("test1-2")),
		stor.SavePart(&types.ObjectIndex{ObjID: objIDOld, Part: 0}, strings.NewReader("test2-1")),
	)

	if err := app.initFromConfig(); err != nil {
		t.Fatalf("Could not init from config: %s", err)
	}
	defer app.ctxCancel()
	time.Sleep(1 * time.Second)

	const expectedObjects = 2
	cacheObjects := app.cacheZones["default"].Algorithm.Stats().Objects()
	if cacheObjects != expectedObjects {
		t.Errorf("Expected object count in cache to be %d but it was %d", expectedObjects, cacheObjects)
	}
}