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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_test
     5  
     6  import (
     7  	"regexp"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/api/base"
    16  	"github.com/juju/juju/cmd/juju/controller"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    19  	"github.com/juju/juju/permission"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  type ShowControllerSuite struct {
    24  	baseControllerSuite
    25  	fakeController *fakeController
    26  	api            func(string) controller.ControllerAccessAPI
    27  }
    28  
    29  var _ = gc.Suite(&ShowControllerSuite{})
    30  
    31  func (s *ShowControllerSuite) SetUpTest(c *gc.C) {
    32  	s.baseControllerSuite.SetUpTest(c)
    33  	s.fakeController = &fakeController{
    34  		modelNames: map[string]string{
    35  			"abc": "controller",
    36  			"def": "my-model",
    37  			"ghi": "controller",
    38  		},
    39  		machines: map[string][]base.Machine{
    40  			"ghi": {
    41  				{Id: "0", InstanceId: "id-0", HasVote: false, WantsVote: true, Status: "active"},
    42  				{Id: "1", InstanceId: "id-1", HasVote: false, WantsVote: true, Status: "down"},
    43  				{Id: "2", InstanceId: "id-2", HasVote: true, WantsVote: true, Status: "active"},
    44  				{Id: "3", InstanceId: "id-3", HasVote: false, WantsVote: false, Status: "active"},
    45  			},
    46  		},
    47  	}
    48  	s.api = func(controllerNamee string) controller.ControllerAccessAPI {
    49  		s.fakeController.controllerName = controllerNamee
    50  		return s.fakeController
    51  	}
    52  }
    53  
    54  func (s *ShowControllerSuite) TestShowOneControllerOneInStore(c *gc.C) {
    55  	s.controllersYaml = `controllers:
    56    mallards:
    57      uuid: this-is-another-uuid
    58      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
    59      ca-cert: this-is-another-ca-cert
    60      cloud: mallards
    61      agent-version: 999.99.99
    62  `
    63  	s.fakeController.store = s.createTestClientStore(c)
    64  
    65  	s.expectedOutput = `
    66  mallards:
    67    details:
    68      uuid: this-is-another-uuid
    69      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
    70      ca-cert: this-is-another-ca-cert
    71      cloud: mallards
    72      agent-version: 999.99.99
    73    models:
    74      controller:
    75        uuid: abc
    76        machine-count: 2
    77        core-count: 4
    78      my-model:
    79        uuid: def
    80        machine-count: 2
    81        core-count: 4
    82    current-model: my-model
    83    account:
    84      user: admin@local
    85      access: superuser
    86  `[1:]
    87  
    88  	s.assertShowController(c, "mallards")
    89  }
    90  
    91  func (s *ShowControllerSuite) TestShowControllerWithPasswords(c *gc.C) {
    92  	s.controllersYaml = `controllers:
    93    mallards:
    94      uuid: this-is-another-uuid
    95      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
    96      ca-cert: this-is-another-ca-cert
    97      cloud: mallards
    98      agent-version: 999.99.99
    99  `
   100  	s.fakeController.store = s.createTestClientStore(c)
   101  
   102  	s.expectedOutput = `
   103  mallards:
   104    details:
   105      uuid: this-is-another-uuid
   106      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
   107      ca-cert: this-is-another-ca-cert
   108      cloud: mallards
   109      agent-version: 999.99.99
   110    models:
   111      controller:
   112        uuid: abc
   113        machine-count: 2
   114        core-count: 4
   115      my-model:
   116        uuid: def
   117        machine-count: 2
   118        core-count: 4
   119    current-model: my-model
   120    account:
   121      user: admin@local
   122      access: superuser
   123      password: hunter2
   124  `[1:]
   125  
   126  	s.assertShowController(c, "mallards", "--show-password")
   127  }
   128  
   129  func (s *ShowControllerSuite) TestShowControllerWithBootstrapConfig(c *gc.C) {
   130  	s.controllersYaml = `controllers:
   131    mallards:
   132      uuid: this-is-another-uuid
   133      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
   134      ca-cert: this-is-another-ca-cert
   135      cloud: mallards
   136      region: mallards1
   137      agent-version: 999.99.99
   138  `
   139  	store := s.createTestClientStore(c)
   140  	store.BootstrapConfig["mallards"] = jujuclient.BootstrapConfig{
   141  		Config: map[string]interface{}{
   142  			"name":  "controller",
   143  			"type":  "maas",
   144  			"extra": "value",
   145  		},
   146  		Credential:    "my-credential",
   147  		CloudType:     "maas",
   148  		Cloud:         "mallards",
   149  		CloudRegion:   "mallards1",
   150  		CloudEndpoint: "http://mallards.local/MAAS",
   151  	}
   152  	s.fakeController.store = store
   153  
   154  	s.expectedOutput = `
   155  mallards:
   156    details:
   157      uuid: this-is-another-uuid
   158      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
   159      ca-cert: this-is-another-ca-cert
   160      cloud: mallards
   161      region: mallards1
   162      agent-version: 999.99.99
   163    models:
   164      controller:
   165        uuid: abc
   166        machine-count: 2
   167        core-count: 4
   168      my-model:
   169        uuid: def
   170        machine-count: 2
   171        core-count: 4
   172    current-model: my-model
   173    account:
   174      user: admin@local
   175      access: superuser
   176  `[1:]
   177  
   178  	s.assertShowController(c, "mallards")
   179  }
   180  
   181  func (s *ShowControllerSuite) TestShowOneControllerManyInStore(c *gc.C) {
   182  	s.fakeController.store = s.createTestClientStore(c)
   183  
   184  	s.expectedOutput = `
   185  aws-test:
   186    details:
   187      uuid: this-is-the-aws-test-uuid
   188      api-endpoints: [this-is-aws-test-of-many-api-endpoints]
   189      ca-cert: this-is-aws-test-ca-cert
   190      cloud: aws
   191      region: us-east-1
   192      agent-version: 999.99.99
   193    controller-machines:
   194      "0":
   195        instance-id: id-0
   196        ha-status: ha-pending
   197      "1":
   198        instance-id: id-1
   199        ha-status: down, lost connection
   200      "2":
   201        instance-id: id-2
   202        ha-status: ha-enabled
   203    models:
   204      controller:
   205        uuid: ghi
   206        machine-count: 2
   207        core-count: 4
   208    current-model: controller
   209    account:
   210      user: admin@local
   211      access: superuser
   212  `[1:]
   213  	s.assertShowController(c, "aws-test")
   214  }
   215  
   216  func (s *ShowControllerSuite) TestShowSomeControllerMoreInStore(c *gc.C) {
   217  	s.fakeController.store = s.createTestClientStore(c)
   218  	s.expectedOutput = `
   219  aws-test:
   220    details:
   221      uuid: this-is-the-aws-test-uuid
   222      api-endpoints: [this-is-aws-test-of-many-api-endpoints]
   223      ca-cert: this-is-aws-test-ca-cert
   224      cloud: aws
   225      region: us-east-1
   226      agent-version: 999.99.99
   227    controller-machines:
   228      "0":
   229        instance-id: id-0
   230        ha-status: ha-pending
   231      "1":
   232        instance-id: id-1
   233        ha-status: down, lost connection
   234      "2":
   235        instance-id: id-2
   236        ha-status: ha-enabled
   237    models:
   238      controller:
   239        uuid: ghi
   240        machine-count: 2
   241        core-count: 4
   242    current-model: controller
   243    account:
   244      user: admin@local
   245      access: superuser
   246  mark-test-prodstack:
   247    details:
   248      uuid: this-is-a-uuid
   249      api-endpoints: [this-is-one-of-many-api-endpoints]
   250      ca-cert: this-is-a-ca-cert
   251      cloud: prodstack
   252      agent-version: 999.99.99
   253    account:
   254      user: admin@local
   255      access: superuser
   256  `[1:]
   257  
   258  	s.assertShowController(c, "aws-test", "mark-test-prodstack")
   259  }
   260  
   261  func (s *ShowControllerSuite) TestShowControllerJsonOne(c *gc.C) {
   262  	s.fakeController.store = s.createTestClientStore(c)
   263  
   264  	s.expectedOutput = `
   265  {"aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert","cloud":"aws","region":"us-east-1","agent-version":"999.99.99"},"controller-machines":{"0":{"instance-id":"id-0","ha-status":"ha-pending"},"1":{"instance-id":"id-1","ha-status":"down, lost connection"},"2":{"instance-id":"id-2","ha-status":"ha-enabled"}},"models":{"controller":{"uuid":"ghi","machine-count":2,"core-count":4}},"current-model":"controller","account":{"user":"admin@local","access":"superuser"}}}
   266  `[1:]
   267  
   268  	s.assertShowController(c, "--format", "json", "aws-test")
   269  }
   270  
   271  func (s *ShowControllerSuite) TestShowControllerJsonMany(c *gc.C) {
   272  	s.fakeController.store = s.createTestClientStore(c)
   273  	s.expectedOutput = `
   274  {"aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert","cloud":"aws","region":"us-east-1","agent-version":"999.99.99"},"controller-machines":{"0":{"instance-id":"id-0","ha-status":"ha-pending"},"1":{"instance-id":"id-1","ha-status":"down, lost connection"},"2":{"instance-id":"id-2","ha-status":"ha-enabled"}},"models":{"controller":{"uuid":"ghi","machine-count":2,"core-count":4}},"current-model":"controller","account":{"user":"admin@local","access":"superuser"}},"mark-test-prodstack":{"details":{"uuid":"this-is-a-uuid","api-endpoints":["this-is-one-of-many-api-endpoints"],"ca-cert":"this-is-a-ca-cert","cloud":"prodstack","agent-version":"999.99.99"},"account":{"user":"admin@local","access":"superuser"}}}
   275  `[1:]
   276  	s.assertShowController(c, "--format", "json", "aws-test", "mark-test-prodstack")
   277  }
   278  
   279  func (s *ShowControllerSuite) TestShowControllerReadFromStoreErr(c *gc.C) {
   280  	s.fakeController.store = s.createTestClientStore(c)
   281  
   282  	msg := "fail getting controller"
   283  	errStore := jujuclienttesting.NewStubStore()
   284  	errStore.SetErrors(errors.New(msg))
   285  	s.store = errStore
   286  	s.expectedErr = msg
   287  
   288  	s.assertShowControllerFailed(c, "test1")
   289  	errStore.CheckCallNames(c, "ControllerByName")
   290  }
   291  
   292  func (s *ShowControllerSuite) TestShowControllerNoArgs(c *gc.C) {
   293  	store := s.createTestClientStore(c)
   294  	s.fakeController.store = store
   295  
   296  	s.expectedOutput = `
   297  {"aws-test":{"details":{"uuid":"this-is-the-aws-test-uuid","api-endpoints":["this-is-aws-test-of-many-api-endpoints"],"ca-cert":"this-is-aws-test-ca-cert","cloud":"aws","region":"us-east-1","agent-version":"999.99.99"},"controller-machines":{"0":{"instance-id":"id-0","ha-status":"ha-pending"},"1":{"instance-id":"id-1","ha-status":"down, lost connection"},"2":{"instance-id":"id-2","ha-status":"ha-enabled"}},"models":{"controller":{"uuid":"ghi","machine-count":2,"core-count":4}},"current-model":"controller","account":{"user":"admin@local","access":"superuser"}}}
   298  `[1:]
   299  	store.CurrentControllerName = "aws-test"
   300  	s.assertShowController(c, "--format", "json")
   301  }
   302  
   303  func (s *ShowControllerSuite) TestShowControllerNoArgsNoCurrent(c *gc.C) {
   304  	store := s.createTestClientStore(c)
   305  	store.CurrentControllerName = ""
   306  	s.expectedErr = regexp.QuoteMeta(`there is no active controller`)
   307  	s.assertShowControllerFailed(c)
   308  }
   309  
   310  func (s *ShowControllerSuite) TestShowControllerNotFound(c *gc.C) {
   311  	s.createTestClientStore(c)
   312  
   313  	s.expectedErr = `controller whoops not found`
   314  	s.assertShowControllerFailed(c, "whoops")
   315  }
   316  
   317  func (s *ShowControllerSuite) TestShowControllerUnrecognizedFlag(c *gc.C) {
   318  	s.expectedErr = `flag provided but not defined: -m`
   319  	s.assertShowControllerFailed(c, "-m", "my.world")
   320  }
   321  
   322  func (s *ShowControllerSuite) TestShowControllerUnrecognizedOptionFlag(c *gc.C) {
   323  	s.expectedErr = `flag provided but not defined: --model`
   324  	s.assertShowControllerFailed(c, "--model", "still.my.world")
   325  }
   326  
   327  func (s *ShowControllerSuite) runShowController(c *gc.C, args ...string) (*cmd.Context, error) {
   328  	return testing.RunCommand(c, controller.NewShowControllerCommandForTest(s.store, s.api), args...)
   329  }
   330  
   331  func (s *ShowControllerSuite) assertShowControllerFailed(c *gc.C, args ...string) {
   332  	_, err := s.runShowController(c, args...)
   333  	c.Assert(err, gc.ErrorMatches, s.expectedErr)
   334  }
   335  
   336  func (s *ShowControllerSuite) assertShowController(c *gc.C, args ...string) {
   337  	context, err := s.runShowController(c, args...)
   338  	c.Assert(err, jc.ErrorIsNil)
   339  	c.Assert(testing.Stdout(context), gc.Equals, s.expectedOutput)
   340  }
   341  
   342  type fakeController struct {
   343  	controllerName string
   344  	store          jujuclient.ClientStore
   345  	modelNames     map[string]string
   346  	machines       map[string][]base.Machine
   347  }
   348  
   349  func (*fakeController) GetControllerAccess(user string) (permission.Access, error) {
   350  	return "superuser", nil
   351  }
   352  
   353  func (*fakeController) ModelConfig() (map[string]interface{}, error) {
   354  	return map[string]interface{}{"agent-version": "999.99.99"}, nil
   355  }
   356  
   357  func (c *fakeController) ModelStatus(models ...names.ModelTag) (result []base.ModelStatus, _ error) {
   358  	for _, mtag := range models {
   359  		result = append(result, base.ModelStatus{
   360  			UUID:              mtag.Id(),
   361  			TotalMachineCount: 2,
   362  			CoreCount:         4,
   363  			Machines:          c.machines[mtag.Id()],
   364  		})
   365  	}
   366  	return result, nil
   367  }
   368  
   369  func (c *fakeController) AllModels() (result []base.UserModel, _ error) {
   370  	all, err := c.store.AllModels(c.controllerName)
   371  	if errors.IsNotFound(err) {
   372  		return result, nil
   373  	}
   374  	if err != nil {
   375  		return nil, err
   376  	}
   377  	for _, m := range all {
   378  		result = append(result, base.UserModel{
   379  			UUID: m.ModelUUID,
   380  			Name: c.modelNames[m.ModelUUID],
   381  		})
   382  	}
   383  	return result, nil
   384  }
   385  
   386  func (*fakeController) Close() error {
   387  	return nil
   388  }