def test_type_enforcement(self):
        ml = ManagedList('test_config_domain',
                         'test_key1',
                         'test_name',
                         'test_description',
                         TEST_ZK_HOSTS,
                         TEST_AWS_KEYFILE,
                         TEST_S3_BUCKET,
                         data_type=int)
        self.assertTrue(ml.data_type is int)
        ml.clear()
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, 456]))
        # reject operations with invalid values
        self.assertRaises(ValueError, ml.add, "xyz")
        self.assertRaises(ValueError, ml.add_many, [789, "101", "78a"])

        # the previous two invalid operation should not change the list, even some of the values are valid
        self.assertEqual([123, 456], ml.get_list())

        # accept `convertable' values
        self.assertEqual(0, ml.add("123"))  # valid but already exists
        self.assertEqual(1, ml.add("789"))
        self.assertEqual([123, 456, 789], ml.get_list())
        self.assertEqual(1, ml.add_many([789, "101"]))
        self.assertEqual([123, 456, 789, 101], ml.get_list())

        # reject set_list if the new list contains invalid value
        self.assertRaises(ValueError, ml.set_list, [123, "xxx"])
        self.assertEqual([123, 456, 789, 101],
                         ml.get_list())  # set_list should be atomic
        self.assertRaises(ValueError, ml.remove, "xyz")
        self.assertEqual(1, ml.remove(123))
        self.assertEqual(1, ml.remove("456"))
        self.assertEqual([789, 101], ml.get_list())

        ml.clear()
        self.assertEqual([], ml.get_list())

        ml = ManagedList('test_config_domain',
                         'test_key2',
                         'test_name',
                         'test_description',
                         TEST_ZK_HOSTS,
                         TEST_AWS_KEYFILE,
                         TEST_S3_BUCKET,
                         data_type=str)
        self.assertTrue(ml.data_type is str)
        ml.clear()

        # test str type
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, "456"]))
        self.assertEqual(1, ml.add_many(["123", 789]))
        self.assertEqual(["123", "456", "789"], ml.get_list())
        ml.set_list([123, 456, 789])
        self.assertEqual(
            ["123", "456", "789"],
            ml.get_list())  # values should be converted to specified type
Beispiel #2
0
    def test_type_enforcement(self):
        ml = ManagedList(
            'test_config_domain', 'test_key1', 'test_name', 'test_description',
            TEST_ZK_HOSTS, TEST_AWS_KEYFILE, TEST_S3_BUCKET, data_type=int)
        self.assertTrue(ml.data_type is int)
        ml.clear()
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, 456]))
        # reject operations with invalid values
        self.assertRaises(ValueError, ml.add, "xyz")
        self.assertRaises(ValueError, ml.add_many, [789, "101", "78a"])

        # the previous two invalid operation should not change the list, even some of the values are valid
        self.assertEqual([123, 456], ml.get_list())

        # accept `convertable' values
        self.assertEqual(0, ml.add("123"))  # valid but already exists
        self.assertEqual(1, ml.add("789"))
        self.assertEqual([123, 456, 789], ml.get_list())
        self.assertEqual(1, ml.add_many([789, "101"]))
        self.assertEqual([123, 456, 789, 101], ml.get_list())

        # reject set_list if the new list contains invalid value
        self.assertRaises(ValueError, ml.set_list, [123, "xxx"])
        self.assertEqual([123, 456, 789, 101], ml.get_list())  # set_list should be atomic
        self.assertRaises(ValueError, ml.remove, "xyz")
        self.assertEqual(1, ml.remove(123))
        self.assertEqual(1, ml.remove("456"))
        self.assertEqual([789, 101], ml.get_list())

        ml.clear()
        self.assertEqual([], ml.get_list())

        ml = ManagedList(
            'test_config_domain', 'test_key2', 'test_name', 'test_description',
            TEST_ZK_HOSTS, TEST_AWS_KEYFILE, TEST_S3_BUCKET,
            data_type=str)
        self.assertTrue(ml.data_type is str)
        ml.clear()

        # test str type
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, "456"]))
        self.assertEqual(1, ml.add_many(["123", 789]))
        self.assertEqual(["123", "456", "789"], ml.get_list())
        ml.set_list([123, 456, 789])
        self.assertEqual(["123", "456", "789"], ml.get_list())  # values should be converted to specified type
