Skip to content

Commit 33aa9c4

Browse files
author
Victor Machado
committed
Implemented test-credentials
1 parent ee4bea8 commit 33aa9c4

File tree

7 files changed

+152
-134
lines changed

7 files changed

+152
-134
lines changed

Diff for: .github/workflows/test-credentials.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test "froster credentials" command
2+
3+
run-name: Test "froster credentials" command
4+
5+
on: [push, pull_request]
6+
7+
jobs:
8+
froster-index:
9+
runs-on: ubuntu-latest
10+
steps:
11+
12+
- uses: actions/checkout@v4
13+
with:
14+
ref: ${{ github.ref }}
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v3
18+
with:
19+
python-version: '3.10'
20+
21+
- name: Create and activate virtual environment
22+
run: |
23+
python -m venv .venv
24+
source .venv/bin/activate
25+
26+
- name: Install froster locally
27+
env:
28+
LOCAL_INSTALL: true
29+
run: ./install.sh
30+
31+
- name: Run test_credentials tests
32+
env:
33+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
34+
AWS_SECRET: ${{ secrets.AWS_SECRET }}
35+
run: python3 tests/test_credentials.py

Diff for: .github/workflows/test-froster-index.yml renamed to .github/workflows/test-index.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ jobs:
2626

2727
- name: Create and activate virtual environment
2828
run: |
29-
python -m venv venv
30-
source venv/bin/activate
29+
python -m venv .venv
30+
source .venv/bin/activate
3131
32-
- name: Install froster
32+
- name: Install froster locally
3333
env:
3434
LOCAL_INSTALL: true
3535
run: ./install.sh

Diff for: .vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"-c",
44
"source .venv/bin/activate"
55
],
6-
"python.pythonPath": ".venv/bin/python"
6+
"python.pythonPath": ".venv/bin/python",
7+
"python.analysis.extraPaths": [
8+
"./tests"
9+
]
710
}

Diff for: froster/froster.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -7533,9 +7533,7 @@ def main():
75337533
if cfg.check_update():
75347534
cmd.subcmd_update(mute_no_update=True)
75357535

7536-
if res:
7537-
sys.exit(0)
7538-
else:
7536+
if not res:
75397537
sys.exit(1)
75407538

75417539
except KeyboardInterrupt:

Diff for: tests/config.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
import string
55
import tempfile
66

7-
87
def random_string(length=4):
98
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
109

11-
NAME = "Test-Bob"
12-
NAME_2 = "Test-Alice"
13-
10+
NAME_1 = "bob"
11+
NAME_2 = "alice"
12+
13+
EMAIL_1 = "[email protected]"
1414
EMAIL_2 = "[email protected]"
1515

16-
AWS_DEFAULT_PATH = os.path.join(tempfile.gettempdir(), '.aws')
17-
AWS_REGION = "eu-west-1"
16+
PROVIDER_1 = "AWS"
17+
PROVIDER_2 = "AWS"
18+
19+
PROFILE_1 = "profile bob"
20+
PROFILE_2 = "profile alice"
21+
22+
AWS_CREDENTIALS_PROFILE_1 = "bob"
23+
AWS_CREDENTIALS_PROFILE_2 = "alice"
24+
25+
AWS_REGION_1 = "eu-west-2"
1826
AWS_REGION_2 = "eu-west-2"
19-
AWS_PROFILE = "froster-unittest-bob"
20-
AWS_PROFILE_2 = "froster-unittest-alice"
2127

2228
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
2329
AWS_SECRET = os.getenv('AWS_SECRET')
@@ -28,15 +34,19 @@ def random_string(length=4):
2834
NIH_SECTION = 'NIH'
2935
S3_SECTION = 'S3'
3036

31-
S3_BUCKET_NAME_CONFIG = 'froster-unittest-config-' + random_string(4)
37+
S3_BUCKET_NAME_CONFIG_1 = 'froster-unittest-config-' + random_string(4)
3238
S3_BUCKET_NAME_CONFIG_2 = 'froster-unittest-config-' + random_string(4)
3339

34-
S3_BUCKET_NAME_INDEX = 'froster-unittest-index-' + random_string(4)
40+
S3_BUCKET_NAME_INDEX_1 = 'froster-unittest-index-' + random_string(4)
3541
S3_BUCKET_NAME_INDEX_2 = 'froster-unittest-index-' + random_string(4)
3642

