def test_allocate_tenant_segment(self):
     for __ in range(VLAN_MIN, VLAN_MAX + 1):
         segment = self.driver.allocate_tenant_segment(self.context)
         alloc = self._get_allocation(self.context, segment)
         self.assertTrue(alloc.allocated)
         vlan_id = segment[api.SEGMENTATION_ID]
         self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
         self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
         self.assertEqual(TENANT_NET, segment[api.PHYSICAL_NETWORK])
 def test_reserve_provider_segment_without_physical_network(self):
     segment = {api.NETWORK_TYPE: p_const.TYPE_VLAN}
     observed = self.driver.reserve_provider_segment(self.context, segment)
     alloc = self._get_allocation(self.context, observed)
     self.assertTrue(alloc.allocated)
     vlan_id = observed[api.SEGMENTATION_ID]
     self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
     self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
     self.assertEqual(TENANT_NET, observed[api.PHYSICAL_NETWORK])
Exemple #3
0
 def assertGreaterEqual(self, first, second, msg=None):
     """Python < v2.7 compatibility.  Assert 'first' >= 'second'."""
     try:
         f = super(TestCase, self).assertGreaterEqual
     except AttributeError:
         self.assertThat(first,
                         matchers.Not(matchers.LessThan(second)),
                         message=msg or '')
     else:
         f(first, second, msg=msg)
Exemple #4
0
    def test_parallel_time_less_than_serial(self):
        @concurrent
        def wait(secs):
            time.sleep(secs)

        before = time.time()
        # ten threads to make it as fast as possible
        wait(concurrent=[{"secs": 1} for _ in range(10)], n_workers=10)
        after = time.time()
        self.assertThat(after - before, matchers.LessThan(5))
Exemple #5
0
    def test_tunnel_pool(self):
        tunnel_ids = set()
        for x in xrange(TUN_MIN, TUN_MAX + 1):
            tunnel_id = ovs_db_v2.reserve_tunnel(self.session)
            self.assertThat(tunnel_id, matchers.GreaterThan(TUN_MIN - 1))
            self.assertThat(tunnel_id, matchers.LessThan(TUN_MAX + 1))
            tunnel_ids.add(tunnel_id)

        with testtools.ExpectedException(n_exc.NoNetworkAvailable):
            tunnel_id = ovs_db_v2.reserve_tunnel(self.session)

        ovs_db_v2.release_tunnel(self.session, tunnel_ids.pop(), TUNNEL_RANGES)
        tunnel_id = ovs_db_v2.reserve_tunnel(self.session)
        self.assertThat(tunnel_id, matchers.GreaterThan(TUN_MIN - 1))
        self.assertThat(tunnel_id, matchers.LessThan(TUN_MAX + 1))
        tunnel_ids.add(tunnel_id)

        for tunnel_id in tunnel_ids:
            ovs_db_v2.release_tunnel(self.session, tunnel_id, TUNNEL_RANGES)
Exemple #6
0
 def _test_list_with_pagination_reverse(self,
                                        resource,
                                        items,
                                        sort,
                                        limit,
                                        expected_page_num,
                                        resources=None,
                                        query_params='',
                                        id=None,
                                        subresource=None,
                                        subresources=None):
     if not resources:
         resources = '%ss' % resource
     if subresource and not subresources:
         subresources = '%ss' % subresource
     resource = resource.replace('-', '_')
     api = self._api_for_resource(resources)
     if subresource:
         marker = items[-1][subresource]['id']
     else:
         marker = items[-1][resource]['id']
     query_str = query_params + '&' if query_params else ''
     query_str = query_str + ("limit=%s&page_reverse=True&"
                              "sort_key=%s&sort_dir=%s&"
                              "marker=%s") % (limit, sort[0], sort[1],
                                              marker)
     req = self.new_list_request(resources,
                                 params=query_str,
                                 id=id,
                                 subresource=subresources)
     if subresource:
         resource = subresource
     if subresources:
         resources = subresources
     item_res = [items[-1][resource]]
     page_num = 0
     resources = resources.replace('-', '_')
     while req:
         page_num = page_num + 1
         res = self.deserialize(self.fmt, req.get_response(api))
         self.assertThat(len(res[resources]), matchers.LessThan(limit + 1))
         res[resources].reverse()
         item_res = item_res + res[resources]
         req = None
         if '%s_links' % resources in res:
             for link in res['%s_links' % resources]:
                 if link['rel'] == 'previous':
                     content_type = 'application/%s' % self.fmt
                     req = testlib_api.create_request(
                         link['href'], '', content_type)
                     self.assertEqual(len(res[resources]), limit)
     self.assertEqual(expected_page_num, page_num)
     expected_res = [item[resource]['id'] for item in items]
     expected_res.reverse()
     self.assertEqual(expected_res, [n['id'] for n in item_res])
