1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package jujuclient_test
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
12
"github.com/juju/juju/jujuclient"
13
"github.com/juju/juju/testing"
16
type ControllersSuite struct {
17
testing.FakeJujuXDGDataHomeSuite
18
store jujuclient.ControllerStore
20
controller jujuclient.ControllerDetails
23
var _ = gc.Suite(&ControllersSuite{})
25
func (s *ControllersSuite) SetUpTest(c *gc.C) {
26
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
27
s.store = jujuclient.NewFileClientStore()
28
s.controllerName = "test.controller"
29
s.controller = jujuclient.ControllerDetails{
30
[]string{"test.server.hostname"},
32
[]string{"test.api.endpoint"},
39
func (s *ControllersSuite) TestControllerMetadataNone(c *gc.C) {
40
c.Assert(s.getControllers(c), gc.IsNil)
43
func (s *ControllersSuite) TestControllerByNameNoFile(c *gc.C) {
44
found, err := s.store.ControllerByName(s.controllerName)
45
c.Assert(err, gc.ErrorMatches, "controller test.controller not found")
46
c.Assert(found, gc.IsNil)
49
func (s *ControllersSuite) TestControllerByNameNoneExists(c *gc.C) {
50
writeTestControllersFile(c)
51
found, err := s.store.ControllerByName(s.controllerName)
52
c.Assert(err, gc.ErrorMatches, "controller test.controller not found")
53
c.Assert(found, gc.IsNil)
56
func (s *ControllersSuite) TestControllerByName(c *gc.C) {
57
name := firstTestControllerName(c)
58
found, err := s.store.ControllerByName(name)
59
c.Assert(err, jc.ErrorIsNil)
60
expected := s.getControllers(c)[name]
61
c.Assert(found, gc.DeepEquals, &expected)
64
func (s *ControllersSuite) TestUpdateControllerAddFirst(c *gc.C) {
65
err := s.store.UpdateController(s.controllerName, s.controller)
66
c.Assert(err, jc.ErrorIsNil)
67
s.assertUpdateSucceeded(c)
70
func (s *ControllersSuite) TestUpdateControllerAddNew(c *gc.C) {
71
s.assertControllerNotExists(c)
72
err := s.store.UpdateController(s.controllerName, s.controller)
73
c.Assert(err, jc.ErrorIsNil)
74
s.assertUpdateSucceeded(c)
77
func (s *ControllersSuite) TestUpdateController(c *gc.C) {
78
s.controllerName = firstTestControllerName(c)
80
err := s.store.UpdateController(s.controllerName, s.controller)
81
c.Assert(err, jc.ErrorIsNil)
82
s.assertUpdateSucceeded(c)
85
func (s *ControllersSuite) TestRemoveControllerNoFile(c *gc.C) {
86
err := s.store.RemoveController(s.controllerName)
87
c.Assert(err, jc.ErrorIsNil)
90
func (s *ControllersSuite) TestRemoveControllerUnknown(c *gc.C) {
91
s.assertControllerNotExists(c)
92
err := s.store.RemoveController(s.controllerName)
93
c.Assert(err, jc.ErrorIsNil)
96
func (s *ControllersSuite) TestRemoveController(c *gc.C) {
97
name := firstTestControllerName(c)
99
err := s.store.RemoveController(name)
100
c.Assert(err, jc.ErrorIsNil)
102
found, err := s.store.ControllerByName(name)
103
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("controller %v not found", name))
104
c.Assert(found, gc.IsNil)
107
func (s *ControllersSuite) TestRemoveControllerRemovesIdenticalControllers(c *gc.C) {
108
name := firstTestControllerName(c)
109
details, err := s.store.ControllerByName(name)
110
c.Assert(err, jc.ErrorIsNil)
111
err = s.store.UpdateController(name+"-copy", *details)
112
c.Assert(err, jc.ErrorIsNil)
114
err = s.store.RemoveController(name)
115
c.Assert(err, jc.ErrorIsNil)
117
for _, name := range []string{name, name + "-copy"} {
118
found, err := s.store.ControllerByName(name)
119
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("controller %v not found", name))
120
c.Assert(found, gc.IsNil)
124
func (s *ControllersSuite) assertWriteFails(c *gc.C, failureMessage string) {
125
err := s.store.UpdateController(s.controllerName, s.controller)
126
c.Assert(err, gc.ErrorMatches, failureMessage)
128
found, err := s.store.ControllerByName(s.controllerName)
129
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("controller %v not found", s.controllerName))
130
c.Assert(found, gc.IsNil)
133
func (s *ControllersSuite) assertControllerNotExists(c *gc.C) {
134
all := writeTestControllersFile(c)
135
_, exists := all.Controllers[s.controllerName]
136
c.Assert(exists, jc.IsFalse)
139
func (s *ControllersSuite) assertUpdateSucceeded(c *gc.C) {
140
c.Assert(s.getControllers(c)[s.controllerName], gc.DeepEquals, s.controller)
143
func (s *ControllersSuite) getControllers(c *gc.C) map[string]jujuclient.ControllerDetails {
144
controllers, err := s.store.AllControllers()
145
c.Assert(err, jc.ErrorIsNil)
149
func firstTestControllerName(c *gc.C) string {
150
all := writeTestControllersFile(c)
151
for key, _ := range all.Controllers {