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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cloudconfig/userdatacfg_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
import (
8
8
        "encoding/base64"
 
9
        "encoding/json"
9
10
        "fmt"
 
11
        "io/ioutil"
10
12
        "path"
 
13
        "path/filepath"
11
14
        "regexp"
12
15
        "strings"
13
16
 
16
19
        jc "github.com/juju/testing/checkers"
17
20
        pacconf "github.com/juju/utils/packaging/config"
18
21
        "github.com/juju/utils/set"
 
22
        "github.com/juju/version"
19
23
        gc "gopkg.in/check.v1"
20
24
        goyaml "gopkg.in/yaml.v2"
21
25
 
34
38
        "github.com/juju/juju/state/multiwatcher"
35
39
        "github.com/juju/juju/testing"
36
40
        "github.com/juju/juju/tools"
37
 
        "github.com/juju/juju/version"
38
41
)
39
42
 
40
43
type cloudinitSuite struct {
159
162
        return cfg
160
163
}
161
164
 
 
165
// setGUI populates the configuration with the Juju GUI tools.
 
166
func (cfg *testInstanceConfig) setGUI(path string) *testInstanceConfig {
 
167
        cfg.GUI = &tools.GUIArchive{
 
168
                URL:     "file://" + filepath.ToSlash(path),
 
169
                Version: version.MustParse("1.2.3"),
 
170
                Size:    42,
 
171
                SHA256:  "1234",
 
172
        }
 
173
        return cfg
 
174
}
 
175
 
162
176
// maybeSetModelConfig sets the Config field to the given envConfig, if not
163
177
// nil.
164
178
func (cfg *testInstanceConfig) maybeSetModelConfig(envConfig *config.Config) *testInstanceConfig {
165
179
        if envConfig != nil {
166
180
                cfg.Config = envConfig
 
181
                if cfg.Bootstrap {
 
182
                        cfg.HostedModelConfig = map[string]interface{}{"name": "hosted-model"}
 
183
                }
167
184
        }
168
185
        return cfg
169
186
}
192
209
func (cfg *testInstanceConfig) setController() *testInstanceConfig {
193
210
        cfg.setMachineID("0")
194
211
        cfg.Constraints = bootstrapConstraints
195
 
        cfg.EnvironConstraints = envConstraints
 
212
        cfg.ModelConstraints = envConstraints
196
213
        cfg.Bootstrap = true
197
214
        cfg.StateServingInfo = stateServingInfo
198
215
        cfg.Jobs = allMachineJobs
309
326
cat > '/var/lib/juju/agents/machine-0/agent\.conf' << 'EOF'\\n.*\\nEOF
310
327
chmod 0600 '/var/lib/juju/agents/machine-0/agent\.conf'
311
328
echo 'Bootstrapping Juju machine agent'.*
312
 
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --constraints 'mem=2048M' --debug
 
329
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --hosted-model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --constraints 'mem=2048M' --debug
313
330
ln -s 1\.2\.3-precise-amd64 '/var/lib/juju/tools/machine-0'
314
331
echo 'Starting Juju machine agent \(jujud-machine-0\)'.*
315
332
cat > /etc/init/jujud-machine-0\.conf << 'EOF'\\ndescription "juju agent for machine-0"\\nauthor "Juju Team <juju@lists\.ubuntu\.com>"\\nstart on runlevel \[2345\]\\nstop on runlevel \[!2345\]\\nrespawn\\nnormal exit 0\\n\\nlimit nofile 20000 20000\\n\\nscript\\n\\n\\n  # Ensure log files are properly protected\\n  touch /var/log/juju/machine-0\.log\\n  chown syslog:syslog /var/log/juju/machine-0\.log\\n  chmod 0600 /var/log/juju/machine-0\.log\\n\\n  exec '/var/lib/juju/tools/machine-0/jujud' machine --data-dir '/var/lib/juju' --machine-id 0 --debug >> /var/log/juju/machine-0\.log 2>&1\\nend script\\nEOF\\n
329
346
sha256sum \$bin/tools\.tar\.gz > \$bin/juju1\.2\.3-raring-amd64\.sha256
330
347
grep '1234' \$bin/juju1\.2\.3-raring-amd64.sha256 \|\| \(echo "Tools checksum mismatch"; exit 1\)
331
348
printf %s '{"version":"1\.2\.3-raring-amd64","url":"http://foo\.com/tools/released/juju1\.2\.3-raring-amd64\.tgz","sha256":"1234","size":10}' > \$bin/downloaded-tools\.txt
332
 
/var/lib/juju/tools/1\.2\.3-raring-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --constraints 'mem=2048M' --debug
 
349
/var/lib/juju/tools/1\.2\.3-raring-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --hosted-model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --constraints 'mem=2048M' --debug
333
350
ln -s 1\.2\.3-raring-amd64 '/var/lib/juju/tools/machine-0'
334
351
rm \$bin/tools\.tar\.gz && rm \$bin/juju1\.2\.3-raring-amd64\.sha256
335
352
`,
384
401
`,
385
402
        },
