~jameinel/juju-core/api-login-describe-facades

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package environs_test

import (
	"io/ioutil"
	. "launchpad.net/gocheck"
	"launchpad.net/juju/go/environs"
	_ "launchpad.net/juju/go/environs/dummy"
	"os"
	"path/filepath"
)

type configTest struct {
	env   string
	check func(c *C, es *environs.Environs)
}

var configTests = []struct {
	env   string
	err   string
	check func(c *C, es *environs.Environs)
}{
	{"'",
		"YAML error:.*",
		nil,
	}, {`
default: unknown
environments:
    only:
        type: unknown
`, `default environment .* does not exist`,
		nil,
	}, {`
environments:
    only:
`,
		`environment .* does not have attributes`,
		nil,
	}, {`
environments:
    only:
        foo: bar
`,
		`environment .* has no type`,
		nil,
	}, {`
environments:
    only:
        type: dummy
`, `.*zookeeper: expected false, got nothing`,
		nil,
	}, {`
environments:
    only:
        type: unknown
        other: anything
        zookeeper: false
`,
		"",
		func(c *C, es *environs.Environs) {
			e, err := es.Open("")
			c.Assert(e, IsNil)
			c.Assert(err, NotNil)
			c.Assert(err.Error(), Equals, `environment "only" has an unknown provider type: "unknown"`)
		},
	},
	// should this fail or not?
	//	// one known environment, no defaults, bad attribute -> parse error
	//	{`
	//environments:
	//    only:
	//        type: dummy
	//        badattr: anything
	//        zookeeper: false
	//`, nil,
	//	},
	// one known environment, no defaults -> parse ok, instantiate ok
	{`
environments:
    only:
        type: dummy
        zookeeper: false
`, "", func(c *C, es *environs.Environs) {
		e, err := es.Open("")
		c.Assert(err, IsNil)
		checkEnvironName(c, e, "only")
	},
	},
	// several environments, no defaults -> parse ok, instantiate maybe error
	{`
environments:
    one:
        type: dummy
        zookeeper: false
    two:
        type: dummy
        zookeeper: false
`, "", func(c *C, es *environs.Environs) {
		e, err := es.Open("")
		c.Assert(err, NotNil)
		e, err = es.Open("one")
		c.Assert(err, IsNil)
		checkEnvironName(c, e, "one")
	},
	},
	// several environments, default -> parse ok, instantiate ok
	{`
default:
    two
environments:
   one:
        type: dummy
        zookeeper: false
   two:
        type: dummy
        zookeeper: false
`, "", func(c *C, es *environs.Environs) {
		e, err := es.Open("")
		c.Assert(err, IsNil)
		checkEnvironName(c, e, "two")
	},
	},
}

// checkEnvironName checks that a new instance started
// by the given Environ has an id starting with name,
// which implies that it is the expected environment.
func checkEnvironName(c *C, e environs.Environ, name string) {
	i0, err := e.StartInstance(0, nil)
	c.Assert(err, IsNil)
	c.Assert(i0, NotNil)
	c.Assert(i0.Id(), Matches, name+".*")
}

func (suite) TestConfig(c *C) {
	for i, t := range configTests {
		c.Logf("running test %v", i)
		es, err := environs.ReadEnvironsBytes([]byte(t.env))
		if t.err != "" {
			c.Check(err, ErrorMatches, t.err)
		} else {
			c.Assert(es, NotNil)
			c.Assert(err, IsNil)
			t.check(c, es)
		}
	}
}

func (suite) TestConfigFile(c *C) {
	d := c.MkDir()
	err := os.Mkdir(filepath.Join(d, ".juju"), 0777)
	c.Assert(err, IsNil)

	path := filepath.Join(d, ".juju", "environments.yaml")
	env := `
environments:
    only:
        type: dummy
        zookeeper: false
`
	err = ioutil.WriteFile(path, []byte(env), 0666)
	c.Assert(err, IsNil)

	// test reading from a named file
	es, err := environs.ReadEnvirons(path)
	c.Assert(err, IsNil)
	e, err := es.Open("")
	c.Assert(err, IsNil)
	checkEnvironName(c, e, "only")

	// test reading from the default environments.yaml file.
	h := os.Getenv("HOME")
	os.Setenv("HOME", d)

	es, err = environs.ReadEnvirons("")
	c.Assert(err, IsNil)
	e, err = es.Open("")
	c.Assert(err, IsNil)
	checkEnvironName(c, e, "only")

	// reset $HOME just in case something else relies on it.
	os.Setenv("HOME", h)
}