def test_repo_existing_keys(self): """ Tests adding a new key when keys have already been written. The new key should be present but the old should be deleted. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('key1', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('keyX', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'keyX'))) self.assertTrue(not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'key1'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_update_key(self): """ Tests adding new contents for a key that already exists. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3', 'key'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_first_time(self): """ Tests adding keys to a repo that has never had keys before (i.e. the repo keys dir doesn't exist). """ # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo1') repo_keys.add_key('key1', 'KEY1') repo_keys.add_key('key2', 'KEY2') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo1'))) key_files = repo_keys.key_filenames() self.assertEqual(2, len(key_files)) for f in key_files: self.assertTrue(os.path.exists(f)) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY1') f = open(key_files[1], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY2')
def test_repo_update_key(self): """ Tests adding new contents for a key that already exists. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3', 'key'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_existing_keys(self): """ Tests adding a new key when keys have already been written. The new key should be present but the old should be deleted. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('key1', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('keyX', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'keyX'))) self.assertTrue(not os.path.exists( os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'key1'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_first_time(self): """ Tests adding keys to a repo that has never had keys before (i.e. the repo keys dir doesn't exist). """ # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo1') repo_keys.add_key('key1', 'KEY1') repo_keys.add_key('key2', 'KEY2') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo1'))) key_files = repo_keys.key_filenames() self.assertEqual(2, len(key_files)) for f in key_files: self.assertTrue(os.path.exists(f)) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY1') f = open(key_files[1], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY2')
def _handle_gpg_keys(repo, gpg_keys, keys_root_dir): """ Handles the processing of any GPG keys that were specified with the repo. The key files will be written to disk, deleting any existing key files that were there. The repo object will be updated with any values related to GPG key information. """ repo_keys = RepoKeyFiles(keys_root_dir, repo.id) if gpg_keys is not None and len(gpg_keys) > 0: repo['gpgcheck'] = '1' for key_name in gpg_keys: repo_keys.add_key(key_name, gpg_keys[key_name]) key_urls = ['file:' + kfn for kfn in repo_keys.key_filenames()] repo['gpgkey'] = '\n'.join(key_urls) else: repo['gpgcheck'] = '0' repo['gpgkey'] = None # Call this in either case to make sure any existing keys were deleted repo_keys.update_filesystem()
def test_repo_remove_keys(self): """ Tests calling update_filesystem for a repo that previously had keys but no longer does. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() self.assertTrue(os.path.exists(repo_keys.key_filenames()[0])) # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.update_filesystem() # Verify self.assertTrue(not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_list = repo_keys.key_filenames() self.assertTrue(key_list is not None) self.assertEqual(0, len(key_list))
def test_repo_remove_keys(self): """ Tests calling update_filesystem for a repo that previously had keys but no longer does. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() self.assertTrue(os.path.exists(repo_keys.key_filenames()[0])) # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.update_filesystem() # Verify self.assertTrue( not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_list = repo_keys.key_filenames() self.assertTrue(key_list is not None) self.assertEqual(0, len(key_list))