~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/provider/null/environ_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Canonical Ltd.
 
1
// Copyright 2012 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
3
 
4
4
package null
5
5
 
6
6
import (
7
 
        "io/ioutil"
8
 
        "os"
9
 
        "path/filepath"
 
7
        "errors"
 
8
        "io"
10
9
        "strings"
11
10
 
12
11
        gc "launchpad.net/gocheck"
13
12
 
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"
81
80
}
82
81
 
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
 
84
        var resultErr error
 
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
 
89
        }
 
90
        s.PatchValue(&runSSHCommand, runSSHCommandTesting)
 
91
        type test struct {
 
92
                stderr string
 
93
                err    error
 
94
                match  string
 
95
        }
 
96
        tests := []test{
 
97
                {"", nil, ""},
 
98
                {"abc", nil, ""},
 
99
                {"", errors.New("oh noes"), "oh noes"},
 
100
                {"123", errors.New("abc"), "abc \\(123\\)"},
 
101
        }
 
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()
 
106
                if t.match == "" {
 
107
                        c.Assert(err, gc.IsNil)
 
108
                } else {
 
109
                        c.Assert(err, gc.ErrorMatches, t.match)
 
110
                }
 
111
        }
85
112
}
86
113
 
87
114
func (s *environSuite) TestLocalStorageConfig(c *gc.C) {
101
128
        c.Assert(strings.Contains(url, "/tools"), jc.IsTrue)
102
129
}
103
130
 
 
131
type dummyStorage struct {
 
132
        storage.Storage
 
133
}
 
134
 
104
135
func (s *environSuite) TestEnvironBootstrapStorager(c *gc.C) {
105
 
        var sshScript = `
106
 
#!/bin/bash --norc
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
111
 
    echo JUJU-RC: $RC
112
 
fi
113
 
`[1:]
114
 
        bin := c.MkDir()
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"))
119
 
 
120
 
        s.PatchEnvironment("RC", "99") // simulate ssh failure
 
136
        var newSSHStorageResult = struct {
 
137
                stor storage.Storage
 
138
                err  error
 
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
 
142
        })
 
143
 
 
144
        var initUbuntuResult error
 
145
        s.PatchValue(&initUbuntuUser, func(host, user, authorizedKeys string, stdin io.Reader, stdout io.Writer) error {
 
146
                return initUbuntuResult
 
147
        })
 
148
 
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))
125
 
 
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))
130
 
 
131
 
        // Check idempotency
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)
 
158
 
 
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)
135
166
}