1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
17
"github.com/juju/replicaset"
18
gitjujutesting "github.com/juju/testing"
19
jc "github.com/juju/testing/checkers"
20
"github.com/juju/utils/ssh"
21
gc "gopkg.in/check.v1"
22
"gopkg.in/juju/names.v2"
23
"gopkg.in/mgo.v2/bson"
25
"github.com/juju/juju/agent"
26
"github.com/juju/juju/apiserver/params"
27
"github.com/juju/juju/environs"
28
"github.com/juju/juju/mongo/mongotest"
29
"github.com/juju/juju/state"
30
statetesting "github.com/juju/juju/state/testing"
31
coretesting "github.com/juju/juju/testing"
32
jujuversion "github.com/juju/juju/version"
35
var _ = gc.Suite(&RestoreSuite{})
37
type RestoreSuite struct {
43
func (r *RestoreSuite) SetUpSuite(c *gc.C) {
44
r.BaseSuite.SetUpSuite(c)
47
func (r *RestoreSuite) SetUpTest(c *gc.C) {
49
r.BaseSuite.SetUpTest(c)
52
func (r *RestoreSuite) createTestFiles(c *gc.C) {
53
tarDirE := path.Join(r.cwd, "TarDirectoryEmpty")
54
err := os.Mkdir(tarDirE, os.FileMode(0755))
55
c.Check(err, jc.ErrorIsNil)
57
tarDirP := path.Join(r.cwd, "TarDirectoryPopulated")
58
err = os.Mkdir(tarDirP, os.FileMode(0755))
59
c.Check(err, jc.ErrorIsNil)
61
tarSubFile1 := path.Join(tarDirP, "TarSubFile1")
62
tarSubFile1Handle, err := os.Create(tarSubFile1)
63
c.Check(err, jc.ErrorIsNil)
64
tarSubFile1Handle.WriteString("TarSubFile1")
65
tarSubFile1Handle.Close()
67
tarSubDir := path.Join(tarDirP, "TarDirectoryPopulatedSubDirectory")
68
err = os.Mkdir(tarSubDir, os.FileMode(0755))
69
c.Check(err, jc.ErrorIsNil)
71
tarFile1 := path.Join(r.cwd, "TarFile1")
72
tarFile1Handle, err := os.Create(tarFile1)
73
c.Check(err, jc.ErrorIsNil)
74
tarFile1Handle.WriteString("TarFile1")
75
tarFile1Handle.Close()
77
tarFile2 := path.Join(r.cwd, "TarFile2")
78
tarFile2Handle, err := os.Create(tarFile2)
79
c.Check(err, jc.ErrorIsNil)
80
tarFile2Handle.WriteString("TarFile2")
81
tarFile2Handle.Close()
82
r.testFiles = []string{tarDirE, tarDirP, tarFile1, tarFile2}
85
func (r *RestoreSuite) TestReplicasetIsReset(c *gc.C) {
86
server := &gitjujutesting.MgoInstance{Params: []string{"--replSet", "juju"}}
87
err := server.Start(coretesting.Certs)
88
c.Assert(err, jc.ErrorIsNil)
89
defer server.DestroyWithLog()
90
mgoAddr := server.Addr()
91
dialInfo := server.DialInfo()
93
var cfg *replicaset.Config
94
dialInfo = server.DialInfo()
95
dialInfo.Addrs = []string{mgoAddr}
96
err = resetReplicaSet(dialInfo, mgoAddr)
98
session, err := server.Dial()
99
c.Assert(err, jc.ErrorIsNil)
100
defer session.Close()
101
cfg, err = replicaset.CurrentConfig(session)
102
c.Assert(err, jc.ErrorIsNil)
103
c.Assert(cfg.Members, gc.HasLen, 1)
104
c.Assert(cfg.Members[0].Address, gc.Equals, mgoAddr)
107
type backupConfigTests struct {
113
func (r *RestoreSuite) TestSetAgentAddressScript(c *gc.C) {
114
testServerAddresses := []string{
115
"FirstNewControllerAddress:30303",
116
"SecondNewControllerAddress:30304",
117
"ThirdNewControllerAddress:30305",
118
"FourthNewControllerAddress:30306",
119
"FiftNewControllerAddress:30307",
120
"SixtNewControllerAddress:30308",
122
for _, address := range testServerAddresses {
123
template := setAgentAddressScript(address)
124
expectedString := fmt.Sprintf("\t\ts/- .*(:[0-9]+)/- %s\\1/\n", address)
125
logger.Infof(fmt.Sprintf("Testing with address %q", address))
126
c.Assert(strings.Contains(template, expectedString), gc.Equals, true)
131
-----BEGIN CERTIFICATE-----
132
MIIBnTCCAUmgAwIBAgIBADALBgkqhkiG9w0BAQUwJjENMAsGA1UEChMEanVqdTEV
133
MBMGA1UEAxMManVqdSB0ZXN0aW5nMB4XDTEyMTExNDE0Mzg1NFoXDTIyMTExNDE0
134
NDM1NFowJjENMAsGA1UEChMEanVqdTEVMBMGA1UEAxMManVqdSB0ZXN0aW5nMFow
135
CwYJKoZIhvcNAQEBA0sAMEgCQQCCOOpn9aWKcKr2GQGtygwD7PdfNe1I9BYiPAqa
136
2I33F5+6PqFdfujUKvoyTJI6XG4Qo/CECaaN9smhyq9DxzMhAgMBAAGjZjBkMA4G
137
A1UdDwEB/wQEAwIABDASBgNVHRMBAf8ECDAGAQH/AgEBMB0GA1UdDgQWBBQQDswP
138
FQGeGMeTzPbHW62EZbbTJzAfBgNVHSMEGDAWgBQQDswPFQGeGMeTzPbHW62EZbbT
139
JzALBgkqhkiG9w0BAQUDQQAqZzN0DqUyEfR8zIanozyD2pp10m9le+ODaKZDDNfH
140
8cB2x26F1iZ8ccq5IC2LtQf1IKJnpTcYlLuDvW6yB96g
141
-----END CERTIFICATE-----
144
func (r *RestoreSuite) TestNewDialInfo(c *gc.C) {
150
expectedPassword string
170
dataDir := path.Join(r.cwd, "dataDir")
171
err := os.Mkdir(dataDir, os.FileMode(0755))
172
c.Assert(err, jc.ErrorIsNil)
174
logDir := path.Join(r.cwd, "logDir")
175
err = os.Mkdir(logDir, os.FileMode(0755))
176
c.Assert(err, jc.ErrorIsNil)
177
for _, testCase := range cases {
178
machineTag, err := names.ParseTag(testCase.machineTag)
179
c.Assert(err, jc.ErrorIsNil)
181
configParams := agent.AgentConfigParams{
186
UpgradedToVersion: jujuversion.Current,
188
Model: coretesting.ModelTag,
189
Password: "placeholder",
191
StateAddresses: []string{"fakeStateAddress:1234"},
192
APIAddresses: []string{"fakeAPIAddress:12345"},
196
privateAddress := "dummyPrivateAddress"
197
servingInfo := params.StateServingInfo{
199
StatePort: statePort,
201
CAPrivateKey: "a ca key",
203
SharedSecret: "a secret",
204
SystemIdentity: "an identity",
207
conf, err := agent.NewStateMachineConfig(configParams, servingInfo)
208
c.Assert(err, jc.ErrorIsNil)
209
conf.SetOldPassword(testCase.oldPassword)
210
conf.SetPassword(testCase.apiPassword)
212
dialInfo, err := newDialInfo(privateAddress, conf)
213
if testCase.expectedError != "" {
214
c.Assert(err, gc.ErrorMatches, testCase.expectedError)
216
c.Assert(err, jc.ErrorIsNil)
217
c.Assert(dialInfo.Username, gc.Equals, testCase.expectedUser)
218
c.Assert(dialInfo.Password, gc.Equals, testCase.expectedPassword)
219
c.Assert(dialInfo.Direct, gc.Equals, true)
220
c.Assert(dialInfo.Addrs, gc.DeepEquals, []string{net.JoinHostPort(privateAddress, strconv.Itoa(statePort))})
225
// TestUpdateMongoEntries has all the testing for this function to avoid creating multiple
227
func (r *RestoreSuite) TestUpdateMongoEntries(c *gc.C) {
228
server := &gitjujutesting.MgoInstance{}
229
err := server.Start(coretesting.Certs)
230
c.Assert(err, jc.ErrorIsNil)
231
defer server.DestroyWithLog()
232
dialInfo := server.DialInfo()
233
mgoAddr := server.Addr()
234
dialInfo.Addrs = []string{mgoAddr}
235
err = updateMongoEntries("1234", "0", "0", dialInfo)
236
c.Assert(err, gc.ErrorMatches, "cannot update machine 0 instance information: not found")
238
session, err := server.Dial()
239
c.Assert(err, jc.ErrorIsNil)
240
defer session.Close()
242
err = session.DB("juju").C("machines").Insert(bson.M{"machineid": "0", "instanceid": "0"})
243
c.Assert(err, jc.ErrorIsNil)
245
query := session.DB("juju").C("machines").Find(bson.M{"machineid": "0", "instanceid": "1234"})
246
n, err := query.Count()
247
c.Assert(err, jc.ErrorIsNil)
248
c.Assert(n, gc.Equals, 0)
250
err = updateMongoEntries("1234", "0", "0", dialInfo)
251
c.Assert(err, jc.ErrorIsNil)
253
query = session.DB("juju").C("machines").Find(bson.M{"machineid": "0", "instanceid": "1234"})
254
n, err = query.Count()
255
c.Assert(err, jc.ErrorIsNil)
256
c.Assert(n, gc.Equals, 1)
259
func (r *RestoreSuite) TestNewConnection(c *gc.C) {
260
server := &gitjujutesting.MgoInstance{}
261
err := server.Start(coretesting.Certs)
262
c.Assert(err, jc.ErrorIsNil)
263
defer server.DestroyWithLog()
265
st := statetesting.Initialize(c, names.NewLocalUserTag("test-admin"), nil, nil, nil)
266
c.Assert(st.Close(), jc.ErrorIsNil)
268
r.PatchValue(&mongoDefaultDialOpts, mongotest.DialOpts)
269
r.PatchValue(&environsGetNewPolicyFunc, func(
270
func(*state.State) (environs.Environ, error),
271
) state.NewPolicyFunc {
274
st, err = newStateConnection(st.ModelTag(), statetesting.NewMongoInfo())
275
c.Assert(err, jc.ErrorIsNil)
276
c.Assert(st.Close(), jc.ErrorIsNil)
279
func (r *RestoreSuite) TestRunViaSSH(c *gc.C) {
284
fakeSSHCommand := func(address string, args []string, options *ssh.Options) *ssh.Cmd {
285
passedAddress = address
287
return ssh.Command("", []string{"ls"}, &ssh.Options{})
290
r.PatchValue(&sshCommand, fakeSSHCommand)
291
runViaSSH("invalidAddress", "invalidScript")
292
c.Assert(passedAddress, gc.Equals, "ubuntu@invalidAddress")
293
c.Assert(passedArgs, gc.DeepEquals, []string{"sudo", "-n", "bash", "-c 'invalidScript'"})