def test_instances_one(self): counter1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.counter"}, source="test", ) msg = rpc.meter_message_from_counter(counter1, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) counter2 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.counter2"}, source="test", ) msg2 = rpc.meter_message_from_counter(counter2, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg2) data = self.get_json("/resources/resource-id") self.assertEquals("resource-id", data["resource_id"])
def test_with_project(self): counter1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.counter"}, source="test_list_resources", ) msg = rpc.meter_message_from_counter(counter1, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) counter2 = sample.Sample( "instance", "cumulative", "", 1, "user-id2", "project-id2", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.counter2"}, source="not-test", ) msg2 = rpc.meter_message_from_counter(counter2, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg2) data = self.get_json("/resources", q=[{"field": "project_id", "value": "project-id"}]) ids = [r["resource_id"] for r in data] self.assertEquals(["resource-id"], ids)
def test_instances(self): sample1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.sample"}, source="test", ) msg = rpc.meter_message_from_counter(sample1, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) sample2 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.sample2"}, source="test", ) msg2 = rpc.meter_message_from_counter(sample2, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg2) data = self.get_json("/resources") self.assertEqual(2, len(data))
def prepare_data(self): c = counter.Counter( 'dummyBigCounter', counter.TYPE_CUMULATIVE, unit='', volume=3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter( 'dummySmallCounter', counter.TYPE_CUMULATIVE, unit='', volume=-3372036854775807, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, 'test-1', ) self.conn.record_metering_data(msg) c = counter.Counter( 'floatCounter', counter.TYPE_CUMULATIVE, unit='', volume=1938495037.53697, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={} ) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, 'test-1', ) self.conn.record_metering_data(msg)
def test_with_invalid_resource_id(self): sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-1', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample', }, source='test_list_resources', ) msg = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) sample2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample2', }, source='test_list_resources', ) msg2 = rpc.meter_message_from_counter( sample2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2) resp1 = self.get_json('/resources/resource-id-1') self.assertEqual(resp1["resource_id"], "resource-id-1") resp2 = self.get_json('/resources/resource-id-2') self.assertEqual(resp2["resource_id"], "resource-id-2") resp3 = self.get_json('/resources/resource-id-3', expect_errors=True) self.assertEqual(resp3.status_code, 404) self.assertEqual(json.loads(resp3.body)['error_message'] ['faultstring'], "Resource resource-id-3 Not Found")
def test_instances(self): timestamps = { 'resource-id': datetime.datetime(2012, 7, 2, 10, 40), 'resource-id-alternate': datetime.datetime(2012, 7, 2, 10, 41), } sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=timestamps['resource-id'], resource_metadata={'display_name': 'test-server', 'tag': 'self.sample', }, source='test', ) msg = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) sample2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=timestamps['resource-id-alternate'], resource_metadata={'display_name': 'test-server', 'tag': 'self.sample2', }, source='test', ) msg2 = rpc.meter_message_from_counter( sample2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2) data = self.get_json('/resources') self.assertEqual(2, len(data)) for res in data: timestamp = timestamps.get(res['resource_id']) self._verify_sample_timestamps(res, timestamp, timestamp)
def test_with_invalid_resource_id(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-1', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = rpc.meter_message_from_counter( counter2, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg2) resp1 = self.get_json('/resources/resource-id-1') self.assertEquals(resp1["resource_id"], "resource-id-1") resp2 = self.get_json('/resources/resource-id-2') self.assertEquals(resp2["resource_id"], "resource-id-2") resp3 = self.get_json('/resources/resource-id-3', expect_errors=True) self.assertEquals(resp3.status_code, 400)
def test_with_source(self): sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample', }, source='test_list_resources', ) msg = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) sample2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample2', }, source='not-test', ) msg2 = rpc.meter_message_from_counter( sample2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', q=[{'field': 'source', 'value': 'test_list_resources', }]) ids = [r['resource_id'] for r in data] self.assertEqual(['resource-id'], ids) sources = [r['source'] for r in data] self.assertEqual(['test_list_resources'], sources)
def test_instance_multiple_samples(self): timestamps = [ datetime.datetime(2012, 7, 2, 10, 41), datetime.datetime(2012, 7, 2, 10, 42), datetime.datetime(2012, 7, 2, 10, 40), ] for timestamp in timestamps: datapoint = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=timestamp, resource_metadata={'display_name': 'test-server', 'tag': 'self.sample-%s' % timestamp, }, source='test', ) msg = rpc.meter_message_from_counter( datapoint, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) data = self.get_json('/resources') self.assertEqual(1, len(data)) self._verify_sample_timestamps(data[0], timestamps[-1], timestamps[1])
def test_with_user_wrong_tenant(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter1', } ) msg2 = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', headers={"X-Roles": "Member", "X-Tenant-Id": "project-wrong"}) ids = set(r['resource_id'] for r in data) self.assertEquals(set(), ids)
def test_metadata(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': {'key': 'value'}, 'ignored_list': ['not-returned'], } ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'test', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') metadata = data[0]['metadata'] self.assertEqual( list(sorted(metadata.iteritems())), [('display_name', 'test-server'), ('tag', 'self.counter'), ])
def test_resource_meter_links(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) data = self.get_json('/resources') links = data[0]['links'] self.assertEqual(len(links), 2) self.assertEqual(links[0]['rel'], 'self') self.assertTrue((self.PATH_PREFIX + '/resources/resource-id') in links[0]['href']) self.assertEqual(links[1]['rel'], 'instance') self.assertTrue((self.PATH_PREFIX + '/meters/instance?' 'q.field=resource_id&q.value=resource-id') in links[1]['href'])
def setUp(self): super(TestAPIACL, self).setUp() self.environ = {'fake.cache': FakeMemcache()} for cnt in [ counter.Counter( 'meter.test', 'cumulative', '', 1, 'user-good', 'project-good', 'resource-good', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}), counter.Counter( 'meter.mine', 'gauge', '', 1, 'user-fred', 'project-good', 'resource-56', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter4'})]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret, 'test_source') self.conn.record_metering_data(msg)
def setUp(self): super(TestMaxProjectVolume, self).setUp() require_map_reduce(self.conn) self.counters = [] for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id-%s' % i, timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, 'source1', ) self.conn.record_metering_data(msg)
def test_resource_meter_links(self): counter1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.counter"}, source="test_list_resources", ) msg = rpc.meter_message_from_counter(counter1, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) data = self.get_json("/resources") links = data[0]["links"] self.assertEqual(len(links), 2) self.assertEqual(links[0]["rel"], "self") self.assertTrue((self.PATH_PREFIX + "/resources/resource-id") in links[0]["href"]) self.assertEqual(links[1]["rel"], "instance") self.assertTrue( (self.PATH_PREFIX + "/meters/instance?" "q.field=resource_id&q.value=resource-id") in links[1]["href"] )
def test_metadata(self): sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample', 'dict_properties': {'key': 'value'}, 'ignored_list': ['not-returned'], }, source='test', ) msg = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) data = self.get_json('/resources') metadata = data[0]['metadata'] self.assertEqual( [(u'dict_properties.key', u'value'), (u'display_name', u'test-server'), (u'tag', u'self.sample') ], list(sorted(metadata.iteritems())))
def setUp(self): super(TestListResourcesBase, self).setUp() for cnt in [ sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter'}, source='test_list_resources', ), sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2'}, source='test_list_resources', ), sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter3'}, source='test_list_resources', ), sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter4'}, source='test_list_resources', )]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg)
def test_metadata(self): sample1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={ "display_name": "test-server", "tag": "self.sample", "dict_properties": {"key": "value"}, "ignored_list": ["not-returned"], }, source="test", ) msg = rpc.meter_message_from_counter(sample1, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) data = self.get_json("/resources") metadata = data[0]["metadata"] self.assertEqual( [(u"dict_properties.key", u"value"), (u"display_name", u"test-server"), (u"tag", u"self.sample")], list(sorted(metadata.iteritems())), )
def setUp(self): super(TestSumResourceVolume, self).setUp() self.counters = [] for i in range(3): c = sample.Sample( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', }, source='source1', ) self.counters.append(c) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg)
def test_with_user_non_admin(self): sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample1', }, source='not-test', ) msg2 = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', headers={"X-Roles": "Member", "X-Project-Id": "project-id2"}) ids = set(r['resource_id'] for r in data) self.assertEqual(set(['resource-id-alternate']), ids)
def test_with_project(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = rpc.meter_message_from_counter( counter2, cfg.CONF.publisher_rpc.metering_secret, 'not-test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources', q=[{'field': 'project_id', 'value': 'project-id', }]) ids = [r['resource_id'] for r in data] self.assertEquals(['resource-id'], ids)
def test_meter_message_from_counter_field(self): def compare(f, c, msg_f, msg): self.assertEqual(msg, c) msg = rpc.meter_message_from_counter(self.TEST_COUNTER, "not-so-secret") name_map = {"name": "counter_name", "type": "counter_type", "unit": "counter_unit", "volume": "counter_volume"} for f in self.TEST_COUNTER._fields: msg_f = name_map.get(f, f) yield compare, f, getattr(self.TEST_COUNTER, f), msg_f, msg[msg_f]
def prepare_data(self): self.counters = [] for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = rpc.meter_message_from_counter( c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg) for i in range(3): c = counter.Counter( 'volume.size', 'gauge', 'GiB', 8 + i, 'user-5', 'project2', 'resource-6', timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = rpc.meter_message_from_counter( c, secret='not-so-secret', source='test', ) self.conn.record_metering_data(msg)
def test_instances(self): counter1 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', } ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, 'test', ) self.conn.record_metering_data(msg) counter2 = counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', } ) msg2 = rpc.meter_message_from_counter( counter2, cfg.CONF.publisher_rpc.metering_secret, 'test', ) self.conn.record_metering_data(msg2) data = self.get_json('/resources') self.assertEquals(2, len(data))
def setUp(self): super(TestListEvents, self).setUp() self.counter1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', 'ignored_dict': {'key': 'value'}, 'ignored_list': ['not-returned'], }, source='test_source', ) msg = rpc.meter_message_from_counter( self.counter1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) self.counter2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project2', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', }, source='source2', ) msg2 = rpc.meter_message_from_counter( self.counter2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2)
def test_instances_one(self): sample1 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample', }, source='test', ) msg = rpc.meter_message_from_counter( sample1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) sample2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.sample2', }, source='test', ) msg2 = rpc.meter_message_from_counter( sample2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2) data = self.get_json('/resources/resource-id') self.assertEqual('resource-id', data['resource_id'])
def setUp(self): super(TestListResourcesBase, self).setUp() for cnt in [ sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.sample"}, source="test_list_resources", ), sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id-alternate", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.sample2"}, source="test_list_resources", ), sample.Sample( "instance", "cumulative", "", 1, "user-id2", "project-id2", "resource-id2", timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={"display_name": "test-server", "tag": "self.sample3"}, source="test_list_resources", ), sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id", timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={"display_name": "test-server", "tag": "self.sample4"}, source="test_list_resources", ), ]: msg = rpc.meter_message_from_counter(cnt, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg)
def setUp(self): super(TestListUsers, self).setUp() counter1 = sample.Sample( 'instance', 'cumulative', 'instance', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter', }, source='test_list_users', ) msg = rpc.meter_message_from_counter( counter1, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) counter2 = sample.Sample( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={'display_name': 'test-server', 'tag': 'self.counter2', }, source='not-test', ) msg2 = rpc.meter_message_from_counter( counter2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg2)
def test_meter_message_from_counter_field(self): def compare(f, c, msg_f, msg): self.assertEqual(msg, c) msg = rpc.meter_message_from_counter(self.TEST_COUNTER, 'not-so-secret') name_map = {'name': 'counter_name', 'type': 'counter_type', 'unit': 'counter_unit', 'volume': 'counter_volume'} for f in self.TEST_COUNTER._fields: msg_f = name_map.get(f, f) yield compare, f, getattr(self.TEST_COUNTER, f), msg_f, msg[msg_f]
def test_with_invalid_resource_id(self): sample1 = sample.Sample( "instance", "cumulative", "", 1, "user-id", "project-id", "resource-id-1", timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={"display_name": "test-server", "tag": "self.sample"}, source="test_list_resources", ) msg = rpc.meter_message_from_counter(sample1, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg) sample2 = sample.Sample( "instance", "cumulative", "", 1, "user-id2", "project-id", "resource-id-2", timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={"display_name": "test-server", "tag": "self.sample2"}, source="test_list_resources", ) msg2 = rpc.meter_message_from_counter(sample2, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg2) resp1 = self.get_json("/resources/resource-id-1") self.assertEqual(resp1["resource_id"], "resource-id-1") resp2 = self.get_json("/resources/resource-id-2") self.assertEqual(resp2["resource_id"], "resource-id-2") resp3 = self.get_json("/resources/resource-id-3", expect_errors=True) self.assertEqual(resp3.status_code, 404) self.assertEqual(json.loads(resp3.body)["error_message"]["faultstring"], "Resource resource-id-3 Not Found")
def setUp(self): super(TestMaxProjectVolume, self).setUp() for i in range(3): s = sample.Sample( 'volume.size', 'gauge', 'GiB', 5 + i, 'user-id', 'project1', 'resource-id-%s' % i, timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.sample', }, source='source1', ) msg = rpc.meter_message_from_counter( s, self.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg)
def setUp(self): super(TestListResourcesBase, self).setUp() for cnt in [ counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter' }, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id-alternate', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2' }, ), counter.Counter( 'instance', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3' }, ), counter.Counter('instance', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 43), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter4' }) ]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources') self.conn.record_metering_data(msg)
def setUp(self): super(TestListMeters, self).setUp() for cnt in [ sample.Sample('meter.test', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter' }, source='test_source'), sample.Sample('meter.test', 'cumulative', '', 3, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 11, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter1' }, source='test_source'), sample.Sample('meter.mine', 'gauge', '', 1, 'user-id', 'project-id', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2' }, source='test_source'), sample.Sample('meter.test', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id3', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3' }, source='test_source'), sample.Sample('meter.mine', 'gauge', '', 1, 'user-id4', 'project-id2', 'resource-id4', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter4', 'properties': { 'prop_1': 'prop_value', 'prop_2': { 'sub_prop_1': 'sub_prop_value' } } }, source='test_source') ]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg)
def setUp(self): super(TestListMeters, self).setUp() for cnt in [ sample.Sample('meter.test', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.sample', 'size': 123, 'util': 0.75, 'is_public': True }, source='test_source'), sample.Sample('meter.test', 'cumulative', '', 3, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime(2012, 7, 2, 11, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.sample1', 'size': 0, 'util': 0.47, 'is_public': False }, source='test_source'), sample.Sample('meter.mine', 'gauge', '', 1, 'user-id', 'project-id', 'resource-id2', timestamp=datetime.datetime(2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.sample2', 'size': 456, 'util': 0.64, 'is_public': False }, source='test_source'), sample.Sample('meter.test', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id3', timestamp=datetime.datetime(2012, 7, 2, 10, 42), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.sample3', 'size': 0, 'util': 0.75, 'is_public': False }, source='test_source'), sample.Sample('meter.mine', 'gauge', '', 1, 'user-id4', 'project-id2', 'resource-id4', timestamp=datetime.datetime(2012, 7, 2, 10, 43), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.sample4', 'properties': { 'prop_1': 'prop_value', 'prop_2': { 'sub_prop_1': 'sub_prop_value' } }, 'size': 0, 'util': 0.58, 'is_public': True }, source='test_source1') ]: msg = rpc.meter_message_from_counter( cnt, self.CONF.publisher_rpc.metering_secret) self.conn.record_metering_data(msg)
def prepare_data(self): self.msgs = [] self.counter = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }) self.msg1 = rpc.meter_message_from_counter( self.counter, cfg.CONF.publisher_rpc.metering_secret, 'test-1', ) self.conn.record_metering_data(self.msg1) self.msgs.append(self.msg1) self.counter2 = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }) self.msg2 = rpc.meter_message_from_counter( self.counter2, cfg.CONF.publisher_rpc.metering_secret, 'test-2', ) self.conn.record_metering_data(self.msg2) self.msgs.append(self.msg2) self.counter3 = counter.Counter('instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-alternate', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3', }) self.msg3 = rpc.meter_message_from_counter( self.counter3, cfg.CONF.publisher_rpc.metering_secret, 'test-3', ) self.conn.record_metering_data(self.msg3) self.msgs.append(self.msg3) for i in range(2, 4): c = counter.Counter( 'instance', counter.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-%s' % i, project_id='project-id-%s' % i, resource_id='resource-id-%s' % i, timestamp=datetime.datetime(2012, 7, 2, 10, 40 + i), resource_metadata={ 'display_name': 'test-server', 'tag': 'counter-%s' % i }, ) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, 'test', ) self.conn.record_metering_data(msg) self.msgs.append(msg)
def prepare_data(self): original_timestamps = [(2012, 7, 2, 10, 40), (2012, 7, 2, 10, 41), (2012, 7, 2, 10, 41), (2012, 7, 2, 10, 42), (2012, 7, 2, 10, 43)] timestamps_for_test_samples_default_order = [(2012, 7, 2, 10, 44), (2011, 5, 30, 18, 3), (2012, 12, 1, 1, 25), (2012, 2, 29, 6, 59), (2013, 5, 31, 23, 7)] timestamp_list = (original_timestamps + timestamps_for_test_samples_default_order) self.msgs = [] self.counter = sample.Sample( 'instance', sample.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id', timestamp=datetime.datetime(*timestamp_list[0]), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter', }, source='test-1', ) self.msg1 = rpc.meter_message_from_counter( self.counter, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(self.msg1) self.msgs.append(self.msg1) self.counter2 = sample.Sample( 'instance', sample.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime(*timestamp_list[1]), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter2', }, source='test-2', ) self.msg2 = rpc.meter_message_from_counter( self.counter2, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(self.msg2) self.msgs.append(self.msg2) self.counter3 = sample.Sample( 'instance', sample.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-alternate', project_id='project-id', resource_id='resource-id-alternate', timestamp=datetime.datetime(*timestamp_list[2]), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter3', }, source='test-3', ) self.msg3 = rpc.meter_message_from_counter( self.counter3, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(self.msg3) self.msgs.append(self.msg3) start_idx = 3 end_idx = len(timestamp_list) for i, ts in zip(range(start_idx - 1, end_idx - 1), timestamp_list[start_idx:end_idx]): c = sample.Sample( 'instance', sample.TYPE_CUMULATIVE, unit='', volume=1, user_id='user-id-%s' % i, project_id='project-id-%s' % i, resource_id='resource-id-%s' % i, timestamp=datetime.datetime(*ts), resource_metadata={ 'display_name': 'test-server', 'tag': 'counter-%s' % i }, source='test', ) msg = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret, ) self.conn.record_metering_data(msg) self.msgs.append(msg)
def setUp(self): super(TestListMeters, self).setUp() for cnt in [ counter.Counter('meter.test', 'cumulative', '', 1, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 10, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter' }), counter.Counter('meter.test', 'cumulative', '', 3, 'user-id', 'project-id', 'resource-id', timestamp=datetime.datetime( 2012, 7, 2, 11, 40), resource_metadata={ 'display_name': 'test-server', 'tag': 'self.counter' }), counter.Counter('meter.mine', 'gauge', '', 1, 'user-id', 'project-id', 'resource-id2', timestamp=datetime.datetime( 2012, 7, 2, 10, 41), resource_metadata={ 'display_name': 'test-server', 'tag': 'two.counter' }), counter.Counter('meter.test', 'cumulative', '', 1, 'user-id2', 'project-id2', 'resource-id3', timestamp=datetime.datetime( 2012, 7, 2, 10, 42), resource_metadata={ 'display_name': 'test-server', 'tag': 'three.counter' }), counter.Counter('meter.mine', 'gauge', '', 1, 'user-id4', 'project-id2', 'resource-id4', timestamp=datetime.datetime( 2012, 7, 2, 10, 43), resource_metadata={ 'display_name': 'test-server', 'tag': 'four.counter' }) ]: msg = rpc.meter_message_from_counter( cnt, cfg.CONF.publisher_rpc.metering_secret, 'test_list_resources') self.conn.record_metering_data(msg)
def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, { '$set': { 'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': { 'meter': { 'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) return # Stubout with the old version DB schema, the one w/o 'counter_unit' self.stubs.Set(self.conn, 'record_metering_data', old_record_metering_data) self.counters = [] c = sample.Sample( 'volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }, source='test', ) self.counters.append(c) msg = rpc.meter_message_from_counter(c, secret='not-so-secret') self.conn.record_metering_data(self.conn, msg) # Create the old format alarm with a dict instead of a # array for matching_metadata alarm = models.Alarm('0ld-4l3rt', 'old-alert', 'test.one', 'eq', 36, 'count', 'me', 'and-da-boys', evaluation_periods=1, period=60, alarm_actions=['http://nowhere/alarms'], matching_metadata={'key': 'value'}) alarm.alarm_id = str(uuid.uuid1()) data = alarm.as_dict() self.conn.db.alarm.update({'alarm_id': alarm.alarm_id}, {'$set': data}, upsert=True)
def test_meter_message_from_counter_signed(self): msg = rpc.meter_message_from_counter(self.TEST_COUNTER, 'not-so-secret') self.assertIn('message_signature', msg)
def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, {'$addToSet': {'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, {'$addToSet': {'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, {'$set': {'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': {'meter': {'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) return # Stubout with the old version DB schema, the one w/o 'counter_unit' self.stubs.Set(self.conn, 'record_metering_data', old_record_metering_data) self.counters = [] c = counter.Counter( 'volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={'display_name': 'test-volume', 'tag': 'self.counter', } ) self.counters.append(c) msg = rpc.meter_message_from_counter( c, secret='not-so-secret', source='test') self.conn.record_metering_data(self.conn, msg)
def test_meter_message_from_counter_signed(): msg = rpc.meter_message_from_counter(TEST_COUNTER, 'not-so-secret', 'src') assert 'message_signature' in msg
def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, { '$set': { 'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': { 'meter': { 'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) return # Stubout with the old version DB schema, the one w/o 'counter_unit' self.stubs.Set(self.conn, 'record_metering_data', old_record_metering_data) self.counters = [] c = sample.Sample( 'volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }, source='test', ) self.counters.append(c) msg = rpc.meter_message_from_counter(c, secret='not-so-secret') self.conn.record_metering_data(self.conn, msg) # Create the old format alarm with a dict instead of a # array for matching_metadata alarm = dict(alarm_id='0ld-4l3rt', enabled=True, name='old-alert', description='old-alert', timestamp=None, meter_name='cpu', user_id='me', project_id='and-da-boys', comparison_operator='lt', threshold=36, statistic='count', evaluation_periods=1, period=60, state="insufficient data", state_timestamp=None, ok_actions=[], alarm_actions=['http://nowhere/alarms'], insufficient_data_actions=[], repeat_actions=False, matching_metadata={'key': 'value'}) self.conn.db.alarm.update({'alarm_id': alarm['alarm_id']}, {'$set': alarm}, upsert=True) alarm['alarm_id'] = 'other-kind-of-0ld-4l3rt' alarm['name'] = 'other-old-alaert' alarm['matching_metadata'] = [{ 'key': 'key1', 'value': 'value1' }, { 'key': 'key2', 'value': 'value2' }] self.conn.db.alarm.update({'alarm_id': alarm['alarm_id']}, {'$set': alarm}, upsert=True)
def main(): cfg.CONF([], project='ceilometer') parser = argparse.ArgumentParser(description='generate metering data', ) parser.add_argument( '--interval', default=10, type=int, help='the period between events, in minutes', ) parser.add_argument( '--start', default=31, help='the number of days in the past to start timestamps', ) parser.add_argument( '--end', default=2, help='the number of days into the future to continue timestamps', ) parser.add_argument( '--type', choices=('gauge', 'cumulative'), default='gauge', help='counter type', ) parser.add_argument( '--unit', default=None, help='counter unit', ) parser.add_argument( '--project', help='project id of owner', ) parser.add_argument( '--user', help='user id of owner', ) parser.add_argument( 'resource', help='the resource id for the meter data', ) parser.add_argument( 'counter', help='the counter name for the meter data', ) parser.add_argument( 'volume', help='the amount to attach to the meter', type=int, default=1, ) args = parser.parse_args() # Set up logging to use the console console = logging.StreamHandler(sys.stderr) console.setLevel(logging.DEBUG) formatter = logging.Formatter('%(message)s') console.setFormatter(formatter) root_logger = logging.getLogger('') root_logger.addHandler(console) root_logger.setLevel(logging.DEBUG) # Connect to the metering database conn = storage.get_connection(cfg.CONF) # Find the user and/or project for a real resource if not (args.user or args.project): for r in conn.get_resources(): if r['resource_id'] == args.resource: args.user = r['user_id'] args.project = r['project_id'] break # Compute start and end timestamps for the # new data. timestamp = timeutils.parse_isotime(args.start) end = timeutils.parse_isotime(args.end) increment = datetime.timedelta(minutes=args.interval) # Generate events n = 0 while timestamp <= end: c = sample.Sample( name=args.counter, type=args.type, unit=args.unit, volume=args.volume, user_id=args.user, project_id=args.project, resource_id=args.resource, timestamp=timestamp, resource_metadata={}, source='artificial', ) data = rpc.meter_message_from_counter( c, cfg.CONF.publisher_rpc.metering_secret) conn.record_metering_data(data) n += 1 timestamp = timestamp + increment print 'Added %d new events' % n return 0