-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
158 lines (135 loc) · 6.27 KB
/
conn_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"github.com/juju/mgo/v2"
"github.com/juju/names/v4"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v2"
gc "gopkg.in/check.v1"
"github.com/juju/juju/controller"
"github.com/juju/juju/state"
statetesting "github.com/juju/juju/state/testing"
"github.com/juju/juju/storage"
"github.com/juju/juju/storage/provider"
dummystorage "github.com/juju/juju/storage/provider/dummy"
"github.com/juju/juju/testing"
)
// ConnSuite provides the infrastructure for all other
// test suites (StateSuite, CharmSuite, MachineSuite, etc).
type ConnSuite struct {
statetesting.StateSuite
annotations *mgo.Collection
charms *mgo.Collection
machines *mgo.Collection
instanceData *mgo.Collection
relations *mgo.Collection
applications *mgo.Collection
units *mgo.Collection
controllers *mgo.Collection
policy statetesting.MockPolicy
modelTag names.ModelTag
}
func (s *ConnSuite) SetUpTest(c *gc.C) {
s.policy = statetesting.MockPolicy{
GetStorageProviderRegistry: func() (storage.ProviderRegistry, error) {
return storage.ChainedProviderRegistry{
dummystorage.StorageProviders(),
provider.CommonStorageProviders(),
}, nil
},
}
s.StateSuite.NewPolicy = func(*state.State) state.Policy {
return &s.policy
}
s.StateSuite.SetUpTest(c)
s.modelTag = s.Model.ModelTag()
jujuDB := s.MgoSuite.Session.DB("juju")
s.annotations = jujuDB.C("annotations")
s.charms = jujuDB.C("charms")
s.machines = jujuDB.C("machines")
s.instanceData = jujuDB.C("instanceData")
s.relations = jujuDB.C("relations")
s.applications = jujuDB.C("applications")
s.units = jujuDB.C("units")
s.controllers = jujuDB.C("controllers")
}
func (s *ConnSuite) AddTestingCharm(c *gc.C, name string) *state.Charm {
return state.AddTestingCharm(c, s.State, name)
}
func (s *ConnSuite) AddTestingApplication(c *gc.C, name string, ch *state.Charm) *state.Application {
return state.AddTestingApplication(c, s.State, name, ch)
}
func (s *ConnSuite) AddTestingApplicationWithStorage(c *gc.C, name string, ch *state.Charm, storage map[string]state.StorageConstraints) *state.Application {
return state.AddTestingApplicationWithStorage(c, s.State, name, ch, storage)
}
func (s *ConnSuite) AddTestingApplicationWithDevices(c *gc.C, name string, ch *state.Charm, devs map[string]state.DeviceConstraints) *state.Application {
return state.AddTestingApplicationWithDevices(c, s.State, name, ch, devs)
}
func (s *ConnSuite) AddTestingApplicationWithBindings(c *gc.C, name string, ch *state.Charm, bindings map[string]string) *state.Application {
return state.AddTestingApplicationWithBindings(c, s.State, name, ch, bindings)
}
func (s *ConnSuite) AddSeriesCharm(c *gc.C, name, series string) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "", "", series, -1)
}
// AddConfigCharm clones a testing charm, replaces its config with
// the given YAML string and adds it to the state, using the given
// revision.
func (s *ConnSuite) AddConfigCharm(c *gc.C, name, configYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "config.yaml", configYaml, "quantal", revision)
}
// AddActionsCharm clones a testing charm, replaces its actions schema with
// the given YAML, and adds it to the state, using the given revision.
func (s *ConnSuite) AddActionsCharm(c *gc.C, name, actionsYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "actions.yaml", actionsYaml, "quantal", revision)
}
// AddManifestCharm clones a testing charm, replaces its manifest schema with
// the given YAML, and adds it to the state, using the given revision.
func (s *ConnSuite) AddManifestCharm(c *gc.C, name, manifestYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "manifest.yaml", manifestYaml, "quantal", revision)
}
// AddLXDProfileCharm clones a testing charm, replaces its lxd profile config with
// the given YAML, and adds it to the state, using the given revision.
func (s *ConnSuite) AddLXDProfileCharm(c *gc.C, name, lxdProfileYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "lxd-profile.yaml", lxdProfileYaml, "quantal", revision)
}
// AddMetaCharm clones a testing charm, replaces its metadata with the
// given YAML string and adds it to the state, using the given revision.
func (s *ConnSuite) AddMetaCharm(c *gc.C, name, metaYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "metadata.yaml", metaYaml, "quantal", revision)
}
// AddMetricsCharm clones a testing charm, replaces its metrics declaration with the
// given YAML string and adds it to the state, using the given revision.
func (s *ConnSuite) AddMetricsCharm(c *gc.C, name, metricsYaml string, revision int) *state.Charm {
return state.AddCustomCharm(c, s.State, name, "metrics.yaml", metricsYaml, "quantal", revision)
}
// NewStateForModelNamed returns an new model with the given modelName, which
// has a unique UUID, and does not need to be closed when the test completes.
func (s *ConnSuite) NewStateForModelNamed(c *gc.C, modelName string) *state.State {
cfg := testing.CustomModelConfig(c, testing.Attrs{
"name": modelName,
"uuid": utils.MustNewUUID().String(),
})
otherOwner := names.NewLocalUserTag("test-admin")
_, otherState, err := s.Controller.NewModel(state.ModelArgs{
Type: state.ModelTypeIAAS,
CloudName: "dummy",
CloudRegion: "dummy-region",
Config: cfg,
Owner: otherOwner,
StorageProviderRegistry: storage.StaticProviderRegistry{},
})
c.Assert(err, jc.ErrorIsNil)
s.AddCleanup(func(*gc.C) { otherState.Close() })
return otherState
}
// SetJujuManagementSpace mimics a controller having been configured with a
// management space name. This is the space that constrains the set of
// addresses that agents should use for controller communication.
func (s *ConnSuite) SetJujuManagementSpace(c *gc.C, space string) {
controllerSettings, err := s.State.ReadSettings(state.ControllersC, "controllerSettings")
c.Assert(err, jc.ErrorIsNil)
controllerSettings.Set(controller.JujuManagementSpace, space)
_, err = controllerSettings.Write()
c.Assert(err, jc.ErrorIsNil)
}