class ConfigBasedManagedListsTestCase(TestCase):
    def setUp(self):
        self.mock_zk_config_manager = mock.patch(
            'kingpin.manageddata.managed_datastructures.ZKConfigManager',
            mock_zk_config_manager.MockZkConfigManager)
        self.mock_zk_config_manager.start()

        self.managed_list = ManagedList('test_config_domain', 'test_key',
                                        'test_name', 'test_description',
                                        TEST_ZK_HOSTS, TEST_AWS_KEYFILE,
                                        TEST_S3_BUCKET)

    def tearDown(self):
        self.mock_zk_config_manager.stop()

    def test_basic_operations(self):
        """Check basic add, remove, count and etc. operations for config based managed list."""
        self.assertEqual([], self.managed_list.get_list())

        self.assertEqual(1, self.managed_list.add('a.com'))
        self.assertEqual(['a.com'], self.managed_list.get_list())
        self.assertEqual(set(['a.com']), self.managed_list.get_set())
        self.assertTrue(self.managed_list.contains('a.com'))
        self.assertFalse(self.managed_list.contains('b.com'))
        self.assertEqual(1, self.managed_list.count())
        self.assertEqual(0, self.managed_list.add('a.com'))

        self.assertEqual(1, self.managed_list.add('b.com'))
        self.assertEqual(['a.com', 'b.com'], self.managed_list.get_list())
        self.assertEqual(set(['a.com', 'b.com']), self.managed_list.get_set())
        self.assertTrue(self.managed_list.contains('a.com'))
        self.assertTrue(self.managed_list.contains('b.com'))
        self.assertEqual(2, self.managed_list.count())

        self.assertEqual(0, self.managed_list.remove('c.com'))
        self.assertEqual(1, self.managed_list.remove('a.com'))
        self.assertEqual(['b.com'], self.managed_list.get_list())
        self.assertEqual(set(['b.com']), self.managed_list.get_set())
        self.assertFalse(self.managed_list.contains('a.com'))
        self.assertTrue(self.managed_list.contains('b.com'))
        self.assertEqual(1, self.managed_list.count())

        self.managed_list.delete()
        self.assertEqual([], self.managed_list.get_list())
        self.assertEqual(0, self.managed_list.count())

        self.assertEqual(2, self.managed_list.add_many(['test1', 'test2']))
        self.assertEqual(self.managed_list.get_set(), set(['test1', 'test2']))

        self.assertEqual(
            2, self.managed_list.add_many(['test1', 'test4', 'test5']))
        self.assertEqual(self.managed_list.get_set(),
                         set(['test1', 'test2', 'test4', 'test5']))

        self.assertEqual(1, self.managed_list.set_list(["abc"]))
        self.assertEqual(["abc"], self.managed_list.get_list())

    def test_type_enforcement(self):
        ml = ManagedList('test_config_domain',
                         'test_key1',
                         'test_name',
                         'test_description',
                         TEST_ZK_HOSTS,
                         TEST_AWS_KEYFILE,
                         TEST_S3_BUCKET,
                         data_type=int)
        self.assertTrue(ml.data_type is int)
        ml.clear()
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, 456]))
        # reject operations with invalid values
        self.assertRaises(ValueError, ml.add, "xyz")
        self.assertRaises(ValueError, ml.add_many, [789, "101", "78a"])

        # the previous two invalid operation should not change the list, even some of the values are valid
        self.assertEqual([123, 456], ml.get_list())

        # accept `convertable' values
        self.assertEqual(0, ml.add("123"))  # valid but already exists
        self.assertEqual(1, ml.add("789"))
        self.assertEqual([123, 456, 789], ml.get_list())
        self.assertEqual(1, ml.add_many([789, "101"]))
        self.assertEqual([123, 456, 789, 101], ml.get_list())

        # reject set_list if the new list contains invalid value
        self.assertRaises(ValueError, ml.set_list, [123, "xxx"])
        self.assertEqual([123, 456, 789, 101],
                         ml.get_list())  # set_list should be atomic
        self.assertRaises(ValueError, ml.remove, "xyz")
        self.assertEqual(1, ml.remove(123))
        self.assertEqual(1, ml.remove("456"))
        self.assertEqual([789, 101], ml.get_list())

        ml.clear()
        self.assertEqual([], ml.get_list())

        ml = ManagedList('test_config_domain',
                         'test_key2',
                         'test_name',
                         'test_description',
                         TEST_ZK_HOSTS,
                         TEST_AWS_KEYFILE,
                         TEST_S3_BUCKET,
                         data_type=str)
        self.assertTrue(ml.data_type is str)
        ml.clear()

        # test str type
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, "456"]))
        self.assertEqual(1, ml.add_many(["123", 789]))
        self.assertEqual(["123", "456", "789"], ml.get_list())
        ml.set_list([123, 456, 789])
        self.assertEqual(
            ["123", "456", "789"],
            ml.get_list())  # values should be converted to specified type

    def test_corrupted_config_data(self):
        """Check managed list only updated to the new config data if it is the correct format,
        otherwise default to empty list."""
        self.managed_list.zk_config_manager._data = "[dasdsad"
        self.managed_list._reload_config_data()
        self.assertEqual([], self.managed_list.get_list())

        self.managed_list.zk_config_manager._data = "{}"
        self.managed_list._reload_config_data()
        self.assertEqual([], self.managed_list.get_list())
