github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/controller/listcontrollers_test.go (about)

     1  // Copyright 2015,2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller_test
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/api/base"
    16  	"github.com/juju/juju/cmd/juju/controller"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  type ListControllersSuite struct {
    23  	baseControllerSuite
    24  	api func(string) controller.ControllerAccessAPI
    25  }
    26  
    27  var _ = gc.Suite(&ListControllersSuite{})
    28  
    29  func (s *ListControllersSuite) TestListControllersEmptyStore(c *gc.C) {
    30  	s.store = jujuclienttesting.NewMemStore()
    31  	context, err := s.runListControllers(c)
    32  	c.Assert(err, jc.ErrorIsNil)
    33  	c.Check(testing.Stdout(context), gc.Equals, "")
    34  	c.Check(testing.Stderr(context), gc.Equals, modelcmd.ErrNoControllersDefined.Error())
    35  }
    36  
    37  func (s *ListControllersSuite) TestListControllers(c *gc.C) {
    38  	s.expectedOutput = `
    39  Use --refresh to see the latest information.
    40  
    41  Controller           Model       User   Access     Cloud/Region        Models  Machines  HA  Version
    42  aws-test             controller  -      -          aws/us-east-1            2         5   -  2.0.1      
    43  mallards*            my-model    admin  superuser  mallards/mallards1       -         -   -  (unknown)  
    44  mark-test-prodstack  -           admin  (unknown)  prodstack                -         -   -  (unknown)  
    45  
    46  `[1:]
    47  
    48  	store := s.createTestClientStore(c)
    49  	delete(store.Accounts, "aws-test")
    50  	s.assertListControllers(c)
    51  }
    52  
    53  func (s *ListControllersSuite) TestListControllersRefresh(c *gc.C) {
    54  	s.createTestClientStore(c)
    55  	s.api = func(controllerNamee string) controller.ControllerAccessAPI {
    56  		fakeController := &fakeController{
    57  			controllerName: controllerNamee,
    58  			modelNames: map[string]string{
    59  				"abc": "controller",
    60  				"def": "my-model",
    61  				"ghi": "controller",
    62  			},
    63  			store: s.store,
    64  		}
    65  		return fakeController
    66  	}
    67  	s.expectedOutput = `
    68  Controller           Model       User   Access     Cloud/Region        Models  Machines  HA  Version
    69  aws-test             controller  admin  (unknown)  aws/us-east-1            1         2   -  2.0.1      
    70  mallards*            my-model    admin  superuser  mallards/mallards1       2         4   -  (unknown)  
    71  mark-test-prodstack  -           admin  (unknown)  prodstack                -         -   -  (unknown)  
    72  
    73  `[1:]
    74  	s.assertListControllers(c, "--refresh")
    75  }
    76  
    77  func (s *ListControllersSuite) setupAPIForControllerMachines() {
    78  	s.api = func(controllerName string) controller.ControllerAccessAPI {
    79  		fakeController := &fakeController{
    80  			controllerName: controllerName,
    81  			modelNames: map[string]string{
    82  				"abc": "controller",
    83  				"def": "my-model",
    84  				"ghi": "controller",
    85  			},
    86  			store: s.store,
    87  		}
    88  		switch controllerName {
    89  		case "aws-test":
    90  			fakeController.machines = map[string][]base.Machine{
    91  				"ghi": {
    92  					{Id: "1", HasVote: true, WantsVote: true, Status: "active"},
    93  					{Id: "2", HasVote: true, WantsVote: true, Status: "down"},
    94  					{Id: "3", HasVote: false, WantsVote: true, Status: "active"},
    95  				},
    96  				"abc": {
    97  					{Id: "1", HasVote: true, WantsVote: true, Status: "active"},
    98  				},
    99  				"def": {
   100  					{Id: "1", HasVote: true, WantsVote: true, Status: "active"},
   101  				},
   102  			}
   103  		case "mallards":
   104  			fakeController.machines = map[string][]base.Machine{
   105  				"abc": {
   106  					{Id: "1", HasVote: true, WantsVote: true, Status: "active"},
   107  				},
   108  			}
   109  		}
   110  		return fakeController
   111  	}
   112  }
   113  
   114  func (s *ListControllersSuite) TestListControllersKnownHAStatus(c *gc.C) {
   115  	s.createTestClientStore(c)
   116  	s.setupAPIForControllerMachines()
   117  	s.expectedOutput = `
   118  Controller           Model       User   Access     Cloud/Region        Models  Machines    HA  Version
   119  aws-test             controller  admin  (unknown)  aws/us-east-1            1         2   1/3  2.0.1      
   120  mallards*            my-model    admin  superuser  mallards/mallards1       2         4  none  (unknown)  
   121  mark-test-prodstack  -           admin  (unknown)  prodstack                -         -     -  (unknown)  
   122  
   123  `[1:]
   124  	s.assertListControllers(c, "--refresh")
   125  }
   126  
   127  func (s *ListControllersSuite) TestListControllersYaml(c *gc.C) {
   128  	s.expectedOutput = `
   129  controllers:
   130    aws-test:
   131      current-model: controller
   132      user: admin
   133      recent-server: this-is-aws-test-of-many-api-endpoints
   134      uuid: this-is-the-aws-test-uuid
   135      api-endpoints: [this-is-aws-test-of-many-api-endpoints]
   136      ca-cert: this-is-aws-test-ca-cert
   137      cloud: aws
   138      region: us-east-1
   139      agent-version: 2.0.1
   140      model-count: 1
   141      machine-count: 2
   142      controller-machines:
   143        active: 1
   144        total: 3
   145    mallards:
   146      current-model: my-model
   147      user: admin
   148      access: superuser
   149      recent-server: this-is-another-of-many-api-endpoints
   150      uuid: this-is-another-uuid
   151      api-endpoints: [this-is-another-of-many-api-endpoints, this-is-one-more-of-many-api-endpoints]
   152      ca-cert: this-is-another-ca-cert
   153      cloud: mallards
   154      region: mallards1
   155      model-count: 2
   156      machine-count: 4
   157      controller-machines:
   158        active: 1
   159        total: 1
   160    mark-test-prodstack:
   161      user: admin
   162      recent-server: this-is-one-of-many-api-endpoints
   163      uuid: this-is-a-uuid
   164      api-endpoints: [this-is-one-of-many-api-endpoints]
   165      ca-cert: this-is-a-ca-cert
   166      cloud: prodstack
   167  current-controller: mallards
   168  `[1:]
   169  
   170  	s.createTestClientStore(c)
   171  	s.setupAPIForControllerMachines()
   172  	s.assertListControllers(c, "--format", "yaml", "--refresh")
   173  }
   174  
   175  func intPtr(i int) *int {
   176  	return &i
   177  }
   178  
   179  func (s *ListControllersSuite) TestListControllersJson(c *gc.C) {
   180  	s.expectedOutput = ""
   181  	s.createTestClientStore(c)
   182  	jsonOut := s.assertListControllers(c, "--format", "json")
   183  	var result controller.ControllerSet
   184  	err := json.Unmarshal([]byte(jsonOut), &result)
   185  	c.Assert(err, jc.ErrorIsNil)
   186  	c.Assert(result, jc.DeepEquals, controller.ControllerSet{
   187  		Controllers: map[string]controller.ControllerItem{
   188  			"aws-test": {
   189  				ControllerUUID: "this-is-the-aws-test-uuid",
   190  				ModelName:      "controller",
   191  				User:           "admin",
   192  				Server:         "this-is-aws-test-of-many-api-endpoints",
   193  				APIEndpoints:   []string{"this-is-aws-test-of-many-api-endpoints"},
   194  				CACert:         "this-is-aws-test-ca-cert",
   195  				Cloud:          "aws",
   196  				CloudRegion:    "us-east-1",
   197  				AgentVersion:   "2.0.1",
   198  				ModelCount:     intPtr(2),
   199  				MachineCount:   intPtr(5),
   200  			},
   201  			"mallards": {
   202  				ControllerUUID: "this-is-another-uuid",
   203  				ModelName:      "my-model",
   204  				User:           "admin",
   205  				Access:         "superuser",
   206  				Server:         "this-is-another-of-many-api-endpoints",
   207  				APIEndpoints:   []string{"this-is-another-of-many-api-endpoints", "this-is-one-more-of-many-api-endpoints"},
   208  				CACert:         "this-is-another-ca-cert",
   209  				Cloud:          "mallards",
   210  				CloudRegion:    "mallards1",
   211  			},
   212  			"mark-test-prodstack": {
   213  				ControllerUUID: "this-is-a-uuid",
   214  				User:           "admin",
   215  				Server:         "this-is-one-of-many-api-endpoints",
   216  				APIEndpoints:   []string{"this-is-one-of-many-api-endpoints"},
   217  				CACert:         "this-is-a-ca-cert",
   218  				Cloud:          "prodstack",
   219  			},
   220  		},
   221  		CurrentController: "mallards",
   222  	})
   223  }
   224  
   225  func (s *ListControllersSuite) TestListControllersReadFromStoreErr(c *gc.C) {
   226  	msg := "fail getting all controllers"
   227  	errStore := jujuclienttesting.NewStubStore()
   228  	errStore.SetErrors(errors.New(msg))
   229  	s.store = errStore
   230  	s.expectedErr = fmt.Sprintf("failed to list controllers: %v", msg)
   231  	s.assertListControllersFailed(c)
   232  	errStore.CheckCallNames(c, "AllControllers")
   233  }
   234  
   235  func (s *ListControllersSuite) TestListControllersUnrecognizedArg(c *gc.C) {
   236  	s.createTestClientStore(c)
   237  	s.expectedErr = `unrecognized args: \["whoops"\]`
   238  	s.assertListControllersFailed(c, "whoops")
   239  }
   240  
   241  func (s *ListControllersSuite) TestListControllersUnrecognizedFlag(c *gc.C) {
   242  	s.createTestClientStore(c)
   243  	s.expectedErr = `flag provided but not defined: -m`
   244  	s.assertListControllersFailed(c, "-m", "my.world")
   245  }
   246  
   247  func (s *ListControllersSuite) TestListControllersUnrecognizedOptionFlag(c *gc.C) {
   248  	s.createTestClientStore(c)
   249  	s.expectedErr = `flag provided but not defined: --model`
   250  	s.assertListControllersFailed(c, "--model", "still.my.world")
   251  }
   252  
   253  func (s *ListControllersSuite) runListControllers(c *gc.C, args ...string) (*cmd.Context, error) {
   254  	return testing.RunCommand(c, controller.NewListControllersCommandForTest(s.store, s.api), args...)
   255  }
   256  
   257  func (s *ListControllersSuite) assertListControllersFailed(c *gc.C, args ...string) {
   258  	_, err := s.runListControllers(c, args...)
   259  	c.Assert(err, gc.ErrorMatches, s.expectedErr)
   260  }
   261  
   262  func (s *ListControllersSuite) assertListControllers(c *gc.C, args ...string) string {
   263  	context, err := s.runListControllers(c, args...)
   264  	c.Assert(err, jc.ErrorIsNil)
   265  	output := testing.Stdout(context)
   266  	if s.expectedOutput != "" {
   267  		c.Assert(output, gc.Equals, s.expectedOutput)
   268  	}
   269  	return output
   270  }