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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package system_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/cmd/envcmd"
    16  	"github.com/juju/juju/cmd/juju/system"
    17  	"github.com/juju/juju/environs/configstore"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type EnvironmentsSuite struct {
    22  	testing.FakeJujuHomeSuite
    23  	api   *fakeEnvMgrAPIClient
    24  	creds *configstore.APICredentials
    25  }
    26  
    27  var _ = gc.Suite(&EnvironmentsSuite{})
    28  
    29  type fakeEnvMgrAPIClient struct {
    30  	err  error
    31  	user string
    32  	envs []base.UserEnvironment
    33  	all  bool
    34  }
    35  
    36  func (f *fakeEnvMgrAPIClient) Close() error {
    37  	return nil
    38  }
    39  
    40  func (f *fakeEnvMgrAPIClient) ListEnvironments(user string) ([]base.UserEnvironment, error) {
    41  	if f.err != nil {
    42  		return nil, f.err
    43  	}
    44  
    45  	f.user = user
    46  	return f.envs, nil
    47  }
    48  
    49  func (f *fakeEnvMgrAPIClient) AllEnvironments() ([]base.UserEnvironment, error) {
    50  	if f.err != nil {
    51  		return nil, f.err
    52  	}
    53  	f.all = true
    54  	return f.envs, nil
    55  }
    56  
    57  func (s *EnvironmentsSuite) SetUpTest(c *gc.C) {
    58  	s.FakeJujuHomeSuite.SetUpTest(c)
    59  
    60  	err := envcmd.WriteCurrentSystem("fake")
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    64  	last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC)
    65  
    66  	envs := []base.UserEnvironment{
    67  		{
    68  			Name:           "test-env1",
    69  			Owner:          "user-admin@local",
    70  			UUID:           "test-env1-UUID",
    71  			LastConnection: &last1,
    72  		}, {
    73  			Name:           "test-env2",
    74  			Owner:          "user-admin@local",
    75  			UUID:           "test-env2-UUID",
    76  			LastConnection: &last2,
    77  		}, {
    78  			Name:  "test-env3",
    79  			Owner: "user-admin@local",
    80  			UUID:  "test-env3-UUID",
    81  		},
    82  	}
    83  	s.api = &fakeEnvMgrAPIClient{envs: envs}
    84  	s.creds = &configstore.APICredentials{User: "admin@local", Password: "password"}
    85  }
    86  
    87  func (s *EnvironmentsSuite) newCommand() cmd.Command {
    88  	command := system.NewEnvironmentsCommand(s.api, s.api, s.creds)
    89  	return envcmd.WrapSystem(command)
    90  }
    91  
    92  func (s *EnvironmentsSuite) checkSuccess(c *gc.C, user string, args ...string) {
    93  	context, err := testing.RunCommand(c, s.newCommand(), args...)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	c.Assert(s.api.user, gc.Equals, user)
    96  	c.Assert(testing.Stdout(context), gc.Equals, ""+
    97  		"NAME       OWNER             LAST CONNECTION\n"+
    98  		"test-env1  user-admin@local  2015-03-20\n"+
    99  		"test-env2  user-admin@local  2015-03-01\n"+
   100  		"test-env3  user-admin@local  never connected\n"+
   101  		"\n")
   102  }
   103  
   104  func (s *EnvironmentsSuite) TestEnvironments(c *gc.C) {
   105  	s.checkSuccess(c, "admin@local")
   106  	s.checkSuccess(c, "bob", "--user", "bob")
   107  }
   108  
   109  func (s *EnvironmentsSuite) TestAllEnvironments(c *gc.C) {
   110  	context, err := testing.RunCommand(c, s.newCommand(), "--all")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(s.api.all, jc.IsTrue)
   113  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   114  		"NAME       OWNER             LAST CONNECTION\n"+
   115  		"test-env1  user-admin@local  2015-03-20\n"+
   116  		"test-env2  user-admin@local  2015-03-01\n"+
   117  		"test-env3  user-admin@local  never connected\n"+
   118  		"\n")
   119  }
   120  
   121  func (s *EnvironmentsSuite) TestEnvironmentsUUID(c *gc.C) {
   122  	context, err := testing.RunCommand(c, s.newCommand(), "--uuid")
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	c.Assert(s.api.user, gc.Equals, "admin@local")
   125  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   126  		"NAME       ENVIRONMENT UUID  OWNER             LAST CONNECTION\n"+
   127  		"test-env1  test-env1-UUID    user-admin@local  2015-03-20\n"+
   128  		"test-env2  test-env2-UUID    user-admin@local  2015-03-01\n"+
   129  		"test-env3  test-env3-UUID    user-admin@local  never connected\n"+
   130  		"\n")
   131  }
   132  
   133  func (s *EnvironmentsSuite) TestUnrecognizedArg(c *gc.C) {
   134  	_, err := testing.RunCommand(c, s.newCommand(), "whoops")
   135  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   136  }
   137  
   138  func (s *EnvironmentsSuite) TestEnvironmentsError(c *gc.C) {
   139  	s.api.err = common.ErrPerm
   140  	_, err := testing.RunCommand(c, s.newCommand())
   141  	c.Assert(err, gc.ErrorMatches, "cannot list environments: permission denied")
   142  }