Exemple #1
0
    def test_inventory_upgrader_patch_error(self):
        inventory_upgrader.Offering.objects.filter.side_effect = [[
            MagicMock(off_id=self._product_off_id)
        ], []]

        self._client_instance.get_products.side_effect = [
            [{
                'id': str(i)
            } for i in range(3, 5)],  # First call
            [self._product3, self._product4],  # Last call
        ]

        self._client_instance.patch_product.side_effect = [None, HTTPError()]

        inventory_upgrader.requests.get.side_effect = HTTPError()

        # Execute the tested method
        upgrader = inventory_upgrader.InventoryUpgrader(self._asset)
        upgrader.run()

        # Check calls
        self.assertEquals([{
            'asset_id': self._asset_pk,
            'pending_offerings': [],
            'pending_products': ['4']
        }], self._ctx_instance.failed_upgrades)
        self._ctx_instance.save.assert_called_once_with()

        inventory_upgrader.DocumentLock.assert_called_once_with(
            'wstore_context', self._ctx_pk, 'ctx')
        self._lock_inst.wait_document.assert_called_once_with()
        self._lock_inst.unlock_document.assert_called_once_with()

        self.assertEquals([
            call(query={
                'productOffering.id': self._product_off_id,
                'fields': 'id'
            }),
            call(query={
                'id': '3,4',
                'fields': 'id,productCharacteristic'
            })
        ], self._client_instance.get_products.call_args_list)

        self.assertEquals([
            call('3', {'productCharacteristic': self._new_asset_chars}),
            call('4', {'productCharacteristic': self._new_asset_chars})
        ], self._client_instance.patch_product.call_args_list)

        inventory_upgrader.requests.get.assert_called_once_with(
            self._product_spec_url)
        self.assertEquals(0, self._resp.raise_for_status.call_count)
        self.assertEquals(0, self._resp.json.call_count)

        self.assertEquals(0,
                          inventory_upgrader.NotificationsHandler.call_count)
Exemple #2
0
    def test_inventory_upgrader_no_offerings(self):
        inventory_upgrader.Offering.objects.filter.return_value = []

        upgrader = inventory_upgrader.InventoryUpgrader(self._asset)
        upgrader.run()

        inventory_upgrader.Offering.objects.filter.assert_called_once_with(
            asset=self._asset)
        inventory_upgrader.Resource.objects.filter.assert_called_once_with(
            bundled_assets=self._asset_pk)
        self.assertEquals([], self._ctx_instance.failed_upgrades)
        self.assertEquals(0, self._client_instance.get_products.call_count)

        self._check_product_spec_retrieved()
Exemple #3
0
    def test_inventory_upgrader_no_products(self):
        inventory_upgrader.Offering.objects.filter.side_effect = [[
            MagicMock(off_id=self._product_off_id)
        ], []]
        self._client_instance.get_products.return_value = []

        # Execute the tested method
        upgrader = inventory_upgrader.InventoryUpgrader(self._asset)
        upgrader.run()

        # Check calls
        self.assertEquals([], self._ctx_instance.failed_upgrades)
        self._check_single_get_call()

        self._check_product_spec_retrieved()
