github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/featuretests/cmd_juju_register_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package featuretests 5 6 import ( 7 "io" 8 "regexp" 9 10 "github.com/juju/cmd" 11 "github.com/juju/cmd/cmdtesting" 12 "github.com/juju/loggo" 13 cookiejar "github.com/juju/persistent-cookiejar" 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 "gopkg.in/macaroon-bakery.v2-unstable/httpbakery" 17 18 "github.com/juju/juju/api" 19 "github.com/juju/juju/cmd/juju/commands" 20 "github.com/juju/juju/juju" 21 jujutesting "github.com/juju/juju/juju/testing" 22 "github.com/juju/juju/jujuclient" 23 ) 24 25 type cmdRegistrationSuite struct { 26 jujutesting.JujuConnSuite 27 } 28 29 func (s *cmdRegistrationSuite) TestAddUserAndRegister(c *gc.C) { 30 // First, add user "bob", and record the "juju register" command 31 // that is printed out. 32 33 context := run(c, nil, "add-user", "bob", "Bob Dobbs") 34 c.Check(cmdtesting.Stderr(context), gc.Equals, "") 35 stdout := cmdtesting.Stdout(context) 36 expectPat := ` 37 User "Bob Dobbs \(bob\)" added 38 Please send this command to bob: 39 juju register (.+) 40 41 "Bob Dobbs \(bob\)" has not been granted access to any models(.|\n)* 42 `[1:] 43 c.Assert(stdout, gc.Matches, expectPat) 44 45 arg := regexp.MustCompile("^" + expectPat + "$").FindStringSubmatch(stdout)[1] 46 c.Logf("juju register %q", arg) 47 48 // Now run the "juju register" command. We need to pass the 49 // controller name and password to set, and we need a different 50 // file store to mimic a different local OS user. 51 s.CreateUserHome(c, &jujutesting.UserHomeParams{ 52 Username: "bob", 53 }) 54 55 // The expected prompt does not include a warning about the controller 56 // name, as this new local user does not have a controller named 57 // "kontroll" registered. 58 prompter := cmdtesting.NewSeqPrompter(c, "»", ` 59 Enter a new password: »hunter2 60 61 Confirm password: »hunter2 62 63 Enter a name for this controller \[kontroll\]: »bob-controller 64 Initial password successfully set for bob. 65 66 Welcome, bob. You are now logged into "bob-controller". 67 68 There are no models available. (.|\n)* 69 `[1:]) 70 71 context = run(c, prompter, "register", arg) 72 prompter.AssertDone() 73 74 // Make sure that the saved server details are sufficient to connect 75 // to the api server. 76 jar, err := cookiejar.New(&cookiejar.Options{ 77 Filename: jujuclient.JujuCookiePath("bob-controller"), 78 }) 79 c.Assert(err, jc.ErrorIsNil) 80 dialOpts := api.DefaultDialOpts() 81 dialOpts.BakeryClient = httpbakery.NewClient() 82 dialOpts.BakeryClient.Jar = jar 83 accountDetails, err := s.ControllerStore.AccountDetails("bob-controller") 84 c.Assert(err, jc.ErrorIsNil) 85 api, err := juju.NewAPIConnection(juju.NewAPIConnectionParams{ 86 Store: s.ControllerStore, 87 ControllerName: "bob-controller", 88 AccountDetails: accountDetails, 89 DialOpts: dialOpts, 90 OpenAPI: api.Open, 91 }) 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(api.Close(), jc.ErrorIsNil) 94 } 95 96 // run runs a juju command with the given arguments. 97 // If stdio is given, it will be used for all input and output 98 // to the command; otherwise cmdtesting.Context will be used. 99 // 100 // It returns the context used to run the command. 101 func run(c *gc.C, stdio io.ReadWriter, args ...string) *cmd.Context { 102 var context *cmd.Context 103 if stdio != nil { 104 context = &cmd.Context{ 105 Dir: c.MkDir(), 106 Stdin: stdio, 107 Stdout: stdio, 108 Stderr: stdio, 109 } 110 } else { 111 context = cmdtesting.Context(c) 112 } 113 command := commands.NewJujuCommand(context) 114 c.Assert(cmdtesting.InitCommand(command, args), jc.ErrorIsNil) 115 err := command.Run(context) 116 c.Assert(err, jc.ErrorIsNil, gc.Commentf("stderr: %q", context.Stderr)) 117 loggo.RemoveWriter("warning") // remove logger added by main command 118 return context 119 }