// Test using a fake jiri root. func TestJiriFakeRoot(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() profilesDBDir := filepath.Join(fake.X.Root, jiri.ProfilesDBDir) pdb := profiles.NewDB() t1, err := profiles.NewTarget("cpu1-os1@1", "A=B,C=D") if err != nil { t.Fatal(err) } pdb.InstallProfile("test", "b", "") if err := pdb.AddProfileTarget("test", "b", t1); err != nil { t.Fatal(err) } if err := pdb.Write(fake.X, "test", profilesDBDir); err != nil { t.Fatal(err) } rd, err := profilesreader.NewReader(fake.X, profilesreader.UseProfiles, profilesDBDir) if err != nil { t.Fatal(err) } if got, want := rd.ProfileNames(), []string{"test:b"}; !reflect.DeepEqual(got, want) { t.Errorf("got %v, want %v", got, want) } dir, sh := buildJiri(t), gosh.NewShell(t) sh.Vars["JIRI_ROOT"] = fake.X.Root sh.Vars["PATH"] = envvar.PrependUniqueToken(sh.Vars["PATH"], ":", dir) run(sh, dir, "jiri", "profile", "list", "-v") }
func TestWrite(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) // test for no version being set. t1, _ := profiles.NewTarget("cpu1-os1", "A=B,C=D") pdb.InstallProfile("test", "b", "") if err := pdb.AddProfileTarget("test", "b", t1); err != nil { t.Fatal(err) } if err := pdb.Write(jirix, "test", filename); err == nil || !strings.HasPrefix(err.Error(), "missing version for profile") { t.Fatalf("was expecing a missing version error, but got %v", err) } pdb.RemoveProfileTarget("test", "b", t1) addProfileAndTargets(t, pdb, "b") addProfileAndTargets(t, pdb, "a") if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } 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) } }
func TestReadDir(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/old_version"); err == nil || !strings.Contains(err.Error(), "files must be at version") { t.Fatalf("missing or wrong error: %v", err) } if err := pdb.Read(jirix, "./testdata/mismatched_versions"); err == nil || !strings.Contains(err.Error(), "files must have the same version") { t.Fatalf("missing or wrong error: %v", err) } if err := pdb.Read(jirix, "./testdata/db_dir"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"m1:a", "m1:b", "m2:a", "m2:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } profile := pdb.LookupProfile("m2", "a") if got, want := profile.Root(), "root"; got != want { t.Errorf("got %v, want %v", got, want) } profile = pdb.LookupProfile("m1", "b") if got, want := profile.Root(), "r1"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestReadingV3AndV4(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() for i, c := range []struct { filename, prefix, variable string version profiles.Version }{ {"v3.xml", "", "", profiles.V3}, {"v4.xml", fake.X.Root, "${JIRI_ROOT}", profiles.V4}, } { pdb := profiles.NewDB() err := pdb.Read(fake.X, filepath.Join("testdata", c.filename)) if err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), c.version; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } target, err := profiles.NewTarget("cpu1-os1@1", "") if err != nil { t.Fatal(err) } p := pdb.LookupProfile("", "a") // We need to expand the variable here for a V4 profile if we want // to get the full absolute path. if got, want := p.Root(), c.variable+"/an/absolute/root"; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } lt := pdb.LookupProfileTarget("", "a", target) if got, want := lt.InstallationDir, c.variable+"/an/absolute/dir"; got != want { t.Errorf("%d: got %v, want %v", i, got, want) } } }
func TestInstallProfile(t *testing.T) { pdb := profiles.NewDB() pdb.InstallProfile("test", "a", "root1") pdb.InstallProfile("test", "a", "root2") profile := pdb.LookupProfile("test", "a") if got, want := profile.Root(), "root1"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestReadingV0(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/legacy.xml"); err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), profiles.Original; got != want { t.Errorf("got %v, want %v", got, want) } oprofiles := pdb.Profiles() 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("cpu-os@1") pdb.InstallProfile("", "__first", "") if err := pdb.AddProfileTarget("", "__first", t1); err != nil { t.Fatal(err) } if err := pdb.Write(jirix, "", filename); err != nil { t.Fatal(err) } if err := pdb.Read(jirix, filename); err != nil { t.Fatal(err) } if got, want := pdb.SchemaVersion(), profiles.V5; got != want { t.Errorf("got %v, want %v", got, want) } nprofiles := pdb.Profiles() if got, want := len(nprofiles), 6; got != want { t.Fatalf("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) } } }
// installedProfileManagers creates a profileManager for all installed // profiles, whether in this process or in a sub command. func installedProfileManagers(jirix *jiri.X, dbpath string, args []string) ([]profileManager, *profiles.DB, error) { db := profiles.NewDB() if err := db.Read(jirix, dbpath); err != nil { fmt.Fprintf(jirix.Stderr(), "Failed to read profiles database %q: %v\n", dbpath, err) return nil, nil, err } mgrs := []profileManager{} names := args if len(names) == 0 { names = db.Names() } for _, name := range names { mgrs = append(mgrs, newProfileManager(name, db)) } return mgrs, db, nil }
func TestEnvFromTarget(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() pdb := profiles.NewDB() pdb.InstallProfile("test", "a", "root") pdb.InstallProfile("test", "b", "root") t1, t2 := &profiles.Target{}, &profiles.Target{} t1.Set("cpu1-os1@1") t1.Env.Set("A=B C=D,B=C Z=Z") t2.Set("cpu1-os1@1") t2.Env.Set("A=Z,B=Z,Z=Z1") pdb.AddProfileTarget("test", "a", *t1) pdb.AddProfileTarget("test", "b", *t2) pdb.Write(fake.X, "test", "profile-manifest") filename := filepath.Join(fake.X.Root, "profile-manifest") if err := pdb.Write(fake.X, "test", filename); err != nil { t.Fatal(err) } rd, err := profilesreader.NewReader(fake.X, profilesreader.UseProfiles, filename) if err != nil { t.Fatal(err) } rd.Vars = envvar.VarsFromSlice([]string{}) t1Target, err := profiles.NewTarget("cpu1-os1@1", "") if err != nil { t.Fatal(err) } rd.MergeEnvFromProfiles(map[string]profilesreader.MergePolicy{ "A": profilesreader.AppendFlag, "B": profilesreader.UseLast, "Z": profilesreader.IgnoreBaseUseLast}, t1Target, "test::a", "test::b") vars := rd.ToMap() if got, want := len(vars), 3; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := rd.Get("A"), "B C=D Z"; got != want { t.Errorf("got %v, want %v", got, want) } if got, want := rd.Get("B"), "Z"; got != want { t.Errorf("got %v, want %v", got, want) } }
func TestRead(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/m1.xml"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"test:a", "test:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } p := pdb.LookupProfile("test", "a") 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) } }
func TestWriteDir(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() dir, err := ioutil.TempDir("", "pdb-") if err != nil { t.Fatal(err) } t1, _ := profiles.NewTarget("cpu1-os1@1", "A=B,C=D") pdb.InstallProfile("i1", "b", "") if err := pdb.AddProfileTarget("i1", "b", t1); err != nil { t.Fatal(err) } t2, _ := profiles.NewTarget("cpu1-os1@2", "A=B,C=D") pdb.InstallProfile("i2", "b", "") if err := pdb.AddProfileTarget("i2", "b", t2); err != nil { t.Fatal(err) } // Write out i1 installer's data if err := pdb.Write(jirix, "i1", dir); err != nil { t.Fatal(err) } if f := filepath.Join(dir, "i1"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } if f := filepath.Join(dir, "i2"); exists(t, f) { t.Errorf("%s exists", f) } // Write out i2 installer's data if err := pdb.Write(jirix, "i2", dir); err != nil { t.Fatal(err) } if f := filepath.Join(dir, "i1"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } if f := filepath.Join(dir, "i2"); !exists(t, f) { t.Errorf("%s doesn't exist", f) } }
func TestReadDirWithPrevFiles(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() if err := pdb.Read(jirix, "./testdata/db_dir_with_prev"); err != nil { t.Fatal(err) } names := pdb.Names() if got, want := names, []string{"m1:a", "m2:b"}; !reflect.DeepEqual(got, want) { t.Fatalf("got %v, want %v", got, want) } profile := pdb.LookupProfile("m1", "a") if got, want := profile.Root(), "root"; got != want { t.Errorf("got %v, want %v", got, want) } profile = pdb.LookupProfile("m2", "b") if got, want := profile.Root(), "r2"; got != want { t.Errorf("got %v, want %v", got, want) } }
// NewReader creates a new profiles reader. If path is of non-zero // length then that path will be read as a profiles database, if not, the // existing, if any, in-memory profiles information will be used. If SkipProfiles // is specified for profilesMode, then no profiles are used. func NewReader(jirix *jiri.X, profilesMode ProfilesMode, path string) (*Reader, error) { pdb := profiles.NewDB() if profilesMode == UseProfiles && len(path) > 0 { if err := pdb.Read(jirix, path); err != nil { return nil, err } } rd := &Reader{ jirix: jirix, path: path, profilesMode: bool(profilesMode), pdb: pdb, } rd.Vars = envvar.VarsFromOS() if profilesMode == SkipProfiles { return rd, nil } if len(os.Getenv("JIRI_PROFILE")) > 0 { return nil, fmt.Errorf(`old style profiles are no longer supported. Please do not set JIRI_PROFILE.`) } return rd, nil }
func TestWriteAndRename(t *testing.T) { pdb := profiles.NewDB() jirix, cleanup := jiritest.NewX(t) defer cleanup() filename := tmpFile() defer os.RemoveAll(filepath.Dir(filename)) addProfileAndTargets(t, pdb, "b") if exists(t, filename) { t.Fatalf("%q exists", filename) } if exists(t, filename+".prev") { t.Fatalf("%q exists", filename+".prev") } if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } if !exists(t, filename) { t.Fatalf("%q exists", filename) } if exists(t, filename+".prev") { t.Fatalf("%q exists", filename+".prev") } if err := pdb.Write(jirix, "test", filename); err != nil { t.Fatal(err) } if !exists(t, filename) { t.Fatalf("%q does not exist", filename) } if !exists(t, filename+".prev") { t.Fatalf("%q does not exist", filename+".prev") } }
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 }