|
| 1 | +""" |
| 2 | + SoftLayer.tests.CLI.modules.sshkey_tests |
| 3 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + :license: MIT, see LICENSE for more details. |
| 6 | +""" |
| 7 | +import json |
| 8 | +import os.path |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | + |
| 12 | +from unittest import mock as mock |
| 13 | + |
| 14 | +from SoftLayer.CLI import exceptions |
| 15 | +from SoftLayer import testing |
| 16 | + |
| 17 | + |
| 18 | +class SshKeyTests(testing.TestCase): |
| 19 | + def test_add_without_key_errors(self): |
| 20 | + result = self.run_command(['sshkey', 'add', 'key1']) |
| 21 | + |
| 22 | + self.assertEqual(result.exit_code, 2) |
| 23 | + self.assertIsInstance(result.exception, exceptions.ArgumentError) |
| 24 | + |
| 25 | + def test_add_with_key_file_and_key_argument_errors(self): |
| 26 | + path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub') |
| 27 | + result = self.run_command(['sshkey', 'add', 'key1', |
| 28 | + '--key=some_key', |
| 29 | + '--in-file=%s' % path]) |
| 30 | + |
| 31 | + self.assertEqual(result.exit_code, 2) |
| 32 | + self.assertIsInstance(result.exception, exceptions.ArgumentError) |
| 33 | + |
| 34 | + def test_add_by_option(self): |
| 35 | + service = self.client['Security_Ssh_Key'] |
| 36 | + mock_key = service.getObject()['key'] |
| 37 | + |
| 38 | + result = self.run_command(['sshkey', 'add', 'key1', |
| 39 | + '--key=%s' % mock_key, |
| 40 | + '--note=my key']) |
| 41 | + |
| 42 | + self.assert_no_fail(result) |
| 43 | + self.assertEqual(json.loads(result.output), |
| 44 | + "SSH key added: aa:bb:cc:dd") |
| 45 | + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject', |
| 46 | + args=({'notes': 'my key', |
| 47 | + 'key': mock_key, |
| 48 | + 'label': 'key1'},)) |
| 49 | + |
| 50 | + def test_add_by_file(self): |
| 51 | + path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub') |
| 52 | + |
| 53 | + result = self.run_command(['sshkey', 'add', 'key1', |
| 54 | + '--in-file=%s' % path]) |
| 55 | + |
| 56 | + self.assert_no_fail(result) |
| 57 | + self.assertEqual(json.loads(result.output), |
| 58 | + "SSH key added: aa:bb:cc:dd") |
| 59 | + service = self.client['Security_Ssh_Key'] |
| 60 | + mock_key = service.getObject()['key'] |
| 61 | + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject', |
| 62 | + args=({'notes': None, |
| 63 | + 'key': mock_key, |
| 64 | + 'label': 'key1'},)) |
| 65 | + |
| 66 | + def test_remove_key(self): |
| 67 | + result = self.run_command(['--really', 'sshkey', 'remove', '1234']) |
| 68 | + |
| 69 | + self.assert_no_fail(result) |
| 70 | + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'deleteObject', |
| 71 | + identifier=1234) |
| 72 | + |
| 73 | + @mock.patch('SoftLayer.CLI.formatting.no_going_back') |
| 74 | + def test_remove_key_fail(self, ngb_mock): |
| 75 | + ngb_mock.return_value = False |
| 76 | + result = self.run_command(['sshkey', 'remove', '1234']) |
| 77 | + |
| 78 | + self.assertEqual(result.exit_code, 2) |
| 79 | + |
| 80 | + def test_edit_key(self): |
| 81 | + result = self.run_command(['sshkey', 'edit', '1234', |
| 82 | + '--label=key1', '--note=my key']) |
| 83 | + |
| 84 | + self.assert_no_fail(result) |
| 85 | + self.assert_called_with('SoftLayer_Security_Ssh_Key', 'editObject', |
| 86 | + args=({'notes': 'my key', |
| 87 | + 'label': 'key1'},), |
| 88 | + identifier=1234) |
| 89 | + |
| 90 | + def test_edit_key_fail(self): |
| 91 | + fixture = self.set_mock('SoftLayer_Security_Ssh_Key', 'editObject') |
| 92 | + fixture.return_value = False |
| 93 | + |
| 94 | + result = self.run_command(['sshkey', 'edit', '1234', |
| 95 | + '--label=key1', '--note=my key']) |
| 96 | + |
| 97 | + self.assertEqual(result.exit_code, 2) |
| 98 | + |
| 99 | + def test_list_keys(self): |
| 100 | + result = self.run_command(['sshkey', 'list']) |
| 101 | + |
| 102 | + self.assert_no_fail(result) |
| 103 | + self.assertEqual(json.loads(result.output), |
| 104 | + [{'notes': '-', |
| 105 | + 'fingerprint': None, |
| 106 | + 'id': '100', |
| 107 | + 'label': 'Test 1'}, |
| 108 | + {'notes': 'my key', |
| 109 | + 'fingerprint': None, |
| 110 | + 'id': '101', |
| 111 | + 'label': 'Test 2'}]) |
| 112 | + |
| 113 | + def test_print_key(self): |
| 114 | + result = self.run_command(['sshkey', 'print', '1234']) |
| 115 | + |
| 116 | + self.assert_no_fail(result) |
| 117 | + self.assertEqual(json.loads(result.output), |
| 118 | + {'id': 1234, 'label': 'label', 'notes': 'notes'}) |
| 119 | + |
| 120 | + def test_print_key_file(self): |
| 121 | + if sys.platform.startswith("win"): |
| 122 | + self.skipTest("Test doesn't work in Windows") |
| 123 | + with tempfile.NamedTemporaryFile() as sshkey_file: |
| 124 | + service = self.client['Security_Ssh_Key'] |
| 125 | + mock_key = service.getObject()['key'] |
| 126 | + result = self.run_command(['sshkey', 'print', '1234', |
| 127 | + '--out-file=%s' % sshkey_file.name]) |
| 128 | + |
| 129 | + self.assert_no_fail(result) |
| 130 | + self.assertEqual(mock_key, sshkey_file.read().decode("utf-8")) |
0 commit comments