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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/charm_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
        "bytes"
8
8
        "fmt"
9
9
        "path/filepath"
 
10
        "strings"
10
11
 
11
12
        "github.com/juju/errors"
12
13
        jc "github.com/juju/testing/checkers"
17
18
        "gopkg.in/mgo.v2"
18
19
 
19
20
        "github.com/juju/juju/state"
 
21
        "github.com/juju/juju/state/storage"
20
22
        "github.com/juju/juju/testcharms"
 
23
        "github.com/juju/juju/testing/factory"
21
24
)
22
25
 
23
26
type CharmSuite struct {
24
27
        ConnSuite
25
 
        curl *charm.URL
 
28
        charm *state.Charm
 
29
        curl  *charm.URL
26
30
}
27
31
 
28
32
var _ = gc.Suite(&CharmSuite{})
29
33
 
30
34
func (s *CharmSuite) SetUpTest(c *gc.C) {
31
35
        s.ConnSuite.SetUpTest(c)
32
 
        added := s.AddTestingCharm(c, "dummy")
33
 
        s.curl = added.URL()
34
 
}
35
 
 
36
 
func (s *CharmSuite) TestCharm(c *gc.C) {
 
36
        s.charm = s.AddTestingCharm(c, "dummy")
 
37
        s.curl = s.charm.URL()
 
38
}
 
39
 
 
40
func (s *CharmSuite) destroy(c *gc.C) {
 
41
        err := s.charm.Destroy()
 
42
        c.Assert(err, jc.ErrorIsNil)
 
43
}
 
44
 
 
45
func (s *CharmSuite) remove(c *gc.C) {
 
46
        s.destroy(c)
 
47
        err := s.charm.Remove()
 
48
        c.Assert(err, jc.ErrorIsNil)
 
49
}
 
50
 
 
51
func (s *CharmSuite) checkRemoved(c *gc.C) {
 
52
        _, err := s.State.Charm(s.curl)
 
53
        c.Check(err, gc.ErrorMatches, `charm ".*" not found`)
 
54
        c.Check(err, jc.Satisfies, errors.IsNotFound)
 
55
}
 
56
 
 
57
func (s *CharmSuite) TestAliveCharm(c *gc.C) {
 
58
        s.testCharm(c)
 
59
}
 
60
 
 
61
func (s *CharmSuite) TestDyingCharm(c *gc.C) {
 
62
        s.destroy(c)
 
63
        s.testCharm(c)
 
64
}
 
65
 
 
66
func (s *CharmSuite) testCharm(c *gc.C) {
37
67
        dummy, err := s.State.Charm(s.curl)
38
68
        c.Assert(err, jc.ErrorIsNil)
39
69
        c.Assert(dummy.URL().String(), gc.Equals, s.curl.String())
74
104
                })
75
105
}
76
106
 
 
107
func (s *CharmSuite) TestRemovedCharmNotFound(c *gc.C) {
 
108
        s.remove(c)
 
109
        s.checkRemoved(c)
 
110
}
 
111
 
 
112
func (s *CharmSuite) TestRemovedCharmNotListed(c *gc.C) {
 
113
        s.remove(c)
 
114
        charms, err := s.State.AllCharms()
 
115
        c.Check(err, jc.ErrorIsNil)
 
116
        c.Check(charms, gc.HasLen, 0)
 
117
}
 
118
 
 
119
func (s *CharmSuite) TestRemoveWithoutDestroy(c *gc.C) {
 
120
        err := s.charm.Remove()
 
121
        c.Assert(err, gc.ErrorMatches, "still alive")
 
122
}
 
123
 
77
124
func (s *CharmSuite) TestCharmNotFound(c *gc.C) {
78
125
        curl := charm.MustParseURL("local:anotherseries/dummy-1")
79
126
        _, err := s.State.Charm(curl)
97
144
        return info
98
145
}
99
146
 
 
147
func (s *CharmSuite) TestDestroyStoreCharm(c *gc.C) {
 
148
        info := s.dummyCharm(c, "cs:precise/dummy-2")
 
149
        sch, err := s.State.AddCharm(info)
 
150
        c.Assert(err, jc.ErrorIsNil)
 
151
        err = sch.Destroy()
 
152
        c.Assert(err, gc.ErrorMatches, "cannot destroy non-local charms")
 
153
}
 
154
 
 
155
func (s *CharmSuite) TestRemoveDeletesStorage(c *gc.C) {
 
156
        // We normally don't actually set up charm storage in state
 
157
        // tests, but we need it here.
 
158
        path := s.charm.StoragePath()
 
159
        stor := storage.NewStorage(s.State.ModelUUID(), s.State.MongoSession())
 
160
        err := stor.Put(path, strings.NewReader("abc"), 3)
 
161
        c.Assert(err, jc.ErrorIsNil)
 
162
 
 
163
        s.destroy(c)
 
164
        closer, _, err := stor.Get(path)
 
165
        c.Assert(err, jc.ErrorIsNil)
 
166
        closer.Close()
 
167
 
 
168
        s.remove(c)
 
169
        _, _, err = stor.Get(path)
 
170
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
171
}
 
172
 
 
173
func (s *CharmSuite) TestReferenceDyingCharm(c *gc.C) {
 
174
 
 
175
        s.destroy(c)
 
176
 
 
177
        args := state.AddApplicationArgs{
 
178
                Name:  "blah",
 
179
                Charm: s.charm,
 
180
        }
 
181
        _, err := s.State.AddApplication(args)
 
182
        c.Check(err, gc.ErrorMatches, `cannot add application "blah": charm: not found or not alive`)
 
183
}
 
