github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/user/credentials_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cmd/envcmd"
    12  	"github.com/juju/juju/cmd/juju/user"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type CredentialsCommandSuite struct {
    17  	BaseSuite
    18  	serverFilename string
    19  }
    20  
    21  var _ = gc.Suite(&CredentialsCommandSuite{})
    22  
    23  func (s *CredentialsCommandSuite) SetUpTest(c *gc.C) {
    24  	s.BaseSuite.SetUpTest(c)
    25  	s.serverFilename = ""
    26  	s.PatchValue(user.ServerFileNotify, func(filename string) {
    27  		s.serverFilename = filename
    28  	})
    29  }
    30  
    31  func (s *CredentialsCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    32  	command := envcmd.WrapSystem(&user.CredentialsCommand{})
    33  	return testing.RunCommand(c, command, args...)
    34  }
    35  
    36  func (s *CredentialsCommandSuite) TestInit(c *gc.C) {
    37  	for i, test := range []struct {
    38  		args        []string
    39  		outPath     string
    40  		errorString string
    41  	}{
    42  		{
    43  		// no args is fine
    44  		}, {
    45  			args:    []string{"--output=foo.bar"},
    46  			outPath: "foo.bar",
    47  		}, {
    48  			args:    []string{"-o", "foo.bar"},
    49  			outPath: "foo.bar",
    50  		}, {
    51  			args:        []string{"foobar"},
    52  			errorString: `unrecognized args: \["foobar"\]`,
    53  		},
    54  	} {
    55  		c.Logf("test %d", i)
    56  		command := &user.CredentialsCommand{}
    57  		err := testing.InitCommand(command, test.args)
    58  		if test.errorString == "" {
    59  			c.Check(command.OutPath, gc.Equals, test.outPath)
    60  		} else {
    61  			c.Check(err, gc.ErrorMatches, test.errorString)
    62  		}
    63  	}
    64  }
    65  
    66  func (s *CredentialsCommandSuite) TestNoArgs(c *gc.C) {
    67  	context, err := s.run(c)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	// User and password are set in BaseSuite.SetUpTest.
    70  	s.assertServerFileMatches(c, s.serverFilename, "user-test", "password")
    71  	expected := `
    72  server file written to .*user-test.server
    73  `[1:]
    74  	c.Assert(testing.Stderr(context), gc.Matches, expected)
    75  }
    76  
    77  func (s *CredentialsCommandSuite) TestFilename(c *gc.C) {
    78  	context, err := s.run(c, "--output=testing.creds")
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	// User and password are set in BaseSuite.SetUpTest.
    81  	s.assertServerFileMatches(c, s.serverFilename, "user-test", "password")
    82  	expected := `
    83  server file written to .*testing.creds
    84  `[1:]
    85  	c.Assert(testing.Stderr(context), gc.Matches, expected)
    86  }