1
// Copyright 2013 Canonical Ltd.
1
// Copyright 2012 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
11
gc "launchpad.net/gocheck"
14
13
"launchpad.net/juju-core/environs"
15
14
"launchpad.net/juju-core/environs/manual"
16
"launchpad.net/juju-core/environs/sshstorage"
15
"launchpad.net/juju-core/environs/storage"
17
16
envtesting "launchpad.net/juju-core/environs/testing"
18
17
"launchpad.net/juju-core/environs/tools"
19
18
"launchpad.net/juju-core/instance"
83
82
func (s *environSuite) TestDestroy(c *gc.C) {
84
c.Assert(s.env.Destroy(), gc.ErrorMatches, "null provider destruction is not implemented yet")
83
var resultStderr string
85
runSSHCommandTesting := func(host string, command []string) (string, error) {
86
c.Assert(host, gc.Equals, "ubuntu@hostname")
87
c.Assert(command, gc.DeepEquals, []string{"sudo", "pkill", "-6", "jujud"})
88
return resultStderr, resultErr
90
s.PatchValue(&runSSHCommand, runSSHCommandTesting)
99
{"", errors.New("oh noes"), "oh noes"},
100
{"123", errors.New("abc"), "abc \\(123\\)"},
102
for i, t := range tests {
103
c.Logf("test %d: %v", i, t)
104
resultStderr, resultErr = t.stderr, t.err
105
err := s.env.Destroy()
107
c.Assert(err, gc.IsNil)
109
c.Assert(err, gc.ErrorMatches, t.match)
87
114
func (s *environSuite) TestLocalStorageConfig(c *gc.C) {
101
128
c.Assert(strings.Contains(url, "/tools"), jc.IsTrue)
131
type dummyStorage struct {
104
135
func (s *environSuite) TestEnvironBootstrapStorager(c *gc.C) {
107
if echo "$*" | grep -q -v sudo; then
108
# We're executing bash inside ssh. Wait
109
# for input to be written before exiting.
110
head -n 1 > /dev/null
115
ssh := filepath.Join(bin, "ssh")
116
err := ioutil.WriteFile(ssh, []byte(sshScript), 0755)
117
c.Assert(err, gc.IsNil)
118
s.PatchEnvironment("PATH", bin+":"+os.Getenv("PATH"))
120
s.PatchEnvironment("RC", "99") // simulate ssh failure
136
var newSSHStorageResult = struct {
139
}{dummyStorage{}, errors.New("failed to get SSH storage")}
140
s.PatchValue(&newSSHStorage, func(sshHost, storageDir, storageTmpdir string) (storage.Storage, error) {
141
return newSSHStorageResult.stor, newSSHStorageResult.err
144
var initUbuntuResult error
145
s.PatchValue(&initUbuntuUser, func(host, user, authorizedKeys string, stdin io.Reader, stdout io.Writer) error {
146
return initUbuntuResult
121
149
ctx := envtesting.NewBootstrapContext(coretesting.Context(c))
122
err = s.env.EnableBootstrapStorage(ctx)
123
c.Assert(err, gc.ErrorMatches, "exit code 99")
124
c.Assert(s.env.Storage(), gc.Not(gc.FitsTypeOf), new(sshstorage.SSHStorage))
126
s.PatchEnvironment("RC", "0")
127
err = s.env.EnableBootstrapStorage(ctx)
128
c.Assert(err, gc.IsNil)
129
c.Assert(s.env.Storage(), gc.FitsTypeOf, new(sshstorage.SSHStorage))
132
err = s.env.EnableBootstrapStorage(ctx)
133
c.Assert(err, gc.IsNil)
134
c.Assert(s.env.Storage(), gc.FitsTypeOf, new(sshstorage.SSHStorage))
150
initUbuntuResult = errors.New("failed to initialise ubuntu user")
151
c.Assert(s.env.EnableBootstrapStorage(ctx), gc.Equals, initUbuntuResult)
152
initUbuntuResult = nil
153
c.Assert(s.env.EnableBootstrapStorage(ctx), gc.Equals, newSSHStorageResult.err)
154
// after the user is initialised once successfully,
155
// another attempt will not be made.
156
initUbuntuResult = errors.New("failed to initialise ubuntu user")
157
c.Assert(s.env.EnableBootstrapStorage(ctx), gc.Equals, newSSHStorageResult.err)
159
// after the bootstrap storage is initialised once successfully,
160
// another attempt will not be made.
161
backup := newSSHStorageResult.err
162
newSSHStorageResult.err = nil
163
c.Assert(s.env.EnableBootstrapStorage(ctx), gc.IsNil)
164
newSSHStorageResult.err = backup
165
c.Assert(s.env.EnableBootstrapStorage(ctx), gc.IsNil)