Merge lp:~gz/juju-core/generatekey_testoverride_1312850 into lp:~go-bot/juju-core/trunk

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merged at revision: 2685
Proposed branch: lp:~gz/juju-core/generatekey_testoverride_1312850
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 115 lines (+36/-1)
6 files modified
utils/ssh/clientkeys_test.go (+2/-0)
utils/ssh/export_test.go (+1/-0)
utils/ssh/generate.go (+4/-1)
utils/ssh/generate_test.go (+26/-0)
utils/ssh/ssh_gocrypto_test.go (+2/-0)
utils/ssh/ssh_test.go (+1/-0)
To merge this branch: bzr merge lp:~gz/juju-core/generatekey_testoverride_1312850
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+217314@code.launchpad.net

Commit message

utils/ssh: Override rsa.GenerateKey in tests

Make ssh tests faster by generating one, smaller,
RSA key and reusing it for all tests. This is
particularly helpful on arm where the math prime
functions are very slow.

https://codereview.appspot.com/95780044/

R=jameinel

Description of the change

utils/ssh: Override rsa.GenerateKey in tests

Make ssh tests faster by generating one, smaller,
RSA key and reusing it for all tests. This is
particularly helpful on arm where the math prime
functions are very slow.

https://codereview.appspot.com/95780044/

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :
Download full text (4.6 KiB)

Reviewers: mp+217314_code.launchpad.net,

Message:
Please take a look.

Description:
utils/ssh: Override rsa.GenerateKey in tests

Make ssh tests faster by generating one, smaller,
RSA key and reusing it for all tests. This is
particularly helpful on arm where the math prime
functions are very slow.

https://code.launchpad.net/~gz/juju-core/generatekey_testoverride_1312850/+merge/217314

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/95780044/

Affected files (+31, -1 lines):
   A [revision details]
   M utils/ssh/clientkeys_test.go
   M utils/ssh/export_test.go
   M utils/ssh/generate.go
   M utils/ssh/generate_test.go
   M utils/ssh/ssh_gocrypto_test.go
   M utils/ssh/ssh_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-20140423115706-1c02li5w6rmcg533
+New revision: <email address hidden>

Index: utils/ssh/clientkeys_test.go
=== modified file 'utils/ssh/clientkeys_test.go'
--- utils/ssh/clientkeys_test.go 2014-03-13 07:54:56 +0000
+++ utils/ssh/clientkeys_test.go 2014-04-25 23:47:12 +0000
@@ -27,6 +27,8 @@
   fakeHome := testing.MakeEmptyFakeHome(c)
   s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
   s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })
+ generateKeyRestorer := overrideGenerateKey(c)
+ s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() })
  }

  func checkFiles(c *gc.C, obtained, expected []string) {

Index: utils/ssh/export_test.go
=== modified file 'utils/ssh/export_test.go'
--- utils/ssh/export_test.go 2014-02-14 04:58:10 +0000
+++ utils/ssh/export_test.go 2014-04-25 23:47:12 +0000
@@ -9,4 +9,5 @@
   InitDefaultClient = initDefaultClient
   DefaultIdentities = &defaultIdentities
   SSHDial = &sshDial
+ RSAGenerateKey = &rsaGenerateKey
  )

Index: utils/ssh/generate.go
=== modified file 'utils/ssh/generate.go'
--- utils/ssh/generate.go 2013-12-19 20:55:25 +0000
+++ utils/ssh/generate.go 2014-04-25 23:47:12 +0000
@@ -14,6 +14,8 @@
   "code.google.com/p/go.crypto/ssh"
  )

+var rsaGenerateKey = rsa.GenerateKey
+
  // KeyBits is used to determine the number of bits to use for the RSA keys
  // created using the GenerateKey function.
  var KeyBits = 2048
@@ -24,7 +26,7 @@
  // be added into an authorized_keys file, and has the comment passed in as