Exemple #7
0
    def test_create_l3_segments(self):
        rids = set()
        for x in moves.range(VXLAN_MIN, VXLAN_MAX + 1):
            rid = str(uuid.uuid1())
            segment = self.driver.create_l3_segments(self.context, rid)
            self.assertThat(segment, matchers.GreaterThan(VXLAN_MIN - 1))
            self.assertThat(segment, matchers.LessThan(VXLAN_MAX + 1))
            rids.add(rid)
        self.assertRaises(h_exc.NoTunnelIdAvailable,
                          self.driver.create_l3_segments, self.context,
                          str(uuid.uuid1()))

        rid = rids.pop()
        self.driver.release_segment(self.context.session, rid)
        segment = self.driver.create_l3_segments(self.context, rid)
        self.assertThat(segment, matchers.GreaterThan(VXLAN_MIN - 1))
        self.assertThat(segment, matchers.LessThan(VXLAN_MAX + 1))
        rids.add(rid)
        for rid in rids:
            self.driver.release_segment(self.context.session, rid)
Exemple #8
0
 def test_reserve_provider_segment_without_segmentation_id(self):
     segment = {
         api.NETWORK_TYPE: p_const.TYPE_VLAN,
         api.PHYSICAL_NETWORK: TENANT_NET
     }
     observed = self.driver.reserve_provider_segment(self.session, segment)
     alloc = self._get_allocation(self.session, observed)
     self.assertTrue(alloc.allocated)
     vlan_id = observed[api.SEGMENTATION_ID]
     self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
     self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
    def test_set_time_override_using_default(self):
        now = timeutils.utcnow_ts()

        # NOTE(kgriffs): Normally it's bad form to sleep in a unit test,
        # but this is the only way to test that set_time_override defaults
        # to setting the override to the current time.
        time.sleep(1)

        timeutils.set_time_override()
        overriden_now = timeutils.utcnow_ts()
        self.assertThat(now, matchers.LessThan(overriden_now))
    def test_periodic_tasks_idle_calculation(self):
        class Manager(manager.Manager):
            @manager.periodic_task(spacing=10)
            def bar(self):
                return 'bar'

        m = Manager()
        m.periodic_tasks(None)
        time.sleep(0.1)
        idle = m.periodic_tasks(None)
        self.assertThat(idle, matchers.GreaterThan(9.7))
        self.assertThat(idle, matchers.LessThan(9.9))
Exemple #11
0
    def test_sync_with_allocated_false(self):
        vlan_ids = set()
        for x in xrange(VLAN_MIN, VLAN_MAX + 1):
            physical_network, vlan_id = ovs_db_v2.reserve_vlan(self.session)
            self.assertEqual(physical_network, PHYS_NET)
            self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
            self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
            vlan_ids.add(vlan_id)

        ovs_db_v2.release_vlan(self.session, PHYS_NET, vlan_ids.pop(),
                               VLAN_RANGES)
        ovs_db_v2.sync_vlan_allocations({})
