Пример #1
0
func (s *MyAPIFSCacheSuite) SetUpSuite(c *C) {
	root, e := ioutil.TempDir(os.TempDir(), "api-")
	c.Assert(e, IsNil)
	s.root = root

	fsroot, e := ioutil.TempDir(os.TempDir(), "api-")
	c.Assert(e, IsNil)

	accessKeyID, err := generateAccessKeyID()
	c.Assert(err, IsNil)
	secretAccessKey, err := generateSecretAccessKey()
	c.Assert(err, IsNil)

	conf := newConfigV2()
	conf.Credentials.AccessKeyID = string(accessKeyID)
	conf.Credentials.SecretAccessKey = string(secretAccessKey)
	s.accessKeyID = string(accessKeyID)
	s.secretAccessKey = string(secretAccessKey)

	// do this only once here
	setGlobalConfigPath(root)

	c.Assert(saveConfig(conf), IsNil)

	cloudServer := cloudServerConfig{
		Address:         ":" + strconv.Itoa(getFreePort()),
		Path:            fsroot,
		MinFreeDisk:     0,
		AccessKeyID:     s.accessKeyID,
		SecretAccessKey: s.secretAccessKey,
		Region:          "us-east-1",
	}
	httpHandler := serverHandler(cloudServer)
	testAPIFSCacheServer = httptest.NewServer(httpHandler)
}
Пример #2
0
func TestExists(t *testing.T) {
	zeroSizedFile, _ := createZeroSizedFileInTempDir()
	defer deleteFileInTempDir(zeroSizedFile)
	nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir()
	defer deleteFileInTempDir(nonZeroSizedFile)
	emptyDirectory, _ := createEmptyTempDir()
	defer deleteTempDir(emptyDirectory)
	nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt"
	nonExistentDir := os.TempDir() + "/this/direcotry/does/not/exist/"

	type test struct {
		input          string
		expectedResult bool
		expectedErr    error
	}

	data := []test{
		{zeroSizedFile.Name(), true, nil},
		{nonZeroSizedFile.Name(), true, nil},
		{emptyDirectory, true, nil},
		{nonExistentFile, false, nil},
		{nonExistentDir, false, nil},
	}
	for i, d := range data {
		exists, err := Exists(d.input, new(afero.OsFs))
		if d.expectedResult != exists {
			t.Errorf("Test %d failed. Expected result %t got %t", i, d.expectedResult, exists)
		}
		if d.expectedErr != err {
			t.Errorf("Test %d failed. Expected %q got %q", i, d.expectedErr, err)
		}
	}

}
Пример #3
0
func (s *MyAPIFSCacheSuite) SetUpSuite(c *C) {
	root, err := ioutil.TempDir(os.TempDir(), "api-")
	c.Assert(err, IsNil)
	s.root = root

	fsroot, err := ioutil.TempDir(os.TempDir(), "api-")
	c.Assert(err, IsNil)

	fs.SetFSMultipartsConfigPath(filepath.Join(root, "multiparts.json"))
	multiparts := &fs.Multiparts{}
	multiparts.ActiveSession = make(map[string]*fs.MultipartSession)
	perr := fs.SaveMultipartsSession(multiparts)
	c.Assert(perr, IsNil)

	accessKeyID, perr := generateAccessKeyID()
	c.Assert(perr, IsNil)
	secretAccessKey, perr := generateSecretAccessKey()
	c.Assert(perr, IsNil)

	authConf := &AuthConfig{}
	authConf.AccessKeyID = string(accessKeyID)
	authConf.SecretAccessKey = string(secretAccessKey)
	s.accessKeyID = string(accessKeyID)
	s.secretAccessKey = string(secretAccessKey)

	// do this only once here
	customConfigPath = root

	perr = saveAuthConfig(authConf)
	c.Assert(perr, IsNil)

	minioAPI := getNewAPI(fsroot, false)
	httpHandler := getAPIHandler(false, minioAPI)
	testAPIFSCacheServer = httptest.NewServer(httpHandler)
}
Пример #4
0
func testConfig() *config.Config {
	conf := config.DefaultConfig()
	conf.StateDir = os.TempDir()
	conf.AllocDir = os.TempDir()
	conf.MaxKillTimeout = 10 * time.Second
	return conf
}
Пример #5
0
func TestCreateTimestampFile(t *testing.T) {
	realDir := filepath.Join(os.TempDir(), "util_test")
	util.Mkdir(realDir, 0755)
	defer util.RemoveAll(realDir)
	timestampFilePath := filepath.Join(realDir, TIMESTAMP_FILE_NAME)
	if err := CreateTimestampFile(realDir); err != nil {
		t.Errorf("Unexpected error: %s", err)
	}
	// Assert timestamp file exists.
	if _, err := os.Stat(timestampFilePath); err != nil {
		t.Errorf("Timestamp file %s was not created!", timestampFilePath)
	}
	// Assert timestamp file contains int64.
	fileContent, err := ioutil.ReadFile(timestampFilePath)
	if err != nil {
		t.Errorf("Unexpected error: %s", err)
	}
	if _, err := strconv.ParseInt(string(fileContent), 10, 64); err != nil {
		t.Errorf("Unexpected value in %s: %s", timestampFilePath, err)
	}

	// Assert error returned when specified dir does not exist.
	nonexistantDir := filepath.Join(os.TempDir(), "util_test_nonexistant")
	util.RemoveAll(nonexistantDir)
	if err := CreateTimestampFile(nonexistantDir); err != nil {
		// Expected error
	} else {
		t.Error("Unexpected lack of error")
	}
}
Пример #6
0
func TestPackTo(t *testing.T) {
	Convey("Pack a dir or file to tar.gz file", t, func() {
		Convey("Pack a dir that does exist and includir root dir", func() {
			So(PackTo("testdata/testdir",
				path.Join(os.TempDir(), "testdata/testdir1.tar.gz"), true), ShouldBeNil)
		})

		Convey("Pack a dir that does exist and does not includir root dir", func() {
			So(PackTo("testdata/testdir",
				path.Join(os.TempDir(), "testdata/testdir2.tar.gz")), ShouldBeNil)
		})

		Convey("Pack a dir that does not exist and does not includir root dir", func() {
			So(PackTo("testdata/testdir404",
				path.Join(os.TempDir(), "testdata/testdir3.tar.gz")), ShouldNotBeNil)
		})

		Convey("Pack a file that does exist", func() {
			So(PackTo("testdata/README.txt",
				path.Join(os.TempDir(), "testdata/testdir4.tar.gz")), ShouldBeNil)
		})

		Convey("Pack a file that does not exist", func() {
			So(PackTo("testdata/README404.txt",
				path.Join(os.TempDir(), "testdata/testdir5.tar.gz")), ShouldNotBeNil)
		})
	})
}
Пример #7
0
func (s *TestSuite) TestDiffObjects(c *C) {
	/// filesystem
	root1, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(root1)

	root2, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(root2)

	objectPath1 := filepath.Join(root1, "object1")
	data := "hello"
	dataLen := len(data)
	perr := putTarget(objectPath1, int64(dataLen), bytes.NewReader([]byte(data)))
	c.Assert(perr, IsNil)

	objectPath2 := filepath.Join(root2, "object1")
	data = "hello"
	dataLen = len(data)
	perr = putTarget(objectPath2, int64(dataLen), bytes.NewReader([]byte(data)))
	c.Assert(perr, IsNil)

	for diff := range doDiffMain(objectPath1, objectPath2, false) {
		c.Assert(diff.Error, IsNil)
	}
}
Пример #8
0
func TestLuaScriptExecuteWithAggregator(t *testing.T) {
	task := pendingLuaScriptTaskWithAggregator()
	mockRun := exec.CommandCollector{}
	exec.SetRunForTesting(mockRun.Run)
	defer exec.SetRunForTesting(exec.DefaultRun)
	mockRun.SetDelegateRun(func(cmd *exec.Command) error {
		runId := getRunId(t, cmd)
		assertFileContents(t, filepath.Join(os.TempDir(), runId+".lua"),
			`print("lualualua")`)
		assertFileContents(t, filepath.Join(os.TempDir(), runId+".aggregator"),
			`print("aaallluuu")`)
		return nil
	})
	err := task.Execute()
	assert.NoError(t, err)
	assert.Len(t, mockRun.Commands(), 1)
	cmd := mockRun.Commands()[0]
	expect.Equal(t, "run_lua_on_workers", cmd.Name)
	expect.Contains(t, cmd.Args, "--gae_task_id=42")
	expect.Contains(t, cmd.Args, "--description=description")
	expect.Contains(t, cmd.Args, "[email protected]")
	expect.Contains(t, cmd.Args, "--pageset_type=All")
	expect.Contains(t, cmd.Args, "--chromium_build=c14d891-586101c")
	runId := getRunId(t, cmd)
	expect.Contains(t, cmd.Args, "--log_id="+runId)
	expect.NotNil(t, cmd.Timeout)
}
Пример #9
0
func TestChromiumPerfExecute(t *testing.T) {
	task := pendingChromiumPerfTask()
	mockRun := exec.CommandCollector{}
	exec.SetRunForTesting(mockRun.Run)
	defer exec.SetRunForTesting(exec.DefaultRun)
	mockRun.SetDelegateRun(func(cmd *exec.Command) error {
		runId := getRunId(t, cmd)
		assertFileContents(t, filepath.Join(os.TempDir(), runId+".chromium.patch"),
			"chromiumpatch\n")
		assertFileContents(t, filepath.Join(os.TempDir(), runId+".skia.patch"),
			"skiapatch\n")
		return nil
	})
	err := task.Execute()
	assert.NoError(t, err)
	assert.Len(t, mockRun.Commands(), 1)
	cmd := mockRun.Commands()[0]
	expect.Equal(t, "run_chromium_perf_on_workers", cmd.Name)
	expect.Contains(t, cmd.Args, "--gae_task_id=42")
	expect.Contains(t, cmd.Args, "--description=description")
	expect.Contains(t, cmd.Args, "[email protected]")
	expect.Contains(t, cmd.Args, "--benchmark_name=benchmark")
	expect.Contains(t, cmd.Args, "--target_platform=Linux")
	expect.Contains(t, cmd.Args, "--pageset_type=All")
	expect.Contains(t, cmd.Args, "--repeat_benchmark=1")
	expect.Contains(t, cmd.Args, "--benchmark_extra_args=benchmarkargs")
	expect.Contains(t, cmd.Args, "--browser_extra_args_nopatch=banp")
	expect.Contains(t, cmd.Args, "--browser_extra_args_withpatch=bawp")
	runId := getRunId(t, cmd)
	expect.Contains(t, cmd.Args, "--log_id="+runId)
	expect.NotNil(t, cmd.Timeout)
}
Пример #10
0
func (s *CmdTestSuite) TestCpTypeB(c *C) {
	/// filesystem
	source, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(source)

	sourcePath := filepath.Join(source, "object1")
	data := "hello"
	dataLen := len(data)
	err = putTarget(sourcePath, int64(dataLen), bytes.NewReader([]byte(data)))
	c.Assert(err, IsNil)

	target, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(target)

	cps, err := newSession()
	c.Assert(err, IsNil)

	cps.URLs = append(cps.URLs, sourcePath)
	cps.URLs = append(cps.URLs, target)
	for err := range doCopyCmdSession(barCp, cps) {
		c.Assert(err, IsNil)
	}

	cps, err = newSession()
	c.Assert(err, IsNil)

	targetURL := server.URL + "/bucket"
	cps.URLs = append(cps.URLs, sourcePath)
	cps.URLs = append(cps.URLs, targetURL)
	for err := range doCopyCmdSession(barCp, cps) {
		c.Assert(err, IsNil)
	}
}
Пример #11
0
func sign(xml string, privateKeyPath string, id string) (string, error) {

	samlXmlsecInput, err := ioutil.TempFile(os.TempDir(), "tmpgs")
	if err != nil {
		return "", err
	}
	defer deleteTempFile(samlXmlsecInput.Name())
	samlXmlsecInput.WriteString("<?xml version='1.0' encoding='UTF-8'?>\n")
	samlXmlsecInput.WriteString(xml)
	samlXmlsecInput.Close()

	samlXmlsecOutput, err := ioutil.TempFile(os.TempDir(), "tmpgs")
	if err != nil {
		return "", err
	}
	defer deleteTempFile(samlXmlsecOutput.Name())
	samlXmlsecOutput.Close()

	// fmt.Println("xmlsec1", "--sign", "--privkey-pem", privateKeyPath,
	// 	"--id-attr:ID", id,
	// 	"--output", samlXmlsecOutput.Name(), samlXmlsecInput.Name())
	output, err := exec.Command("xmlsec1", "--sign", "--privkey-pem", privateKeyPath,
		"--id-attr:ID", id,
		"--output", samlXmlsecOutput.Name(), samlXmlsecInput.Name()).CombinedOutput()
	if err != nil {
		return "", errors.New(err.Error() + " : " + string(output))
	}

	samlSignedRequest, err := ioutil.ReadFile(samlXmlsecOutput.Name())
	if err != nil {
		return "", err
	}
	samlSignedRequestXML := strings.Trim(string(samlSignedRequest), "\n")
	return samlSignedRequestXML, nil
}
Пример #12
0
func (task *LuaScriptTask) Execute() error {
	runId := runId(task)
	chromiumBuildDir := ctutil.ChromiumBuildDir(task.ChromiumRev, task.SkiaRev, "")
	// TODO(benjaminwagner): Since run_lua_on_workers only reads the lua script in order to
	// upload to Google Storage, eventually we should move the upload step here to avoid writing
	// to disk. Not sure if we can/should do the same for the aggregator script.
	luaScriptName := runId + ".lua"
	luaScriptPath := filepath.Join(os.TempDir(), luaScriptName)
	if err := ioutil.WriteFile(luaScriptPath, []byte(task.LuaScript), 0666); err != nil {
		return err
	}
	defer skutil.Remove(luaScriptPath)
	if task.LuaAggregatorScript != "" {
		luaAggregatorName := runId + ".aggregator"
		luaAggregatorPath := filepath.Join(os.TempDir(), luaAggregatorName)
		if err := ioutil.WriteFile(luaAggregatorPath, []byte(task.LuaAggregatorScript), 0666); err != nil {
			return err
		}
		defer skutil.Remove(luaAggregatorPath)
	}
	return exec.Run(&exec.Command{
		Name: "run_lua_on_workers",
		Args: []string{
			"--emails=" + task.Username,
			"--description=" + task.Description,
			"--gae_task_id=" + strconv.FormatInt(task.Id, 10),
			"--pageset_type=" + task.PageSets,
			"--chromium_build=" + chromiumBuildDir,
			"--run_id=" + runId,
			"--log_dir=" + logDir,
			"--log_id=" + runId,
		},
		Timeout: ctutil.MASTER_SCRIPT_RUN_LUA_TIMEOUT,
	})
}
Пример #13
0
func getNormalConfig() *Config {
	//!TODO: split into different test case composable builders
	c := new(Config)
	c.System = SystemSection{Pidfile: filepath.Join(os.TempDir(), "nedomi.pid")}
	c.Logger = LoggerSection{Type: "nillogger"}

	cz := &CacheZoneSection{
		ID:             "test1",
		Type:           "disk",
		Path:           os.TempDir(),
		StorageObjects: 20,
		PartSize:       1024,
		Algorithm:      "lru",
	}
	c.CacheZones = map[string]*CacheZoneSection{"test1": cz}

	c.HTTP = new(HTTP)
	c.HTTP.Listen = ":5435"
	c.HTTP.Logger = c.Logger
	c.HTTP.Servers = []*VirtualHost{&VirtualHost{
		BaseVirtualHost: BaseVirtualHost{
			Name:         "localhost",
			UpstreamType: "simple",
			CacheKey:     "test",
			HandlerType:  "proxy",
			Logger:       &c.Logger,
		},
		CacheZone: cz,
	}}
	c.HTTP.Servers[0].UpstreamAddress, _ = url.Parse("http://www.google.com")

	return c
}
Пример #14
0
// InitializeEnvironment creates new transaction for the test
func (env *Environment) InitializeEnvironment() error {
	var err error
	_, file := filepath.Split(env.testFileName)
	env.dbFile, err = ioutil.TempFile(os.TempDir(), file)
	if err != nil {
		return fmt.Errorf("Failed to create a temporary file in %s: %s", os.TempDir(), err.Error())
	}
	env.dbConnection, err = newDBConnection(env.dbFile.Name())
	if err != nil {
		return fmt.Errorf("Failed to connect to database: %s", err.Error())
	}
	env.Environment = gohan_otto.NewEnvironment(env.dbConnection, &middleware.FakeIdentity{}, 30*time.Second)
	env.SetUp()
	env.addTestingAPI()

	script, err := env.VM.Compile(env.testFileName, env.testSource)
	if err != nil {
		return fmt.Errorf("Failed to compile the file '%s': %s", env.testFileName, err.Error())
	}

	env.VM.Run(script)
	err = env.loadSchemas()
	if err != nil {
		schema.ClearManager()
		return fmt.Errorf("Failed to load schema for '%s': %s", env.testFileName, err.Error())
	}

	err = db.InitDBWithSchemas("sqlite3", env.dbFile.Name(), true, false)
	if err != nil {
		schema.ClearManager()
		return fmt.Errorf("Failed to init DB: %s", err.Error())
	}

	return nil
}
Пример #15
0
func saveHosts(newHosts []string) {
	var newHostsBytes []byte
	for _, newHost := range newHosts {
		newHostsBytes = append(newHostsBytes, []byte(newHost+"\n")...)
	}

	err := ioutil.WriteFile(os.TempDir()+"/newhosts.tmp", newHostsBytes, 0644)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"error": err,
			"file":  os.TempDir() + "/newhosts.tmp",
		}).Fatal("Error writing to file")
	}

	_, err = exec.Command(
		"sudo",
		"mv",
		os.TempDir()+"/newhosts.tmp",
		"/etc/hosts").CombinedOutput()
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"from":  os.TempDir() + "/newhosts.tmp",
			"to":    "etc/hosts",
			"error": err,
		}).Fatal("File to move file")
	}
}
Пример #16
0
func TestOIDCDiscoveryNoKeyEndpoint(t *testing.T) {
	var err error
	expectErr := fmt.Errorf("failed to fetch provider config after 0 retries")

	cert := path.Join(os.TempDir(), "oidc-cert")
	key := path.Join(os.TempDir(), "oidc-key")

	defer os.Remove(cert)
	defer os.Remove(key)

	generateSelfSignedCert(t, "127.0.0.1", cert, key)

	op := newOIDCProvider(t)
	srv := httptest.NewUnstartedServer(op.mux)
	srv.TLS = &tls.Config{Certificates: make([]tls.Certificate, 1)}
	srv.TLS.Certificates[0], err = tls.LoadX509KeyPair(cert, key)
	if err != nil {
		t.Fatalf("Cannot load cert/key pair: %v", err)
	}
	srv.StartTLS()
	// TODO: Uncomment when fix #19254
	// defer srv.Close()

	op.pcfg = oidc.ProviderConfig{
		Issuer: mustParseURL(t, srv.URL), // An invalid ProviderConfig. Keys endpoint is required.
	}

	_, err = New(OIDCOptions{srv.URL, "client-foo", cert, "sub", "", 0, 0})
	if !reflect.DeepEqual(err, expectErr) {
		t.Errorf("Expecting %v, but got %v", expectErr, err)
	}
}
Пример #17
0
func TestFlush(t *testing.T) {
	Convey("Do some operations and flush to file system", t, func() {
		z, err := Create(path.Join(os.TempDir(), "testdata/TestFlush.tar.gz"))
		So(err, ShouldBeNil)

		z.AddEmptyDir("level1/level2/level3/level4")
		So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil)

		// Add symbolic links.
		So(z.AddFile("testdata/test.lnk", "testdata/test.lnk"), ShouldBeNil)
		So(z.AddFile("testdata/testdir.lnk", "testdata/testdir.lnk"), ShouldBeNil)

		fmt.Println("Flushing to local file system...")
		So(z.Flush(), ShouldBeNil)
	})

	Convey("Do some operation and flush to io.Writer", t, func() {
		f, err := os.Create(path.Join(os.TempDir(), "testdata/TestFlush2.tar.gz"))
		So(err, ShouldBeNil)
		So(f, ShouldNotBeNil)

		z := New(f)
		z.AddEmptyDir("level1/level2/level3/level4")
		So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil)

		fmt.Println("Flushing to local io.Writer...")
		So(z.Flush(), ShouldBeNil)
		So(z.Flush(), ShouldBeNil)
	})
}
Пример #18
0
func testTaskRunner(restarts bool) (*MockTaskStateUpdater, *TaskRunner) {
	logger := testLogger()
	conf := DefaultConfig()
	conf.StateDir = os.TempDir()
	conf.AllocDir = os.TempDir()
	upd := &MockTaskStateUpdater{}
	alloc := mock.Alloc()
	task := alloc.Job.TaskGroups[0].Tasks[0]
	consulClient, _ := NewConsulService(&consulServiceConfig{logger, "127.0.0.1:8500", "", "", false, false, &structs.Node{}})
	// Initialize the port listing. This should be done by the offer process but
	// we have a mock so that doesn't happen.
	task.Resources.Networks[0].ReservedPorts = []structs.Port{{"", 80}}

	allocDir := allocdir.NewAllocDir(filepath.Join(conf.AllocDir, alloc.ID))
	allocDir.Build([]*structs.Task{task})

	ctx := driver.NewExecContext(allocDir, alloc.ID)
	rp := structs.NewRestartPolicy(structs.JobTypeService)
	restartTracker := newRestartTracker(rp)
	if !restarts {
		restartTracker = noRestartsTracker()
	}

	state := alloc.TaskStates[task.Name]
	tr := NewTaskRunner(logger, conf, upd.Update, ctx, mock.Alloc(), task, state, restartTracker, consulClient)
	return upd, tr
}
Пример #19
0
func applyRepoPatches(chromiumSrcDir, runID string) error {
	// Apply Skia patch.
	skiaDir := filepath.Join(chromiumSrcDir, "third_party", "skia")
	skiaPatch := filepath.Join(os.TempDir(), runID+".skia.patch")
	skiaPatchFile, _ := os.Open(skiaPatch)
	skiaPatchFileInfo, _ := skiaPatchFile.Stat()
	if skiaPatchFileInfo.Size() > 10 {
		if err := ApplyPatch(skiaPatch, skiaDir); err != nil {
			return fmt.Errorf("Could not apply Skia's patch in %s: %s", skiaDir, err)
		}
	}
	// Apply Blink patch.
	blinkDir := filepath.Join(chromiumSrcDir, "third_party", "WebKit")
	blinkPatch := filepath.Join(os.TempDir(), runID+".blink.patch")
	blinkPatchFile, _ := os.Open(blinkPatch)
	blinkPatchFileInfo, _ := blinkPatchFile.Stat()
	if blinkPatchFileInfo.Size() > 10 {
		if err := ApplyPatch(blinkPatch, blinkDir); err != nil {
			return fmt.Errorf("Could not apply Blink's patch in %s: %s", blinkDir, err)
		}
	}
	// Apply Chromium patch.
	chromiumPatch := filepath.Join(os.TempDir(), runID+".chromium.patch")
	chromiumPatchFile, _ := os.Open(chromiumPatch)
	chromiumPatchFileInfo, _ := chromiumPatchFile.Stat()
	if chromiumPatchFileInfo.Size() > 10 {
		if err := ApplyPatch(chromiumPatch, chromiumSrcDir); err != nil {
			return fmt.Errorf("Could not apply Chromium's patch in %s: %s", chromiumSrcDir, err)
		}
	}
	return nil
}
Пример #20
0
func TestSearchExist(t *testing.T) {
	t.Parallel()

	nodes, _ := ioutil.TempFile(os.TempDir(), "bplus_search_test")
	defer os.Remove(nodes.Name())

	values, _ := ioutil.TempFile(os.TempDir(), "bplus_search_test")
	defer os.Remove(values.Name())

	var invalid, val BPlusValue

	invalid = append(invalid, []byte("INVALID INVALID BACON INVALID")...)
	val = append(val, []byte("This is a valid value.")...)

	for i := 0; i < 5; i++ {
		Insert(nodes, values, BPlusKey(i), invalid)
	}
	Insert(nodes, values, 5, val)
	for i := 6; i < 10; i++ {
		Insert(nodes, values, BPlusKey(i), invalid)
	}

	found := Search(nodes, values, 5)
	if found == nil {
		t.Error("found == nil")
	}
	if !found.Equal(val) {
		t.Log("found  ", found)
		t.Log("wanted ", val)
		t.Fail()
	}
}
Пример #21
0
func (s *TestSuite) TestDiffDirs(c *C) {
	/// filesystem
	root1, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(root1)

	root2, err := ioutil.TempDir(os.TempDir(), "cmd-")
	c.Assert(err, IsNil)
	defer os.RemoveAll(root2)

	var perr *probe.Error
	for i := 0; i < 10; i++ {
		objectPath := filepath.Join(root1, "object"+strconv.Itoa(i))
		data := "hello"
		dataLen := len(data)
		perr = putTarget(objectPath, int64(dataLen), bytes.NewReader([]byte(data)))
		c.Assert(perr, IsNil)
	}

	for i := 0; i < 10; i++ {
		objectPath := filepath.Join(root2, "object"+strconv.Itoa(i))
		data := "hello"
		dataLen := len(data)
		perr = putTarget(objectPath, int64(dataLen), bytes.NewReader([]byte(data)))
		c.Assert(perr, IsNil)
	}

	for diff := range doDiffMain(root1, root2, false) {
		c.Assert(diff.Error, IsNil)
	}
}
Пример #22
0
func BenchmarkGetAll(b *testing.B) {
	b.StopTimer()

	nodes, _ := ioutil.TempFile(os.TempDir(), "bplus_search_benchmark")
	defer os.Remove(nodes.Name())

	values, _ := ioutil.TempFile(os.TempDir(), "bplus_search_benchmark")
	defer os.Remove(values.Name())

	var val BPlusValue

	val = append(val, []byte("Test123")...)

	for i := 0; i < 1000; i++ {
		Insert(nodes, values, BPlusKey(i), val)
	}

	b.StartTimer()

	// One iteration of BenchmarkGetAll = one Search call in BenchmarkSearch. This is done so the values are comparable.
	for i := 0; i < b.N/1000+1; i++ {
		for it := GetAll(nodes, values); it.Valid(); it.Next() {
			it.Value()
		}
	}
}
func TestTempfilenameFromExecutableFilePath(t *testing.T) {
	var p MackerelPlugin

	wd, _ := os.Getwd()
	// not PluginWithPrefix, regular filename
	expect1 := filepath.Join(os.TempDir(), "mackerel-plugin-foobar")
	filename1 := p.generateTempfilePath(filepath.Join(wd, "foobar"))
	if filename1 != expect1 {
		t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect1, filename1)
	}

	// not PluginWithPrefix, contains some characters to be sanitized
	expect2 := filepath.Join(os.TempDir(), "mackerel-plugin-some_sanitized_name_1.2")
	filename2 := p.generateTempfilePath(filepath.Join(wd, "some sanitized:name+1.2"))
	if filename2 != expect2 {
		t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect2, filename2)
	}

	// not PluginWithPrefix, begins with "mackerel-plugin-"
	expect3 := filepath.Join(os.TempDir(), "mackerel-plugin-trimmed")
	filename3 := p.generateTempfilePath(filepath.Join(wd, "mackerel-plugin-trimmed"))
	if filename3 != expect3 {
		t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect3, filename3)
	}

	// PluginWithPrefix ignores current filename
	pPrefix := NewMackerelPlugin(testP{})
	expectForPrefix := filepath.Join(os.TempDir(), "mackerel-plugin-testP")
	filenameForPrefix := pPrefix.generateTempfilePath(filepath.Join(wd, "foo"))
	if filenameForPrefix != expectForPrefix {
		t.Errorf("pPrefix.generateTempfilePath() should be %s, but: %s", expectForPrefix, filenameForPrefix)
	}
}
Пример #24
0
func TestOIDCDiscoveryNoKeyEndpoint(t *testing.T) {
	var err error
	expectErr := fmt.Errorf("OIDC provider must provide 'jwks_uri' for public key discovery")

	cert := path.Join(os.TempDir(), "oidc-cert")
	key := path.Join(os.TempDir(), "oidc-key")

	defer os.Remove(cert)
	defer os.Remove(key)

	generateSelfSignedCert(t, "127.0.0.1", cert, key)

	op := newOIDCProvider(t)
	srv := httptest.NewUnstartedServer(op.mux)
	srv.TLS = &tls.Config{Certificates: make([]tls.Certificate, 1)}
	srv.TLS.Certificates[0], err = tls.LoadX509KeyPair(cert, key)
	if err != nil {
		t.Fatalf("Cannot load cert/key pair: %v", err)
	}
	srv.StartTLS()
	// TODO: Uncomment when fix #19254
	// defer srv.Close()

	op.pcfg = oidc.ProviderConfig{
		Issuer: srv.URL,
	}

	_, err = New(srv.URL, "client-foo", cert, "sub", "")
	if !reflect.DeepEqual(err, expectErr) {
		t.Errorf("Expecting %v, but got %v", expectErr, err)
	}
}
Пример #25
0
func TestDirExists(t *testing.T) {
	type test struct {
		input    string
		expected bool
	}

	data := []test{
		{".", true},
		{"./", true},
		{"..", true},
		{"../", true},
		{"./..", true},
		{"./../", true},
		{os.TempDir(), true},
		{os.TempDir() + FilePathSeparator, true},
		{"/", true},
		{"/some-really-random-directory-name", false},
		{"/some/really/random/directory/name", false},
		{"./some-really-random-local-directory-name", false},
		{"./some/really/random/local/directory/name", false},
	}

	for i, d := range data {
		exists, _ := DirExists(filepath.FromSlash(d.input), new(afero.OsFs))
		if d.expected != exists {
			t.Errorf("Test %d failed. Expected %t got %t", i, d.expected, exists)
		}
	}
}
Пример #26
0
func TestWriteTempfile(t *testing.T) {
	// Use TempDir to figure out the path to a valid directory
	dir, err := ioutil.TempDir(os.TempDir(), "prefix")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(dir)

	from_file, err := ioutil.TempFile(os.TempDir(), "prefix")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(from_file.Name())
	from_file.WriteString("some content")
	from_file.Sync()
	from_file.Seek(0, 0)

	f := File{}
	f.SetTag("foo")
	f.SetFilename("bar")
	f.TagDir = filepath.Join(dir, f.Tag)
	err = f.EnsureTagDirectoryExists()
	if err != nil {
		t.Fatal(err)
	}
	err = f.WriteTempfile(from_file, dir)
	if err != nil {
		t.Fatal(err)
	}
	if f.Bytes != 12 {
		t.Fatal("The amount of bytes was unexpected:", f.Bytes)
	}
}
Пример #27
0
// NewTestEnvironment creates a new Environment suitable for use in tests.
//
// This function should only be used in testing
func newTestEnvironment() *runtime.Environment {
	storage, err := runtime.NewTemporaryStorage(os.TempDir())
	nilOrPanic(err, "Failed to create temporary storage at: ", os.TempDir())

	folder, err := storage.NewFolder()
	nilOrPanic(err, "Failed to create temporary storage folder")

	// Set finalizer so that we always get the temporary folder removed.
	// This is should really only be used in tests, otherwise it would better to
	// call Remove() manually.
	rt.SetFinalizer(folder, func(f runtime.TemporaryFolder) {
		f.Remove()
	})

	logger, err := runtime.CreateLogger(os.Getenv("LOGGING_LEVEL"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating logger. %s", err)
		os.Exit(1)
	}

	return &runtime.Environment{
		GarbageCollector: &gc.GarbageCollector{},
		TemporaryStorage: folder,
		Log:              logger,
	}
}
Пример #28
0
func TestSymlinkWhenAFileExistsAtIntendedPath(t *testing.T) {
	osFs, _ := createOsFs()
	filePath := filepath.Join(os.TempDir(), "SymlinkTestIdempotent1File")
	symlinkPath := filepath.Join(os.TempDir(), "SymlinkTestIdempotent1Symlink")

	osFs.WriteToFile(filePath, "some content")
	defer os.Remove(filePath)

	osFs.WriteToFile(symlinkPath, "some other content")
	defer os.Remove(symlinkPath)

	// Repoints symlink to new destination
	err := osFs.Symlink(filePath, symlinkPath)
	assert.NoError(t, err)

	defer os.Remove(symlinkPath)

	symlinkStats, err := os.Lstat(symlinkPath)
	assert.NoError(t, err)
	assert.Equal(t, os.ModeSymlink, os.ModeSymlink&symlinkStats.Mode())

	symlinkFile, err := os.Open(symlinkPath)
	assert.NoError(t, err)
	assert.Equal(t, "some content", readFile(symlinkFile))
}
Пример #29
0
func main() {
	fmt.Println("TEMP DIR:", os.TempDir())
	http.ListenAndServe(":8080", http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		if req.Method == "POST" {
			sourc, _, err := req.FormFile("my-file")
			if err != nil {
				http.Error(res, err.Error(), 500)
				return
			}
			defer sourc.Close()

			dest, err := os.Create(filepath.Join(os.TempDir(), "file.txt"))
			if err != nil {
				http.Error(res, err.Error(), 500)
				return
			}
			defer dest.Close()

			io.Copy(dest, sourc)
		}

		res.Header().Set("Content-Type", "text/html; charset=utf-8")
		io.WriteString(res, `
      <form method="POST" enctype="multipart/form-data">
        <input type="file" name="my-file">
        <input type="submit">
      </form>
      `)
	}))
}
func TestGetZip(t *testing.T) {
	wti := NewWebTranslateIt(TEST_PROJECT_TOKEN)
	project, err := wti.GetProject()

	if err != nil {
		t.Errorf("Error getting project: %v", err)
	} else if project.Name != "WebTranslateIt" {
		t.Errorf("Received wrong project from API, expected name WebTranslateIt but got %q", project.Name)
	} else if zipFile, err := project.ZipFile(); err != nil {
		t.Errorf("Error getting zip file of the project %q: %v", project.Name, err)
	} else if zipFile.Size() == 0 {
		t.Errorf("Zip file is empty")
	} else {
		var (
			err  error
			path string
			file *os.File
			data map[string][]byte
		)
		// Save as *os.File
		file, err = ioutil.TempFile(os.TempDir(), "webtranslateit_go_client")
		if err != nil {
			t.Fatalf("Error creating temp file %v", err)
		}

		// Save file
		if err = zipFile.SaveToFile(file); err != nil {
			file.Close()
			t.Fatalf("Error saving zip file to *os.File %q %v", file.Name(), err)
		}
		file.Close()

		// Remove temp file
		if err = os.Remove(file.Name()); err != nil {
			t.Fatalf("Error removing temp file %q %v", file.Name(), err)
		}

		// Save to path
		path = filepath.Join(os.TempDir(), "webtranslateit_go_client_TestGetZip.zip")
		if err = zipFile.SaveToPath(path); err != nil {
			t.Fatalf("Error saving zip file to path %q %v", path, err)
		}

		// Remove temp file
		if err = os.Remove(path); err != nil {
			t.Fatalf("Error removing temp file %q %v", path, err)
		}

		data, err = zipFile.Extract()
		if err != nil {
			t.Errorf("Error extracting zip file %v", err)
		}

		for _, f := range project.ProjectFiles {
			if _, ok := data[f.Name]; !ok {
				t.Errorf("Among the extracted files no file %q", f.Name)
			}
		}
	}
}