the
  // comment part of the key.
  func GenerateKey(comment string) (private, public string, err error) {
- key, err := rsa.GenerateKey(rand.Reader, KeyBits)
+ key, err := rsaGenerateKey(rand.Reader, KeyBits)
   if err != nil {
    return "", "", err
   }

Index: utils/ssh/generate_test.go
=== modified file 'utils/ssh/generate_test.go'
--- utils/ssh/generate_test.go 2014-03-13 07:54:56 +0000
+++ utils/ssh/generate_test.go 2014-04-25 23:47:12 +0000
@@ -4,6 +4,10 @@
  package ssh_test

  import (
+ "crypto/rsa"
+ "io"
+
+ "github.com/juju/testing"
   jc "github.com/juju/testing/checkers"
   gc "launchpad.net/gocheck"

@@ -17,7 +21,23 @@

  var _ = gc.Suite(&GenerateSuite{})

+var pregeneratedKey *rsa.Pr...

Read more...

Revision history for this message
John A Meinel (jameinel) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utils/ssh/clientkeys_test.go'
2--- utils/ssh/clientkeys_test.go 2014-03-13 07:54:56 +0000
3+++ utils/ssh/clientkeys_test.go 2014-04-26 16:31:32 +0000
4@@ -27,6 +27,8 @@
5 fakeHome := testing.MakeEmptyFakeHome(c)
6 s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
7 s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })
8+ generateKeyRestorer := overrideGenerateKey(c)
9+ s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() })
10 }
11
12 func checkFiles(c *gc.C, obtained, expected []string) {
13
14=== modified file 'utils/ssh/export_test.go'
15--- utils/ssh/export_test.go 2014-02-14 04:58:10 +0000
16+++ utils/ssh/export_test.go 2014-04-26 16:31:32 +0000
17@@ -9,4 +9,5 @@
18 InitDefaultClient = initDefaultClient
19 DefaultIdentities = &defaultIdentities
20 SSHDial = &sshDial
21+ RSAGenerateKey = &rsaGenerateKey
22 )
23
24=== modified file 'utils/ssh/generate.go'
25--- utils/ssh/generate.go 2013-12-19 20:55:25 +0000
26+++ utils/ssh/generate.go 2014-04-26 16:31:32 +0000
27@@ -14,6 +14,9 @@
28 "code.google.com/p/go.crypto/ssh"
29 )
30
31+// rsaGenerateKey allows for tests to patch out rsa key generation
32+var rsaGenerateKey = rsa.GenerateKey
33+
34 // KeyBits is used to determine the number of bits to use for the RSA keys
35 // created using the GenerateKey function.
36 var KeyBits = 2048
37@@ -24,7 +27,7 @@
38 // be added into an authorized_keys file, and has the comment passed in as the
39 // comment part of the key.
40 func GenerateKey(comment string) (private, public string, err error) {
41- key, err := rsa.GenerateKey(rand.Reader, KeyBits)
42+ key, err := rsaGenerateKey(rand.Reader, KeyBits)
43 if err != nil {
44 return "", "", err
45 }
46
47=== modified file 'utils/ssh/generate_test.go'
48--- utils/ssh/generate_test.go 2014-03-13 07:54:56 +0000
49+++ utils/ssh/generate_test.go 2014-04-26 16:31:32 +0000
50@@ -4,6 +4,10 @@
51 package ssh_test
52
53 import (
54+ "crypto/rsa"
55+ "io"
56+
57+ "github.com/juju/testing"
58 jc "github.com/juju/testing/checkers"
59 gc "launchpad.net/gocheck"
60
61@@ -17,7 +21,29 @@
62
63 var _ = gc.Suite(&GenerateSuite{})
64
65+var pregeneratedKey *rsa.PrivateKey
66+
67+// overrideGenerateKey patches out rsa.GenerateKey to create a single testing
68+// key which is saved and used between tests to save computation time.
69+func overrideGenerateKey(c *gc.C) testing.Restorer {
70+ restorer := testing.PatchValue(ssh.RSAGenerateKey, func(random io.Reader, bits int) (*rsa.PrivateKey, error) {
71+ if pregeneratedKey != nil {
72+ return pregeneratedKey, nil
73+ }
74+ // Ignore requested bits and just use 512 bits for speed
75+ key, err := rsa.GenerateKey(random, 512)
76+ if err != nil {
77+ return nil, err
78+ }
79+ key.Precompute()
80+ pregeneratedKey = key
81+ return key, nil
82+ })
83+ return restorer
84+}
85+
86 func (s *GenerateSuite) TestGenerate(c *gc.C) {
87+ defer overrideGenerateKey(c).Restore()
88 private, public, err := ssh.GenerateKey("some-comment")
89
90 c.Check(err, gc.IsNil)
91
92=== modified file 'utils/ssh/ssh_gocrypto_test.go'
93--- utils/ssh/ssh_gocrypto_test.go 2014-04-12 05:53:58 +0000
94+++ utils/ssh/ssh_gocrypto_test.go 2014-04-26 16:31:32 +0000
95@@ -87,6 +87,8 @@
96
97 func (s *SSHGoCryptoCommandSuite) SetUpTest(c *gc.C) {
98 s.LoggingSuite.SetUpTest(c)
99+ generateKeyRestorer := overrideGenerateKey(c)
100+ s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() })
101 client, err := ssh.NewGoCryptoClient()
102 c.Assert(err, gc.IsNil)
103 s.client = client
104
105=== modified file 'utils/ssh/ssh_test.go'
106--- utils/ssh/ssh_test.go 2014-04-12 05:53:58 +0000
107+++ utils/ssh/ssh_test.go 2014-04-26 16:31:32 +0000
108@@ -152,6 +152,7 @@
109 }
110
111 func (s *SSHCommandSuite) TestCommandClientKeys(c *gc.C) {
112+ defer overrideGenerateKey(c).Restore()
113 clientKeysDir := c.MkDir()
114 defer ssh.ClearClientKeys()
115 err := ssh.LoadClientKeys(clientKeysDir)

Subscribers

People subscribed via source and target branches

to status/vote changes: