def test_remove(self): """ Verify that the remove method can remove items from the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": ["preexisting_value"]} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Remove an item from the store which we have previously seen res = self.assertRaisesNothing(syspurpose_store.remove, "already_present_key", "preexisting_value") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], []) self.assertTrue( res, "The remove method should return true when the store has changed") # Try to remove an item that we've previously not seen res = self.assertRaisesNothing(syspurpose_store.remove, "new_key", "any_value") self.assertFalse( res, "The remove method should return false when the store has not changed" )
def test_add(self): """ Verify that the add method of SyspurposeStore is able to add items to lists of items in the store, whether they existed prior or not. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": ["preexisting_value"]} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Brand new unseen key is added res = self.assertRaisesNothing(syspurpose_store.add, "new_key", "new_value") self.assertIn("new_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["new_key"], ["new_value"]) self.assertTrue( res, "The add method should return true when the store has changed") # Add to an already seen existing key res = self.assertRaisesNothing(syspurpose_store.add, "already_present_key", "new_value_2") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], ["preexisting_value", "new_value_2"]) self.assertTrue( res, "The add method should return true when the store has changed")
def test_unset_with_unicode_strings(self): """ Verify that the unset method of SyspurposeStore is able to unset unicode strings from items in the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {u'ονόματα': [u'Νίκος']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) res = self.assertRaisesNothing(syspurpose_store.unset, "ονόματα") # We expect a value of true from this method when the store was changed self.assertTrue( res, "The unset method should return true when the store has changed") self.assertNotIn(u'ονόματα', syspurpose_store.contents, msg="Expected the key to no longer be present") res = self.assertRaisesNothing(syspurpose_store.unset, 'άκυρο_κλειδί') # We expect falsey values when the store was not modified self.assertFalse( res, "The unset method should return false when the store has not changed" ) self.assertNotIn( u'άκυρο_κλειδί', syspurpose_store.contents, msg="The key passed to unset, has been added to the store")
def _update_file(cls, path, data): """ Write the contents of data to file in the first mode we can (effectively to create or update the file) :param path: The string path to the file location we should update :param data: The data to write to the file :return: None """ # Check if /etc/rhsm/syspurpose directory exists cls._create_missing_dir(USER_SYSPURPOSE_DIR) # Check if /var/lib/rhsm/cache/ directory exists cls._create_missing_dir(CACHE_DIR) # Then we can try to create syspurpose.json file try: f = io.open(path, 'w+', encoding='utf-8') except OSError as e: if e.errno != 17: raise else: write_to_file_utf8(f, data) f.flush() f.close() log.debug('Successfully updated syspurpose values at \'%s\'.' % path) log.debug('Failed to update syspurpose values at \'%s\'.' % path)
def write(self, fp=None): """ Write the current contents to the file at self.path """ if not fp: with io.open(self.path, 'w', encoding='utf-8') as f: write_to_file_utf8(f, self.contents) f.flush() else: write_to_file_utf8(fp, self.contents)
def write(self, fp=None): """ Write the current contents to the file at self.path """ if not fp: with io.open(self.path, 'w', encoding='utf-8') as f: write_to_file_utf8(f, self.contents) f.flush() else: write_to_file_utf8(fp, self.contents)
def test_read(self): """ Does read properly initialize a new SyspurposeStore? """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"arbitrary_key": "arbitrary_value"} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore.read, temp_dir) self.assertDictEqual(syspurpose_store.contents, test_data)
def test_read(self): """ Does read properly initialize a new SyspurposeStore? """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"arbitrary_key": "arbitrary_value"} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore.read, temp_dir) self.assertDictEqual(syspurpose_store.contents, test_data)
def test_read_file_with_unicode_content(self): """ The SyspurposeStore.read_file method should return True if the file with unicode content was successfully read. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {'key1': u'Νίκος', 'key2': [u'value_with_ř']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) self.assertTrue(os.path.exists(temp_dir)) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) res = self.assertRaisesNothing(syspurpose_store.read_file) self.assertTrue(bool(res))
def test_read_file_with_unicode_content(self): """ The SyspurposeStore.read_file method should return True if the file with unicode content was successfully read. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {'key1': u'Νίκος', 'key2': [u'value_with_ř']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) self.assertTrue(os.path.exists(temp_dir)) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) res = self.assertRaisesNothing(syspurpose_store.read_file) self.assertTrue(bool(res))
def test_sync_object_update_consumer_set_data(self, mock_rhsm, mock_exists): # Mocking of rhsm.config mock_rhsm.config = Mock() mock_rhsm.config.DEFAULT_PROXY_PORT = "3128" mock_rhsm.config.initConfig = Mock() instance_config = mock_rhsm.config.initConfig.return_value instance_config.get = Mock(return_value="/path/to/cert") # Mocking of rhsm.connection mock_rhsm.connection = Mock() mock_rhsm.connection.UEPConnection = Mock() instance_uep_connection = mock_rhsm.connection.UEPConnection.return_value instance_uep_connection.updateConsumer = Mock() # Mocking of rhsm.certificate mock_rhsm.certificate = Mock() mock_rhsm.certificate.create_from_file = Mock() instance_certificate = mock_rhsm.certificate.create_from_file.return_value instance_certificate.subject = Mock() instance_certificate.subject.get = Mock( return_value="9d4778ae-80fe-4eed-a631-6be35fded7fe") mock_exists.return_value = True syspurpose_sync = sync.SyspurposeSync("proxy.example.com", "1234", "user", "secret") temp_file = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = { "role": "foo-role", "service_level_agreement": "foo-sla", "addons": ["foo-addon", "bar-addon"], "usage": "foo-usage" } with io.open(temp_file, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = files.SyspurposeStore(temp_file) syspurpose_store.contents = dict(**test_data) syspurpose_sync.send_syspurpose_to_candlepin(syspurpose_store) instance_uep_connection.updateConsumer.assert_called_once_with( uuid="9d4778ae-80fe-4eed-a631-6be35fded7fe", role="foo-role", service_level="foo-sla", addons=["foo-addon", "bar-addon"], usage="foo-usage")
def update_file(path, data): """ Write the contents of data to file in the first mode we can (effectively to create or update the file) :param path: The string path to the file location we should update :param data: The data to write to the file :return: None """ # Check if /etc/rhsm/syspurpose directory exists if not os.path.isdir(USER_SYSPURPOSE_DIR): # If not create the file log.debug('Trying to create directory: %s' % USER_SYSPURPOSE_DIR) try: os.makedirs(USER_SYSPURPOSE_DIR, mode=0o755, exist_ok=True) except Exception as err: log.warning('Unable to create directory: %s, error: %s' % (USER_SYSPURPOSE_DIR, err)) # Check if /var/lib/rhsm/cache/ directory exists if not os.path.isdir(CACHE_DIR): log.debug('Creating directory: %s' % CACHE_DIR) try: os.makedirs(CACHE_DIR, mode=0o755, exist_ok=True) except Exception as err: log.warning('Unable to create directory: %s, error: %s' % (CACHE_DIR, err)) # Then we can try to create syspurpose.json file modes = ['w+'] for mode in modes: try: f = io.open(path, mode, encoding='utf-8') except OSError as e: if e.errno != 17: raise else: write_to_file_utf8(f, data) f.flush() f.close() log.debug('Successfully updated syspurpose values at \'%s\'.' % path) return True log.debug('Failed to update syspurpose values at \'%s\'.' % path) return False
def test_remove_with_unicode_strings(self): """ Verify that the remove method of SyspurposeStore is able to remove unicode strings from lists of items in the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {u'ονόματα': [u'Νίκος', u'Κώστας']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Remove from an already seen existing key res = self.assertRaisesNothing(syspurpose_store.remove, 'ονόματα', 'Κώστας') self.assertIn(u'ονόματα', syspurpose_store.contents) self.assertEqual(syspurpose_store.contents[u'ονόματα'], [u'Νίκος']) self.assertTrue(res, "The add method should return true when the store has changed")
def test_add_new_value_to_key_with_null_value(self): """ Verify that the add method of SyspurposeStore is able to add item to key with null value """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": None} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Brand new unseen key is added res = self.assertRaisesNothing(syspurpose_store.add, "already_present_key", "new_value") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], ["new_value"]) self.assertTrue(res, "The add method should return true when the store has changed")
def test_add_new_value_to_key_with_null_value(self): """ Verify that the add method of SyspurposeStore is able to add item to key with null value """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": None} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Brand new unseen key is added res = self.assertRaisesNothing(syspurpose_store.add, "already_present_key", "new_value") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], ["new_value"]) self.assertTrue(res, "The add method should return true when the store has changed")
def test_remove_with_unicode_strings(self): """ Verify that the remove method of SyspurposeStore is able to remove unicode strings from lists of items in the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {u'ονόματα': [u'Νίκος', u'Κώστας']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Remove from an already seen existing key res = self.assertRaisesNothing(syspurpose_store.remove, 'ονόματα', 'Κώστας') self.assertIn(u'ονόματα', syspurpose_store.contents) self.assertEqual(syspurpose_store.contents[u'ονόματα'], [u'Νίκος']) self.assertTrue(res, "The add method should return true when the store has changed")
def test_sync_object_update_consumer_set_data(self, mock_rhsm): # Mocking of rhsm.config mock_rhsm.config = Mock() mock_rhsm.config.DEFAULT_PROXY_PORT = "3128" mock_rhsm.config.initConfig = Mock() instance_config = mock_rhsm.config.initConfig.return_value instance_config.get = Mock(return_value="/path/to/cert") # Mocking of rhsm.connection mock_rhsm.connection = Mock() mock_rhsm.connection.UEPConnection = Mock() instance_uep_connection = mock_rhsm.connection.UEPConnection.return_value instance_uep_connection.updateConsumer = Mock() # Mocking of rhsm.certificate mock_rhsm.certificate = Mock() mock_rhsm.certificate.create_from_file = Mock() instance_certificate = mock_rhsm.certificate.create_from_file.return_value instance_certificate.subject = Mock() instance_certificate.subject.get = Mock(return_value="9d4778ae-80fe-4eed-a631-6be35fded7fe") syspurpose_sync = sync.SyspurposeSync("proxy.example.com", "1234", "user", "secret") temp_file = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = { "role": "foo-role", "service_level_agreement": "foo-sla", "addons": ["foo-addon", "bar-addon"], "usage": "foo-usage" } with io.open(temp_file, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = files.SyspurposeStore(temp_file) syspurpose_store.contents = dict(**test_data) syspurpose_sync.send_syspurpose_to_candlepin(syspurpose_store) instance_uep_connection.updateConsumer.assert_called_once_with( uuid="9d4778ae-80fe-4eed-a631-6be35fded7fe", role="foo-role", service_level="foo-sla", addons=["foo-addon", "bar-addon"], usage="foo-usage" )
def _read_file(self, file_contents=None, expected_contents=None): """ Utility method for logic common to the *read_file* tests. :param file_contents: :param expected_contents: :return: None """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') with io.open(temp_dir, 'w', encoding='utf-8') as f: if file_contents and not isinstance(file_contents, str): utils.write_to_file_utf8(f, file_contents) else: f.write(utils.make_utf8(file_contents or '')) f.flush() self.assertTrue(os.path.exists(temp_dir), "Unable to create test file in temp dir") # Actually do the test syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) self.assertRaisesNothing(syspurpose_store.read_file) self.assertEqual(syspurpose_store.contents, expected_contents)
def _read_file(self, file_contents=None, expected_contents=None): """ Utility method for logic common to the *read_file* tests. :param file_contents: :param expected_contents: :return: None """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') with io.open(temp_dir, 'w', encoding='utf-8') as f: if file_contents and not isinstance(file_contents, str): utils.write_to_file_utf8(f, file_contents) else: f.write(utils.make_utf8(file_contents or '')) f.flush() self.assertTrue(os.path.exists(temp_dir), "Unable to create test file in temp dir") # Actually do the test syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) self.assertRaisesNothing(syspurpose_store.read_file) self.assertEqual(syspurpose_store.contents, expected_contents)
def test_remove(self): """ Verify that the remove method can remove items from the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": ["preexisting_value"]} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Remove an item from the store which we have previously seen res = self.assertRaisesNothing(syspurpose_store.remove, "already_present_key", "preexisting_value") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], []) self.assertTrue(res, "The remove method should return true when the store has changed") # Try to remove an item that we've previously not seen res = self.assertRaisesNothing(syspurpose_store.remove, "new_key", "any_value") self.assertFalse(res, "The remove method should return false when the store has not changed")
def update_file(path, data): """ Write the contents of data to file in the first mode we can (effectively to create or update the file) :param path: The string path to the file location we should update :param data: The data to write to the file :return: None """ modes = ['w+'] for mode in modes: try: f = io.open(path, mode, encoding='utf-8') except OSError as e: if e.errno != 17: raise else: write_to_file_utf8(f, data) f.flush() f.close() log.debug('Successfully updated syspurpose values at \'%s\'.' % path) return True log.debug('Failed to update syspurpose values at \'%s\'.' % path) return False
def update_file(path, data): """ Write the contents of data to file in the first mode we can (effectively to create or update the file) :param path: The string path to the file location we should update :param data: The data to write to the file :return: None """ modes = ['w+'] for mode in modes: try: f = io.open(path, mode, encoding='utf-8') except OSError as e: if e.errno != 17: raise else: write_to_file_utf8(f, data) f.flush() f.close() log.debug('Successfully updated syspurpose values at \'%s\'.' % path) return True log.debug('Failed to update syspurpose values at \'%s\'.' % path) return False
def test_add(self): """ Verify that the add method of SyspurposeStore is able to add items to lists of items in the store, whether they existed prior or not. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {"already_present_key": ["preexisting_value"]} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) # Brand new unseen key is added res = self.assertRaisesNothing(syspurpose_store.add, "new_key", "new_value") self.assertIn("new_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["new_key"], ["new_value"]) self.assertTrue(res, "The add method should return true when the store has changed") # Add to an already seen existing key res = self.assertRaisesNothing(syspurpose_store.add, "already_present_key", "new_value_2") self.assertIn("already_present_key", syspurpose_store.contents) self.assertEqual(syspurpose_store.contents["already_present_key"], ["preexisting_value", "new_value_2"]) self.assertTrue(res, "The add method should return true when the store has changed")
def test_unset_with_unicode_strings(self): """ Verify that the unset method of SyspurposeStore is able to unset unicode strings from items in the store. """ temp_dir = os.path.join(self._mktmp(), 'syspurpose_file.json') test_data = {u'ονόματα': [u'Νίκος']} with io.open(temp_dir, 'w', encoding='utf-8') as f: utils.write_to_file_utf8(f, test_data) syspurpose_store = self.assertRaisesNothing(files.SyspurposeStore, temp_dir) syspurpose_store.contents = dict(**test_data) res = self.assertRaisesNothing(syspurpose_store.unset, "ονόματα") # We expect a value of true from this method when the store was changed self.assertTrue(res, "The unset method should return true when the store has changed") self.assertIn(u'ονόματα', syspurpose_store.contents, msg="Expected the key to still be in the contents, but reset to None") # We expect the item to have been unset to None self.assertEqual(syspurpose_store.contents[u'ονόματα'], None) res = self.assertRaisesNothing(syspurpose_store.unset, 'άκυρο_κλειδί') # We expect falsey values when the store was not modified self.assertFalse(res, "The unset method should return false when the store has not changed") self.assertNotIn(u'άκυρο_κλειδί', syspurpose_store.contents, msg="The key passed to unset, has been added to the store")