|
2 | 2 | from unittest.mock import MagicMock |
3 | 3 | from unittest.mock import mock_open |
4 | 4 | from unittest.mock import patch |
| 5 | +from unittest.mock import PropertyMock |
| 6 | + |
5 | 7 | import s3rpm |
6 | 8 |
|
7 | | -from pyrpm.tools.createrepo import YumRepository |
| 9 | +import botocore |
8 | 10 | import os |
9 | 11 | import json |
10 | 12 | import shutil |
11 | | -import gnupg |
12 | 13 |
|
13 | | -class S3AptTest(unittest.TestCase): |
| 14 | +class SubFunctionsTest(unittest.TestCase): |
14 | 15 |
|
15 | 16 | def setUp(self): |
16 | 17 | os.environ['BUCKET_NAME'] = 'bucket' |
17 | | - os.environ['REPO_DIR'] = 'test/repo' |
| 18 | + os.environ['REPO_DIR'] = 'test_s3rpm' |
18 | 19 | os.environ['GPG_KEY'] = '' |
19 | 20 | os.environ['PUBLIC'] = 'True' |
20 | 21 | os.environ['GPG_PASS']='123' |
21 | | - |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + if os.path.exists('test_s3rpm'): |
| 25 | + shutil.rmtree('test_s3rpm') |
22 | 26 |
|
23 | 27 | def test_public_private(self): |
24 | 28 | os.environ['PUBLIC'] = 'True' |
25 | | - assert s3rpm.get_public() == 'public-read' |
| 29 | + self.assertEqual(s3rpm.get_public(), 'public-read') |
26 | 30 |
|
27 | 31 | os.environ['PUBLIC'] = '' |
28 | | - assert s3rpm.get_public() == 'private' |
| 32 | + self.assertEqual(s3rpm.get_public(), 'private') |
29 | 33 |
|
30 | 34 | os.environ['PUBLIC'] = 'False' |
31 | | - assert s3rpm.get_public() == 'private' |
| 35 | + self.assertEqual(s3rpm.get_public(), 'private') |
32 | 36 |
|
33 | 37 |
|
34 | 38 | @patch('s3rpm.boto3') |
35 | 39 | def test_file_existance(self, s3_mock): |
36 | 40 | ret = s3rpm.check_bucket_file_existance('path') |
37 | | - assert ret == True |
| 41 | + self.assertEqual(ret, True) |
38 | 42 | s3_mock.resource().Object.assert_called_with("bucket", "path") |
39 | 43 | s3_mock.resource().Object().load.assert_called_with() |
40 | 44 |
|
| 45 | + #404 error |
| 46 | + p = PropertyMock(side_effect=botocore.exceptions.ClientError({'Error':{'Code': '404','Message':'no msg'}}, 'aa')) |
| 47 | + s3_mock.resource().Object().load = p |
41 | 48 |
|
| 49 | + ret = s3rpm.check_bucket_file_existance('path') |
| 50 | + self.assertEqual(ret, False) |
| 51 | + #non404 error |
| 52 | + p = PropertyMock(side_effect=botocore.exceptions.ClientError({'Error':{'Code': '403','Message':'no msg'}}, 'aa')) |
| 53 | + s3_mock.resource().Object().load = p |
| 54 | + |
| 55 | + with self.assertRaises(botocore.exceptions.ClientError): |
| 56 | + s3rpm.check_bucket_file_existance('path') |
| 57 | + |
| 58 | + @patch('s3rpm.YumRepository') |
42 | 59 | @patch('s3rpm.boto3') |
43 | 60 | @patch('s3rpm.check_bucket_file_existance') |
44 | | - def test_cache(self, check_mock, s3_mock): |
45 | | - repo = YumRepository('test/repo/') |
46 | | - cache = '{"/pkgname":"ID"}' |
| 61 | + def test_cache(self, check_mock, s3_mock, yum_mock): |
| 62 | + yum_mock = MagicMock(repodir='test_s3rpm/') |
| 63 | + cache = '{"pkgname" : "ID"}' |
| 64 | + repo = yum_mock |
47 | 65 | m = mock_open(read_data=cache) |
48 | 66 | check_mock.return_value = True |
49 | 67 |
|
50 | 68 | with patch('s3rpm.open', m): |
51 | 69 | cachenew = s3rpm.get_cache(repo) |
52 | | - assert json.loads(cache) == cachenew |
| 70 | + s3_mock.client().download_file.assert_called_with('bucket', 'test_s3rpm/repo_cache', 'test_s3rpm/repo_cache') |
| 71 | + self.assertEqual(json.loads(cache), cachenew) |
53 | 72 |
|
54 | 73 | check_mock.return_value = False |
55 | 74 |
|
56 | 75 | cachenew = s3rpm.get_cache(repo) |
57 | | - assert cachenew == {} |
58 | | - |
| 76 | + self.assertEqual(cachenew, {}) |
59 | 77 |
|
| 78 | + @patch('s3rpm.YumRepository') |
| 79 | + @patch('s3rpm.YumPackage') |
60 | 80 | @patch('s3rpm.get_cache') |
61 | 81 | @patch('s3rpm.boto3') |
62 | | - def test_new_files(self, s3_mock, cache_mock): |
63 | | - cache_mock.return_value = {"/pkgname-0.3.7-x86_64.rpm": "7cd368172d218ed2001ad7306ff74c727f0b1d7bfa5433d9b265e7830bf60184"} |
64 | | - repo = YumRepository('test/repo/') |
65 | | - repo.read() |
66 | | - cache = {"/pkgname-0.3.7-x86_64.rpm": "7cd368172d218ed2001ad7306ff74c727f0b1d7bfa5433d9b265e7830bf60184", "/pkgname-0.3.8-x86_64.rpm": "edcdbd077673a759585b1ebd4589d900c230a63e2c91c787861bcdcec9004707"} |
| 82 | + def test_new_files(self, s3_mock, cache_mock, yump_mock, yum_mock): |
| 83 | + |
| 84 | + cache_mock.return_value = {"/pkgname-0.3.7-x86_64.rpm": "test_id1"} |
| 85 | + yum_mock = MagicMock(repodir='test_s3rpm/') |
| 86 | + repo = yum_mock |
| 87 | + yump_mock.return_value = MagicMock(checksum='test_id2') |
| 88 | + cache = {"/pkgname-0.3.7-x86_64.rpm": "test_id1", "/pkgname-0.3.8-x86_64.rpm": "test_id2"} |
| 89 | + |
| 90 | + |
| 91 | + s3_mock.resource().Bucket().objects.filter.return_value = [MagicMock(key='test.file'),MagicMock(key='test_s3rpm/pkgname-0.3.8-x86_64.rpm'), MagicMock(key='test_s3rpm/pkgname-0.3.7-x86_64.rpm')] |
| 92 | + m = mock_open(read_data='') |
| 93 | + with patch('s3rpm.open', m): |
| 94 | + reponew, cachenew = s3rpm.check_changed_files(repo) |
67 | 95 |
|
68 | | - s3_mock.resource().Bucket().objects.filter.return_value = [MagicMock(key='test.file'),MagicMock(key='test/repo/pkgname-0.3.8-x86_64.rpm'), MagicMock(key='test/repo/pkgname-0.3.7-x86_64.rpm')] |
69 | | - reponew, cachenew = s3rpm.check_changed_files(repo) |
| 96 | + self.assertEqual(cache, cachenew) |
| 97 | + self.assertEqual(yum_mock.add_package.call_count, 1) |
70 | 98 |
|
71 | | - assert cache == cachenew |
72 | | - self.assertEqual(len(list(reponew.packages())), 2) |
73 | 99 |
|
74 | | - |
| 100 | + @patch('s3rpm.YumRepository') |
75 | 101 | @patch('s3rpm.get_cache') |
76 | 102 | @patch('s3rpm.boto3') |
77 | | - def test_delete_files(self, s3_mock, cache_mock): |
78 | | - cache_mock.return_value = {"/pkgname-0.3.7-x86_64.rpm": "7cd368172d218ed2001ad7306ff74c727f0b1d7bfa5433d9b265e7830bf60184"} |
79 | | - repo = YumRepository('test/repo/') |
| 103 | + def test_delete_files(self, s3_mock, cache_mock, yum_mock): |
| 104 | + cache_mock.return_value = {"/pkgname-0.3.7-x86_64.rpm": "test_id1"} |
| 105 | + yum_mock = MagicMock(repodir='test_s3rpm/') |
| 106 | + repo = yum_mock |
80 | 107 | cache = {} |
81 | 108 |
|
82 | 109 | s3_mock.resource().Bucket().objects.filter.return_value = [MagicMock(key='test.file')] |
83 | | - reponew, cachenew = s3rpm.check_changed_files(repo) |
84 | | - assert cache == cachenew |
85 | | - self.assertEqual(len(list(reponew.packages())), 0) |
| 110 | + _, cachenew = s3rpm.check_changed_files(repo) |
| 111 | + self.assertEqual(cache, cachenew) |
| 112 | + self.assertEqual(yum_mock.remove_package.call_count, 1) |
| 113 | + |
| 114 | + @patch('s3rpm.YumRepository') |
| 115 | + @patch('s3rpm.gnupg') |
| 116 | + @patch('s3rpm.boto3') |
| 117 | + def test_gpg(self, s3_mock, gpg_mock, yum_mock): |
| 118 | + os.environ['GPG_KEY'] = 'KeyNowExists' |
| 119 | + m = mock_open() |
| 120 | + repo = yum_mock() |
| 121 | + with patch('s3rpm.open', m): |
| 122 | + s3rpm.sign_md_file(repo) |
| 123 | + gpg_mock.GPG().sign_file.assert_called_with(s3rpm.open(), binary=False, clearsign=True, detach=True, passphrase='123') |
| 124 | + s3_mock.resource().Object().put.assert_called_with(ACL='public-read', Body=str(gpg_mock.GPG().sign_file())) |
| 125 | + |
| 126 | + def test_create_dir(self): |
| 127 | + ret = s3rpm.create_new_dir_if_not_exist('test_s3rpm/testfolder') |
| 128 | + self.assertEqual(True, ret) |
| 129 | + ret = s3rpm.create_new_dir_if_not_exist('test_s3rpm/testfolder') |
| 130 | + self.assertEqual(False, ret) |
86 | 131 |
|
87 | 132 |
|
88 | | - @patch('s3rpm.shutil') |
| 133 | +class HandlerTest(unittest.TestCase): |
| 134 | + def setUp(self): |
| 135 | + os.environ['BUCKET_NAME'] = 'bucket' |
| 136 | + os.environ['REPO_DIR'] = 'test_s3rpm' |
| 137 | + os.environ['GPG_KEY'] = '' |
| 138 | + os.environ['PUBLIC'] = 'True' |
| 139 | + os.environ['GPG_PASS']='123' |
| 140 | + self.m = mock_open(read_data='') |
| 141 | + |
| 142 | + def tearDown(self): |
| 143 | + if os.path.exists('test_s3rpm'): |
| 144 | + shutil.rmtree('test_s3rpm') |
89 | 145 | @patch('s3rpm.get_cache') |
90 | 146 | @patch('s3rpm.YumRepository') |
91 | 147 | @patch('s3rpm.boto3') |
92 | | - def test_hander(self, s3_mock, repo_mock, cache_mock, shutil_mock): |
| 148 | + def test_defined_repodir(self, s3_mock, yum_mock, cache_mock, ): |
93 | 149 | cache_mock.return_value = {"pkgname":"ID"} |
94 | | - os.environ['REPO_DIR'] = '/test/repo/' |
95 | 150 |
|
96 | | - repo_mock.return_value = YumRepository('test/repo/') |
97 | | - m = mock_open(read_data='') |
98 | | - with patch('s3rpm.open', m): |
| 151 | + yum_mock.return_value = MagicMock(repodir='test_s3rpm/') |
| 152 | + with patch('s3rpm.open', self.m): |
99 | 153 | s3rpm.lambda_handler(S3_EVENT, {}) |
100 | 154 | self.assertEqual(len(s3_mock.resource().Object().put.mock_calls), 5) |
101 | | - self.assertEqual(os.environ['REPO_DIR'],'test/repo') |
102 | | - |
| 155 | + self.assertEqual(os.environ['REPO_DIR'],'test_s3rpm') |
| 156 | + |
| 157 | + @patch('s3rpm.gnupg') |
| 158 | + @patch('s3rpm.shutil') |
| 159 | + @patch('s3rpm.get_cache') |
| 160 | + @patch('s3rpm.YumRepository') |
| 161 | + @patch('s3rpm.boto3') |
| 162 | + def test_gpg_test(self, s3_mock, yum_mock, cache_mock, sh_mock, gpg_mock): |
| 163 | + cache_mock.return_value = {"pkgname":"ID"} |
| 164 | + |
| 165 | + os.environ['GPG_KEY'] = 'KeyNowExists' |
103 | 166 | check = MagicMock() |
104 | 167 | check.return_value = False |
105 | | - repo_mock.return_value = YumRepository('test/testrepo/') |
106 | | - with patch('s3rpm.open', m): |
| 168 | + yum_mock.return_value = MagicMock(repodir='test_s3rpm/testrepo/') |
| 169 | + with patch('s3rpm.open', self.m): |
107 | 170 | with patch('s3rpm.check_bucket_file_existance', check): |
108 | 171 | s3rpm.lambda_handler(S3_EVENT, {}) |
109 | | - assert os.path.exists('test/testrepo/repodata/') == True |
110 | | - if os.path.exists('test/testrepo/repodata/'): |
111 | | - shutil.rmtree('test/testrepo/repodata/') |
| 172 | + gpg_mock.GPG().sign_file.assert_called_with(s3rpm.open(), binary=False, clearsign=True, detach=True, passphrase='123') |
| 173 | + assert os.path.exists('test_s3rpm/testrepo/') == True |
112 | 174 |
|
113 | | - |
114 | | - @patch('s3rpm.gnupg') |
115 | 175 | @patch('s3rpm.boto3') |
116 | | - def test_gpg(self, s3_mock, gpg_mock): |
117 | | - repo = YumRepository('test/repo') |
118 | | - os.environ['GPG_KEY'] = 'test/sec.key' |
119 | | - m = mock_open() |
120 | | - with patch('s3rpm.open', m): |
121 | | - s3rpm.sign_md_file(repo) |
122 | | - gpg_mock.GPG().sign_file.assert_called_with(s3rpm.open(), binary=False, clearsign=True, detach=True, passphrase='123') |
123 | | - |
124 | | -S3_EVENT = {"Records":[{"s3": {"object": {"key": "test/repo/pkgname-0.3.8-x86_64.rpm",},"bucket": {"name": "bucket",},},"eventName": "ObjectCreated:Put",}]} |
| 176 | + def test_bad_repo_dir_and_bucket_name(self, s3_mock): |
| 177 | + os.environ['REPO_DIR'] = '/test/repo/' |
| 178 | + os.environ['BUCKET_NAME'] = 'iamfakebucket' |
| 179 | + s3rpm.lambda_handler(S3_EVENT, {}) |
| 180 | + self.assertEqual(os.environ['REPO_DIR'], 'test/repo') |
| 181 | + s3_mock.client.assert_called_with('s3') |
| 182 | + self.assertEqual(len(s3_mock.resource().Object().put.mock_calls), 0) |
| 183 | +S3_EVENT = {"Records":[{"s3": {"object": {"key": "test_s3rpm/repo/pkgname-0.3.8-x86_64.rpm",},"bucket": {"name": "bucket",},},"eventName": "ObjectCreated:Put"}]} |
125 | 184 | if __name__ == '__main__': |
126 | 185 | unittest.main() |
127 | | - |
128 | | -def createrepo(): |
129 | | - repo = YumRepository('test/repo/') |
130 | | - repo.add_package(YumPackage(open('test/repo/pkgname-0.3.7-x86_64.rpm', 'rb'))) |
131 | | - repo.save() |
|
0 commit comments