~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to environs/cloudinit_test.go

[r=jtv] Unify providers' user-data composition functions.

All the "physical" providers (EC2, OpenStack, MAAS, Azure) were generating
user data in essentially the same way. The MAAS one looked different but
actually just added an optional bit that was still completely generic. And so
this branch unifies the implementations.

Construction of the cloudinit.MachineConfig was lifted out of the function.
That may seem arbitrary, but it's part of a greater plan: in a later step of
the ongoing refactoring the construction can be lifted out of the providers'
internalStartInstance methods as well, doing away with the EC2 and OpenStack
providers' startInstanceParams structs. This actually shortens some of the
data flows and reduces cognitive load, which can only be good for maintenance.

After that step we can have a fresh look at sanitizing NewMachineConfig (which
arguably should take more parameters, or not exist at all, but is in an
intermediate state that facilitates the changes happening now). We may also
look into re-introducing some sort of dedicated Parameter Object for
internalStartInstance, but by then the cluster of methods Bootstrap /
StartInstance / internalStartInstance will look a bit different and we can
find the most maintainable solution for that new situation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
        "time"
8
8
 
9
9
        . "launchpad.net/gocheck"
 
10
        "launchpad.net/goyaml"
10
11
 
11
12
        "launchpad.net/juju-core/cert"
12
13
        "launchpad.net/juju-core/constraints"
17
18
        "launchpad.net/juju-core/state/api"
18
19
        "launchpad.net/juju-core/testing"
19
20
        "launchpad.net/juju-core/utils"
 
21
        "launchpad.net/juju-core/version"
20
22
)
21
23
 
22
24
type CloudInitSuite struct{}
94
96
        err = cert.Verify(srvCertPEM, []byte(testing.CACert), time.Now().AddDate(10, 0, 1))
95
97
        c.Assert(err, NotNil)
96
98
}
 
99
 
 
100
func (*CloudInitSuite) TestUserData(c *C) {
 
101
        testJujuHome := c.MkDir()
 
102
        defer config.SetJujuHome(config.SetJujuHome(testJujuHome))
 
103
        tools := &state.Tools{
 
104
                URL:    "http://foo.com/tools/juju1.2.3-linux-amd64.tgz",
 
105
                Binary: version.MustParseBinary("1.2.3-linux-amd64"),
 
106
        }
 
107
        envConfig, err := config.New(map[string]interface{}{
 
108
                "type":            "maas",
 
109
                "name":            "foo",
 
110
                "default-series":  "series",
 
111
                "authorized-keys": "keys",
 
112
                "ca-cert":         testing.CACert,
 
113
        })
 
114
        c.Assert(err, IsNil)
 
115
 
 
116
        cfg := &cloudinit.MachineConfig{
 
117
                MachineId:       "10",
 
118
                MachineNonce:    "5432",
 
119
                Tools:           tools,
 
120
                StateServerCert: []byte(testing.ServerCert),
 
121
                StateServerKey:  []byte(testing.ServerKey),
 
122
                StateInfo: &state.Info{
 
123
                        Password: "pw1",
 
124
                        CACert:   []byte("CA CERT\n" + testing.CACert),
 
125
                },
 
126
                APIInfo: &api.Info{
 
127
                        Password: "pw2",
 
128
                        CACert:   []byte("CA CERT\n" + testing.CACert),
 
129
                },
 
130
                DataDir:      environs.DataDir,
 
131
                Config:       envConfig,
 
132
                StatePort:    envConfig.StatePort(),
 
133
                APIPort:      envConfig.APIPort(),
 
134
                StateServer:  true,
 
135
                ProviderType: "dummy",
 
136
        }
 
137
        script1 := "script1"
 
138
        script2 := "script2"
 
139
        scripts := []string{script1, script2}
 
140
        result, err := environs.ComposeUserData(cfg, scripts...)
 
141
        c.Assert(err, IsNil)
 
142
 
 
143
        unzipped, err := utils.Gunzip(result)
 
144
        c.Assert(err, IsNil)
 
145
 
 
146
        config := make(map[interface{}]interface{})
 
147
        err = goyaml.Unmarshal(unzipped, &config)
 
148
        c.Assert(err, IsNil)
 
149
 
 
150
        // Just check that the cloudinit config looks good.
 
151
        c.Check(config["apt_upgrade"], Equals, true)
 
152
        // The scripts given to userData where added as the first
 
153
        // commands to be run.
 
154
        runCmd := config["runcmd"].([]interface{})
 
155
        c.Check(runCmd[0], Equals, script1)
 
156
        c.Check(runCmd[1], Equals, script2)
 
157
}