~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/manual/config_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 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package manual
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "regexp"
 
9
 
 
10
        jc "github.com/juju/testing/checkers"
 
11
        gc "gopkg.in/check.v1"
 
12
 
 
13
        "github.com/juju/juju/environs"
 
14
        "github.com/juju/juju/environs/config"
 
15
        coretesting "github.com/juju/juju/testing"
 
16
)
 
17
 
 
18
type configSuite struct {
 
19
        coretesting.FakeJujuXDGDataHomeSuite
 
20
}
 
21
 
 
22
var _ = gc.Suite(&configSuite{})
 
23
 
 
24
func CloudSpec() environs.CloudSpec {
 
25
        return environs.CloudSpec{
 
26
                Name:     "manual",
 
27
                Type:     "manual",
 
28
                Endpoint: "hostname",
 
29
        }
 
30
}
 
31
 
 
32
func MinimalConfigValues() map[string]interface{} {
 
33
        return map[string]interface{}{
 
34
                "name":            "test",
 
35
                "type":            "manual",
 
36
                "uuid":            coretesting.ModelTag.Id(),
 
37
                "controller-uuid": coretesting.ModelTag.Id(),
 
38
                "firewall-mode":   "instance",
 
39
                "bootstrap-user":  "",
 
40
                // While the ca-cert bits aren't entirely minimal, they avoid the need
 
41
                // to set up a fake home.
 
42
                "ca-cert":        coretesting.CACert,
 
43
                "ca-private-key": coretesting.CAKey,
 
44
        }
 
45
}
 
46
 
 
47
func MinimalConfig(c *gc.C) *config.Config {
 
48
        minimal := MinimalConfigValues()
 
49
        testConfig, err := config.New(config.UseDefaults, minimal)
 
50
        c.Assert(err, jc.ErrorIsNil)
 
51
        return testConfig
 
52
}
 
53
 
 
54
func getModelConfig(c *gc.C, attrs map[string]interface{}) *environConfig {
 
55
        testConfig, err := config.New(config.UseDefaults, attrs)
 
56
        c.Assert(err, jc.ErrorIsNil)
 
57
        envConfig, err := manualProvider{}.validate(testConfig, nil)
 
58
        c.Assert(err, jc.ErrorIsNil)
 
59
        return envConfig
 
60
}
 
61
 
 
62
func (s *configSuite) TestConfigMutability(c *gc.C) {
 
63
        testConfig := MinimalConfig(c)
 
64
        valid, err := manualProvider{}.Validate(testConfig, nil)
 
65
        c.Assert(err, jc.ErrorIsNil)
 
66
        unknownAttrs := valid.UnknownAttrs()
 
67
 
 
68
        // Make sure the immutable values can't be changed. It'd be nice to be
 
69
        // able to change these, but that would involve somehow updating the
 
70
        // machine agent's config/upstart config.
 
71
        oldConfig := testConfig
 
72
        for k, v := range map[string]interface{}{
 
73
                "bootstrap-user": "new-username",
 
74
        } {
 
75
                testConfig = MinimalConfig(c)
 
76
                testConfig, err = testConfig.Apply(map[string]interface{}{k: v})
 
77
                c.Assert(err, jc.ErrorIsNil)
 
78
                _, err := manualProvider{}.Validate(testConfig, oldConfig)
 
79
                oldv := unknownAttrs[k]
 
80
                errmsg := fmt.Sprintf("cannot change %s from %q to %q", k, oldv, v)
 
81
                c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(errmsg))
 
82
        }
 
83
}
 
84
 
 
85
func (s *configSuite) TestBootstrapUser(c *gc.C) {
 
86
        values := MinimalConfigValues()
 
87
        testConfig := getModelConfig(c, values)
 
88
        c.Assert(testConfig.bootstrapUser(), gc.Equals, "")
 
89
        values["bootstrap-user"] = "ubuntu"
 
90
        testConfig = getModelConfig(c, values)
 
91
        c.Assert(testConfig.bootstrapUser(), gc.Equals, "ubuntu")
 
92
}