Exemple #4
0
    def test_inventory_upgrader_page_error(self):
        inventory_upgrader.Offering.objects.filter.side_effect = [[
            MagicMock(off_id=self._product_off_id),
            MagicMock(off_id=self._product_off_id2)
        ], [], []]

        # Mock inventory client methods
        self._client_instance.get_products.side_effect = [
            [{
                'id': str(i)
            } for i in range(1, 5)],  # First call off1
            HTTPError(),  # Second call - HTTPError retrieving page
            [self._product3, self._product4],  # Last call off1
            HTTPError()  # Error retrieving second offering
        ]

        self._client_instance.patch_product.side_effect = [
            self._product3, self._product4
        ]

        self._not_handler.send_product_upgraded_notification.side_effect = Exception(
        )

        # Execute the tested method
        upgrader = inventory_upgrader.InventoryUpgrader(self._asset)
        upgrader.run()

        # Check calls
        self.assertEquals([{
            'asset_id': self._asset_pk,
            'pending_offerings': [self._product_off_id2],
            'pending_products': ['1', '2']
        }], self._ctx_instance.failed_upgrades)
        self._ctx_instance.save.assert_called_once_with()

        inventory_upgrader.DocumentLock.assert_called_once_with(
            'wstore_context', self._ctx_pk, 'ctx')
        self._lock_inst.wait_document.assert_called_once_with()
        self._lock_inst.unlock_document.assert_called_once_with()

        self.assertEquals([
            call(query={
                'productOffering.id': self._product_off_id,
                'fields': 'id'
            }),
            call(query={
                'id': '1,2',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'id': '3,4',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'productOffering.id': self._product_off_id2,
                'fields': 'id'
            })
        ], self._client_instance.get_products.call_args_list)

        self.assertEquals([
            call('3', {'productCharacteristic': self._new_asset_chars}),
            call('4', {'productCharacteristic': self._new_asset_chars})
        ], self._client_instance.patch_product.call_args_list)

        self.assertEquals([
            call(order_id='33'),
            call(order_id='44'),
        ], inventory_upgrader.Order.objects.get.call_args_list)

        self.assertEquals([call('3'), call('4')],
                          self._order_int.get_product_contract.call_args_list)

        self.assertEquals(
            [call(), call()],
            inventory_upgrader.NotificationsHandler.call_args_list)

        self.assertEquals([
            call(self._order_int, self._order_int.get_product_contract(),
                 self._product_spec_name),
            call(self._order_int, self._order_int.get_product_contract(),
                 self._product_spec_name),
        ], self._not_handler.send_product_upgraded_notification.call_args_list)

        self._check_product_spec_retrieved()
