Exemple #1
0
func TestRead(t *testing.T) {
	profiles.Clear()
	ctx := tool.NewDefaultContext()
	if err := profiles.Read(ctx, "./testdata/m1.xml"); err != nil {
		t.Fatal(err)
	}

	cmp := func(a, b []string) bool {
		if len(a) != len(b) {
			return false
		}
		for i, s := range a {
			if s != b[i] {
				return false
			}
		}
		return true
	}
	names := profiles.Profiles()
	sort.Strings(names)
	if got, want := names, []string{"a", "b"}; !cmp(got, want) {
		t.Errorf("got %v, want %v", got, want)
	}
	p := profiles.LookupProfile("a")
	if got, want := p.Targets()[0].Tag(), "t1"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := p.Targets()[0].OS(), "os1"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := p.Targets()[1].Version(), "bar"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
}
Exemple #2
0
// TestLocalProjects tests the behavior of the LocalProjects method with
// different ScanModes.
func TestLocalProjects(t *testing.T) {
	ctx := tool.NewDefaultContext()
	rootDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		t.Fatalf("TempDir() failed: %v", err)
	}
	defer ctx.Run().RemoveAll(rootDir)
	oldRoot := os.Getenv("JIRI_ROOT")
	if err := os.Setenv("JIRI_ROOT", rootDir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	manifestDir := setupNewProject(t, ctx, rootDir, ".manifest", false)

	// Create some projects.
	numProjects, projectPaths := 3, []string{}
	for i := 0; i < numProjects; i++ {
		projectName := localProjectName(i)
		projectPath := setupNewProject(t, ctx, rootDir, projectName, true)
		project := Project{
			Path:     projectPath,
			Name:     projectName,
			Protocol: "git",
		}
		if err := writeMetadata(ctx, project, projectPath); err != nil {
			t.Fatalf("writeMetadata %v %v) failed: %v\n", project, projectPath, err)
		}
		projectPaths = append(projectPaths, projectPath)
	}

	// Create manifest but only tell it about the first project.
	createRemoteManifest(t, ctx, manifestDir, projectPaths[:1])

	// LocalProjects with scanMode = FastScan should only find the first
	// project.
	foundProjects, err := LocalProjects(ctx, FastScan)
	if err != nil {
		t.Fatalf("LocalProjects(%v) failed: %v", FastScan, err)
	}
	checkProjectsMatchPaths(t, foundProjects, projectPaths[:1])

	// LocalProjects with scanMode = FullScan should find all projects.
	foundProjects, err = LocalProjects(ctx, FullScan)
	if err != nil {
		t.Fatalf("LocalProjects(%v) failed: %v", FastScan, err)
	}
	checkProjectsMatchPaths(t, foundProjects, projectPaths[:])

	// Check that deleting a project forces LocalProjects to run a full scan,
	// even if FastScan is specified.
	if err := ctx.Run().RemoveAll(projectPaths[0]); err != nil {
		t.Fatalf("RemoveAll(%v) failed: %v", projectPaths[0])
	}
	foundProjects, err = LocalProjects(ctx, FastScan)
	if err != nil {
		t.Fatalf("LocalProjects(%v) failed: %v", FastScan, err)
	}
	checkProjectsMatchPaths(t, foundProjects, projectPaths[1:])
}
Exemple #3
0
func TestBackwardsCompatibility(t *testing.T) {
	profiles.Clear()

	getProfiles := func() []*profiles.Profile {
		db := []*profiles.Profile{}
		names := profiles.Profiles()
		sort.Strings(names)
		for _, name := range names {
			db = append(db, profiles.LookupProfile(name))
		}
		return db
	}

	ctx := tool.NewDefaultContext()
	if err := profiles.Read(ctx, "./testdata/legacy.xml"); err != nil {
		t.Fatal(err)
	}

	if got, want := profiles.SchemaVersion(), profiles.Original; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	oprofiles := getProfiles()
	if got, want := len(oprofiles), 5; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))

	var t1 profiles.Target
	t1.Set("tag=cpu,os")
	profiles.AddProfileTarget("__first", t1)

	if err := profiles.Write(ctx, filename); err != nil {
		t.Fatal(err)
	}

	if err := profiles.Read(ctx, filename); err != nil {
		t.Fatal(err)
	}

	if got, want := profiles.SchemaVersion(), profiles.V3; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	nprofiles := getProfiles()
	if got, want := len(nprofiles), 6; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	if got, want := nprofiles[0].Name, "__first"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}

	for i, v := range nprofiles[1:] {
		if got, want := v.Name, oprofiles[i].Name; got != want {
			t.Errorf("got %v, want %v", got, want)
		}
	}
}
Exemple #4
0
func TestConfigHelper(t *testing.T) {
	ctx := tool.NewDefaultContext()
	ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, "release/go/src/v.io/jiri/profiles/testdata/m2.xml")
	if err != nil {
		t.Fatal(err)
	}
	ch.Vars = envvar.VarsFromOS()
	ch.Delete("CGO_CFLAGS")
	target, _ := profiles.NewTarget("native=")
	ch.SetEnvFromProfiles(profiles.CommonConcatVariables(), map[string]bool{}, "go,syncbase", target)
	if got, want := ch.Get("CGO_CFLAGS"), "-IX -IY -IA -IB"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
}
Exemple #5
0
func TestWrite(t *testing.T) {
	profiles.Clear()
	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))
	ctx := tool.NewDefaultContext()

	addProfileAndTargets(t, "b")
	addProfileAndTargets(t, "a")
	profiles.Write(ctx, filename)

	g, _ := ioutil.ReadFile(filename)
	w, _ := ioutil.ReadFile("./testdata/m1.xml")
	if got, want := removeDate(strings.TrimSpace(string(g))), strings.TrimSpace(string(w)); got != want {
		t.Fatalf("got %v, want %v", got, want)
	}
}
Exemple #6
0
// setupTest creates a setup for testing the review tool.
func setupTest(t *testing.T, installHook bool) (ctx *tool.Context, cwd string, root *project.FakeJiriRoot, repoPath, originPath, gerritPath string) {
	ctx = tool.NewDefaultContext()
	cwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("Getwd() failed: %v", err)
	}
	root, err = project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	repoPath, originPath, gerritPath = createTestRepos(t, ctx, root.Dir)
	if installHook == true {
		for _, path := range []string{repoPath, originPath, gerritPath} {
			installCommitMsgHook(t, ctx, path)
		}
	}
	chdir(t, ctx, repoPath)
	return
}
Exemple #7
0
// NewX is similar to jiri.NewX, but is meant for usage in a testing environment.
func NewX(t *testing.T) (*jiri.X, func()) {
	ctx := tool.NewDefaultContext()
	root, err := ctx.NewSeq().TempDir("", "")
	if err != nil {
		t.Fatalf("TempDir() failed: %v", err)
	}
	oldRoot := os.Getenv(jiri.RootEnv)
	if err := os.Setenv(jiri.RootEnv, root); err != nil {
		t.Fatalf("Setenv(%q, %q) failed: %v", jiri.RootEnv, root, err)
	}
	cleanup := func() {
		if err := os.Setenv(jiri.RootEnv, oldRoot); err != nil {
			t.Fatalf("Setenv(%q, %q) failed: %v", jiri.RootEnv, oldRoot, err)
		}
		if err := ctx.NewSeq().RemoveAll(root).Done(); err != nil {
			t.Fatalf("RemoveAll(%q) failed: %v", root, err)
		}
	}
	return &jiri.X{Context: ctx, Root: root}, cleanup
}
Exemple #8
0
// TestJiriRootSymlink checks that JiriRoot interprets the value
// of the JIRI_ROOT environment variable as a path, evaluates any
// symlinks the path might contain, and returns the result.
func TestJiriRootSymlink(t *testing.T) {
	ctx := tool.NewDefaultContext()

	// Create a temporary directory.
	tmpDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		t.Fatalf("TempDir() failed: %v", err)
	}
	defer ctx.Run().RemoveAll(tmpDir)

	// Make sure tmpDir is not a symlink itself.
	tmpDir, err = filepath.EvalSymlinks(tmpDir)
	if err != nil {
		t.Fatalf("EvalSymlinks(%v) failed: %v", tmpDir, err)
	}

	// Create a directory and a symlink to it.
	root, perm := filepath.Join(tmpDir, "root"), os.FileMode(0700)
	if err := ctx.Run().MkdirAll(root, perm); err != nil {
		t.Fatalf("%v", err)
	}
	symRoot := filepath.Join(tmpDir, "sym_root")
	if err := ctx.Run().Symlink(root, symRoot); err != nil {
		t.Fatalf("%v", err)
	}

	// Set the JIRI_ROOT to the symlink created above and check
	// that JiriRoot() evaluates the symlink.
	oldRoot := os.Getenv("JIRI_ROOT")
	if err := os.Setenv("JIRI_ROOT", symRoot); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)
	got, err := project.JiriRoot()
	if err != nil {
		t.Fatalf("%v", err)
	}
	if want := root; got != want {
		t.Fatalf("unexpected output: got %v, want %v", got, want)
	}
}
Exemple #9
0
func TestConfigSerialization(t *testing.T) {
	ctx := tool.NewDefaultContext()

	root, err := project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer func() {
		if err := root.Cleanup(ctx); err != nil {
			t.Fatalf("%v", err)
		}
	}()
	oldRoot, err := project.JiriRoot()
	if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	config := NewConfig(
		APICheckProjectsOpt(apiCheckProjects),
		CopyrightCheckProjectsOpt(copyrightCheckProjects),
		GoWorkspacesOpt(goWorkspaces),
		JenkinsMatrixJobsOpt(jenkinsMatrixJobs),
		ProjectTestsOpt(projectTests),
		TestDependenciesOpt(testDependencies),
		TestGroupsOpt(testGroups),
		TestPartsOpt(testParts),
		VDLWorkspacesOpt(vdlWorkspaces),
	)

	if err := SaveConfig(ctx, config); err != nil {
		t.Fatalf("%v", err)
	}
	gotConfig, err := LoadConfig(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}

	testConfigAPI(t, gotConfig)
}
Exemple #10
0
func TestEnvFromTarget(t *testing.T) {
	profiles.Clear()
	root, _ := project.JiriRoot()
	ctx := tool.NewDefaultContext()
	profiles.InstallProfile("a", "root")
	profiles.InstallProfile("b", "root")
	t1, t2 := &profiles.Target{}, &profiles.Target{}
	t1.Set("t1=cpu1-os1")
	t1.Env.Set("A=B C=D, B=C Z=Z")
	t2.Set("t1=cpu1-os1")
	t2.Env.Set("A=Z,B=Z,Z=Z")
	profiles.AddProfileTarget("a", *t1)
	profiles.AddProfileTarget("b", *t2)
	tmpdir, err := ioutil.TempDir(".", "pdb")
	if err != nil {
		t.Fatal(err)
	}
	filename := filepath.Join("release", "go", "src", "v.io", "jiri", "profiles", tmpdir, "manifest")
	if err := profiles.Write(ctx, filepath.Join(root, filename)); err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpdir)
	ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, filename)
	if err != nil {
		t.Fatal(err)
	}
	ch.Vars = envvar.VarsFromSlice([]string{})
	target, _ := profiles.NewTarget("t1=")
	ch.SetEnvFromProfiles(map[string]string{"A": " "}, map[string]bool{"Z": true}, "a,b", target)
	vars := ch.ToMap()
	if got, want := len(vars), 3; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := ch.Get("A"), "B C=D Z"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
	if got, want := ch.Get("B"), "Z"; got != want {
		t.Errorf("got %v, want %v", got, want)
	}
}
Exemple #11
0
func ExampleManager() {
	myProfile := "myNewProfile"
	var target profiles.Target

	init := func() {
		profiles.Register(myProfile, newProfile(myProfile))
		flags := flag.NewFlagSet("example", flag.ContinueOnError)
		profiles.RegisterTargetAndEnvFlags(flags, &target)
		flags.Parse([]string{"--target=myTarget=arm-linux", "--env=A=B,C=D", "--env=E=F"})
	}
	init()

	mgr := profiles.LookupManager(myProfile)
	if mgr == nil {
		panic("manager not found for: " + myProfile)
	}

	ctx := tool.NewDefaultContext()
	// Install myNewProfile for target.
	if err := mgr.Install(ctx, target); err != nil {
		panic("failed to find manager for: " + myProfile)
	}

	fmt.Println(mgr.String())

	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))

	if err := profiles.Write(ctx, filename); err != nil {
		panic(err)
	}

	// Clear the profile manifest information in order to mimic starting
	// a new process and reading the manifest file.
	profiles.Clear()

	// Read the profile manifest.
	profiles.Read(ctx, filename)

	mgr = profiles.LookupManager(myProfile)
	if mgr == nil {
		panic("manager not found for: " + myProfile)
	}

	fmt.Println(mgr.String())
	mgr.Uninstall(ctx, target)
	fmt.Println(mgr.String())
	fmt.Println(mgr.VersionInfo().Supported())
	fmt.Println(mgr.VersionInfo().Default())

	// Output:
	// Profile: myNewProfile: installed
	// [myTarget=arm-linux@]
	//
	// Profile: myNewProfile: installed
	// [myTarget=arm-linux@]
	//
	// Profile: myNewProfile: uninstalled
	//
	// [4 3 2]
	// 3
}
Exemple #12
0
func TestOncall(t *testing.T) {
	ctx := tool.NewDefaultContext()
	root, err := project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer func() {
		if err := root.Cleanup(ctx); err != nil {
			t.Fatalf("%v", err)
		}
	}()
	oldRoot, err := project.JiriRoot()
	if err != nil {
		t.Fatalf("%v", err)
	}
	if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	// Create a oncall.v1.xml file.
	createOncallFile(t, ctx)
	type testCase struct {
		targetTime    time.Time
		expectedShift *OncallShift
	}
	testCases := []testCase{
		testCase{
			targetTime:    time.Date(2013, time.November, 5, 12, 0, 0, 0, time.Local),
			expectedShift: nil,
		},
		testCase{
			targetTime: time.Date(2014, time.November, 5, 12, 0, 0, 0, time.Local),
			expectedShift: &OncallShift{
				Primary:   "spetrovic",
				Secondary: "suharshs",
				Date:      "Nov 5, 2014 12:00:00 PM",
			},
		},
		testCase{
			targetTime: time.Date(2014, time.November, 5, 14, 0, 0, 0, time.Local),
			expectedShift: &OncallShift{
				Primary:   "spetrovic",
				Secondary: "suharshs",
				Date:      "Nov 5, 2014 12:00:00 PM",
			},
		},
		testCase{
			targetTime: time.Date(2014, time.November, 20, 14, 0, 0, 0, time.Local),
			expectedShift: &OncallShift{
				Primary:   "jsimsa",
				Secondary: "toddw",
				Date:      "Nov 19, 2014 12:00:00 PM",
			},
		},
	}
	for _, test := range testCases {
		got, err := Oncall(ctx, test.targetTime)
		if err != nil {
			t.Fatalf("want no errors, got: %v", err)
		}
		if !reflect.DeepEqual(test.expectedShift, got) {
			t.Fatalf("want %#v, got %#v", test.expectedShift, got)
		}
	}
}
Exemple #13
0
// TestUpdateUniverse is a comprehensive test of the "jiri update"
// logic that handles projects.
//
// TODO(jsimsa): Add tests for the logic that updates tools.
func TestUpdateUniverse(t *testing.T) {
	// Setup an instance of jiri universe, creating the remote repositories for
	// the manifest and projects under the "remote" directory, which is ignored
	// from the consideration of LocalProjects().
	ctx := tool.NewDefaultContext()
	rootDir, err := ctx.Run().TempDir("", "")
	if err != nil {
		t.Fatalf("TempDir() failed: %v", err)
	}
	defer ctx.Run().RemoveAll(rootDir)
	localDir := filepath.Join(rootDir, "local")
	remoteDir := filepath.Join(rootDir, "remote")
	localManifest := setupNewProject(t, ctx, localDir, ".manifest", false)
	writeEmptyMetadata(t, ctx, localManifest)
	remoteManifest := setupNewProject(t, ctx, remoteDir, "test-remote-manifest", false)
	addRemote(t, ctx, localManifest, "origin", remoteManifest)
	numProjects, remoteProjects := 5, []string{}
	for i := 0; i < numProjects; i++ {
		remoteProject := setupNewProject(t, ctx, remoteDir, remoteProjectName(i), true)
		remoteProjects = append(remoteProjects, remoteProject)
	}
	createRemoteManifest(t, ctx, remoteManifest, remoteProjects)
	oldRoot := os.Getenv("JIRI_ROOT")
	if err := os.Setenv("JIRI_ROOT", localDir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	// Check that calling UpdateUniverse() creates local copies of
	// the remote repositories, advancing projects to HEAD or to
	// the fixed revision set in the manifest.
	for _, remoteProject := range remoteProjects {
		writeReadme(t, ctx, remoteProject, "revision 1")
	}
	holdProjectBack(t, ctx, remoteManifest, remoteProjects[0])
	for _, remoteProject := range remoteProjects {
		writeReadme(t, ctx, remoteProject, "revision 2")
	}
	if err := UpdateUniverse(ctx, false); err != nil {
		t.Fatalf("%v", err)
	}
	checkCreateFn := func(i int, revision string) {
		localProject := filepath.Join(localDir, localProjectName(i))
		checkGitIgnore(t, ctx, localProject)
		if i == 0 {
			checkReadme(t, ctx, localProject, "revision 1")
		} else {
			checkReadme(t, ctx, localProject, revision)
		}
	}
	for i, _ := range remoteProjects {
		checkCreateFn(i, "revision 2")
	}

	// Commit more work to the remote repositories and check that
	// calling UpdateUniverse() advances project to HEAD or to the
	// fixed revision set in the manifest.
	holdProjectBack(t, ctx, remoteManifest, remoteProjects[1])
	for _, remoteProject := range remoteProjects {
		writeReadme(t, ctx, remoteProject, "revision 3")
	}
	if err := UpdateUniverse(ctx, false); err != nil {
		t.Fatalf("%v", err)
	}
	checkUpdateFn := func(i int, revision string) {
		if i == 1 {
			checkReadme(t, ctx, filepath.Join(localDir, localProjectName(i)), "revision 2")
		} else {
			checkCreateFn(i, revision)
		}
	}
	for i, _ := range remoteProjects {
		checkUpdateFn(i, "revision 3")
	}

	// Create an uncommitted file and make sure UpdateUniverse()
	// does not drop it. This ensures that the "git reset --hard"
	// mechanism used for pointing the master branch to a fixed
	// revision does not lose work in progress.
	file, perm, want := filepath.Join(remoteProjects[1], "uncommitted_file"), os.FileMode(0644), []byte("uncommitted work")
	if err := ioutil.WriteFile(file, want, perm); err != nil {
		t.Fatalf("WriteFile(%v, %v) failed: %v", file, err, perm)
	}
	if err := UpdateUniverse(ctx, false); err != nil {
		t.Fatalf("%v", err)
	}
	got, err := ioutil.ReadFile(file)
	if err != nil {
		t.Fatalf("%v", err)
	}
	if bytes.Compare(got, want) != 0 {
		t.Fatalf("unexpected content %v:\ngot\n%s\nwant\n%s\n", remoteProjects[1], got, want)
	}

	// Update the local path at which a remote project is to be
	// located and check that UpdateUniverse() moves the local
	// copy of the project.
	destination := filepath.Join("test", localProjectName(2))
	moveProject(t, ctx, remoteManifest, remoteProjects[2], destination)
	if err := UpdateUniverse(ctx, false); err != nil {
		t.Fatalf("%v", err)
	}
	checkMoveFn := func(i int, revision string) {
		if i == 2 {
			checkReadme(t, ctx, filepath.Join(localDir, destination), revision)
		} else {
			checkUpdateFn(i, revision)
		}
	}
	for i, _ := range remoteProjects {
		checkMoveFn(i, "revision 3")
	}

	// Delete a remote project and check that UpdateUniverse()
	// deletes the local copy of the project.
	deleteProject(t, ctx, remoteManifest, remoteProjects[3])
	if err := UpdateUniverse(ctx, true); err != nil {
		t.Fatalf("%v", err)
	}
	checkDeleteFn := func(i int, revision string) {
		if i == 3 {
			localProject := filepath.Join(localDir, localProjectName(i))
			if _, err := ctx.Run().Stat(localProject); err == nil {
				t.Fatalf("project %v has not been deleted", localProject)
			} else {
				if !os.IsNotExist(err) {
					t.Fatalf("%v", err)
				}
			}
		} else {
			checkMoveFn(i, revision)
		}
	}
	for i, _ := range remoteProjects {
		checkDeleteFn(i, "revision 3")
	}

	// Commit to a non-master branch of a remote project and check that
	// UpdateUniverse() can update the local project to point to a revision on
	// that branch.
	writeReadme(t, ctx, remoteProjects[4], "master commit")
	createAndCheckoutBranch(t, ctx, remoteProjects[4], "non_master")
	writeReadme(t, ctx, remoteProjects[4], "non master commit")
	remoteBranchRevision := currentRevision(t, ctx, remoteProjects[4])
	setRevisionForProject(t, ctx, remoteManifest, remoteProjects[4], remoteBranchRevision)
	if err := UpdateUniverse(ctx, true); err != nil {
		t.Fatalf("%v", err)
	}
	localProject := filepath.Join(localDir, localProjectName(4))
	localBranchRevision := currentRevision(t, ctx, localProject)
	if localBranchRevision != remoteBranchRevision {
		t.Fatalf("project 4 is at revision %v, expected %v\n", localBranchRevision, remoteBranchRevision)
	}
	// Reset back to origin/master so the next update without a "revision" works.
	resetToOriginMaster(t, ctx, localProject)

	// Create a local manifest that imports the remote manifest
	// and check that UpdateUniverse() has no effect.
	createLocalManifestStub(t, ctx, localDir)
	if err := UpdateUniverse(ctx, true); err != nil {
		t.Fatalf("%v", err)
	}

	checkRebaseFn := func(i int, revision string) {
		if i == 4 {
			checkReadme(t, ctx, localProject, "non master commit")
		} else {
			checkDeleteFn(i, revision)
		}
	}
	for i, _ := range remoteProjects {
		checkRebaseFn(i, "revision 3")
	}

	// Create a local manifest that matches the remote manifest,
	// then revert the remote manifest to its initial version and
	// check that UpdateUniverse() has no effect.
	createLocalManifestCopy(t, ctx, localDir, remoteManifest)
	createRemoteManifest(t, ctx, remoteManifest, remoteProjects)
	if err := UpdateUniverse(ctx, true); err != nil {
		t.Fatalf("%v", err)
	}
	for i, _ := range remoteProjects {
		checkRebaseFn(i, "revision 3")
	}
}
Exemple #14
0
func testSetPathHelper(t *testing.T, name string) {
	profiles.Clear()
	ctx := tool.NewDefaultContext()

	// Setup a fake JIRI_ROOT.
	root, err := project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer func() {
		if err := root.Cleanup(ctx); err != nil {
			t.Fatalf("%v", err)
		}
	}()

	// Create a test project and identify it as a Go workspace.
	if err := root.CreateRemoteProject(ctx, "test"); err != nil {
		t.Fatalf("%v", err)
	}
	if err := root.AddProject(ctx, project.Project{
		Name:   "test",
		Path:   "test",
		Remote: root.Projects["test"],
	}); err != nil {
		t.Fatalf("%v", err)
	}
	if err := root.UpdateUniverse(ctx, false); err != nil {
		t.Fatalf("%v", err)
	}
	var config *util.Config
	switch name {
	case "GOPATH":
		config = util.NewConfig(util.GoWorkspacesOpt([]string{"test", "does/not/exist"}))
	case "VDLPATH":
		config = util.NewConfig(util.VDLWorkspacesOpt([]string{"test", "does/not/exist"}))
	}

	oldRoot, err := project.JiriRoot()
	if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	if err := profiles.Write(ctx, filepath.Join(root.Dir, "profiles-manifest")); err != nil {
		t.Fatal(err)
	}

	if err := util.SaveConfig(ctx, config); err != nil {
		t.Fatalf("%v", err)
	}

	// Retrieve Jiri_ROOT through JiriRoot() to account for symlinks.
	jiriRoot, err := project.JiriRoot()
	if err != nil {
		t.Fatalf("%v", err)
	}

	ch, err := profiles.NewConfigHelper(ctx, profiles.UseProfiles, "profiles-manifest")
	if err != nil {
		t.Fatal(err)
	}
	ch.Vars = envvar.VarsFromOS()
	ch.Set(name, "")

	var want string
	switch name {
	case "GOPATH":
		want = filepath.Join(jiriRoot, "test")
		ch.SetGoPath()
	case "VDLPATH":
		// Make a fake src directory.
		want = filepath.Join(jiriRoot, "test", "src")
		if err := ctx.Run().MkdirAll(want, 0755); err != nil {
			t.Fatalf("%v", err)
		}
		ch.SetVDLPath()
	}
	if got := ch.Get(name); got != want {
		t.Fatalf("unexpected value: got %v, want %v", got, want)
	}
}
Exemple #15
0
func TestCreate(t *testing.T) {
	ctx := tool.NewDefaultContext()

	// Setup a fake JIRI_ROOT instance.
	root, err := project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer func() {
		if err := root.Cleanup(ctx); err != nil {
			t.Fatalf("%v", err)
		}
	}()

	// Setup the initial remote and local projects.
	numProjects, remoteProjects := 2, []string{}
	for i := 0; i < numProjects; i++ {
		if err := root.CreateRemoteProject(ctx, remoteProjectName(i)); err != nil {
			t.Fatalf("%v", err)
		}
		if err := root.AddProject(ctx, project.Project{
			Name:   remoteProjectName(i),
			Path:   localProjectName(i),
			Remote: root.Projects[remoteProjectName(i)],
		}); err != nil {
			t.Fatalf("%v", err)
		}
	}

	// Point the JIRI_ROOT environment variable to the fake.
	oldRoot, err := project.JiriRoot()
	if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	// Create initial commits in the remote projects and use
	// UpdateUniverse() to mirror them locally.
	for i := 0; i < numProjects; i++ {
		writeReadme(t, ctx, root.Projects[remoteProjectName(i)], "revision 1")
	}
	if err := project.UpdateUniverse(ctx, true); err != nil {
		t.Fatalf("%v", err)
	}

	// Create a local snapshot.
	var stdout bytes.Buffer
	env := &cmdline.Env{Stdout: &stdout}
	remoteFlag = false
	if err := runSnapshotCreate(env, []string{"test-local"}); err != nil {
		t.Fatalf("%v", err)
	}

	// Remove the local project repositories.
	for i, _ := range remoteProjects {
		localProject := filepath.Join(root.Dir, localProjectName(i))
		if err := ctx.Run().RemoveAll(localProject); err != nil {
			t.Fatalf("%v", err)
		}
	}

	// Check that invoking the UpdateUniverse() with the local
	// snapshot restores the local repositories.
	snapshotDir, err := project.LocalSnapshotDir()
	if err != nil {
		t.Fatalf("%v", err)
	}
	snapshotFile := filepath.Join(snapshotDir, "test-local")
	localCtx := ctx.Clone(tool.ContextOpts{
		Manifest: &snapshotFile,
	})
	if err := project.UpdateUniverse(localCtx, true); err != nil {
		t.Fatalf("%v", err)
	}
	for i, _ := range remoteProjects {
		localProject := filepath.Join(root.Dir, localProjectName(i))
		checkReadme(t, ctx, localProject, "revision 1")
	}

	// Create a remote snapshot.
	remoteFlag = true
	root.EnableRemoteManifestPush(ctx)
	if err := runSnapshotCreate(env, []string{"test-remote"}); err != nil {
		t.Fatalf("%v", err)
	}

	// Remove the local project repositories.
	for i, _ := range remoteProjects {
		localProject := filepath.Join(root.Dir, localProjectName(i))
		if err := ctx.Run().RemoveAll(localProject); err != nil {
			t.Fatalf("%v", err)
		}
	}

	// Check that invoking the UpdateUniverse() with the remote snapshot
	// restores the local repositories.
	manifest := "snapshot/test-remote"
	remoteCtx := ctx.Clone(tool.ContextOpts{
		Manifest: &manifest,
	})
	if err := project.UpdateUniverse(remoteCtx, true); err != nil {
		t.Fatalf("%v", err)
	}
	for i, _ := range remoteProjects {
		localProject := filepath.Join(root.Dir, localProjectName(i))
		checkReadme(t, ctx, localProject, "revision 1")
	}
}
Exemple #16
0
func TestList(t *testing.T) {
	ctx := tool.NewDefaultContext()

	// Setup a fake JIRI_ROOT.
	root, err := project.NewFakeJiriRoot(ctx)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer func() {
		if err := root.Cleanup(ctx); err != nil {
			t.Fatalf("%v", err)
		}
	}()
	oldRoot, err := project.JiriRoot()
	if err := os.Setenv("JIRI_ROOT", root.Dir); err != nil {
		t.Fatalf("%v", err)
	}
	defer os.Setenv("JIRI_ROOT", oldRoot)

	remoteSnapshotDir, err := project.RemoteSnapshotDir()
	if err != nil {
		t.Fatalf("%v", err)
	}
	localSnapshotDir, err := project.LocalSnapshotDir()
	if err != nil {
		t.Fatalf("%v", err)
	}

	// Create a test suite.
	tests := []config{
		config{
			remote: false,
			dir:    localSnapshotDir,
		},
		config{
			remote: true,
			dir:    remoteSnapshotDir,
		},
	}
	labels := []label{
		label{
			name:      "beta",
			snapshots: []string{"beta-1", "beta-2", "beta-3"},
		},
		label{
			name:      "stable",
			snapshots: []string{"stable-1", "stable-2", "stable-3"},
		},
	}

	for _, test := range tests {
		remoteFlag = test.remote
		// Create the snapshots directory and populate it with the
		// data specified by the test suite.
		for _, label := range labels {
			createLabelDir(t, ctx, test.dir, label.name, label.snapshots)
		}

		// Check that running "jiri snapshot list" with no arguments
		// returns the expected output.
		var stdout bytes.Buffer
		env := &cmdline.Env{Stdout: &stdout}
		if err != nil {
			t.Fatalf("%v", err)
		}
		if err := runSnapshotList(env, nil); err != nil {
			t.Fatalf("%v", err)
		}
		got, want := stdout.String(), generateOutput(labels)
		if got != want {
			t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want)
		}

		// Check that running "jiri snapshot list" with one argument
		// returns the expected output.
		stdout.Reset()
		if err := runSnapshotList(env, []string{"stable"}); err != nil {
			t.Fatalf("%v", err)
		}
		got, want = stdout.String(), generateOutput(labels[1:])
		if got != want {
			t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want)
		}

		// Check that running "jiri snapshot list" with
		// multiple arguments returns the expected output.
		stdout.Reset()
		if err := runSnapshotList(env, []string{"beta", "stable"}); err != nil {
			t.Fatalf("%v", err)
		}
		got, want = stdout.String(), generateOutput(labels)
		if got != want {
			t.Fatalf("unexpected output:\ngot\n%v\nwant\n%v\n", got, want)
		}
	}
}
Exemple #17
0
func ExampleManager() {
	pdb := profiles.NewDB()
	myInstaller := "myProject"
	myProfile := "myNewProfile"
	profileName := profiles.QualifiedProfileName(myInstaller, myProfile)
	var target profiles.Target

	init := func() {
		mgr := newProfileMgr(myInstaller, myProfile)
		profilesmanager.Register(mgr)
		flags := flag.NewFlagSet("example", flag.ContinueOnError)
		profiles.RegisterTargetAndEnvFlags(flags, &target)
		flags.Parse([]string{"--target=arm-linux@1", "--env=A=B,C=D", "--env=E=F"})
	}
	init()

	profileRoot := jiri.NewRelPath("profiles")
	mgr := profilesmanager.LookupManager(profileName)
	if mgr == nil {
		panic("manager not found for: " + profileName)
	}

	jirix := &jiri.X{Context: tool.NewDefaultContext()}
	// Install myNewProfile for target.
	if err := mgr.Install(jirix, pdb, profileRoot, target); err != nil {
		panic("failed to find manager for: " + profileName)
	}

	fmt.Println(mgr.String())

	filename := tmpFile()
	defer os.RemoveAll(filepath.Dir(filename))

	if err := pdb.Write(jirix, "test", filename); err != nil {
		panic(err)
	}

	// Start with a new profile data base.
	pdb = profiles.NewDB()
	// Read the profile database.
	pdb.Read(jirix, filename)

	mgr = profilesmanager.LookupManager(profileName)
	if mgr == nil {
		panic("manager not found for: " + profileName)
	}
	fmt.Println(mgr.String())
	mgr.Uninstall(jirix, pdb, profileRoot, target)
	fmt.Println(mgr.String())
	fmt.Println(mgr.VersionInfo().Supported())
	fmt.Println(mgr.VersionInfo().Default())

	// Output:
	// Profile: myNewProfile: installed
	// [arm-linux@1]
	//
	// Profile: myNewProfile: installed
	// [arm-linux@1]
	//
	// Profile: myNewProfile: not installed
	//
	// [4 3 2]
	// 3
}