386
403
 
 
404
        // non controller with GUI (the GUI is not installed)
 
405
        {
 
406
                cfg: makeNormalConfig("quantal").setGUI("/path/to/gui.tar.bz2"),
 
407
                expectScripts: `
 
408
set -xe
 
409
install -D -m 644 /dev/null '/etc/init/juju-clean-shutdown\.conf'
 
410
printf '%s\\n' '.*"Stop all network interfaces on shutdown".*' > '/etc/init/juju-clean-shutdown\.conf'
 
411
install -D -m 644 /dev/null '/var/lib/juju/nonce.txt'
 
412
printf '%s\\n' 'FAKE_NONCE' > '/var/lib/juju/nonce.txt'
 
413
test -e /proc/self/fd/9 \|\| exec 9>&2
 
414
\(\[ ! -e /home/ubuntu/\.profile \] \|\| grep -q '.juju-proxy' /home/ubuntu/.profile\) \|\| printf .* >> /home/ubuntu/.profile
 
415
mkdir -p /var/lib/juju/locks
 
416
\(id ubuntu &> /dev/null\) && chown ubuntu:ubuntu /var/lib/juju/locks
 
417
mkdir -p /var/log/juju
 
418
chown syslog:adm /var/log/juju
 
419
bin='/var/lib/juju/tools/1\.2\.3-quantal-amd64'
 
420
mkdir -p \$bin
 
421
echo 'Fetching tools.*
 
422
curl .* --noproxy "\*" --insecure -o \$bin/tools\.tar\.gz 'https://state-addr\.testing\.invalid:54321/tools/1\.2\.3-quantal-amd64'
 
423
sha256sum \$bin/tools\.tar\.gz > \$bin/juju1\.2\.3-quantal-amd64\.sha256
 
424
grep '1234' \$bin/juju1\.2\.3-quantal-amd64.sha256 \|\| \(echo "Tools checksum mismatch"; exit 1\)
 
425
tar zxf \$bin/tools.tar.gz -C \$bin
 
426
printf %s '{"version":"1\.2\.3-quantal-amd64","url":"http://foo\.com/tools/released/juju1\.2\.3-quantal-amd64\.tgz","sha256":"1234","size":10}' > \$bin/downloaded-tools\.txt
 
427
mkdir -p '/var/lib/juju/agents/machine-99'
 
428
cat > '/var/lib/juju/agents/machine-99/agent\.conf' << 'EOF'\\n.*\\nEOF
 
429
chmod 0600 '/var/lib/juju/agents/machine-99/agent\.conf'
 
430
ln -s 1\.2\.3-quantal-amd64 '/var/lib/juju/tools/machine-99'
 
431
echo 'Starting Juju machine agent \(jujud-machine-99\)'.*
 
432
cat > /etc/init/jujud-machine-99\.conf << 'EOF'\\ndescription "juju agent for machine-99"\\nauthor "Juju Team <juju@lists\.ubuntu\.com>"\\nstart on runlevel \[2345\]\\nstop on runlevel \[!2345\]\\nrespawn\\nnormal exit 0\\n\\nlimit nofile 20000 20000\\n\\nscript\\n\\n\\n  # Ensure log files are properly protected\\n  touch /var/log/juju/machine-99\.log\\n  chown syslog:syslog /var/log/juju/machine-99\.log\\n  chmod 0600 /var/log/juju/machine-99\.log\\n\\n  exec '/var/lib/juju/tools/machine-99/jujud' machine --data-dir '/var/lib/juju' --machine-id 99 --debug >> /var/log/juju/machine-99\.log 2>&1\\nend script\\nEOF\\n
 
433
start jujud-machine-99
 
434
rm \$bin/tools\.tar\.gz && rm \$bin/juju1\.2\.3-quantal-amd64\.sha256
 
435
`,
 
436
        },
 
437
 
387
438
        // CentOS non controller with systemd
388
439
        {
389
440
                cfg:          makeNormalConfig("centos7"),
430
481
                setEnvConfig: true,
431
482
                inexactMatch: true,
432
483
                expectScripts: `
433
 
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --instance-id 'i-bootstrap' --constraints 'mem=2048M' --debug
 
484
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --hosted-model-config '[^']*' --instance-id 'i-bootstrap' --constraints 'mem=2048M' --debug
434
485
`,
435
486
        },
436
487
 
437
488
        // empty environ contraints.