Exemple #12
0
    def test_vxlan_pool(self):
        vxlan_ids = set()
        for x in moves.xrange(VXLAN_MIN, VXLAN_MAX + 1):
            vxlan = n1kv_db_v2.reserve_vxlan(self.session, self.net_p)
            vxlan_id = vxlan[2]
            self.assertThat(vxlan_id, matchers.GreaterThan(VXLAN_MIN - 1))
            self.assertThat(vxlan_id, matchers.LessThan(VXLAN_MAX + 1))
            vxlan_ids.add(vxlan_id)

        self.assertRaises(n_exc.NoNetworkAvailable, n1kv_db_v2.reserve_vxlan,
                          self.session, self.net_p)
        n1kv_db_v2.release_vxlan(self.session, vxlan_ids.pop())
        vxlan = n1kv_db_v2.reserve_vxlan(self.session, self.net_p)
        vxlan_id = vxlan[2]
        self.assertThat(vxlan_id, matchers.GreaterThan(VXLAN_MIN - 1))
        self.assertThat(vxlan_id, matchers.LessThan(VXLAN_MAX + 1))
        vxlan_ids.add(vxlan_id)

        for vxlan_id in vxlan_ids:
            n1kv_db_v2.release_vxlan(self.session, vxlan_id)
        n1kv_db_v2.delete_network_profile(self.session, self.net_p.id)
 def test_server_in_group(self):
     group = self.useFixture(RpcServerGroupFixture(self.url))
     client = group.client(cast=True)
     for i in range(20):
         client.add(increment=1)
     group.sync()
     total = 0
     for s in group.servers:
         ival = s.endpoint.ival
         self.assertThat(ival, matchers.GreaterThan(0))
         self.assertThat(ival, matchers.LessThan(20))
         total += ival
     self.assertEqual(20, total)
Exemple #14
0
    def test_network_pool(self):
        vlan_ids = set()
        for x in moves.xrange(VLAN_MIN, VLAN_MAX + 1):
            physical_network, vlan_id = lb_db.reserve_network(self.session)
            self.assertEqual(physical_network, PHYS_NET)
            self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
            self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
            vlan_ids.add(vlan_id)

        with testtools.ExpectedException(n_exc.NoNetworkAvailable):
            physical_network, vlan_id = lb_db.reserve_network(self.session)

        for vlan_id in vlan_ids:
            lb_db.release_network(self.session, PHYS_NET, vlan_id, VLAN_RANGES)
Exemple #15
0
    def test_vlan_pool(self):
        vlan_ids = set()
        for x in moves.xrange(VLAN_MIN, VLAN_MAX + 1):
            (physical_network, seg_type, vlan_id,
             m_ip) = n1kv_db_v2.reserve_vlan(self.session, self.net_p)
            self.assertEqual(physical_network, PHYS_NET)
            self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
            self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
            vlan_ids.add(vlan_id)

        self.assertRaises(n_exc.NoNetworkAvailable, n1kv_db_v2.reserve_vlan,
                          self.session, self.net_p)

        n1kv_db_v2.release_vlan(self.session, PHYS_NET, vlan_ids.pop())
        physical_network, seg_type, vlan_id, m_ip = (n1kv_db_v2.reserve_vlan(
            self.session, self.net_p))
        self.assertEqual(physical_network, PHYS_NET)
        self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
        self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
        vlan_ids.add(vlan_id)

        for vlan_id in vlan_ids:
            n1kv_db_v2.release_vlan(self.session, PHYS_NET, vlan_id)
