예제 #1
0
 def test_init(self):
     resource_type = 'type'
     resource_labels = {'key': 'value'}
     resource = Resource(resource_type, resource_labels)
     self.assertEqual(resource.type, resource_type)
     self.assertEqual(resource.labels, resource_labels)
     self.assertIsNot(resource.labels, resource_labels)
예제 #2
0
    def test_merge_resource(self):
        with self.assertRaises(ValueError):
            resource_module.merge_resources(None)
        with self.assertRaises(ValueError):
            resource_module.merge_resources([])

        r1 = Resource(None, {'lk1': 'lv11'})
        r2 = Resource('t2', {'lk1': 'lv12', 'lk2': 'lv22'})
        r3 = Resource('t3', {'lk2': 'lv23', 'lk3': 'lv33'})

        merged = resource_module.merge_resources([r1, r2, r3])
        self.assertEqual(merged.type, 't2')
        self.assertDictEqual(merged.labels, {
            'lk1': 'lv11',
            'lk2': 'lv22',
            'lk3': 'lv33'
        })
예제 #3
0
 def test_get_labels(self):
     label_key = 'key'
     label_value = 'value'
     resource_labels = {label_key: label_value}
     resource = Resource('type', resource_labels)
     self.assertEqual(resource.get_labels(), resource_labels)
     self.assertIsNot(resource.get_labels(), resource_labels)
     got_labels = resource.get_labels()
     got_labels[label_key] = mock.Mock()
     self.assertNotEqual(resource.get_labels(), got_labels)
     self.assertEqual(resource.get_labels()[label_key], label_value)
예제 #4
0
    def test_merge(self):
        r1 = Resource('t1', {'lk1': 'lv1'})
        r2 = Resource('t2', {'lk2': 'lv2'})

        r1m2 = r1.merge(r2)
        self.assertIsNot(r1m2, r1)
        self.assertIsNot(r1m2, r2)
        self.assertEqual(r1m2.type, r1.type)
        self.assertNotEqual(r1m2.type, r2.type)
        self.assertDictEqual(r1m2.labels, {'lk1': 'lv1', 'lk2': 'lv2'})

        r2m1 = r2.merge(r1)
        self.assertIsNot(r2m1, r1)
        self.assertIsNot(r2m1, r2)
        self.assertEqual(r2m1.type, r2.type)
        self.assertNotEqual(r2m1.type, r1.type)
        self.assertDictEqual(r2m1.labels, {'lk1': 'lv1', 'lk2': 'lv2'})
예제 #5
0
 def test_get_type(self):
     resource_type = 'type'
     resource = Resource(resource_type, None)
     self.assertEqual(resource.get_type(), resource_type)
예제 #6
0
 def test_default_init(self):
     resource = Resource()
     self.assertIsNone(resource.type)
     self.assertIsNotNone(resource.labels)
     self.assertDictEqual(resource.labels, {})
예제 #7
0
 def test_init_bad_args(self):
     long_string = (
         "long string is looooooooooooooooooooooooooooooooooooooooooooooooo"
         "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
         "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
         "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong")
     with self.assertRaises(ValueError):
         Resource('', {})
     with self.assertRaises(ValueError):
         Resource(chr(31), {'key': 'value'})
     with self.assertRaises(ValueError):
         Resource(long_string, {'key': 'value'})
     with self.assertRaises(ValueError):
         Resource('type', {"": 'value'})
     with self.assertRaises(ValueError):
         Resource('type', {None: 'value'})
     with self.assertRaises(ValueError):
         Resource('type', {'key': None})
     with self.assertRaises(ValueError):
         Resource('type', {chr(31): 'value'})
     with self.assertRaises(ValueError):
         Resource('type', {'key': chr(31)})
     with self.assertRaises(ValueError):
         Resource(long_string, {long_string: 'value'})
     with self.assertRaises(ValueError):
         Resource(long_string, {long_string: long_string})
     with self.assertRaises(ValueError):
         Resource('type', {'key1': 'value1', None: 'value2'})
     with self.assertRaises(ValueError):
         Resource('type', {'key1': 'value1', 'key2': None})
     # Empty (but not null) label values are ok
     Resource('type', {'key': ""})
예제 #8
0
    def test_merge_resource_no_type(self):
        r1 = Resource(None)
        r2 = Resource(None)

        merged = resource_module.merge_resources([r1, r2])
        self.assertEqual(merged.type, None)
예제 #9
0
 def test_merge_overwrite(self):
     r1 = Resource('t1', {'lk1': 'lv11'})
     r2 = Resource('t2', {'lk1': 'lv12', 'lk2': 'lv22'})
     self.assertEqual(r1.merge(r2).labels, {'lk1': 'lv11', 'lk2': 'lv22'})
     self.assertEqual(r2.merge(r1).labels, {'lk1': 'lv12', 'lk2': 'lv22'})
예제 #10
0
 def test_merge_self(self):
     resource = Resource('type', {'key': 'value'})
     merged = resource.merge(resource)
     self.assertEqual(merged.type, resource.type)
     self.assertDictEqual(merged.labels, resource.labels)