438
489
        {
439
490
                cfg: makeBootstrapConfig("precise").mutate(func(cfg *testInstanceConfig) {
440
 
                        cfg.EnvironConstraints = constraints.Value{}
 
491
                        cfg.ModelConstraints = constraints.Value{}
441
492
                }),
442
493
                setEnvConfig: true,
443
494
                inexactMatch: true,
444
495
                expectScripts: `
445
 
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --debug
 
496
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --data-dir '/var/lib/juju' --model-config '[^']*' --hosted-model-config '[^']*' --instance-id 'i-bootstrap' --bootstrap-constraints 'mem=4096M' --debug
446
497
`,
447
498
        },
448
499
 
593
644
        }
594
645
}
595
646
 
 
647
func (*cloudinitSuite) TestCloudInitWithGUI(c *gc.C) {
 
648
        guiPath := path.Join(c.MkDir(), "gui.tar.bz2")
 
649
        err := ioutil.WriteFile(guiPath, []byte("content"), 0644)
 
650
        cfg := makeBootstrapConfig("precise").setGUI(guiPath)
 
651
        envConfig := minimalModelConfig(c)
 
652
        testConfig := cfg.maybeSetModelConfig(envConfig).render()
 
653
        ci, err := cloudinit.New(testConfig.Series)
 
654
        c.Assert(err, jc.ErrorIsNil)
 
655
        udata, err := cloudconfig.NewUserdataConfig(&testConfig, ci)
 
656
        c.Assert(err, jc.ErrorIsNil)
 
657
        err = udata.Configure()
 
658
        c.Assert(err, jc.ErrorIsNil)
 
659
        c.Check(ci, gc.NotNil)
 
660
        data, err := ci.RenderYAML()
 
661
        c.Assert(err, jc.ErrorIsNil)
 
662
 
 
663
        configKeyValues := make(map[interface{}]interface{})
 
664
        err = goyaml.Unmarshal(data, &configKeyValues)
 
665
        c.Assert(err, jc.ErrorIsNil)
 
666
 
 
667
        guiJson, err := json.Marshal(cfg.GUI)
 
668
        c.Assert(err, jc.ErrorIsNil)
 
669
 
 
670
        scripts := getScripts(configKeyValues)
 
671
        expectedScripts := regexp.QuoteMeta(fmt.Sprintf(`sha256sum $gui/gui.tar.bz2 > $gui/jujugui.sha256
 
672
grep '1234' $gui/jujugui.sha256 || (echo Juju GUI checksum mismatch; exit 1)
 
673
printf %%s '%s' > $gui/downloaded-gui.txt
 
674
rm $gui/gui.tar.bz2 $gui/jujugui.sha256 $gui/downloaded-gui.txt
 
675
`, guiJson))
 
676
        assertScriptMatch(c, scripts, expectedScripts, false)
 
677
}
 
678
 
 
679
func (*cloudinitSuite) TestCloudInitWithGUIError(c *gc.C) {
 
680
        cfg := makeBootstrapConfig("precise").setGUI("/path/to/gui.tar.bz2")
 
681
        envConfig := minimalModelConfig(c)
 
682
        testConfig := cfg.maybeSetModelConfig(envConfig).render()
 
683
        ci, err := cloudinit.New(testConfig.Series)
 
684
        c.Assert(err, jc.ErrorIsNil)
 
685
        udata, err := cloudconfig.NewUserdataConfig(&testConfig, ci)
 
686
        c.Assert(err, jc.ErrorIsNil)
 
687
        err = udata.Configure()
 
688
        c.Assert(err, gc.ErrorMatches, "cannot fetch Juju GUI: cannot read Juju GUI archive: .*")
 
689
}
 
690
 
596
691
func (*cloudinitSuite) TestCloudInitConfigure(c *gc.C) {
597
692
        for i, test := range cloudinitTests {
598
693
                testConfig := test.cfg.maybeSetModelConfig(minimalModelConfig(c)).render()
631
726
                }
632
727
        }
633
728
        expected := "jujud bootstrap-state --data-dir '.*' --model-config '.*'" +
 
729
                " --hosted-model-config '[^']*'" +
634
730
                " --instance-id '.*' --bootstrap-constraints 'mem=4096M'" +
635
731
                " --constraints 'mem=2048M' --show-log"
636
732
        assertScriptMatch(c, scripts, expected, false)
976
1072
                        ModelTag: testing.ModelTag,
977
1073
                },
978
1074
                Config:                  minimalModelConfig(c),
 
1075
                HostedModelConfig:       map[string]interface{}{"name": "hosted-model"},
979
1076
                DataDir:                 jujuDataDir("quantal"),
980
1077
                LogDir:                  jujuLogDir("quantal"),
981
1078
                MetricsSpoolDir:         metricsSpoolDir("quantal"),