Exemple #5
0
    def test_inventory_upgrader(self):
        # Mock assets and offerings
        asset_bundle = MagicMock(bundled_assets=[self._asset.pk])
        inventory_upgrader.Resource.objects.filter.return_value = [
            asset_bundle
        ]

        offering1 = MagicMock(off_id=self._product_off_id,
                              pk=self._product_off_pk,
                              asset=self._asset)
        offering2 = MagicMock(off_id=self._product_off_id2,
                              pk=self._product_off_pk2,
                              asset=self._asset)
        offering3 = MagicMock(off_id=self._product_off_id3,
                              pk=self._product_off_pk3,
                              asset=asset_bundle)

        inventory_upgrader.Offering.objects.filter.side_effect = [
            # Single offering retrieved from the assets
            [offering1, offering2],
            [offering3],

            # Bundle results
            [MagicMock(off_id=self._off_bundle_id, pk=self._off_bundle_pk)],
            [],
            [MagicMock(off_id=self._off_bundle_id2, pk=self._off_bundle_pk2)],

            # Offering id validation results
            [offering1],
            [MagicMock()],
            [offering1],
            [MagicMock()],
            [offering1],
            [MagicMock()],
            [offering3],
            [offering3],
            [offering3],
            [],
            [],
            []
        ]

        # Mock inventory client methods
        self._client_instance.get_products.side_effect = [
            [{
                'id': str(i)
            } for i in range(1, 6)],  # First call
            [self._product1, self._product2],  # Second call
            [self._product3, self._product4],  # Third call
            [self._product5],  # Last call
            [{
                'id': str(i)
            } for i in range(6, 7)],  # First call
            [self._product6],  # Last call
            [{
                'id': str(i)
            } for i in range(7, 8)],
            [self._product_p_bundle],
            [{
                'id': str(i)
            } for i in range(8, 9)],
            [self._product_off_bundle],
            [{
                'id': str(i)
            } for i in range(9, 10)],
            [self._product_mix_bundle]
        ]

        self._client_instance.patch_product.side_effect = [
            self._product1, self._product2, self._product3, self._product4,
            self._product5, self._product6, self._product_p_bundle,
            self._product_off_bundle, self._product_mix_bundle
        ]

        # Execute the tested method
        upgrader = inventory_upgrader.InventoryUpgrader(self._asset)
        upgrader.run()

        inventory_upgrader.Resource.objects.filter.assert_called_once_with(
            bundled_assets=self._asset_pk)
        self.assertEquals([
            call(asset=self._asset),
            call(asset=asset_bundle),
            call(bundled_offerings=self._product_off_pk),
            call(bundled_offerings=self._product_off_pk2),
            call(bundled_offerings=self._product_off_pk3),
            call(off_id=self._product_off_id),
            call(off_id=u'asd'),
            call(off_id=self._product_off_id),
            call(off_id=u'asd'),
            call(off_id=self._product_off_id),
            call(off_id=u'asd'),
            call(off_id=self._product_off_id3),
            call(off_id=self._product_off_id3),
            call(off_id=self._product_off_id3),
            call(off_id=u'asd'),
            call(off_id=u'asd'),
            call(off_id=u'asd')
        ], inventory_upgrader.Offering.objects.filter.call_args_list)

        # Check calls
        self.assertEquals([], self._ctx_instance.failed_upgrades)

        self.assertEquals([
            call(query={
                'productOffering.id': self._product_off_id,
                'fields': 'id'
            }),
            call(query={
                'id': '1,2',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'id': '3,4',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'id': '5',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'productOffering.id': self._product_off_id2,
                'fields': 'id'
            }),
            call(query={
                'id': '6',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'productOffering.id': self._product_off_id3,
                'fields': 'id'
            }),
            call(query={
                'id': '7',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'productOffering.id': self._off_bundle_id,
                'fields': 'id'
            }),
            call(query={
                'id': '8',
                'fields': 'id,productCharacteristic'
            }),
            call(query={
                'productOffering.id': self._off_bundle_id2,
                'fields': 'id'
            }),
            call(query={
                'id': '9',
                'fields': 'id,productCharacteristic'
            })
        ], self._client_instance.get_products.call_args_list)

        exp_charp1 = deepcopy(self._new_asset_chars)
        exp_charp1.insert(0, {'name': 'speed', 'value': '100mbps'})
        exp_charp2 = deepcopy(self._new_asset_chars)
        exp_charp2.insert(0, {'name': 'speed', 'value': '20mbps'})

        self.assertEquals([
            call('1', {'productCharacteristic': exp_charp1}),
            call('2', {'productCharacteristic': exp_charp2}),
            call('3', {'productCharacteristic': self._new_asset_chars}),
            call('4', {'productCharacteristic': self._new_asset_chars}),
            call('5', {'productCharacteristic': self._new_asset_chars}),
            call('6', {'productCharacteristic': self._new_asset_chars}),
            call('7',
                 {'productCharacteristic': self._new_product_bundle_chars}),
            call('8', {'productCharacteristic': self._new_off_bundle_chars}),
            call('9', {'productCharacteristic': self._new_mixed_bundle_chars})
        ], self._client_instance.patch_product.call_args_list)

        self.assertEquals([
            call(order_id='11'),
            call(order_id='22'),
            call(order_id='33'),
            call(order_id='44'),
            call(order_id='55'),
            call(order_id='66'),
            call(order_id='77'),
            call(order_id='88'),
            call(order_id='99')
        ], inventory_upgrader.Order.objects.get.call_args_list)

        self.assertEquals([
            call('1'),
            call('2'),
            call('3'),
            call('4'),
            call('5'),
            call('6'),
            call('7'),
            call('8'),
            call('9')
        ], self._order_int.get_product_contract.call_args_list)

        self.assertEquals(
            [call() for i in range(0, 9)],
            inventory_upgrader.NotificationsHandler.call_args_list)

        self.assertEquals([
            call(self._order_int, self._order_int.get_product_contract(),
                 self._product_spec_name) for i in range(0, 9)
        ], self._not_handler.send_product_upgraded_notification.call_args_list)

        self._check_product_spec_retrieved()