-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close file descriptor for temp file before attempt to remove it (#40)
* Close file descriptor for temp file before attempt to remove it * Bump version + add tests * Minor refactor + additional tests * Remove debug statements
- Loading branch information
1 parent
8bc6d83
commit 9807266
Showing
5 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import base64 | ||
import os | ||
import sys | ||
|
||
from apsconnectcli.action_logger import Logger | ||
|
||
LOG_DIR = os.path.expanduser('~/.apsconnect') | ||
|
||
if not os.path.exists(LOG_DIR): | ||
os.makedirs(LOG_DIR) | ||
|
||
LOG_FILE = os.path.join(LOG_DIR, "apsconnect.log") | ||
|
||
sys.stdout = Logger(LOG_FILE, sys.stdout) | ||
sys.stderr = Logger(LOG_FILE, sys.stderr) | ||
|
||
|
||
def read_cluster_certificate(ca_cert): | ||
try: | ||
with open(ca_cert) as _file: | ||
ca_cert_data = base64.b64encode(_file.read().encode()) | ||
except Exception as e: | ||
print("Unable to read ca_cert file, error: {}".format(e)) | ||
sys.exit(1) | ||
else: | ||
return ca_cert_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import base64 | ||
import sys | ||
from unittest import TestCase | ||
|
||
from apsconnectcli.cluster import read_cluster_certificate | ||
|
||
if sys.version_info >= (3,): | ||
from unittest.mock import patch, MagicMock | ||
|
||
_BUILTINS_OPEN = 'builtins.open' | ||
_BUILTINS_PRINT = 'builtins.print' | ||
else: | ||
from mock import patch, MagicMock | ||
|
||
_BUILTINS_OPEN = 'apsconnectcli.cluster.open' | ||
_BUILTINS_PRINT = 'apsconnectcli.cluster.print' | ||
|
||
|
||
class TestClusterOperation(TestCase): | ||
def test_read_cluster_certificate_file_not_found(self): | ||
with patch(_BUILTINS_OPEN) as open_mock, \ | ||
patch('apsconnectcli.cluster.sys') as sys_mock: | ||
open_mock.side_effect = Exception("All is lost") | ||
|
||
read_cluster_certificate(None) | ||
|
||
sys_mock.exit.assert_called_with(1) | ||
|
||
def test_read_cluster_certificate_ok(self): | ||
with patch(_BUILTINS_OPEN) as open_mock, \ | ||
patch('apsconnectcli.cluster.sys') as sys_mock: | ||
_file = MagicMock() | ||
_file.read.return_value = "Certificate data" | ||
open_mock.return_value.__enter__.return_value = _file | ||
data = read_cluster_certificate(None) | ||
|
||
self.assertEqual(base64.b64decode(data).decode(), "Certificate data") | ||
sys_mock.exit.assert_not_called() |