Exemple #16
0
    def test_vxlan_pool(self):
        vxlan_ids = set()
        profile = n1kv_db_v2.create_network_profile(
            self.session, TEST_NETWORK_PROFILE_VXLAN)
        for x in xrange(VXLAN_MIN, VXLAN_MAX + 1):
            vxlan = n1kv_db_v2.reserve_vxlan(self.session, profile)
            vxlan_id = vxlan[2]
            self.assertThat(vxlan_id, matchers.GreaterThan(VXLAN_MIN - 1))
            self.assertThat(vxlan_id, matchers.LessThan(VXLAN_MAX + 1))
            vxlan_ids.add(vxlan_id)

        self.assertRaises(q_exc.NoNetworkAvailable, n1kv_db_v2.reserve_vxlan,
                          self.session, profile)
        n1kv_db_v2.release_vxlan(self.session, vxlan_ids.pop(), VXLAN_RANGES)
        vxlan = n1kv_db_v2.reserve_vxlan(self.session, profile)
        vxlan_id = vxlan[2]
        self.assertThat(vxlan_id, matchers.GreaterThan(VXLAN_MIN - 1))
        self.assertThat(vxlan_id, matchers.LessThan(VXLAN_MAX + 1))
        vxlan_ids.add(vxlan_id)

        for vxlan_id in vxlan_ids:
            n1kv_db_v2.release_vxlan(self.session, vxlan_id, VXLAN_RANGES)
        n1kv_db_v2.delete_network_profile(self.session, profile.id)
Exemple #17
0
    def test_segmentationId_pool(self):
        vlan_ids = set()
        for x in xrange(VLAN_MIN, VLAN_MAX + 1):
            physical_network, vlan_id = mlnx_db.reserve_network(self.session)
            self.assertEqual(physical_network, PHYS_NET)
            self.assertThat(vlan_id, matchers.GreaterThan(VLAN_MIN - 1))
            self.assertThat(vlan_id, matchers.LessThan(VLAN_MAX + 1))
            vlan_ids.add(vlan_id)

        self.assertRaises(n_exc.NoNetworkAvailable, mlnx_db.reserve_network,
                          self.session)
        for vlan_id in vlan_ids:
            mlnx_db.release_network(self.session, PHYS_NET, vlan_id,
                                    VLAN_RANGES)
