def test_transfer_entitlement_keys(self): """Test the entitlement keys transfer method of the subscription token transfer task.""" # simulate entitlement keys not existing with tempfile.TemporaryDirectory() as sysroot: task = TransferSubscriptionTokensTask( sysroot=sysroot, transfer_subscription_tokens=True) task._copy_pem_files = Mock() task._copy_pem_files.return_value = True task._transfer_entitlement_keys() sysroot_path = util.join_paths( sysroot, TransferSubscriptionTokensTask.RHSM_ENTITLEMENT_KEYS_PATH) task._copy_pem_files.assert_called_once_with( TransferSubscriptionTokensTask.RHSM_ENTITLEMENT_KEYS_PATH, sysroot_path) # simulate entitlement keys not existing # - this is a critical error and should raise an exception # (without proper certificates and keys the target system # would be unable to communicate with the Red Hat subscription # infrastructure) with tempfile.TemporaryDirectory() as sysroot: task = TransferSubscriptionTokensTask( sysroot=sysroot, transfer_subscription_tokens=True) task._copy_pem_files = Mock() task._copy_pem_files.return_value = False with self.assertRaises(SubscriptionTokenTransferError): task._transfer_entitlement_keys()
def test_copy_pem_files(self): """Test PEM file transfer method of the subscription token transfer task.""" with tempfile.TemporaryDirectory() as sysroot: task = TransferSubscriptionTokensTask( sysroot=sysroot, transfer_subscription_tokens=True) # input path does not exist with tempfile.TemporaryDirectory() as tempdir: input_dir = os.path.join(tempdir, "input") output_dir = os.path.join(tempdir, "output") self.assertFalse(task._copy_pem_files(input_dir, output_dir)) # input path is file with tempfile.TemporaryDirectory() as tempdir: input_dir = os.path.join(tempdir, "input") output_dir = os.path.join(tempdir, "output") os.mknod(input_dir) self.assertFalse(task._copy_pem_files(input_dir, output_dir)) # input path directory empty & not_empty=True with tempfile.TemporaryDirectory() as tempdir: input_dir = os.path.join(tempdir, "input") output_dir = os.path.join(tempdir, "output") os.mkdir(input_dir) self.assertFalse( task._copy_pem_files(input_dir, output_dir, not_empty=True)) # input path directory empty & not_empty=False with tempfile.TemporaryDirectory() as tempdir: input_dir = os.path.join(tempdir, "input") output_dir = os.path.join(tempdir, "output") os.mkdir(input_dir) self.assertTrue( task._copy_pem_files(input_dir, output_dir, not_empty=False)) # the output dir should have been created and should be empty self.assertTrue(os.path.isdir(output_dir)) self.assertEqual(os.listdir(output_dir), []) # test pem file transfer with tempfile.TemporaryDirectory() as tempdir: input_dir = os.path.join(tempdir, "input") output_dir = os.path.join(tempdir, "output") os.mkdir(input_dir) # couple pem files os.mknod(os.path.join(input_dir, "foo.pem")) os.mknod(os.path.join(input_dir, "bar.pem")) os.mknod(os.path.join(input_dir, "baz.pem")) # some unrelated files os.mknod(os.path.join(input_dir, "something.txt")) os.mknod(os.path.join(input_dir, "stuff.conf")) # unrelated subfolder unrelated_subfolder = os.path.join(input_dir, "unrelated") os.mkdir(unrelated_subfolder) os.mknod(os.path.join(unrelated_subfolder, "subfolder.pem")) os.mknod(os.path.join(unrelated_subfolder, "subfolder.txt")) # the method should return True self.assertTrue( task._copy_pem_files(input_dir, output_dir, not_empty=True)) # output folder should contain only the expected pem files # - turn the two lists to sets to avoid ordering issues self.assertEqual(set(os.listdir(output_dir)), set(["foo.pem", "bar.pem", "baz.pem"]))