~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cloudinit/cloudinit_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package cloudinit_test
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        . "launchpad.net/gocheck"
 
6
        "launchpad.net/juju-core/cloudinit"
 
7
        "testing"
 
8
)
 
9
 
 
10
// TODO integration tests, but how?
 
11
 
 
12
type S struct{}
 
13
 
 
14
var _ = Suite(S{})
 
15
 
 
16
func Test1(t *testing.T) {
 
17
        TestingT(t)
 
18
}
 
19
 
 
20
var ctests = []struct {
 
21
        name      string
 
22
        expect    string
 
23
        setOption func(cfg *cloudinit.Config)
 
24
}{
 
25
        {
 
26
                "User",
 
27
                "user: me\n",
 
28
                func(cfg *cloudinit.Config) {
 
29
                        cfg.SetUser("me")
 
30
                },
 
31
        },
 
32
        {
 
33
                "AptUpgrade",
 
34
                "apt_upgrade: true\n",
 
35
                func(cfg *cloudinit.Config) {
 
36
                        cfg.SetAptUpgrade(true)
 
37
                },
 
38
        },
 
39
        {
 
40
                "AptUpdate",
 
41
                "apt_update: true\n",
 
42
                func(cfg *cloudinit.Config) {
 
43
                        cfg.SetAptUpdate(true)
 
44
                },
 
45
        },
 
46
        {
 
47
                "AptMirror",
 
48
                "apt_mirror: http://foo.com\n",
 
49
                func(cfg *cloudinit.Config) {
 
50
                        cfg.SetAptMirror("http://foo.com")
 
51
                },
 
52
        },
 
53
        {
 
54
                "AptPreserveSourcesList",
 
55
                "apt_mirror: true\n",
 
56
                func(cfg *cloudinit.Config) {
 
57
                        cfg.SetAptPreserveSourcesList(true)
 
58
                },
 
59
        },
 
60
        {
 
61
                "DebconfSelections",
 
62
                "debconf_selections: '# Force debconf priority to critical.\n\n  debconf debconf/priority select critical\n\n'\n",
 
63
                func(cfg *cloudinit.Config) {
 
64
                        cfg.SetDebconfSelections("# Force debconf priority to critical.\ndebconf debconf/priority select critical\n")
 
65
                },
 
66
        },
 
67
        {
 
68
                "DisableEC2Metadata",
 
69
                "disable_ec2_metadata: true\n",
 
70
                func(cfg *cloudinit.Config) {
 
71
                        cfg.SetDisableEC2Metadata(true)
 
72
                },
 
73
        },
 
74
        {
 
75
                "FinalMessage",
 
76
                "final_message: goodbye\n",
 
77
                func(cfg *cloudinit.Config) {
 
78
                        cfg.SetFinalMessage("goodbye")
 
79
                },
 
80
        },
 
81
        {
 
82
                "Locale",
 
83
                "locale: en_us\n",
 
84
                func(cfg *cloudinit.Config) {
 
85
                        cfg.SetLocale("en_us")
 
86
                },
 
87
        },
 
88
        {
 
89
                "DisableRoot",
 
90
                "disable_root: false\n",
 
91
                func(cfg *cloudinit.Config) {
 
92
                        cfg.SetDisableRoot(false)
 
93
                },
 
94
        },
 
95
        {
 
96
                "SSHAuthorizedKeys",
 
97
                "ssh_authorized_keys:\n- key1\n- key2\n",
 
98
                func(cfg *cloudinit.Config) {
 
99
                        cfg.AddSSHAuthorizedKeys("key1")
 
100
                        cfg.AddSSHAuthorizedKeys("key2")
 
101
                },
 
102
        },
 
103
        {
 
104
                "SSHAuthorizedKeys",
 
105
                "ssh_authorized_keys:\n- key1\n- key2\n- key3\n",
 
106
                func(cfg *cloudinit.Config) {
 
107
                        cfg.AddSSHAuthorizedKeys("#command\nkey1")
 
108
                        cfg.AddSSHAuthorizedKeys("key2\n# comment\n\nkey3\n")
 
109
                        cfg.AddSSHAuthorizedKeys("")
 
110
                },
 
111
        },
 
112
        {
 
113
                "SSHKeys RSAPrivate",
 
114
                "ssh_keys:\n  rsa_private: key1data\n",
 
115
                func(cfg *cloudinit.Config) {
 
116
                        cfg.AddSSHKey(cloudinit.RSAPrivate, "key1data")
 
117
                },
 
118
        },
 
119
        {
 
120
                "SSHKeys RSAPublic",
 
121
                "ssh_keys:\n  rsa_public: key2data\n",
 
122
                func(cfg *cloudinit.Config) {
 
123
                        cfg.AddSSHKey(cloudinit.RSAPublic, "key2data")
 
124
                },
 
125
        },
 
126
        {
 
127
                "SSHKeys DSAPublic",
 
128
                "ssh_keys:\n  dsa_public: key1data\n",
 
129
                func(cfg *cloudinit.Config) {
 
130
                        cfg.AddSSHKey(cloudinit.DSAPublic, "key1data")
 
131
                },
 
132
        },
 
133
        {
 
134
                "SSHKeys DSAPrivate",
 
135
                "ssh_keys:\n  dsa_private: key2data\n",
 
136
                func(cfg *cloudinit.Config) {
 
137
                        cfg.AddSSHKey(cloudinit.DSAPrivate, "key2data")
 
138
                },
 
139
        },
 
140
        {
 
141
                "Output",
 
142
                "output:\n  all:\n  - '>foo'\n  - '|bar'\n",
 
143
                func(cfg *cloudinit.Config) {
 
144
                        cfg.SetOutput("all", ">foo", "|bar")
 
145
                },
 
146
        },
 
147
        {
 
148
                "Output",
 
149
                "output:\n  all: '>foo'\n",
 
150
                func(cfg *cloudinit.Config) {
 
151
                        cfg.SetOutput(cloudinit.OutAll, ">foo", "")
 
152
                },
 
153
        },
 
154
        {
 
155
                "AptSources",
 
156
                "apt_sources:\n- source: keyName\n  key: someKey\n",
 
157
                func(cfg *cloudinit.Config) {
 
158
                        cfg.AddAptSource("keyName", "someKey")
 
159
                },
 
160
        },
 
161
        {
 
162
                "AptSources",
 
163
                "apt_sources:\n- source: keyName\n  keyid: someKey\n  keyserver: foo.com\n",
 
164
                func(cfg *cloudinit.Config) {
 
165
                        cfg.AddAptSourceWithKeyId("keyName", "someKey", "foo.com")
 
166
                },
 
167
        },
 
168
        {
 
169
                "Packages",
 
170
                "packages:\n- juju\n- ubuntu\n",
 
171
                func(cfg *cloudinit.Config) {
 
172
                        cfg.AddPackage("juju")
 
173
                        cfg.AddPackage("ubuntu")
 
174
                },
 
175
        },
 
176
        {
 
177
                "BootCmd",
 
178
                "bootcmd:\n- ls > /dev\n- - ls\n  - '>with space'\n",
 
179
                func(cfg *cloudinit.Config) {
 
180
                        cfg.AddBootCmd("ls > /dev")
 
181
                        cfg.AddBootCmdArgs("ls", ">with space")
 
182
                },
 
183
        },
 
184
        {
 
185
                "Mounts",
 
186
                "mounts:\n- - x\n  - \"y\"\n- - z\n  - w\n",
 
187
                func(cfg *cloudinit.Config) {
 
188
                        cfg.AddMount("x", "y")
 
189
                        cfg.AddMount("z", "w")
 
190
                },
 
191
        },
 
192
        {
 
193
                "Attr",
 
194
                "arbitraryAttr: someValue\n",
 
195
                func(cfg *cloudinit.Config) {
 
196
                        cfg.SetAttr("arbitraryAttr", "someValue")
 
197
                },
 
198
        },
 
199
}
 
200
 
 
201
const header = "#cloud-config\n"
 
202
 
 
203
func (S) TestOutput(c *C) {
 
204
        for _, t := range ctests {
 
205
                cfg := cloudinit.New()
 
206
                t.setOption(cfg)
 
207
                data, err := cfg.Render()
 
208
                c.Assert(err, IsNil)
 
209
                c.Assert(data, NotNil)
 
210
                c.Assert(string(data), Equals, header+t.expect, Commentf("test %q output differs", t.name))
 
211
        }
 
212
}
 
213
 
 
214
//#cloud-config
 
215
//packages:
 
216
//- juju
 
217
//- ubuntu
 
218
func ExampleConfig() {
 
219
        cfg := cloudinit.New()
 
220
        cfg.AddPackage("juju")
 
221
        cfg.AddPackage("ubuntu")
 
222
        data, err := cfg.Render()
 
223
        if err != nil {
 
224
                fmt.Printf("render error: %v", err)
 
225
                return
 
226
        }
 
227
        fmt.Printf("%s", data)
 
228
}