github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/profiles/profilesmanager/manager_test.go (about)

     1  // Copyright 2015 The Vanadium Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package profilesmanager_test
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"v.io/jiri"
    15  	"v.io/jiri/profiles"
    16  	"v.io/jiri/profiles/profilesmanager"
    17  	"v.io/jiri/tool"
    18  )
    19  
    20  type myNewProfileMgr struct {
    21  	installer, name, root string
    22  	versionInfo           *profiles.VersionInfo
    23  	profile               *profiles.Profile
    24  }
    25  
    26  func newProfileMgr(installer, name string) *myNewProfileMgr {
    27  	supported := map[string]interface{}{
    28  		"2": nil,
    29  		"4": nil,
    30  		"3": nil,
    31  	}
    32  	return &myNewProfileMgr{
    33  		installer:   installer,
    34  		name:        name,
    35  		versionInfo: profiles.NewVersionInfo("test", supported, "3"),
    36  	}
    37  }
    38  
    39  func (p *myNewProfileMgr) Name() string {
    40  	return p.name
    41  }
    42  
    43  func (p *myNewProfileMgr) Installer() string {
    44  	return p.installer
    45  }
    46  
    47  func (p *myNewProfileMgr) Info() string {
    48  	return `
    49  The myNewProfile is for testing purposes only
    50  `
    51  }
    52  
    53  func (p *myNewProfileMgr) VersionInfo() *profiles.VersionInfo {
    54  	return p.versionInfo
    55  }
    56  
    57  func (p *myNewProfileMgr) String() string {
    58  	if p.profile == nil {
    59  		return fmt.Sprintf("Profile: %s: not installed\n", p.name)
    60  	}
    61  	return fmt.Sprintf("Profile: %s: installed\n%s\n", p.name, p.profile.Targets())
    62  }
    63  
    64  func (p *myNewProfileMgr) AddFlags(*flag.FlagSet, profiles.Action) {
    65  }
    66  
    67  func (p *myNewProfileMgr) OSPackages(jirix *jiri.X, pdb *profiles.DB, root jiri.RelPath, target profiles.Target) ([]string, error) {
    68  	return nil, nil
    69  }
    70  
    71  func (p *myNewProfileMgr) Install(jirix *jiri.X, pdb *profiles.DB, root jiri.RelPath, target profiles.Target) error {
    72  	p.profile = pdb.InstallProfile(p.name, "", "root")
    73  	return pdb.AddProfileTarget(p.name, "", target)
    74  }
    75  
    76  func (p *myNewProfileMgr) Uninstall(jirix *jiri.X, pdb *profiles.DB, root jiri.RelPath, target profiles.Target) error {
    77  	if pdb.RemoveProfileTarget(p.name, "", target) {
    78  		p.profile = nil
    79  	}
    80  	return nil
    81  }
    82  
    83  func tmpFile() string {
    84  	dirname, err := ioutil.TempDir("", "pdb")
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	return filepath.Join(dirname, "manifest")
    89  }
    90  
    91  func ExampleManager() {
    92  	pdb := profiles.NewDB()
    93  	myInstaller := "myProject"
    94  	myProfile := "myNewProfile"
    95  	profileName := profiles.QualifiedProfileName(myInstaller, myProfile)
    96  	var target profiles.Target
    97  
    98  	init := func() {
    99  		mgr := newProfileMgr(myInstaller, myProfile)
   100  		profilesmanager.Register(mgr)
   101  		flags := flag.NewFlagSet("example", flag.ContinueOnError)
   102  		profiles.RegisterTargetAndEnvFlags(flags, &target)
   103  		flags.Parse([]string{"--target=arm-linux@1", "--env=A=B,C=D", "--env=E=F"})
   104  	}
   105  	init()
   106  
   107  	profileRoot := jiri.NewRelPath("profiles")
   108  	mgr := profilesmanager.LookupManager(profileName)
   109  	if mgr == nil {
   110  		panic("manager not found for: " + profileName)
   111  	}
   112  
   113  	jirix := &jiri.X{Context: tool.NewDefaultContext()}
   114  	// Install myNewProfile for target.
   115  	if err := mgr.Install(jirix, pdb, profileRoot, target); err != nil {
   116  		panic("failed to find manager for: " + profileName)
   117  	}
   118  
   119  	fmt.Println(mgr.String())
   120  
   121  	filename := tmpFile()
   122  	defer os.RemoveAll(filepath.Dir(filename))
   123  
   124  	if err := pdb.Write(jirix, "test", filename); err != nil {
   125  		panic(err)
   126  	}
   127  
   128  	// Start with a new profile data base.
   129  	pdb = profiles.NewDB()
   130  	// Read the profile database.
   131  	pdb.Read(jirix, filename)
   132  
   133  	mgr = profilesmanager.LookupManager(profileName)
   134  	if mgr == nil {
   135  		panic("manager not found for: " + profileName)
   136  	}
   137  	fmt.Println(mgr.String())
   138  	mgr.Uninstall(jirix, pdb, profileRoot, target)
   139  	fmt.Println(mgr.String())
   140  	fmt.Println(mgr.VersionInfo().Supported())
   141  	fmt.Println(mgr.VersionInfo().Default())
   142  
   143  	// Output:
   144  	// Profile: myNewProfile: installed
   145  	// [arm-linux@1]
   146  	//
   147  	// Profile: myNewProfile: installed
   148  	// [arm-linux@1]
   149  	//
   150  	// Profile: myNewProfile: not installed
   151  	//
   152  	// [4 3 2]
   153  	// 3
   154  }