Use private key to create the ssh public key.
if private key exist but no public key, use the private key to
create the public key.
Bug: b/110856450
Test: ./run_tests.sh && m acloud && rm ~/.ssh/acloud_rsa.pub && acloud create
Change-Id: Ie2b482610b7c1a6331745a8e8e9a57b0363d9092
diff --git a/internal/lib/utils_test.py b/internal/lib/utils_test.py
index f00fb2f..02cf8aa 100644
--- a/internal/lib/utils_test.py
+++ b/internal/lib/utils_test.py
@@ -124,9 +124,9 @@
"""Test when the key pair already exists."""
public_key = "/fake/public_key"
private_key = "/fake/private_key"
- self.Patch(
- os.path, "exists", side_effect=lambda path: path == public_key)
+ self.Patch(os.path, "exists", side_effect=[True, True])
self.Patch(subprocess, "check_call")
+ self.Patch(os, "makedirs", return_value=True)
utils.CreateSshKeyPairIfNotExist(private_key, public_key)
self.assertEqual(subprocess.check_call.call_count, 0) #pylint: disable=no-member
@@ -135,6 +135,7 @@
public_key = "/fake/public_key"
private_key = "/fake/private_key"
self.Patch(os.path, "exists", return_value=False)
+ self.Patch(os, "makedirs", return_value=True)
self.Patch(subprocess, "check_call")
self.Patch(os, "rename")
utils.CreateSshKeyPairIfNotExist(private_key, public_key)
@@ -145,6 +146,21 @@
stdout=mock.ANY,
stderr=mock.ANY)
+ def testCreatePublicKeyAreCreated(self):
+ """Test when the PublicKey created."""
+ public_key = "/fake/public_key"
+ private_key = "/fake/private_key"
+ self.Patch(os.path, "exists", side_effect=[False, True, True])
+ self.Patch(os, "makedirs", return_value=True)
+ mock_open = mock.mock_open(read_data=public_key)
+ self.Patch(__builtins__, "open", mock_open, create=True)
+ self.Patch(subprocess, "check_output")
+ self.Patch(os, "rename")
+ utils.CreateSshKeyPairIfNotExist(private_key, public_key)
+ self.assertEqual(subprocess.check_output.call_count, 1) #pylint: disable=no-member
+ subprocess.check_output.assert_called_with( #pylint: disable=no-member
+ utils.SSH_KEYGEN_PUB_CMD +["-f", private_key])
+
def TestRetryOnException(self):
"""Test Retry."""