github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/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  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/loggo"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/api"
    16  	"github.com/juju/juju/cmd/juju/commands"
    17  	"github.com/juju/juju/juju"
    18  	jujutesting "github.com/juju/juju/juju/testing"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  type cmdRegistrationSuite struct {
    23  	jujutesting.JujuConnSuite
    24  }
    25  
    26  func (s *cmdRegistrationSuite) run(c *gc.C, stdin io.Reader, args ...string) *cmd.Context {
    27  	context := testing.Context(c)
    28  	if stdin != nil {
    29  		context.Stdin = stdin
    30  	}
    31  	command := commands.NewJujuCommand(context)
    32  	c.Assert(testing.InitCommand(command, args), jc.ErrorIsNil)
    33  	c.Assert(command.Run(context), jc.ErrorIsNil)
    34  	loggo.RemoveWriter("warning") // remove logger added by main command
    35  	return context
    36  }
    37  
    38  func (s *cmdRegistrationSuite) TestAddUserAndRegister(c *gc.C) {
    39  	// First, add user "bob", and record the "juju register" command
    40  	// that is printed out.
    41  	context := s.run(c, nil, "add-user", "bob", "Bob Dobbs")
    42  	c.Check(testing.Stderr(context), gc.Equals, "")
    43  	stdout := testing.Stdout(context)
    44  	c.Check(stdout, gc.Matches, `
    45  User "Bob Dobbs \(bob\)" added
    46  Please send this command to bob:
    47      juju register .*
    48  
    49  "Bob Dobbs \(bob\)" has not been granted access to any models(.|\n)*
    50  `[1:])
    51  	jujuRegisterCommand := strings.Fields(strings.TrimSpace(
    52  		strings.SplitN(stdout[strings.Index(stdout, "juju register"):], "\n", 2)[0],
    53  	))
    54  	c.Logf("%q", jujuRegisterCommand)
    55  
    56  	// Now run the "juju register" command. We need to pass the
    57  	// controller name and password to set.
    58  	stdin := strings.NewReader("bob-controller\nhunter2\nhunter2\n")
    59  	args := jujuRegisterCommand[1:] // drop the "juju"
    60  	context = s.run(c, stdin, args...)
    61  	c.Check(testing.Stdout(context), gc.Equals, "")
    62  	c.Check(testing.Stderr(context), gc.Equals, `
    63  Please set a name for this controller: 
    64  Enter password: 
    65  Confirm password: 
    66  
    67  Welcome, bob. You are now logged into "bob-controller".
    68  
    69  There are no models available. You can add models with
    70  "juju add-model", or you can ask an administrator or owner
    71  of a model to grant access to that model with "juju grant".
    72  
    73  `[1:])
    74  
    75  	// Make sure that the saved server details are sufficient to connect
    76  	// to the api server.
    77  	accountDetails, err := s.ControllerStore.AccountByName("bob-controller", "bob@local")
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	api, err := juju.NewAPIConnection(juju.NewAPIConnectionParams{
    80  		Store:           s.ControllerStore,
    81  		ControllerName:  "bob-controller",
    82  		AccountDetails:  accountDetails,
    83  		BootstrapConfig: noBootstrapConfig,
    84  		DialOpts:        api.DefaultDialOpts(),
    85  	})
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	c.Assert(api.Close(), jc.ErrorIsNil)
    88  }