~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/featuretests/cmd_juju_controller_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
        "github.com/juju/cmd"
13
13
        "github.com/juju/errors"
 
14
        "github.com/juju/names"
14
15
        jc "github.com/juju/testing/checkers"
15
16
        gc "gopkg.in/check.v1"
16
17
 
 
18
        "github.com/juju/juju/api"
17
19
        "github.com/juju/juju/api/modelmanager"
18
20
        undertakerapi "github.com/juju/juju/api/undertaker"
19
21
        "github.com/juju/juju/cmd/juju/commands"
20
 
        "github.com/juju/juju/cmd/modelcmd"
21
 
        "github.com/juju/juju/environs/configstore"
 
22
        "github.com/juju/juju/environs/config"
22
23
        "github.com/juju/juju/juju"
23
24
        jujutesting "github.com/juju/juju/juju/testing"
 
25
        "github.com/juju/juju/jujuclient"
24
26
        "github.com/juju/juju/provider/dummy"
25
27
        "github.com/juju/juju/state"
26
28
        "github.com/juju/juju/testing"
40
42
        return context
41
43
}
42
44
 
43
 
func (s *cmdControllerSuite) createEnv(c *gc.C, envname string, isServer bool) {
 
45
func (s *cmdControllerSuite) createModelAdminUser(c *gc.C, modelname string, isServer bool) {
44
46
        modelManager := modelmanager.NewClient(s.APIState)
45
47
        _, err := modelManager.CreateModel(s.AdminUserTag(c).Id(), nil, map[string]interface{}{
46
 
                "name":            envname,
 
48
                "name":       modelname,
 
49
                "controller": isServer,
 
50
        })
 
51
        c.Assert(err, jc.ErrorIsNil)
 
52
}
 
53
 
 
54
func (s *cmdControllerSuite) createModelNormalUser(c *gc.C, modelname string, isServer bool) {
 
55
        s.run(c, "add-user", "test")
 
56
        modelManager := modelmanager.NewClient(s.APIState)
 
57
        _, err := modelManager.CreateModel(names.NewLocalUserTag("test").Id(), nil, map[string]interface{}{
 
58
                "name":            modelname,
47
59
                "authorized-keys": "ssh-key",
48
60
                "controller":      isServer,
49
61
        })
52
64
 
53
65
func (s *cmdControllerSuite) TestControllerListCommand(c *gc.C) {
54
66
        context := s.run(c, "list-controllers")
55
 
        expectedOutput := `
56
 
CONTROLLER   MODEL       USER         SERVER
57
 
dummymodel*  dummymodel  admin@local  
 
67
        expectedOutput := fmt.Sprintf(`
 
68
CONTROLLER  MODEL  USER         SERVER
 
69
kontroll*   admin  admin@local  %s
58
70
 
59
 
`[1:]
 
71
`[1:], s.APIState.Addr())
60
72
        c.Assert(testing.Stdout(context), gc.Equals, expectedOutput)
61
73
}
62
74
 
63
 
func (s *cmdControllerSuite) TestControllerModelsCommand(c *gc.C) {
64
 
        c.Assert(modelcmd.WriteCurrentController("dummymodel"), jc.ErrorIsNil)
65
 
        s.createEnv(c, "new-model", false)
66
 
        context := s.run(c, "list-models")
67
 
        c.Assert(testing.Stdout(context), gc.Equals, ""+
68
 
                "NAME         OWNER        LAST CONNECTION\n"+
69
 
                "dummymodel*  admin@local  just now\n"+
70
 
                "new-model    admin@local  never connected\n"+
 
75
func (s *cmdControllerSuite) TestCreateModelAdminUser(c *gc.C) {
 
76
        s.createModelAdminUser(c, "new-model", false)
 
77
        context := s.run(c, "list-models")
 
78
        c.Assert(testing.Stdout(context), gc.Equals, ""+
 
79
                "NAME       OWNER        LAST CONNECTION\n"+
 
80
                "admin*     admin@local  just now\n"+
 
81
                "new-model  admin@local  never connected\n"+
 
82
                "\n")
 
83
}
 
84
 
 
85
func (s *cmdControllerSuite) TestCreateModelNormalUser(c *gc.C) {
 
86
        s.createModelAdminUser(c, "new-model", false)
 
87
        context := s.run(c, "list-models")
 
88
        c.Assert(testing.Stdout(context), gc.Equals, ""+
 
89
                "NAME       OWNER        LAST CONNECTION\n"+
 
90
                "admin*     admin@local  just now\n"+
 
91
                "new-model  admin@local  never connected\n"+
71
92
                "\n")
72
93
}
73
94
 
74
95
func (s *cmdControllerSuite) TestCreateModel(c *gc.C) {
75
 
        c.Assert(modelcmd.WriteCurrentController("dummymodel"), jc.ErrorIsNil)
76
96
        // The JujuConnSuite doesn't set up an ssh key in the fake home dir,
77
97
        // so fake one on the command line.  The dummy provider also expects
78
98
        // a config value for 'controller'.
82
102
 
83
103
        // Make sure that the saved server details are sufficient to connect
84
104
        // to the api server.
85
 
        api, err := juju.NewAPIConnection(s.ControllerStore, "dummymodel", "admin@local", "new-model", nil)
 
105
        accountDetails, err := s.ControllerStore.AccountByName("kontroll", "admin@local")
 
106
        c.Assert(err, jc.ErrorIsNil)
 
107
        modelDetails, err := s.ControllerStore.ModelByName("kontroll", "admin@local", "new-model")
 
108
        c.Assert(err, jc.ErrorIsNil)
 
109
        api, err := juju.NewAPIConnection(juju.NewAPIConnectionParams{
 
110
                Store:           s.ControllerStore,
 
111
                ControllerName:  "kontroll",
 
112
                AccountDetails:  accountDetails,
 
113
                ModelUUID:       modelDetails.ModelUUID,
 
114
                BootstrapConfig: noBootstrapConfig,
 
115
                DialOpts:        api.DefaultDialOpts(),
 
116
        })
86
117
        c.Assert(err, jc.ErrorIsNil)
87
118
        api.Close()
88
119
}
123
154
                }
124
155
        }()
125
156
 
126
 
        s.run(c, "destroy-controller", "dummymodel", "-y", "--destroy-all-models", "--debug")
 
157
        s.run(c, "destroy-controller", "kontroll", "-y", "--destroy-all-models", "--debug")
127
158
        close(stop)
128
159
        <-done
129
160
 
130
 
        store, err := configstore.Default()
131
 
        _, err = store.ReadInfo("dummymodel")
 
161
        store := jujuclient.NewFileClientStore()
 
162
        _, err := store.ControllerByName("kontroll")
132
163
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
133
164
}
134
165
 
