blob: 9899d752e5f72bd06d71c5233a7400b593dae614 [file] [log] [blame]
Fang Deng69498c32017-03-02 14:29:30 -08001#!/usr/bin/env python
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Tests for acloud.internal.lib.utils."""
18
19import getpass
20import os
21import subprocess
22
23import mock
24
25import unittest
26from acloud.internal.lib import driver_test_lib
27from acloud.internal.lib import utils
28
29
30class UtilsTest(driver_test_lib.BaseDriverTest):
31
32 def testCreateSshKeyPair_KeyAlreadyExists(self):
33 """Test when the key pair already exists."""
34 public_key = "/fake/public_key"
35 private_key = "/fake/private_key"
36 self.Patch(os.path, "exists", side_effect=lambda path: path == public_key)
37 self.Patch(subprocess, "check_call")
38 utils.CreateSshKeyPairIfNotExist(private_key, public_key)
39 self.assertEqual(subprocess.check_call.call_count, 0)
40
41 def testCreateSshKeyPair_KeyAreCreated(self):
42 """Test when the key pair created."""
43 public_key = "/fake/public_key"
44 private_key = "/fake/private_key"
45 self.Patch(os.path, "exists", return_value=False)
46 self.Patch(subprocess, "check_call")
47 self.Patch(os, "rename")
48 utils.CreateSshKeyPairIfNotExist(private_key, public_key)
49 self.assertEqual(subprocess.check_call.call_count, 1)
50 subprocess.check_call.assert_called_with(
51 utils.SSH_KEYGEN_CMD + ["-C", getpass.getuser(), "-f", private_key],
52 stdout=mock.ANY, stderr=mock.ANY)
53
54
55if __name__ == "__main__":
56 unittest.main()