~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to cloudinit/cloudinit_test.go

  • Committer: Roger Peppe
  • Date: 2011-12-07 17:03:34 UTC
  • mto: (25.3.4 go-trunk)
  • mto: This revision was merged to the branch mainline in revision 27.
  • Revision ID: roger.peppe@canonical.com-20111207170334-soasb88g2x5mpkf5
add cloudinit package

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/go/cloudinit"
 
7
        "testing"
 
8
)
 
9
// TODO integration tests, but how?
 
10
 
 
11
type S struct{}
 
12
 
 
13
var _ = Suite(S{})
 
14
 
 
15
func Test1(t *testing.T) {
 
16
        TestingT(t)
 
17
}
 
18
 
 
19
var ctests = []struct {
 
20
        expect string
 
21
        name   string
 
22
        option cloudinit.Option
 
23
}{
 
24
        {"user: me\n",
 
25
                "User", cloudinit.User("me")},
 
26
        {"apt_upgrade: true\n",
 
27
                "AptUpgrade", cloudinit.AptUpgrade(true)},
 
28
        {"apt_update: true\n",
 
29
                "AptUpdate", cloudinit.AptUpdate(true)},
 
30
        {"apt_mirror: http://foo.com\n",
 
31
                "AptMirror", cloudinit.AptMirror("http://foo.com")},
 
32
        {"apt_mirror: true\n",
 
33
                "AptPreserveSourcesList", cloudinit.AptPreserveSourcesList(true)},
 
34
        {"apt_old_mirror: http://foo.com\n",
 
35
                "AptOldMirror", cloudinit.AptOldMirror("http://foo.com")},
 
36
        {"apt_sources:\n- source: keyName\n  key: someKey\n",
 
37
                "AptSources", cloudinit.AptSources(cloudinit.NewSource("keyName", "someKey"))},
 
38
        {"apt_sources:\n- source: keyName\n  keyid: someKey\n  keyserver: foo.com\n",
 
39
                "AptSources", cloudinit.AptSources(cloudinit.NewSourceWithKeyId("keyName", "someKey", "foo.com"))},
 
40
        {"debconf_selections: true\n",
 
41
                "DebConfSelections", cloudinit.DebConfSelections(true)},
 
42
        {"packages:\n- juju\n- ubuntu\n",
 
43
                "Packages", cloudinit.Packages("juju", "ubuntu")},
 
44
        {"bootcmd:\n- ls > /dev\n- - ls\n  - '>with space'\n",
 
45
                "BootCmd", cloudinit.BootCmd(cloudinit.NewLiteralCommand("ls > /dev"), cloudinit.NewArgListCommand("ls", ">with space"))},
 
46
        {"disable_ec2_metadata: true\n",
 
47
                "DisableEC2Metadata", cloudinit.DisableEC2Metadata(true)},
 
48
        {"final_message: goodbye\n",
 
49
                "FinalMessage", cloudinit.FinalMessage("goodbye")},
 
50
        {"locale: en_us\n",
 
51
                "Locale", cloudinit.Locale("en_us")},
 
52
        {"mounts:\n- - x\n  - \"y\"\n- - z\n  - w\n",
 
53
                "Mounts", cloudinit.Mounts([][]string{{"x", "y"}, {"z", "w"}})},
 
54
        {"output:\n  all:\n    stdout: '>foo'\n    stderr: '|bar'\n",
 
55
                "Output", cloudinit.Output(map[string]cloudinit.OutputSpec{"all": {">foo", "|bar"}})},
 
56
        {"ssh_keys:\n- - rsa_private\n  - key1data\n- - dsa_public\n  - key2data\n",
 
57
                "SSHKeys", cloudinit.SSHKeys([]cloudinit.Key{
 
58
                        {cloudinit.RSA | cloudinit.Private, "key1data"},
 
59
                        {cloudinit.DSA | cloudinit.Public, "key2data"},
 
60
                })},
 
61
        {"disable_root: false\n",
 
62
                "DisableRoot", cloudinit.DisableRoot(false)},
 
63
        {"ssh_authorized_keys:\n- key1\n- key2\n",
 
64
                "SSHAuthorizedKeys", cloudinit.SSHAuthorizedKeys("key1", "key2")},
 
65
}
 
66
 
 
67
const header = "#cloud-config\n"
 
68
 
 
69
func (S) TestOutput(c *C) {
 
70
        for _, t := range ctests {
 
71
                cfg := cloudinit.New()
 
72
                cfg.Set(t.option)
 
73
                data, err := cfg.Render()
 
74
                c.Assert(err, IsNil)
 
75
                c.Assert(data, NotNil)
 
76
                c.Assert(string(data), Equals, header+t.expect, Bug("test %q output differs", t.name))
 
77
        }
 
78
}
 
79
 
 
80
var atests = []struct {
 
81
        expect  string
 
82
        name    string
 
83
        options []cloudinit.Option
 
84
}{
 
85
        {"ssh_authorized_keys:\n- key1\n- key2\n- key3\n- key4\n",
 
86
                "SSHAuthorizedKeys",
 
87
                []cloudinit.Option{
 
88
                        cloudinit.SSHAuthorizedKeys("key1", "key2"),
 
89
                        cloudinit.SSHAuthorizedKeys("key3", "key4"),
 
90
                },
 
91
        },
 
92
        {"apt_sources:\n- source: keyName\n  keyid: someKey\n  keyserver: foo.com\n- source: keyName\n  key: someKey\n",
 
93
                "AptSources",
 
94
                []cloudinit.Option{
 
95
                        cloudinit.AptSources(cloudinit.NewSourceWithKeyId("keyName", "someKey", "foo.com")),
 
96
                        cloudinit.AptSources(cloudinit.NewSource("keyName", "someKey")),
 
97
                },
 
98
        },
 
99
}
 
100
 
 
101
func (S) TestAppend(c *C) {
 
102
        for _, t := range atests {
 
103
                cfg := cloudinit.New()
 
104
                cfg.Set(t.options[0])
 
105
                for _, o := range t.options[1:] {
 
106
                        cfg.Append(o)
 
107
                }
 
108
                data, err := cfg.Render()
 
109
                c.Assert(err, IsNil)
 
110
                c.Assert(data, NotNil)
 
111
                c.Assert(string(data), Equals, header+t.expect, Bug("test %q output differs", t.name))
 
112
        }
 
113
}
 
114
 
 
115
//#cloud-config
 
116
//packages:
 
117
//- juju
 
118
//- ubuntu
 
119
func ExampleConfig() {
 
120
        cfg := cloudinit.New()
 
121
        cfg.Set(cloudinit.Packages("juju", "ubuntu"))
 
122
        data, err := cfg.Render()
 
123
        if err != nil {
 
124
                fmt.Printf("render error: %v", err)
 
125
                return
 
126
        }
 
127
        fmt.Printf("%s", data)
 
128
}