Exemple #18
0
    def test_datetime_math(self):
        self.context['dt1'] = self.eval('now()')
        time.sleep(0.1)
        self.context['dt2'] = self.eval('now()')
        delta = TS(milliseconds=120)
        self.assertIsInstance(self.eval('$dt2 - $dt1'), TS)
        self.assertThat(self.eval('$dt2 - $dt1'), matchers.LessThan(delta))
        self.assertTrue(self.eval('($dt2 - $dt1) + $dt1 = $dt2'))
        self.assertTrue(self.eval('$dt1 + ($dt2 - $dt1) = $dt2'))
        self.assertThat(self.eval('($dt2 - $dt1) * 2'),
                        matchers.LessThan(2 * delta))
        self.assertThat(self.eval('2.1 * ($dt2 - $dt1)'),
                        matchers.LessThan(2 * delta))
        self.assertTrue(self.eval('-($dt1 - $dt2) = +($dt2 - $dt1)'))
        self.assertTrue(self.eval('$dt2 > $dt1'))
        self.assertTrue(self.eval('$dt2 >= $dt1'))
        self.assertTrue(self.eval('$dt2 != $dt1'))
        self.assertTrue(self.eval('$dt1 = $dt1'))
        self.assertTrue(self.eval('$dt1 < $dt2'))
        self.assertTrue(self.eval('$dt1 <= $dt2'))
        self.assertEqual(-1, self.eval('($dt2 - $dt1) / ($dt1 - $dt2)'))
        self.assertTrue(self.eval('$dt2 - ($dt2 - $dt1) = $dt1'))
        self.assertEqual(
            0,
            self.eval('($dt2 - $dt1) - ($dt2 - $dt1)').total_seconds())

        delta2 = self.eval('($dt2 - $dt1) / 2.1')
        self.assertThat(delta2, matchers.LessThan(delta / 2))
        self.assertTrue(self.eval('$dt1 + $ < $dt2', delta2))
        self.assertTrue(self.eval('$ + $dt1 < $dt2', delta2))
        self.assertTrue(self.eval('$dt2 - $dt1 > $', delta2))
        self.assertTrue(self.eval('$dt2 - $dt1 >= $', delta2))
        self.assertTrue(self.eval('$dt2 - $dt1 != $', delta2))
        self.assertFalse(self.eval('$dt2 - $dt1 < $', delta2))
        self.assertFalse(self.eval('$dt2 - $dt1 <= $', delta2))
        self.assertTrue(self.eval('($dt2 - $dt1) + $ > $', delta2))
    def test_reserve_provider_segment(self):
        tunnel_ids = set()
        specs = {
            api.NETWORK_TYPE: self.TYPE,
            api.PHYSICAL_NETWORK: 'None',
            api.SEGMENTATION_ID: None
        }

        for x in moves.range(TUN_MIN, TUN_MAX + 1):
            segment = self.driver.reserve_provider_segment(self.context, specs)
            self.assertEqual(self.TYPE, segment[api.NETWORK_TYPE])
            self.assertThat(segment[api.SEGMENTATION_ID],
                            matchers.GreaterThan(TUN_MIN - 1))
            self.assertThat(segment[api.SEGMENTATION_ID],
                            matchers.LessThan(TUN_MAX + 1))
            tunnel_ids.add(segment[api.SEGMENTATION_ID])

        with testtools.ExpectedException(exc.NoNetworkAvailable):
            segment = self.driver.reserve_provider_segment(self.context, specs)

        segment = {
            api.NETWORK_TYPE: self.TYPE,
            api.PHYSICAL_NETWORK: 'None',
            api.SEGMENTATION_ID: tunnel_ids.pop()
        }
        self.driver.release_segment(self.context, segment)
        segment = self.driver.reserve_provider_segment(self.context, specs)
        self.assertThat(segment[api.SEGMENTATION_ID],
                        matchers.GreaterThan(TUN_MIN - 1))
        self.assertThat(segment[api.SEGMENTATION_ID],
                        matchers.LessThan(TUN_MAX + 1))
        tunnel_ids.add(segment[api.SEGMENTATION_ID])

        for tunnel_id in tunnel_ids:
            segment[api.SEGMENTATION_ID] = tunnel_id
            self.driver.release_segment(self.context, segment)
Exemple #20
0
 def test_reserve_provider_segment_without_physical_network(self):
     with self.pnet(self._pnet1) as pnet:
         pnet_data = pnet['providernet']
         with self.pnet_range(pnet_data, self._pnet_range1) as pnet_range:
             data = pnet_range['providernet_range']
             segment = {api.NETWORK_TYPE: pnet_data['type']}
             observed = self.driver.reserve_provider_segment(
                 self.context, segment, tenant_id=self._tenant_id)
             alloc = self._get_allocation(self.context, observed)
             self.assertTrue(alloc.allocated)
             vlan_id = observed[api.SEGMENTATION_ID]
             self.assertEqual(alloc.physical_network, pnet_data['name'])
             self.assertThat(vlan_id,
                             matchers.GreaterThan(data['minimum'] - 1))
             self.assertThat(vlan_id,
                             matchers.LessThan(data['maximum'] + 1))
             self.driver.release_segment(self.context, observed)
 def test_server_in_group(self):
     if self.url.startswith("amqp:"):
         self.skipTest("QPID-6307")
     group = self.useFixture(utils.RpcServerGroupFixture(self.url))
     client = group.client(cast=True)
     for i in range(20):
         client.add(increment=1)
     for i in range(len(group.servers)):
         # expect each server to get a sync
         client.sync()
     group.sync(server="all")
     total = 0
     for s in group.servers:
         ival = s.endpoint.ival
         self.assertThat(ival, matchers.GreaterThan(0))
         self.assertThat(ival, matchers.LessThan(20))
         total += ival
     self.assertEqual(20, total)
