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
=== 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-26 16:31:32 +0000
@@ -27,6 +27,8 @@
27 fakeHome := testing.MakeEmptyFakeHome(c)27 fakeHome := testing.MakeEmptyFakeHome(c)
28 s.AddCleanup(func(*gc.C) { fakeHome.Restore() })28 s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
29 s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })29 s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })
30 generateKeyRestorer := overrideGenerateKey(c)
31 s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() })
30}32}
3133
32func checkFiles(c *gc.C, obtained, expected []string) {34func checkFiles(c *gc.C, obtained, expected []string) {
3335
=== 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-26 16:31:32 +0000
@@ -9,4 +9,5 @@
9 InitDefaultClient = initDefaultClient9 InitDefaultClient = initDefaultClient
10 DefaultIdentities = &defaultIdentities10 DefaultIdentities = &defaultIdentities
11 SSHDial = &sshDial11 SSHDial = &sshDial
12 RSAGenerateKey = &rsaGenerateKey
12)13)
1314
=== modified file 'utils/ssh/generate.go'
--- utils/ssh/generate.go 2013-12-19 20:55:25 +0000
+++ utils/ssh/generate.go 2014-04-26 16:31:32 +0000
@@ -14,6 +14,9 @@
14 "code.google.com/p/go.crypto/ssh"14 "code.google.com/p/go.crypto/ssh"
15)15)
1616
17// rsaGenerateKey allows for tests to patch out rsa key generation
18var rsaGenerateKey = rsa.GenerateKey
19
17// KeyBits is used to determine the number of bits to use for the RSA keys20// KeyBits is used to determine the number of bits to use for the RSA keys
18// created using the GenerateKey function.21// created using the GenerateKey function.
19var KeyBits = 204822var KeyBits = 2048
@@ -24,7 +27,7 @@
24// be added into an authorized_keys file, and has the comment passed in as the27// be added into an authorized_keys file, and has the comment passed in as the
25// comment part of the key.28// comment part of the key.
26func GenerateKey(comment string) (private, public string, err error) {29func GenerateKey(comment string) (private, public string, err error) {
27 key, err := rsa.GenerateKey(rand.Reader, KeyBits)30 key, err := rsaGenerateKey(rand.Reader, KeyBits)
28 if err != nil {31 if err != nil {
29 return "", "", err32 return "", "", err
30 }33 }
3134
=== 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-26 16:31:32 +0000
@@ -4,6 +4,10 @@
4package ssh_test4package ssh_test
55
6import (6import (
7 "crypto/rsa"
8 "io"
9
10 "github.com/juju/testing"
7 jc "github.com/juju/testing/checkers"11 jc "github.com/juju/testing/checkers"
8 gc "launchpad.net/gocheck"12 gc "launchpad.net/gocheck"
913
@@ -17,7 +21,29 @@
1721
18var _ = gc.Suite(&GenerateSuite{})22var _ = gc.Suite(&GenerateSuite{})
1923
24var pregeneratedKey *rsa.PrivateKey
25
26// overrideGenerateKey patches out rsa.GenerateKey to create a single testing
27// key which is saved and used between tests to save computation time.
28func overrideGenerateKey(c *gc.C) testing.Restorer {
29 restorer := testing.PatchValue(ssh.RSAGenerateKey, func(random io.Reader, bits int) (*rsa.PrivateKey, error) {
30 if pregeneratedKey != nil {
31 return pregeneratedKey, nil
32 }
33 // Ignore requested bits and just use 512 bits for speed
34 key, err := rsa.GenerateKey(random, 512)
35 if err != nil {
36 return nil, err
37 }
38 key.Precompute()
39 pregeneratedKey = key
40 return key, nil
41 })
42 return restorer
43}
44
20func (s *GenerateSuite) TestGenerate(c *gc.C) {45func (s *GenerateSuite) TestGenerate(c *gc.C) {
46 defer overrideGenerateKey(c).Restore()
21 private, public, err := ssh.GenerateKey("some-comment")47 private, public, err := ssh.GenerateKey("some-comment")
2248
23 c.Check(err, gc.IsNil)49 c.Check(err, gc.IsNil)
2450
=== modified file 'utils/ssh/ssh_gocrypto_test.go'
--- utils/ssh/ssh_gocrypto_test.go 2014-04-12 05:53:58 +0000
+++ utils/ssh/ssh_gocrypto_test.go 2014-04-26 16:31:32 +0000
@@ -87,6 +87,8 @@
8787
88func (s *SSHGoCryptoCommandSuite) SetUpTest(c *gc.C) {88func (s *SSHGoCryptoCommandSuite) SetUpTest(c *gc.C) {
89 s.LoggingSuite.SetUpTest(c)89 s.LoggingSuite.SetUpTest(c)
90 generateKeyRestorer := overrideGenerateKey(c)
91 s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() })
90 client, err := ssh.NewGoCryptoClient()92 client, err := ssh.NewGoCryptoClient()
91 c.Assert(err, gc.IsNil)93 c.Assert(err, gc.IsNil)
92 s.client = client94 s.client = client
9395
=== modified file 'utils/ssh/ssh_test.go'
--- utils/ssh/ssh_test.go 2014-04-12 05:53:58 +0000
+++ utils/ssh/ssh_test.go 2014-04-26 16:31:32 +0000
@@ -152,6 +152,7 @@
152}152}
153153
154func (s *SSHCommandSuite) TestCommandClientKeys(c *gc.C) {154func (s *SSHCommandSuite) TestCommandClientKeys(c *gc.C) {
155 defer overrideGenerateKey(c).Restore()
155 clientKeysDir := c.MkDir()156 clientKeysDir := c.MkDir()
156 defer ssh.ClearClientKeys()157 defer ssh.ClearClientKeys()
157 err := ssh.LoadClientKeys(clientKeysDir)158 err := ssh.LoadClientKeys(clientKeysDir)

Subscribers

People subscribed via source and target branches

to status/vote changes: