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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package model_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	gitjujutesting "github.com/juju/testing"
    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/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/model"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    19  	"github.com/juju/juju/status"
    20  	"github.com/juju/juju/testing"
    21  )
    22  
    23  type ShowCommandSuite struct {
    24  	testing.FakeJujuXDGDataHomeSuite
    25  	fake           fakeModelShowClient
    26  	store          *jujuclienttesting.MemStore
    27  	expectedOutput attrs
    28  }
    29  
    30  var _ = gc.Suite(&ShowCommandSuite{})
    31  
    32  type fakeModelShowClient struct {
    33  	gitjujutesting.Stub
    34  	info params.ModelInfo
    35  	err  *params.Error
    36  }
    37  
    38  func (f *fakeModelShowClient) Close() error {
    39  	f.MethodCall(f, "Close")
    40  	return f.NextErr()
    41  }
    42  
    43  func (f *fakeModelShowClient) ModelInfo(tags []names.ModelTag) ([]params.ModelInfoResult, error) {
    44  	f.MethodCall(f, "ModelInfo", tags)
    45  	if len(tags) != 1 {
    46  		return nil, errors.Errorf("expected 1 tag, got %d", len(tags))
    47  	}
    48  	if tags[0] != testing.ModelTag {
    49  		return nil, errors.Errorf("expected %s, got %s", testing.ModelTag.Id(), tags[0].Id())
    50  	}
    51  	return []params.ModelInfoResult{{Result: &f.info, Error: f.err}}, f.NextErr()
    52  }
    53  
    54  type attrs map[string]interface{}
    55  
    56  func (s *ShowCommandSuite) SetUpTest(c *gc.C) {
    57  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    58  	lastConnection := time.Date(2015, 3, 20, 0, 0, 0, 0, time.UTC)
    59  	statusSince := time.Date(2016, 4, 5, 0, 0, 0, 0, time.UTC)
    60  
    61  	users := []params.ModelUserInfo{{
    62  		UserName:       "admin@local",
    63  		LastConnection: &lastConnection,
    64  		Access:         "write",
    65  	}, {
    66  		UserName:    "bob@local",
    67  		DisplayName: "Bob",
    68  		Access:      "read",
    69  	}}
    70  
    71  	s.fake.ResetCalls()
    72  	s.fake.err = nil
    73  	s.fake.info = params.ModelInfo{
    74  		Name:           "mymodel",
    75  		UUID:           testing.ModelTag.Id(),
    76  		ControllerUUID: "1ca2293b-fdb9-4299-97d6-55583bb39364",
    77  		OwnerTag:       "user-admin@local",
    78  		CloudTag:       "cloud-some-cloud",
    79  		CloudRegion:    "some-region",
    80  		ProviderType:   "openstack",
    81  		Life:           params.Alive,
    82  		Status: params.EntityStatus{
    83  			Status: status.Active,
    84  			Since:  &statusSince,
    85  		},
    86  		Users: users,
    87  	}
    88  
    89  	s.expectedOutput = attrs{
    90  		"mymodel": attrs{
    91  			"name":            "mymodel",
    92  			"model-uuid":      "deadbeef-0bad-400d-8000-4b1d0d06f00d",
    93  			"controller-uuid": "1ca2293b-fdb9-4299-97d6-55583bb39364",
    94  			"controller-name": "testing",
    95  			"owner":           "admin@local",
    96  			"cloud":           "some-cloud",
    97  			"region":          "some-region",
    98  			"type":            "openstack",
    99  			"life":            "alive",
   100  			"status": attrs{
   101  				"current": "active",
   102  				"since":   "2016-04-05",
   103  			},
   104  			"users": attrs{
   105  				"admin@local": attrs{
   106  					"access":          "write",
   107  					"last-connection": "2015-03-20",
   108  				},
   109  				"bob@local": attrs{
   110  					"display-name":    "Bob",
   111  					"access":          "read",
   112  					"last-connection": "never connected",
   113  				},
   114  			},
   115  		},
   116  	}
   117  
   118  	s.store = jujuclienttesting.NewMemStore()
   119  	s.store.CurrentControllerName = "testing"
   120  	s.store.Controllers["testing"] = jujuclient.ControllerDetails{}
   121  	s.store.Accounts["testing"] = jujuclient.AccountDetails{
   122  		User: "admin@local",
   123  	}
   124  	err := s.store.UpdateModel("testing", "admin@local/mymodel", jujuclient.ModelDetails{
   125  		testing.ModelTag.Id(),
   126  	})
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	s.store.Models["testing"].CurrentModel = "admin@local/mymodel"
   129  }
   130  
   131  func (s *ShowCommandSuite) TestShow(c *gc.C) {
   132  	_, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store))
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	s.fake.CheckCalls(c, []gitjujutesting.StubCall{
   135  		{"ModelInfo", []interface{}{[]names.ModelTag{testing.ModelTag}}},
   136  		{"Close", nil},
   137  	})
   138  }
   139  
   140  func (s *ShowCommandSuite) TestShowFormatYaml(c *gc.C) {
   141  	ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "yaml")
   142  	c.Assert(err, jc.ErrorIsNil)
   143  	c.Assert(testing.Stdout(ctx), jc.YAMLEquals, s.expectedOutput)
   144  }
   145  
   146  func (s *ShowCommandSuite) TestShowFormatJson(c *gc.C) {
   147  	ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "json")
   148  	c.Assert(err, jc.ErrorIsNil)
   149  	c.Assert(testing.Stdout(ctx), jc.JSONEquals, s.expectedOutput)
   150  }
   151  
   152  func (s *ShowCommandSuite) TestUnrecognizedArg(c *gc.C) {
   153  	_, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "admin", "whoops")
   154  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
   155  }