/
export_test.go
234 lines (196 loc) · 6.38 KB
/
export_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"github.com/juju/charm"
charmtesting "github.com/juju/charm/testing"
"github.com/juju/names"
jujutxn "github.com/juju/txn"
txntesting "github.com/juju/txn/testing"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"labix.org/v2/mgo/txn"
gc "launchpad.net/gocheck"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/testing"
)
func SetTestHooks(c *gc.C, st *State, hooks ...jujutxn.TestHook) txntesting.TransactionChecker {
return txntesting.SetTestHooks(c, st.transactionRunner, hooks...)
}
func SetBeforeHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
return txntesting.SetBeforeHooks(c, st.transactionRunner, fs...)
}
func SetAfterHooks(c *gc.C, st *State, fs ...func()) txntesting.TransactionChecker {
return txntesting.SetAfterHooks(c, st.transactionRunner, fs...)
}
func SetRetryHooks(c *gc.C, st *State, block, check func()) txntesting.TransactionChecker {
return txntesting.SetRetryHooks(c, st.transactionRunner, block, check)
}
// SetPolicy updates the State's policy field to the
// given Policy, and returns the old value.
func SetPolicy(st *State, p Policy) Policy {
old := st.policy
st.policy = p
return old
}
// TestingInitialize initializes the state and returns it. If state was not
// already initialized, and cfg is nil, the minimal default environment
// configuration will be used.
func TestingInitialize(c *gc.C, cfg *config.Config, policy Policy) *State {
if cfg == nil {
cfg = testing.EnvironConfig(c)
}
st, err := Initialize(TestingMongoInfo(), cfg, TestingDialOpts(), policy)
c.Assert(err, gc.IsNil)
return st
}
type (
CharmDoc charmDoc
MachineDoc machineDoc
RelationDoc relationDoc
ServiceDoc serviceDoc
UnitDoc unitDoc
)
func (doc *MachineDoc) String() string {
m := &Machine{doc: machineDoc(*doc)}
return m.String()
}
func ServiceSettingsRefCount(st *State, serviceName string, curl *charm.URL) (int, error) {
key := serviceSettingsKey(serviceName, curl)
var doc settingsRefsDoc
if err := st.settingsrefs.FindId(key).One(&doc); err == nil {
return doc.RefCount, nil
}
return 0, mgo.ErrNotFound
}
func AddTestingCharm(c *gc.C, st *State, name string) *Charm {
return addCharm(c, st, "quantal", charmtesting.Charms.Dir(name))
}
func AddTestingService(c *gc.C, st *State, name string, ch *Charm) *Service {
return AddTestingServiceWithNetworks(c, st, name, ch, nil)
}
func AddTestingServiceWithNetworks(c *gc.C, st *State, name string, ch *Charm, networks []string) *Service {
service, err := st.AddService(name, "user-admin", ch, networks)
c.Assert(err, gc.IsNil)
return service
}
func AddCustomCharm(c *gc.C, st *State, name, filename, content, series string, revision int) *Charm {
path := charmtesting.Charms.ClonedDirPath(c.MkDir(), name)
if filename != "" {
config := filepath.Join(path, filename)
err := ioutil.WriteFile(config, []byte(content), 0644)
c.Assert(err, gc.IsNil)
}
ch, err := charm.ReadDir(path)
c.Assert(err, gc.IsNil)
if revision != -1 {
ch.SetRevision(revision)
}
return addCharm(c, st, series, ch)
}
func addCharm(c *gc.C, st *State, series string, ch charm.Charm) *Charm {
ident := fmt.Sprintf("%s-%s-%d", series, ch.Meta().Name, ch.Revision())
curl := charm.MustParseURL("local:" + series + "/" + ident)
bundleURL, err := url.Parse("http://bundles.testing.invalid/" + ident)
c.Assert(err, gc.IsNil)
sch, err := st.AddCharm(ch, curl, bundleURL, ident+"-sha256")
c.Assert(err, gc.IsNil)
return sch
}
var MachineIdLessThan = machineIdLessThan
var JobNames = jobNames
// SCHEMACHANGE
// This method is used to reset a deprecated machine attribute.
func SetMachineInstanceId(m *Machine, instanceId string) {
m.doc.InstanceId = instance.Id(instanceId)
}
// SCHEMACHANGE
// ClearInstanceDocId sets instanceid on instanceData for machine to "".
func ClearInstanceDocId(c *gc.C, m *Machine) {
ops := []txn.Op{
{
C: m.st.instanceData.Name,
Id: m.doc.Id,
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"instanceid", ""}}}},
},
}
err := m.st.runTransaction(ops)
c.Assert(err, gc.IsNil)
}
// SCHEMACHANGE
// This method is used to reset the ownertag attribute
func SetServiceOwnerTag(s *Service, ownerTag string) {
s.doc.OwnerTag = ownerTag
}
// SCHEMACHANGE
// Get the owner directly
func GetServiceOwnerTag(s *Service) string {
return s.doc.OwnerTag
}
func SetPasswordHash(e Authenticator, passwordHash string) error {
type hasSetPasswordHash interface {
setPasswordHash(string) error
}
return e.(hasSetPasswordHash).setPasswordHash(passwordHash)
}
// Return the underlying PasswordHash stored in the database. Used by the test
// suite to check that the PasswordHash gets properly updated to new values
// when compatibility mode is detected.
func GetPasswordHash(e Authenticator) string {
type hasGetPasswordHash interface {
getPasswordHash() string
}
return e.(hasGetPasswordHash).getPasswordHash()
}
func init() {
logSize = logSizeTests
}
// TxnRevno returns the txn-revno field of the document
// associated with the given Id in the given collection.
func TxnRevno(st *State, coll string, id interface{}) (int64, error) {
var doc struct {
TxnRevno int64 `bson:"txn-revno"`
}
err := st.db.C(coll).FindId(id).One(&doc)
if err != nil {
return 0, err
}
return doc.TxnRevno, nil
}
// MinUnitsRevno returns the Revno of the minUnits document
// associated with the given service name.
func MinUnitsRevno(st *State, serviceName string) (int, error) {
var doc minUnitsDoc
if err := st.minUnits.FindId(serviceName).One(&doc); err != nil {
return 0, err
}
return doc.Revno, nil
}
func ParseTag(st *State, tag names.Tag) (string, string, error) {
return st.parseTag(tag)
}
// Return the PasswordSalt that goes along with the PasswordHash
func GetUserPasswordSaltAndHash(u *User) (string, string) {
return u.doc.PasswordSalt, u.doc.PasswordHash
}
var NewAddress = newAddress
func CheckUserExists(st *State, name string) (bool, error) {
return st.checkUserExists(name)
}
var StateServerAvailable = &stateServerAvailable
func EnsureActionMarker(prefix string) string {
return ensureActionMarker(prefix)
}
func GetActionResultId(actionId string) (string, bool) {
return convertActionIdToActionResultId(actionId)
}
var (
GetOrCreatePorts = getOrCreatePorts
GetPorts = getPorts
)