Exemple #22
0
    def test_handle_router_snat_rules_add_rules(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        ri = l3_agent.RouterInfo(_uuid(), self.conf.root_helper,
                                 self.conf.use_namespaces, None)
        ex_gw_port = {'fixed_ips': [{'ip_address': '192.168.1.4'}]}
        internal_cidrs = ['10.0.0.0/24']
        agent._handle_router_snat_rules(ri, ex_gw_port, internal_cidrs,
                                        "iface", "add_rules")

        nat_rules = map(str, ri.iptables_manager.ipv4['nat'].rules)
        wrap_name = ri.iptables_manager.wrap_name

        jump_float_rule = "-A %s-snat -j %s-float-snat" % (wrap_name,
                                                           wrap_name)
        internal_net_rule = ("-A %s-snat -s %s -j SNAT --to-source %s") % (
            wrap_name, internal_cidrs[0],
            ex_gw_port['fixed_ips'][0]['ip_address'])

        self.assertIn(jump_float_rule, nat_rules)

        self.assertIn(internal_net_rule, nat_rules)
        self.assertThat(nat_rules.index(jump_float_rule),
                        matchers.LessThan(nat_rules.index(internal_net_rule)))
Exemple #23
0
    def test_calculate_backoff(self):
        sec = utils.calculate_backoff(0, 10, 2, 0)
        self.assertEqual(0, sec)

        sec = utils.calculate_backoff(9, 10, 2, 0)
        self.assertEqual(1.8, sec)

        sec = utils.calculate_backoff(4, 10, 2, 0)
        self.assertEqual(0.8, sec)

        sec = utils.calculate_backoff(4, 10, 2, 1)
        if sec != 0.8:
            self.assertThat(sec, matchers.GreaterThan(0.8))
            self.assertThat(sec, matchers.LessThan(1.8))

        self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, -2, -1)
        self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, -2, 0)
        self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, 2, -1)

        self.assertRaises(ValueError, utils.calculate_backoff, -2, 10, 2, 0)
        self.assertRaises(ValueError, utils.calculate_backoff, -1, 10, 2, 0)
        self.assertRaises(ValueError, utils.calculate_backoff, 10, 10, 2, 0)
        self.assertRaises(ValueError, utils.calculate_backoff, 11, 10, 2, 0)
Exemple #24
0
    def test_queue_lifecycle(self):
        # Test queue creation
        created = self.controller.create('test',
                                         metadata=dict(meta='test_meta'),
                                         project=self.project)
        self.assertTrue(created)

        # Test queue existence
        self.assertTrue(self.controller.exists('test', project=self.project))

        # Test queue retrieval
        interaction = self.controller.list(project=self.project)
        queue = list(next(interaction))[0]
        self.assertEqual(queue['name'], 'test')

        # Test queue metadata retrieval
        metadata = self.controller.get('test', project=self.project)
        self.assertEqual(metadata['meta'], 'test_meta')

        # Touching an existing queue does not affect metadata
        created = self.controller.create('test', project=self.project)
        self.assertFalse(created)

        metadata = self.controller.get('test', project=self.project)
        self.assertEqual(metadata['meta'], 'test_meta')

        client_uuid = uuid.uuid4()

        # Test queue statistic
        _insert_fixtures(self.message_controller,
                         'test',
                         project=self.project,
                         client_uuid=client_uuid,
                         num=6)

        # NOTE(kgriffs): We can't get around doing this, because
        # we don't know how the storage drive may be calculating
        # message timestamps (and may not be monkey-patchable).
        time.sleep(1.2)

        _insert_fixtures(self.message_controller,
                         'test',
                         project=self.project,
                         client_uuid=client_uuid,
                         num=6)

        stats = self.controller.stats('test', project=self.project)
        message_stats = stats['messages']

        self.assertEqual(message_stats['free'], 12)
        self.assertEqual(message_stats['claimed'], 0)
        self.assertEqual(message_stats['total'], 12)

        oldest = message_stats['oldest']
        newest = message_stats['newest']

        self.assertNotEqual(oldest, newest)

        age = oldest['age']
        self.assertThat(age, matchers.GreaterThan(0))

        # NOTE(kgriffs): Ensure is different enough
        # for the next comparison to work.
        soon = timeutils.utcnow() + datetime.timedelta(seconds=60)

        for message_stat in (oldest, newest):
            created_iso = message_stat['created']
            created = timeutils.parse_isotime(created_iso)
            self.assertThat(timeutils.normalize_time(created),
                            matchers.LessThan(soon))

            self.assertIn('id', message_stat)

        self.assertThat(oldest['created'],
                        matchers.LessThan(newest['created']))

        # Test queue deletion
        self.controller.delete('test', project=self.project)

        # Test queue existence
        self.assertFalse(self.controller.exists('test', project=self.project))