135
166
func (s *cmdControllerSuite) TestRemoveBlocks(c *gc.C) {
136
 
        c.Assert(modelcmd.WriteCurrentController("dummymodel"), jc.ErrorIsNil)
137
167
        s.State.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
138
168
        s.State.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
139
169
 
152
182
        st.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
153
183
        st.Close()
154
184
 
155
 
        s.run(c, "kill-controller", "dummymodel", "-y")
 
185
        s.run(c, "kill-controller", "kontroll", "-y")
156
186
 
157
 
        store, err := configstore.Default()
158
 
        _, err = store.ReadInfo("dummymodel")
 
187
        store := jujuclient.NewFileClientStore()
 
188
        _, err := store.ControllerByName("kontroll")
159
189
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
160
190
}
161
191
 
162
192
func (s *cmdControllerSuite) TestListBlocks(c *gc.C) {
163
 
        c.Assert(modelcmd.WriteCurrentController("dummymodel"), jc.ErrorIsNil)
164
193
        s.State.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyModel")
165
194
        s.State.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock")
166
195
 
167
196
        ctx := s.run(c, "list-all-blocks", "--format", "json")
168
 
        expected := fmt.Sprintf(`[{"name":"dummymodel","model-uuid":"%s","owner-tag":"%s","blocks":["BlockDestroy","BlockChange"]}]`,
 
197
        expected := fmt.Sprintf(`[{"name":"admin","model-uuid":"%s","owner-tag":"%s","blocks":["BlockDestroy","BlockChange"]}]`,
169
198
                s.State.ModelUUID(), s.AdminUserTag(c).String())
170
199
 
171
200
        strippedOut := strings.Replace(testing.Stdout(ctx), "\n", "", -1)
190
219
        mClock := testing.NewClock(startTime)
191
220
        undertaker.NewUndertaker(client, mClock)
192
221
 
193
 
        store, err := configstore.Default()
194
 
        _, err = store.ReadInfo("dummymodel:dummymodel")
 
222
        store := jujuclient.NewFileClientStore()
 
223
        _, err := store.ControllerByName("kontroll")
195
224
        c.Assert(err, jc.ErrorIsNil)
196
225
 
197
 
        s.run(c, "kill-controller", "dummymodel", "-y")
 
226
        s.run(c, "kill-controller", "kontroll", "-y")
198
227
 
199
228
        // Ensure that Destroy was called on the hosted model ...
200
229
        opRecvTimeout(c, st, opc, dummy.OpDestroy{})
201
230
 
202
 
        // ... and that the configstore was removed.
203
 
        _, err = store.ReadInfo("dummymodel")
 
231
        // ... and that the details were removed removed from
 
232
        // the client store.
 
233
        _, err = store.ControllerByName("kontroll")
204
234
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
205
235
}
206
236
 
222
252
                }
223
253
        }
224
254
}
 
255
 
 
256
func noBootstrapConfig(controllerName string) (*config.Config, error) {
 
257
        return nil, errors.NotFoundf("bootstrap config for controller %s", controllerName)
 
258
}