Beispiel #4
0
class ConfigBasedManagedListsTestCase(TestCase):

    def setUp(self):
        self.mock_zk_config_manager = mock.patch(
            'kingpin.manageddata.managed_datastructures.ZKConfigManager', mock_zk_config_manager.MockZkConfigManager)
        self.mock_zk_config_manager.start()

        self.managed_list = ManagedList(
            'test_config_domain', 'test_key', 'test_name', 'test_description',
            TEST_ZK_HOSTS, TEST_AWS_KEYFILE, TEST_S3_BUCKET)

    def tearDown(self):
        self.mock_zk_config_manager.stop()

    def test_basic_operations(self):
        """Check basic add, remove, count and etc. operations for config based managed list."""
        self.assertEqual([], self.managed_list.get_list())

        self.assertEqual(1, self.managed_list.add('a.com'))
        self.assertEqual(['a.com'], self.managed_list.get_list())
        self.assertEqual(set(['a.com']), self.managed_list.get_set())
        self.assertTrue(self.managed_list.contains('a.com'))
        self.assertFalse(self.managed_list.contains('b.com'))
        self.assertEqual(1, self.managed_list.count())
        self.assertEqual(0, self.managed_list.add('a.com'))

        self.assertEqual(1, self.managed_list.add('b.com'))
        self.assertEqual(['a.com', 'b.com'], self.managed_list.get_list())
        self.assertEqual(set(['a.com', 'b.com']), self.managed_list.get_set())
        self.assertTrue(self.managed_list.contains('a.com'))
        self.assertTrue(self.managed_list.contains('b.com'))
        self.assertEqual(2, self.managed_list.count())

        self.assertEqual(0, self.managed_list.remove('c.com'))
        self.assertEqual(1, self.managed_list.remove('a.com'))
        self.assertEqual(['b.com'], self.managed_list.get_list())
        self.assertEqual(set(['b.com']), self.managed_list.get_set())
        self.assertFalse(self.managed_list.contains('a.com'))
        self.assertTrue(self.managed_list.contains('b.com'))
        self.assertEqual(1, self.managed_list.count())

        self.managed_list.delete()
        self.assertEqual([], self.managed_list.get_list())
        self.assertEqual(0, self.managed_list.count())

        self.assertEqual(2, self.managed_list.add_many(['test1', 'test2']))
        self.assertEqual(self.managed_list.get_set(), set(['test1', 'test2']))

        self.assertEqual(2, self.managed_list.add_many(
            ['test1', 'test4', 'test5']))
        self.assertEqual(self.managed_list.get_set(), set(
            ['test1', 'test2', 'test4', 'test5']))

        self.assertEqual(1, self.managed_list.set_list(["abc"]))
        self.assertEqual(["abc"], self.managed_list.get_list())

    def test_type_enforcement(self):
        ml = ManagedList(
            'test_config_domain', 'test_key1', 'test_name', 'test_description',
            TEST_ZK_HOSTS, TEST_AWS_KEYFILE, TEST_S3_BUCKET, data_type=int)
        self.assertTrue(ml.data_type is int)
        ml.clear()
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, 456]))
        # reject operations with invalid values
        self.assertRaises(ValueError, ml.add, "xyz")
        self.assertRaises(ValueError, ml.add_many, [789, "101", "78a"])

        # the previous two invalid operation should not change the list, even some of the values are valid
        self.assertEqual([123, 456], ml.get_list())

        # accept `convertable' values
        self.assertEqual(0, ml.add("123"))  # valid but already exists
        self.assertEqual(1, ml.add("789"))
        self.assertEqual([123, 456, 789], ml.get_list())
        self.assertEqual(1, ml.add_many([789, "101"]))
        self.assertEqual([123, 456, 789, 101], ml.get_list())

        # reject set_list if the new list contains invalid value
        self.assertRaises(ValueError, ml.set_list, [123, "xxx"])
        self.assertEqual([123, 456, 789, 101], ml.get_list())  # set_list should be atomic
        self.assertRaises(ValueError, ml.remove, "xyz")
        self.assertEqual(1, ml.remove(123))
        self.assertEqual(1, ml.remove("456"))
        self.assertEqual([789, 101], ml.get_list())

        ml.clear()
        self.assertEqual([], ml.get_list())

        ml = ManagedList(
            'test_config_domain', 'test_key2', 'test_name', 'test_description',
            TEST_ZK_HOSTS, TEST_AWS_KEYFILE, TEST_S3_BUCKET,
            data_type=str)
        self.assertTrue(ml.data_type is str)
        ml.clear()

        # test str type
        self.assertEqual([], ml.get_list())
        self.assertEqual(2, ml.add_many([123, "456"]))
        self.assertEqual(1, ml.add_many(["123", 789]))
        self.assertEqual(["123", "456", "789"], ml.get_list())
        ml.set_list([123, 456, 789])
        self.assertEqual(["123", "456", "789"], ml.get_list())  # values should be converted to specified type

    def test_corrupted_config_data(self):
        """Check managed list only updated to the new config data if it is the correct format,
        otherwise default to empty list."""
        self.managed_list.zk_config_manager._data = "[dasdsad"
        self.managed_list._reload_config_data()
        self.assertEqual([], self.managed_list.get_list())

        self.managed_list.zk_config_manager._data = "{}"
        self.managed_list._reload_config_data()
        self.assertEqual([], self.managed_list.get_list())