Exemple #25
0
    def test_queue_lifecycle(self):
        # Test Queue Creation
        created = self.controller.create('test', project=self.project)
        self.assertTrue(created)

        # Test Queue Existence
        self.assertTrue(self.controller.exists('test', project=self.project))

        # Test Queue retrieval
        metadata = self.controller.get_metadata('test', project=self.project)
        self.assertEqual(metadata, {})

        # Test Queue Update
        created = self.controller.set_metadata('test',
                                               project=self.project,
                                               metadata=dict(meta='test_meta'))

        metadata = self.controller.get_metadata('test', project=self.project)
        self.assertEqual(metadata['meta'], 'test_meta')

        # Touching an existing queue does not affect metadata
        created = self.controller.create('test', project=self.project)
        self.assertFalse(created)

        metadata = self.controller.get_metadata('test', project=self.project)
        self.assertEqual(metadata['meta'], 'test_meta')

        # Test Queue Statistic
        _insert_fixtures(self.message_controller,
                         'test',
                         project=self.project,
                         client_uuid='my_uuid',
                         num=6)

        # NOTE(kgriffs): We can't get around doing this, because
        # we don't know how the storage drive may be calculating
        # message timestamps (and may not be monkey-patchable).
        time.sleep(1)

        _insert_fixtures(self.message_controller,
                         'test',
                         project=self.project,
                         client_uuid='my_uuid',
                         num=6)

        stats = self.controller.stats('test', project=self.project)
        message_stats = stats['messages']

        self.assertEqual(message_stats['free'], 12)
        self.assertEqual(message_stats['claimed'], 0)
        self.assertEqual(message_stats['total'], 12)

        oldest = message_stats['oldest']
        newest = message_stats['newest']

        self.assertNotEqual(oldest, newest)

        # NOTE(kgriffs): Ensure "now" is different enough
        # for the next comparison to work.
        timeutils.set_time_override()
        timeutils.advance_time_seconds(10)

        for message_stat in (oldest, newest):
            created_iso = message_stat['created']
            created = timeutils.parse_isotime(created_iso)
            self.assertThat(timeutils.normalize_time(created),
                            matchers.LessThan(timeutils.utcnow()))

            self.assertIn('id', message_stat)

        self.assertThat(oldest['created'],
                        matchers.LessThan(newest['created']))

        # Test Queue Deletion
        self.controller.delete('test', project=self.project)

        # Test Queue Existence
        self.assertFalse(self.controller.exists('test', project=self.project))

        # Test DoesNotExist Exception
        with testing.expect(storage.exceptions.DoesNotExist):
            self.controller.get_metadata('test', project=self.project)

        with testing.expect(storage.exceptions.DoesNotExist):
            self.controller.set_metadata('test', '{}', project=self.project)
Exemple #26
0
 def test_backoff_delay(self):
     for _ in range(100):
         delay = util.retry_backoff_delay(self.attempt, self.scale_factor,
                                          self.jitter_max)
         self.assertThat(delay, matchers.GreaterThan(self.delay_from))
         self.assertThat(delay, matchers.LessThan(self.delay_to))