示例#1
0
文件: init_test.go 项目: na--/nedomi
func appFromExampleConfig(t *testing.T) (*Application, func()) {
	var path, err = utils.ProjectPath()
	if err != nil {
		t.Fatalf("Was not able to find the project dir: %s", err)
	}

	path1, cleanup1 := testutils.GetTestFolder(t)
	path2, cleanup2 := testutils.GetTestFolder(t)

	//!TODO: maybe construct an config ourselves
	// We are using the example config for this test. This might not be
	// so great an idea. But I tried to construct a config programatically
	// for about an hour and a half and I failed.
	var configGetter = configGetterForExampleConfig(path, path1, path2)
	app, err := New(types.AppVersion{}, configGetter)
	if err != nil {
		t.Fatalf("Error creating an app: %s", err)
	}

	if err := app.initFromConfig(); err != nil {
		t.Fatalf("Error initializing app: %s", err)
	}

	return app, func() {
		cleanup1()
		cleanup2()
	}
}
示例#2
0
文件: impl_test.go 项目: na--/nedomi
func TestConstructor(t *testing.T) {
	t.Parallel()
	workingDiskPath, cleanup := testutils.GetTestFolder(t)
	defer cleanup()

	cfg := &config.CacheZone{Path: workingDiskPath, PartSize: 10}
	l := mock.NewLogger()

	if _, err := New(nil, l); err == nil {
		t.Error("Expected to receive error with nil config")
	}
	if _, err := New(cfg, nil); err == nil {
		t.Error("Expected to receive error with nil logger")
	}

	if _, err := New(&config.CacheZone{Path: "/an/invalid/path", PartSize: 10}, l); err == nil {
		t.Error("Expected to receive error with an invalid path")
	}
	if _, err := New(&config.CacheZone{Path: "/", PartSize: 10}, l); err == nil {
		t.Error("Expected to receive error with root path")
	}
	if _, err := New(&config.CacheZone{Path: workingDiskPath, PartSize: 0}, l); err == nil {
		t.Error("Expected to receive error with invalid part size")
	}

	if _, err := New(cfg, l); err != nil {
		t.Errorf("Received unexpected error while creating a normal disk storage: %s", err)
	}
}
示例#3
0
文件: init_test.go 项目: na--/nedomi
func TestReinit(t *testing.T) {
	t.Parallel()

	app, cleanup := appFromExampleConfig(t)
	defer cleanup()
	cfg := *app.cfg
	path3, cleanup3 := testutils.GetTestFolder(t)
	defer cleanup3()
	cfg.CacheZones["zone3"] = &config.CacheZone{
		ID:             "zone3",
		Type:           "disk",
		Path:           path3,
		StorageObjects: 300,
		PartSize:       4096,
		Algorithm:      "lru",
	}
	replaceZone(&cfg, "zone2", cfg.CacheZones["zone3"])
	app.cfg = &cfg
	if err := app.reinitFromConfig(); err != nil {
		t.Fatalf("Error upon reiniting app: %s", err)
	}

	if _, ok := app.cacheZones["zone3"]; !ok {
		t.Error("No zone3 cache zone after reinit")
	}

	if _, ok := app.cacheZones["zone2"]; ok {
		t.Error("zone2 cache zone still present after reinit")
	}
}
示例#4
0
文件: env_test.go 项目: na--/nedomi
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))
}
示例#5
0
文件: env_test.go 项目: na--/nedomi
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))
}
示例#6
0
func newTestAppFromMap(t testing.TB, fsmap map[string]string) *testApp {
	up := mock.NewRequestHandler(fsMapHandler(fsmap))
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)
	loc := &types.Location{}
	var err error
	loc.Logger = newStdLogger()
	loc.CacheKey = "test"
	loc.CacheKeyIncludesQuery = false

	path, cleanup := testutils.GetTestFolder(t)

	cz := &config.CacheZone{
		ID:             "1",
		Type:           "disk",
		Path:           path,
		StorageObjects: 200,
		Algorithm:      "lru",
		PartSize:       5,
	}

	st, err := storage.New(cz, loc.Logger)
	if err != nil {
		panic(err)
	}
	ca, err := cache.New(cz, st.DiscardPart, loc.Logger)
	if err != nil {
		panic(err)
	}
	loc.Cache = &types.CacheZone{
		ID:        cz.ID,
		PartSize:  cz.PartSize,
		Algorithm: ca,
		Scheduler: storage.NewScheduler(),
		Storage:   st,
	}

	cacheHandler, err := New(nil, loc, up)
	if err != nil {
		t.Fatal(err)
	}
	app := &testApp{
		TB:           t,
		up:           up,
		ctx:          context.Background(),
		cacheHandler: cacheHandler,
		fsmap:        fsmap,
		cleanup:      cleanup,
	}
	return app
}
示例#7
0
文件: utils_test.go 项目: na--/nedomi
func getTestDiskStorage(t *testing.T, partSize int) (*Disk, string, func()) {
	diskPath, cleanup := testutils.GetTestFolder(t)

	d, err := New(&config.CacheZone{
		Path:     diskPath,
		PartSize: types.BytesSize(partSize),
	}, mock.NewLogger())

	if err != nil {
		t.Fatalf("Could not create storage: %s", err)
	}

	return d, diskPath, cleanup
}
示例#8
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())
	}
}
示例#9
0
文件: env_test.go 项目: na--/nedomi
func TestWithFakeUser(t *testing.T) {
	t.Parallel()

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

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

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

	if err == nil {
		t.Errorf("There was no error when user `%s` was used", notExistingUser)
	}

	if _, ok := err.(user.UnknownUserError); !ok {
		t.Errorf("The fake user's error was not UknownUserError. It was `%s`", err)
	}
}
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)
	}
}