github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/controller/listmodels_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_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  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/api/base"
    15  	"github.com/juju/juju/apiserver/common"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/cmd/juju/controller"
    18  	"github.com/juju/juju/jujuclient"
    19  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    20  	"github.com/juju/juju/status"
    21  	"github.com/juju/juju/testing"
    22  )
    23  
    24  type ModelsSuite struct {
    25  	testing.FakeJujuXDGDataHomeSuite
    26  	api   *fakeModelMgrAPIClient
    27  	store *jujuclienttesting.MemStore
    28  }
    29  
    30  var _ = gc.Suite(&ModelsSuite{})
    31  
    32  type fakeModelMgrAPIClient struct {
    33  	err          error
    34  	user         string
    35  	models       []base.UserModel
    36  	all          bool
    37  	inclMachines bool
    38  }
    39  
    40  func (f *fakeModelMgrAPIClient) Close() error {
    41  	return nil
    42  }
    43  
    44  func (f *fakeModelMgrAPIClient) ListModels(user string) ([]base.UserModel, error) {
    45  	if f.err != nil {
    46  		return nil, f.err
    47  	}
    48  
    49  	f.user = user
    50  	return f.models, nil
    51  }
    52  
    53  func (f *fakeModelMgrAPIClient) AllModels() ([]base.UserModel, error) {
    54  	if f.err != nil {
    55  		return nil, f.err
    56  	}
    57  	f.all = true
    58  	return f.models, nil
    59  }
    60  
    61  func (f *fakeModelMgrAPIClient) ModelInfo(tags []names.ModelTag) ([]params.ModelInfoResult, error) {
    62  	results := make([]params.ModelInfoResult, len(tags))
    63  	for i, tag := range tags {
    64  		for _, model := range f.models {
    65  			if model.UUID != tag.Id() {
    66  				continue
    67  			}
    68  			result := &params.ModelInfo{
    69  				Name:     model.Name,
    70  				UUID:     model.UUID,
    71  				OwnerTag: names.NewUserTag(model.Owner).String(),
    72  				CloudTag: "cloud-dummy",
    73  			}
    74  			switch model.Name {
    75  			case "test-model1":
    76  				last1 := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    77  				result.Status.Status = status.Active
    78  				if f.user != "" {
    79  					result.Users = []params.ModelUserInfo{{
    80  						UserName:       f.user,
    81  						LastConnection: &last1,
    82  						Access:         params.ModelReadAccess,
    83  					}}
    84  				}
    85  				if f.inclMachines {
    86  					one := uint64(1)
    87  					result.Machines = []params.ModelMachineInfo{
    88  						{Id: "0", Hardware: &params.MachineHardware{Cores: &one}}, {Id: "1"},
    89  					}
    90  				}
    91  			case "test-model2":
    92  				last2 := time.Date(2015, 3, 1, 0, 0, 0, 0, time.UTC)
    93  				result.Status.Status = status.Active
    94  				if f.user != "" {
    95  					result.Users = []params.ModelUserInfo{{
    96  						UserName:       f.user,
    97  						LastConnection: &last2,
    98  						Access:         params.ModelWriteAccess,
    99  					}}
   100  				}
   101  			case "test-model3":
   102  				result.Status.Status = status.Destroying
   103  			}
   104  			results[i].Result = result
   105  		}
   106  	}
   107  	return results, nil
   108  }
   109  
   110  func (s *ModelsSuite) SetUpTest(c *gc.C) {
   111  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
   112  
   113  	models := []base.UserModel{
   114  		{
   115  			Name:  "test-model1",
   116  			Owner: "admin@local",
   117  			UUID:  "test-model1-UUID",
   118  		}, {
   119  			Name:  "test-model2",
   120  			Owner: "carlotta@local",
   121  			UUID:  "test-model2-UUID",
   122  		}, {
   123  			Name:  "test-model3",
   124  			Owner: "daiwik@external",
   125  			UUID:  "test-model3-UUID",
   126  		},
   127  	}
   128  	s.api = &fakeModelMgrAPIClient{
   129  		models: models,
   130  		user:   "admin@local",
   131  	}
   132  	s.store = jujuclienttesting.NewMemStore()
   133  	s.store.CurrentControllerName = "fake"
   134  	s.store.Controllers["fake"] = jujuclient.ControllerDetails{}
   135  	s.store.Models["fake"] = &jujuclient.ControllerModels{
   136  		CurrentModel: "admin@local/test-model1",
   137  	}
   138  	s.store.Accounts["fake"] = jujuclient.AccountDetails{
   139  		User:     "admin@local",
   140  		Password: "password",
   141  	}
   142  }
   143  
   144  func (s *ModelsSuite) newCommand() cmd.Command {
   145  	return controller.NewListModelsCommandForTest(s.api, s.api, s.store)
   146  }
   147  
   148  func (s *ModelsSuite) TestModelsOwner(c *gc.C) {
   149  	context, err := testing.RunCommand(c, s.newCommand())
   150  	c.Assert(err, jc.ErrorIsNil)
   151  	c.Assert(s.api.user, gc.Equals, "admin@local")
   152  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   153  		"CONTROLLER: fake\n"+
   154  		"\n"+
   155  		"MODEL                        OWNER            STATUS      ACCESS  LAST CONNECTION\n"+
   156  		"test-model1*                 admin@local      active      read    2015-03-20\n"+
   157  		"carlotta/test-model2         carlotta@local   active      write   2015-03-01\n"+
   158  		"daiwik@external/test-model3  daiwik@external  destroying          never connected\n"+
   159  		"\n")
   160  }
   161  
   162  func (s *ModelsSuite) TestModelsNonOwner(c *gc.C) {
   163  	context, err := testing.RunCommand(c, s.newCommand(), "--user", "bob")
   164  	c.Assert(err, jc.ErrorIsNil)
   165  	c.Assert(s.api.user, gc.Equals, "bob")
   166  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   167  		"CONTROLLER: fake\n"+
   168  		"\n"+
   169  		"MODEL                        OWNER            STATUS      ACCESS  LAST CONNECTION\n"+
   170  		"admin/test-model1*           admin@local      active      read    2015-03-20\n"+
   171  		"carlotta/test-model2         carlotta@local   active      write   2015-03-01\n"+
   172  		"daiwik@external/test-model3  daiwik@external  destroying          never connected\n"+
   173  		"\n")
   174  }
   175  
   176  func (s *ModelsSuite) TestAllModels(c *gc.C) {
   177  	context, err := testing.RunCommand(c, s.newCommand(), "--all")
   178  	c.Assert(err, jc.ErrorIsNil)
   179  	c.Assert(s.api.all, jc.IsTrue)
   180  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   181  		"CONTROLLER: fake\n"+
   182  		"\n"+
   183  		"MODEL                        OWNER            STATUS      ACCESS  LAST CONNECTION\n"+
   184  		"admin/test-model1*           admin@local      active      read    2015-03-20\n"+
   185  		"carlotta/test-model2         carlotta@local   active      write   2015-03-01\n"+
   186  		"daiwik@external/test-model3  daiwik@external  destroying          never connected\n"+
   187  		"\n")
   188  }
   189  
   190  func (s *ModelsSuite) TestAllModelsNoneCurrent(c *gc.C) {
   191  	delete(s.store.Models, "fake")
   192  	context, err := testing.RunCommand(c, s.newCommand())
   193  	c.Assert(err, jc.ErrorIsNil)
   194  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   195  		"CONTROLLER: fake\n"+
   196  		"\n"+
   197  		"MODEL                        OWNER            STATUS      ACCESS  LAST CONNECTION\n"+
   198  		"test-model1                  admin@local      active      read    2015-03-20\n"+
   199  		"carlotta/test-model2         carlotta@local   active      write   2015-03-01\n"+
   200  		"daiwik@external/test-model3  daiwik@external  destroying          never connected\n"+
   201  		"\n")
   202  }
   203  
   204  func (s *ModelsSuite) TestModelsUUID(c *gc.C) {
   205  	s.api.inclMachines = true
   206  	context, err := testing.RunCommand(c, s.newCommand(), "--uuid")
   207  	c.Assert(err, jc.ErrorIsNil)
   208  	c.Assert(s.api.user, gc.Equals, "admin@local")
   209  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   210  		"CONTROLLER: fake\n"+
   211  		"\n"+
   212  		"MODEL                        UUID              OWNER            STATUS      MACHINES  CORES  ACCESS  LAST CONNECTION\n"+
   213  		"test-model1*                 test-model1-UUID  admin@local      active             2      1  read    2015-03-20\n"+
   214  		"carlotta/test-model2         test-model2-UUID  carlotta@local   active             0      -  write   2015-03-01\n"+
   215  		"daiwik@external/test-model3  test-model3-UUID  daiwik@external  destroying         0      -          never connected\n"+
   216  		"\n")
   217  }
   218  
   219  func (s *ModelsSuite) TestModelsMachineInfo(c *gc.C) {
   220  	s.api.inclMachines = true
   221  	context, err := testing.RunCommand(c, s.newCommand())
   222  	c.Assert(err, jc.ErrorIsNil)
   223  	c.Assert(s.api.user, gc.Equals, "admin@local")
   224  	c.Assert(testing.Stdout(context), gc.Equals, ""+
   225  		"CONTROLLER: fake\n"+
   226  		"\n"+
   227  		"MODEL                        OWNER            STATUS      MACHINES  CORES  ACCESS  LAST CONNECTION\n"+
   228  		"test-model1*                 admin@local      active             2      1  read    2015-03-20\n"+
   229  		"carlotta/test-model2         carlotta@local   active             0      -  write   2015-03-01\n"+
   230  		"daiwik@external/test-model3  daiwik@external  destroying         0      -          never connected\n"+
   231  		"\n")
   232  }
   233  
   234  func (s *ModelsSuite) TestUnrecognizedArg(c *gc.C) {
   235  	_, err := testing.RunCommand(c, s.newCommand(), "whoops")
   236  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   237  }
   238  
   239  func (s *ModelsSuite) TestModelsError(c *gc.C) {
   240  	s.api.err = common.ErrPerm
   241  	_, err := testing.RunCommand(c, s.newCommand())
   242  	c.Assert(err, gc.ErrorMatches, "cannot list models: permission denied")
   243  }