-
Notifications
You must be signed in to change notification settings - Fork 0
/
environ_test.go
165 lines (138 loc) · 5.07 KB
/
environ_test.go
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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"github.com/juju/errors"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/state"
"github.com/juju/juju/testing"
)
type EnvironSuite struct {
ConnSuite
}
var _ = gc.Suite(&EnvironSuite{})
func (s *EnvironSuite) TestEnvironment(c *gc.C) {
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
expectedTag := names.NewEnvironTag(env.UUID())
c.Assert(env.Tag(), gc.Equals, expectedTag)
c.Assert(env.ServerTag(), gc.Equals, expectedTag)
c.Assert(env.Name(), gc.Equals, "testenv")
c.Assert(env.Owner(), gc.Equals, s.Owner)
c.Assert(env.Life(), gc.Equals, state.Alive)
}
func (s *EnvironSuite) TestNewEnvironmentNonExistentLocalUser(c *gc.C) {
cfg, _ := s.createTestEnvConfig(c)
owner := names.NewUserTag("non-existent@local")
_, _, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, gc.ErrorMatches, `cannot create environment: user "non-existent" not found`)
}
func (s *EnvironSuite) TestNewEnvironment(c *gc.C) {
cfg, uuid := s.createTestEnvConfig(c)
owner := names.NewUserTag("test@remote")
env, st, err := s.State.NewEnvironment(cfg, owner)
c.Assert(err, jc.ErrorIsNil)
defer st.Close()
envTag := names.NewEnvironTag(uuid)
assertEnvMatches := func(env *state.Environment) {
c.Assert(env.UUID(), gc.Equals, envTag.Id())
c.Assert(env.Tag(), gc.Equals, envTag)
c.Assert(env.ServerTag(), gc.Equals, s.envTag)
c.Assert(env.Owner(), gc.Equals, owner)
c.Assert(env.Name(), gc.Equals, "testing")
c.Assert(env.Life(), gc.Equals, state.Alive)
}
assertEnvMatches(env)
// Since the environ tag for the State connection is different,
// asking for this environment through FindEntity returns a not found error.
env, err = s.State.GetEnvironment(envTag)
c.Assert(err, jc.ErrorIsNil)
assertEnvMatches(env)
env, err = st.Environment()
c.Assert(err, jc.ErrorIsNil)
assertEnvMatches(env)
_, err = s.State.FindEntity(envTag)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
entity, err := st.FindEntity(envTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(entity.Tag(), gc.Equals, envTag)
// Ensure the environment is functional by adding a machine
_, err = st.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
}
func (s *EnvironSuite) TestStateServerEnvironment(c *gc.C) {
env, err := s.State.StateServerEnvironment()
c.Assert(err, jc.ErrorIsNil)
expectedTag := names.NewEnvironTag(env.UUID())
c.Assert(env.Tag(), gc.Equals, expectedTag)
c.Assert(env.ServerTag(), gc.Equals, expectedTag)
c.Assert(env.Name(), gc.Equals, "testenv")
c.Assert(env.Owner(), gc.Equals, s.Owner)
c.Assert(env.Life(), gc.Equals, state.Alive)
}
func (s *EnvironSuite) TestStateServerEnvironmentAccessibleFromOtherEnvironments(c *gc.C) {
cfg, _ := s.createTestEnvConfig(c)
_, st, err := s.State.NewEnvironment(cfg, names.NewUserTag("test@remote"))
defer st.Close()
env, err := st.StateServerEnvironment()
c.Assert(err, jc.ErrorIsNil)
c.Assert(env.Tag(), gc.Equals, s.envTag)
c.Assert(env.Name(), gc.Equals, "testenv")
c.Assert(env.Owner(), gc.Equals, s.Owner)
c.Assert(env.Life(), gc.Equals, state.Alive)
}
// createTestEnvConfig returns a new environment config and its UUID for testing.
func (s *EnvironSuite) createTestEnvConfig(c *gc.C) (*config.Config, string) {
uuid, err := utils.NewUUID()
c.Assert(err, jc.ErrorIsNil)
return testing.CustomEnvironConfig(c, testing.Attrs{
"name": "testing",
"uuid": uuid.String(),
}), uuid.String()
}
func (s *EnvironSuite) TestEnvironmentConfigSameEnvAsState(c *gc.C) {
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
cfg, err := env.Config()
c.Assert(err, jc.ErrorIsNil)
uuid, exists := cfg.UUID()
c.Assert(exists, jc.IsTrue)
c.Assert(uuid, gc.Equals, s.State.EnvironUUID())
}
func (s *EnvironSuite) TestEnvironmentConfigDifferentEnvThanState(c *gc.C) {
otherState := s.factory.MakeEnvironment(c, nil)
defer otherState.Close()
env, err := otherState.Environment()
c.Assert(err, jc.ErrorIsNil)
cfg, err := env.Config()
c.Assert(err, jc.ErrorIsNil)
uuid, exists := cfg.UUID()
c.Assert(exists, jc.IsTrue)
c.Assert(uuid, gc.Equals, env.UUID())
c.Assert(uuid, gc.Not(gc.Equals), s.State.EnvironUUID())
}
func (s *EnvironSuite) TestDestroyStateServerEnvironment(c *gc.C) {
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
err = env.Destroy()
c.Assert(err, jc.ErrorIsNil)
}
func (s *EnvironSuite) TestDestroyOtherEnvironment(c *gc.C) {
st2 := s.factory.MakeEnvironment(c, nil)
defer st2.Close()
env, err := st2.Environment()
c.Assert(err, jc.ErrorIsNil)
err = env.Destroy()
c.Assert(err, jc.ErrorIsNil)
}
func (s *EnvironSuite) TestDestroyStateServerEnvironmentFails(c *gc.C) {
st2 := s.factory.MakeEnvironment(c, nil)
defer st2.Close()
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
c.Assert(env.Destroy(), gc.ErrorMatches, "failed to destroy environment: state server environment cannot be destroyed before all other environments are destroyed")
}