37-
S3_ARCHIVE_DIR = 'froster_bob'
38-
S3_ARCHIVE_DIR_2 = 'froster_alice'
39-
S3_STORAGE_CLASS = 'DEEP_ARCHIVE'
43+
S3_BUCKET_NAME_CREDENTIALS_1 = 'froster-unittest-credentials-' + random_string(4)
44+
S3_BUCKET_NAME_CREDENTIALS_2 = 'froster-unittest-credentials-' + random_string(4)
45+
46+
S3_ARCHIVE_DIR_1 = 'froster-bob'
47+
S3_ARCHIVE_DIR_2 = 'froster-alice'
48+
49+
S3_STORAGE_CLASS_1 = 'DEEP_ARCHIVE'
4050
S3_STORAGE_CLASS_2 = 'GLACIER'
4151

4252
SLURM_WALLTIME_DAYS = 8

Diff for: tests/test_credentials.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import sys
2+
import unittest
3+
from unittest.mock import patch
4+
from froster import *
5+
from tests.config import *
6+
7+
8+
class TestCredentials(unittest.TestCase):
9+
'''Test the froster credentials command.'''
10+
11+
# Method executed once before all tests
12+
@classmethod
13+
def setUpClass(cls):
14+
'''- Set up class.'''
15+
16+
# Setup the froster configuration by mocking user input
17+
with \
18+
patch('sys.argv', ['froster', 'config']), \
19+
patch('inquirer.text', side_effect=[NAME_1, EMAIL_1, PROFILE_1, AWS_CREDENTIALS_PROFILE_1, AWS_ACCESS_KEY_ID, AWS_SECRET, S3_BUCKET_NAME_CREDENTIALS_1, S3_ARCHIVE_DIR_1]), \
20+
patch('inquirer.confirm', side_effect=[False, False, True, False]), \
21+
patch('inquirer.list_input', side_effect=['+ Create new profile', PROVIDER_1, '+ Create new credentials', AWS_REGION_1, '+ Create new bucket', S3_STORAGE_CLASS_1]), \
22+
patch('builtins.print') as mock_print:
23+
main()
24+
25+
# Method executed once after all tests
26+
27+
@classmethod
28+
def tearDownClass(cls):
29+
'''- Tear down class.'''
30+
31+
# Delete the S3 buckets
32+
with \
33+
patch('sys.argv', ['froster', '--debug', 'delete', '--bucket', S3_BUCKET_NAME_CONFIG_1]), \
34+
patch('builtins.print') as mock_print:
35+
main()
36+
37+
with \
38+
patch('sys.argv', ['froster', '--debug', 'delete', '--bucket', S3_BUCKET_NAME_CONFIG_2]), \
39+
patch('builtins.print') as mock_print:
40+
main()
41+
42+
def test_subcmd_credentials(self):
43+
'''- Test the froster credentials command.'''
44+
45+
# Run the index command and check if sys.exit(0), which means no issues detected while executing the command
46+
with \
47+
patch('sys.argv', ['froster', 'credentials']), \
48+
patch('builtins.print') as mock_print:
49+
self.assertFalse(main())
50+
51+
52+
if __name__ == '__main__':
53+
54+
try:
55+
if True:
56+
unittest.main(verbosity=2)
57+
else:
58+
suite = unittest.TestSuite()
59+
# FULL CONFIGURATION
60+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfig))
61+
62+
# PARTIAL CONFIGURATION
63+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigUser))
64+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigAWS))
65+
# suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestConfigShared))
66+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigNIH))
67+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigS3))
68+
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigSlurm))
69+
70+
# BASIC TEST CASE FOR EVERY TEST
71+
# suite.addTest(TestConfig('test_subcmd_config'))
72+
# suite.addTest(TestConfigUser('test_set_user'))
73+
# suite.addTest(TestConfigAWS('test_set_aws'))
74+
# suite.addTest(TestConfigShared('test_set_shared'))
75+
# suite.addTest(TestConfigNIH('test_set_nih'))
76+
suite.addTest(TestConfigS3('test_set_s3'))
77+
78+
runner = unittest.TextTestRunner(verbosity=2)
79+
runner.run(suite)
80+
81+
except KeyboardInterrupt:
82+
print("\nTests interrupted by the user. Exiting...")
83+
sys.exit(1)
84+
except Exception as e:
85+
print("\nAn error occurred during the tests execution: %s" % e)
86+
sys.exit(1)

Diff for: tests/test_index.py

-114
This file was deleted.

0 commit comments

Comments
 (0)