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

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/utils/ssh/clientkeys_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.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package ssh_test
 
5
 
 
6
import (
 
7
        "io/ioutil"
 
8
        "os"
 
9
 
 
10
        gc "launchpad.net/gocheck"
 
11
 
 
12
        "launchpad.net/juju-core/testing"
 
13
        jc "launchpad.net/juju-core/testing/checkers"
 
14
        "launchpad.net/juju-core/testing/testbase"
 
15
        "launchpad.net/juju-core/utils"
 
16
        "launchpad.net/juju-core/utils/ssh"
 
17
)
 
18
 
 
19
type ClientKeysSuite struct {
 
20
        testbase.LoggingSuite
 
21
}
 
22
 
 
23
var _ = gc.Suite(&ClientKeysSuite{})
 
24
 
 
25
func (s *ClientKeysSuite) SetUpTest(c *gc.C) {
 
26
        s.LoggingSuite.SetUpTest(c)
 
27
        fakeHome := testing.MakeEmptyFakeHome(c)
 
28
        s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
 
29
        s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })
 
30
}
 
31
 
 
32
func checkFiles(c *gc.C, obtained, expected []string) {
 
33
        var err error
 
34
        for i, e := range expected {
 
35
                expected[i], err = utils.NormalizePath(e)
 
36
                c.Assert(err, gc.IsNil)
 
37
        }
 
38
        c.Assert(obtained, jc.SameContents, expected)
 
39
}
 
40
 
 
41
func checkPublicKeyFiles(c *gc.C, expected ...string) {
 
42
        keys := ssh.PublicKeyFiles()
 
43
        checkFiles(c, keys, expected)
 
44
}
 
45
 
 
46
func checkPrivateKeyFiles(c *gc.C, expected ...string) {
 
47
        keys := ssh.PrivateKeyFiles()
 
48
        checkFiles(c, keys, expected)
 
49
}
 
50
 
 
51
func (s *ClientKeysSuite) TestPublicKeyFiles(c *gc.C) {
 
52
        // LoadClientKeys will create the specified directory
 
53
        // and populate it with a key pair.
 
54
        err := ssh.LoadClientKeys("~/.juju/ssh")
 
55
        c.Assert(err, gc.IsNil)
 
56
        checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
 
57
        // All files ending with .pub in the client key dir get picked up.
 
58
        priv, pub, err := ssh.GenerateKey("whatever")
 
59
        c.Assert(err, gc.IsNil)
 
60
        err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
 
61
        c.Assert(err, gc.IsNil)
 
62
        err = ssh.LoadClientKeys("~/.juju/ssh")
 
63
        c.Assert(err, gc.IsNil)
 
64
        // The new public key won't be observed until the
 
65
        // corresponding private key exists.
 
66
        checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
 
67
        err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
 
68
        c.Assert(err, gc.IsNil)
 
69
        err = ssh.LoadClientKeys("~/.juju/ssh")
 
70
        c.Assert(err, gc.IsNil)
 
71
        checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
 
72
}
 
73
 
 
74
func (s *ClientKeysSuite) TestPrivateKeyFiles(c *gc.C) {
 
75
        // Create/load client keys. They will be cached in memory:
 
76
        // any files added to the directory will not be considered
 
77
        // unless LoadClientKeys is called again.
 
78
        err := ssh.LoadClientKeys("~/.juju/ssh")
 
79
        c.Assert(err, gc.IsNil)
 
80
        checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
 
81
        priv, pub, err := ssh.GenerateKey("whatever")
 
82
        c.Assert(err, gc.IsNil)
 
83
        err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
 
84
        c.Assert(err, gc.IsNil)
 
85
        err = ssh.LoadClientKeys("~/.juju/ssh")
 
86
        c.Assert(err, gc.IsNil)
 
87
        // The new private key won't be observed until the
 
88
        // corresponding public key exists.
 
89
        checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
 
90
        err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
 
91
        c.Assert(err, gc.IsNil)
 
92
        // new keys won't be reported until we call LoadClientKeys again
 
93
        checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
 
94
        checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
 
95
        err = ssh.LoadClientKeys("~/.juju/ssh")
 
96
        c.Assert(err, gc.IsNil)
 
97
        checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
 
98
        checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa", "~/.juju/ssh/whatever")
 
99
}
 
100
 
 
101
func (s *ClientKeysSuite) TestLoadClientKeysDirExists(c *gc.C) {
 
102
        err := os.MkdirAll(testing.HomePath(".juju", "ssh"), 0755)
 
103
        c.Assert(err, gc.IsNil)
 
104
        err = ssh.LoadClientKeys("~/.juju/ssh")
 
105
        c.Assert(err, gc.IsNil)
 
106
        checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
 
107
}