~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/jujuclient/controllers_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 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package jujuclient_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "github.com/juju/juju/jujuclient"
 
13
        "github.com/juju/juju/testing"
 
14
)
 
15
 
 
16
type ControllersSuite struct {
 
17
        testing.FakeJujuXDGDataHomeSuite
 
18
        store          jujuclient.ControllerStore
 
19
        controllerName string
 
20
        controller     jujuclient.ControllerDetails
 
21
}
 
22
 
 
23
var _ = gc.Suite(&ControllersSuite{})
 
24
 
 
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"},
 
31
                "test.uuid",
 
32
                []string{"test.api.endpoint"},
 
33
                "test.ca.cert",
 
34
                "aws",
 
35
                "southeastasia",
 
36
        }
 
37
}
 
38
 
 
39
func (s *ControllersSuite) TestControllerMetadataNone(c *gc.C) {
 
40
        c.Assert(s.getControllers(c), gc.IsNil)
 
41
}
 
42
 
 
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)
 
47
}
 
48
 
 
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)
 
54
}
 
55
 
 
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)
 
62
}
 
63
 
 
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)
 
68
}
 
69
 
 
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)
 
75
}
 
76
 
 
77
func (s *ControllersSuite) TestUpdateController(c *gc.C) {
 
78
        s.controllerName = firstTestControllerName(c)
 
79
 
 
80
        err := s.store.UpdateController(s.controllerName, s.controller)
 
81
        c.Assert(err, jc.ErrorIsNil)
 
82
        s.assertUpdateSucceeded(c)
 
83
}
 
84
 
 
85
func (s *ControllersSuite) TestRemoveControllerNoFile(c *gc.C) {
 
86
        err := s.store.RemoveController(s.controllerName)
 
87
        c.Assert(err, jc.ErrorIsNil)
 
88
}
 
89
 
 
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)
 
94
}
 
95
 
 
96
func (s *ControllersSuite) TestRemoveController(c *gc.C) {
 
97
        name := firstTestControllerName(c)
 
98
 
 
99
        err := s.store.RemoveController(name)
 
100
        c.Assert(err, jc.ErrorIsNil)
 
101
 
 
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)
 
105
}
 
106
 
 
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)
 
113
 
 
114
        err = s.store.RemoveController(name)
 
115
        c.Assert(err, jc.ErrorIsNil)
 
116
 
 
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)
 
121
        }
 
122
}
 
123
 
 
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)
 
127
 
 
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)
 
131
}
 
132
 
 
133
func (s *ControllersSuite) assertControllerNotExists(c *gc.C) {
 
134
        all := writeTestControllersFile(c)
 
135
        _, exists := all.Controllers[s.controllerName]
 
136
        c.Assert(exists, jc.IsFalse)
 
137
}
 
138
 
 
139
func (s *ControllersSuite) assertUpdateSucceeded(c *gc.C) {
 
140
        c.Assert(s.getControllers(c)[s.controllerName], gc.DeepEquals, s.controller)
 
141
}
 
142
 
 
143
func (s *ControllersSuite) getControllers(c *gc.C) map[string]jujuclient.ControllerDetails {
 
144
        controllers, err := s.store.AllControllers()
 
145
        c.Assert(err, jc.ErrorIsNil)
 
146
        return controllers
 
147
}
 
148
 
 
149
func firstTestControllerName(c *gc.C) string {
 
150
        all := writeTestControllersFile(c)
 
151
        for key, _ := range all.Controllers {
 
152
                return key
 
153
        }
 
154
        return ""
 
155
}