184
 
 
185
func (s *CharmSuite) TestReferenceDyingCharmRace(c *gc.C) {
 
186
 
 
187
        defer state.SetBeforeHooks(c, s.State, func() {
 
188
                s.destroy(c)
 
189
        }).Check()
 
190
 
 
191
        args := state.AddApplicationArgs{
 
192
                Name:  "blah",
 
193
                Charm: s.charm,
 
194
        }
 
195
        _, err := s.State.AddApplication(args)
 
196
        // bad message: see lp:1621754. should match
 
197
        // TestReferenceDyingCharm above.
 
198
        c.Check(err, gc.ErrorMatches, `cannot add application "blah": application already exists`)
 
199
}
 
200
 
 
201
func (s *CharmSuite) TestDestroyReferencedCharm(c *gc.C) {
 
202
        s.Factory.MakeApplication(c, &factory.ApplicationParams{
 
203
                Charm: s.charm,
 
204
        })
 
205
 
 
206
        err := s.charm.Destroy()
 
207
        c.Check(err, gc.ErrorMatches, "charm in use")
 
208
}
 
209
 
 
210
func (s *CharmSuite) TestDestroyReferencedCharmRace(c *gc.C) {
 
211
 
 
212
        defer state.SetBeforeHooks(c, s.State, func() {
 
213
                s.Factory.MakeApplication(c, &factory.ApplicationParams{
 
214
                        Charm: s.charm,
 
215
                })
 
216
        }).Check()
 
217
 
 
218
        err := s.charm.Destroy()
 
219
        c.Check(err, gc.ErrorMatches, "charm in use")
 
220
}
 
221
 
 
222
func (s *CharmSuite) TestDestroyUnreferencedCharm(c *gc.C) {
 
223
        app := s.Factory.MakeApplication(c, &factory.ApplicationParams{
 
224
                Charm: s.charm,
 
225
        })
 
226
        err := app.Destroy()
 
227
        c.Assert(err, jc.ErrorIsNil)
 
228
 
 
229
        err = s.charm.Destroy()
 
230
        c.Assert(err, jc.ErrorIsNil)
 
231
}
 
232
 
 
233
func (s *CharmSuite) TestDestroyUnitReferencedCharm(c *gc.C) {
 
234
        app := s.Factory.MakeApplication(c, &factory.ApplicationParams{
 
235
                Charm: s.charm,
 
236
        })
 
237
        unit := s.Factory.MakeUnit(c, &factory.UnitParams{
 
238
                Application: app,
 
239
                SetCharmURL: true,
 
240
        })
 
241
 
 
242
        // set app charm to something different
 
243
        info := s.dummyCharm(c, "cs:quantal/dummy-2")
 
244
        newCh, err := s.State.AddCharm(info)
 
245
        c.Assert(err, jc.ErrorIsNil)
 
246
        err = app.SetCharm(state.SetCharmConfig{Charm: newCh})
 
247
        c.Assert(err, jc.ErrorIsNil)
 
248
 
 
249
        // unit should still reference original charm until updated
 
250
        err = s.charm.Destroy()
 
251
        c.Assert(err, gc.ErrorMatches, "charm in use")
 
252
        err = unit.SetCharmURL(info.ID)
 
253
        c.Assert(err, jc.ErrorIsNil)
 
254
        err = s.charm.Destroy()
 
255
        c.Assert(err, jc.ErrorIsNil)
 
256
}
 
257
 
 
258
func (s *CharmSuite) TestDestroyFinalUnitReference(c *gc.C) {
 
259
        app := s.Factory.MakeApplication(c, &factory.ApplicationParams{
 
260
                Charm: s.charm,
 
261
        })
 
262
        unit := s.Factory.MakeUnit(c, &factory.UnitParams{
 
263
                Application: app,
 
264
                SetCharmURL: true,
 
265
        })
 
266
 
 
267
        err := app.Destroy()
 
268
        c.Assert(err, jc.ErrorIsNil)
 
269
        removeUnit(c, unit)
 
270
 
 
271
        assertCleanupCount(c, s.State, 1)
 
272
        s.checkRemoved(c)
 
273
}
 
274
 
100
275
func (s *CharmSuite) TestAddCharm(c *gc.C) {
101
276
        // Check that adding charms from scratch works correctly.
102
277
        info := s.dummyCharm(c, "")
210
385
        c.Assert(curl.Revision, gc.Equals, 1234)
211
386
}
212
387
 
 
388
func (s *CharmSuite) TestPrepareLocalCharmUploadRemoved(c *gc.C) {
 
389
        // Remove the fixture charm and try to re-add it; it gets a new
 
390
        // revision.
 
391
        s.remove(c)
 
392
        curl, err := s.State.PrepareLocalCharmUpload(s.curl)
 
393
        c.Assert(err, jc.ErrorIsNil)
 
394
        c.Assert(curl.Revision, gc.Equals, s.curl.Revision+1)
 
395
}
 
396
 
213
397
func (s *CharmSuite) TestPrepareStoreCharmUpload(c *gc.C) {
214
398
        // First test the sanity checks.
215
399
        sch, err := s.State.PrepareStoreCharmUpload(charm.MustParseURL("cs:quantal/dummy"))