~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/show_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                Cloud:          "some-cloud",
 
79
                CloudRegion:    "some-region",
 
80
                ProviderType:   "openstack",
 
81
                Life:           params.Alive,
 
82
                Status: params.EntityStatus{
 
83
                        Status: status.StatusActive,
 
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
                        "owner":           "admin@local",
 
95
                        "cloud":           "some-cloud",
 
96
                        "region":          "some-region",
 
97
                        "type":            "openstack",
 
98
                        "life":            "alive",
 
99
                        "status": attrs{
 
100
                                "current": "active",
 
101
                                "since":   "2016-04-05",
 
102
                        },
 
103
                        "users": attrs{
 
104
                                "admin@local": attrs{
 
105
                                        "access":          "write",
 
106
                                        "last-connection": "2015-03-20",
 
107
                                },
 
108
                                "bob@local": attrs{
 
109
                                        "display-name":    "Bob",
 
110
                                        "access":          "read",
 
111
                                        "last-connection": "never connected",
 
112
                                },
 
113
                        },
 
114
                },
 
115
        }
 
116
 
 
117
        s.store = jujuclienttesting.NewMemStore()
 
118
        s.store.CurrentControllerName = "testing"
 
119
        s.store.Controllers["testing"] = jujuclient.ControllerDetails{}
 
120
        s.store.Accounts["testing"] = jujuclient.AccountDetails{
 
121
                User: "admin@local",
 
122
        }
 
123
        err := s.store.UpdateModel("testing", "admin@local/mymodel", jujuclient.ModelDetails{
 
124
                testing.ModelTag.Id(),
 
125
        })
 
126
        c.Assert(err, jc.ErrorIsNil)
 
127
        s.store.Models["testing"].CurrentModel = "admin@local/mymodel"
 
128
}
 
129
 
 
130
func (s *ShowCommandSuite) TestShow(c *gc.C) {
 
131
        _, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store))
 
132
        c.Assert(err, jc.ErrorIsNil)
 
133
        s.fake.CheckCalls(c, []gitjujutesting.StubCall{
 
134
                {"ModelInfo", []interface{}{[]names.ModelTag{testing.ModelTag}}},
 
135
                {"Close", nil},
 
136
        })
 
137
}
 
138
 
 
139
func (s *ShowCommandSuite) TestShowFormatYaml(c *gc.C) {
 
140
        ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "yaml")
 
141
        c.Assert(err, jc.ErrorIsNil)
 
142
        c.Assert(testing.Stdout(ctx), jc.YAMLEquals, s.expectedOutput)
 
143
}
 
144
 
 
145
func (s *ShowCommandSuite) TestShowFormatJson(c *gc.C) {
 
146
        ctx, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "--format", "json")
 
147
        c.Assert(err, jc.ErrorIsNil)
 
148
        c.Assert(testing.Stdout(ctx), jc.JSONEquals, s.expectedOutput)
 
149
}
 
150
 
 
151
func (s *ShowCommandSuite) TestUnrecognizedArg(c *gc.C) {
 
152
        _, err := testing.RunCommand(c, model.NewShowCommandForTest(&s.fake, s.store), "-m", "admin", "whoops")
 
153
        c.Assert(err, gc.ErrorMatches, `unrecognized args: \["whoops"\]`)
 
154
}