~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/payload/context/register_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 details.
 
3
 
 
4
package context
 
5
 
 
6
import (
 
7
        "io/ioutil"
 
8
        "path/filepath"
 
9
 
 
10
        "github.com/juju/cmd"
 
11
        "github.com/juju/errors"
 
12
        "github.com/juju/testing"
 
13
        jc "github.com/juju/testing/checkers"
 
14
        gc "gopkg.in/check.v1"
 
15
        "gopkg.in/juju/charm.v6-unstable"
 
16
 
 
17
        "github.com/juju/juju/payload"
 
18
)
 
19
 
 
20
type registerSuite struct {
 
21
        testing.IsolationSuite
 
22
}
 
23
 
 
24
var _ = gc.Suite(&registerSuite{})
 
25
 
 
26
func (registerSuite) TestInitNilArgs(c *gc.C) {
 
27
        r := RegisterCmd{}
 
28
        err := r.Init(nil)
 
29
        c.Assert(err, gc.NotNil)
 
30
}
 
31
 
 
32
func (registerSuite) TestInitTooFewArgs(c *gc.C) {
 
33
        r := RegisterCmd{}
 
34
        err := r.Init([]string{"foo", "bar"})
 
35
        c.Assert(err, gc.NotNil)
 
36
}
 
37
 
 
38
func (registerSuite) TestInit(c *gc.C) {
 
39
        r := RegisterCmd{}
 
40
        err := r.Init([]string{"type", "class", "id"})
 
41
        c.Assert(err, jc.ErrorIsNil)
 
42
        c.Assert(r.typ, gc.Equals, "type")
 
43
        c.Assert(r.class, gc.Equals, "class")
 
44
        c.Assert(r.id, gc.Equals, "id")
 
45
        c.Assert(r.labels, gc.HasLen, 0)
 
46
}
 
47
 
 
48
func (registerSuite) TestInitWithLabels(c *gc.C) {
 
49
        r := RegisterCmd{}
 
50
        err := r.Init([]string{"type", "class", "id", "tag1", "tag 2"})
 
51
        c.Assert(err, jc.ErrorIsNil)
 
52
        c.Assert(r.typ, gc.Equals, "type")
 
53
        c.Assert(r.class, gc.Equals, "class")
 
54
        c.Assert(r.id, gc.Equals, "id")
 
55
        c.Assert(r.labels, gc.DeepEquals, []string{"tag1", "tag 2"})
 
56
}
 
57
 
 
58
func (registerSuite) TestRun(c *gc.C) {
 
59
        f := &stubRegisterContext{}
 
60
        r := RegisterCmd{hctx: f}
 
61
        err := r.Init([]string{"type", "class", "id", "tag1", "tag 2"})
 
62
        c.Assert(err, jc.ErrorIsNil)
 
63
 
 
64
        ctx := setupMetadata(c)
 
65
        err = r.Run(ctx)
 
66
        c.Assert(err, jc.ErrorIsNil)
 
67
        c.Check(f.flushed, jc.IsTrue)
 
68
        c.Check(f.payload, jc.DeepEquals, payload.Payload{
 
69
                PayloadClass: charm.PayloadClass{
 
70
                        Name: "class",
 
71
                        Type: "type",
 
72
                },
 
73
                ID:     "id",
 
74
                Status: payload.StateRunning,
 
75
                Labels: []string{"tag1", "tag 2"},
 
76
                Unit:   "a-application/0",
 
77
        })
 
78
        // TODO (natefinch): we need to do something with the labels
 
79
}
 
80
 
 
81
func (registerSuite) TestRunUnknownClass(c *gc.C) {
 
82
        f := &stubRegisterContext{}
 
83
        r := RegisterCmd{hctx: f}
 
84
        err := r.Init([]string{"type", "badclass", "id"})
 
85
        c.Assert(err, jc.ErrorIsNil)
 
86
 
 
87
        ctx := setupMetadata(c)
 
88
        err = r.Run(ctx)
 
89
        c.Assert(err, gc.ErrorMatches, "payload \"badclass\" not found in metadata.yaml")
 
90
}
 
91
 
 
92
func (registerSuite) TestRunUnknownType(c *gc.C) {
 
93
        f := &stubRegisterContext{}
 
94
        r := RegisterCmd{hctx: f}
 
95
        err := r.Init([]string{"badtype", "class", "id"})
 
96
        c.Assert(err, jc.ErrorIsNil)
 
97
 
 
98
        ctx := setupMetadata(c)
 
99
        err = r.Run(ctx)
 
100
        c.Assert(err, gc.ErrorMatches, "incorrect type \"badtype\" for payload \"class\", expected \"type\"")
 
101
}
 
102
 
 
103
func (registerSuite) TestRunTrackErr(c *gc.C) {
 
104
        f := &stubRegisterContext{trackerr: errors.Errorf("boo")}
 
105
        r := RegisterCmd{hctx: f}
 
106
        err := r.Init([]string{"type", "class", "id", "tag1", "tag 2"})
 
107
        c.Assert(err, jc.ErrorIsNil)
 
108
 
 
109
        ctx := setupMetadata(c)
 
110
        err = r.Run(ctx)
 
111
        c.Assert(err, gc.ErrorMatches, "boo")
 
112
}
 
113
 
 
114
func (registerSuite) TestRunFlushErr(c *gc.C) {
 
115
        f := &stubRegisterContext{flusherr: errors.Errorf("boo")}
 
116
        r := RegisterCmd{hctx: f}
 
117
        err := r.Init([]string{"type", "class", "id", "tag1", "tag 2"})
 
118
        c.Assert(err, jc.ErrorIsNil)
 
119
 
 
120
        ctx := setupMetadata(c)
 
121
        err = r.Run(ctx)
 
122
        c.Assert(err, gc.ErrorMatches, "boo")
 
123
}
 
124
 
 
125
type stubRegisterContext struct {
 
126
        Component
 
127
        payload  payload.Payload
 
128
        flushed  bool
 
129
        trackerr error
 
130
        flusherr error
 
131
}
 
132
 
 
133
func (f *stubRegisterContext) Track(pl payload.Payload) error {
 
134
        f.payload = pl
 
135
        return f.trackerr
 
136
}
 
137
 
 
138
func (f *stubRegisterContext) Flush() error {
 
139
        f.flushed = true
 
140
        return f.flusherr
 
141
}
 
142
 
 
143
func setupMetadata(c *gc.C) *cmd.Context {
 
144
        dir := c.MkDir()
 
145
        path := filepath.Join(dir, "metadata.yaml")
 
146
        ioutil.WriteFile(path, []byte(metadataContents), 0660)
 
147
        return &cmd.Context{Dir: dir}
 
148
}
 
149
 
 
150
const metadataContents = `name: ducksay
 
151
summary: Testing charm payload management
 
152
maintainer: juju@canonical.com <Juju>
 
153
description: |
 
154
  Testing payloads
 
155
subordinate: false
 
156
payloads:
 
157
  class:
 
158
    type: type
 
159
    lifecycle: ["restart"]
 
160
`