Пример #1
0
def test_from_bytes():
    from azafea.event_processors.endless.activation.v1.handler import Activation

    created_at = datetime.utcnow().replace(tzinfo=timezone.utc)
    activation = Activation.from_serialized(
        json.dumps({
            'image': 'eos-eos3.7-amd64-amd64.190419-225606.base',
            'vendor': 'the vendor',
            'product': 'product',
            'release': 'release',
            'serial': 'serial',
            'mac_hash': 694551690,
            'created_at': created_at.strftime('%Y-%m-%d %H:%M:%S.%fZ'),
        }).encode('utf-8'))

    assert activation.image == 'eos-eos3.7-amd64-amd64.190419-225606.base'
    assert activation.vendor == 'the vendor'
    assert activation.product == 'product'
    assert activation.release == 'release'
    assert activation.image_product == 'eos'
    assert activation.image_branch == 'eos3.7'
    assert activation.image_arch == 'amd64'
    assert activation.image_platform == 'amd64'
    assert activation.image_timestamp == datetime(2019,
                                                  4,
                                                  19,
                                                  22,
                                                  56,
                                                  6,
                                                  tzinfo=timezone.utc)
    assert activation.image_personality == 'base'

    # SQLAlchemy only transforms the string into a datetime when querying from the DB
    assert activation.created_at == created_at.strftime(
        '%Y-%m-%d %H:%M:%S.%fZ')
Пример #2
0
def test_invalid_country(country):
    from azafea.event_processors.endless.activation.v1.handler import Activation

    with pytest.raises(ValueError) as excinfo:
        Activation(country=country)

    assert f'country has wrong length: {country}' in str(excinfo.value)
Пример #3
0
    def test_normalize_already_normalized_vendor(self):
        from azafea.event_processors.endless.activation.v1.handler import Activation

        # Create the table
        self.run_subcommand('initdb')
        self.ensure_tables(Activation)

        # Insert an activation with a known normalized vendor
        created_at = datetime.utcnow().replace(tzinfo=timezone.utc)
        vendor = 'Endless'
        assert vendor == normalize_vendor(vendor)

        with self.db as dbsession:
            dbsession.add(
                Activation(image='eos-eos3.7-amd64-amd64.190419-225606.base',
                           product='product',
                           release='release',
                           country='HK',
                           created_at=created_at,
                           vendor=vendor))

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.vendor == vendor

        # Normalize the activation vendors
        self.run_subcommand('test_normalize_already_normalized_vendor',
                            'normalize-vendors')

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.vendor == vendor
Пример #4
0
def test_invalid_longitude():
    from azafea.event_processors.endless.activation.v1.handler import Activation
    longitude = 18.7
    with pytest.raises(ValueError) as excinfo:
        Activation(longitude=longitude)

    assert f'longitude is not an integer + 0.5: {longitude}' in str(
        excinfo.value)
Пример #5
0
    def test_parse_old_images(self):
        from azafea.event_processors.endless.activation.v1.handler import Activation

        # Create the table
        self.run_subcommand('initdb')
        self.ensure_tables(Activation)

        # Insert an activation without parsed image components
        image_id = 'eos-eos3.7-amd64-amd64.190419-225606.base'
        created_at = datetime.utcnow().replace(tzinfo=timezone.utc)

        with self.db as dbsession:
            dbsession.add(
                Activation(image=image_id,
                           product='product',
                           release='release',
                           country='HK',
                           created_at=created_at,
                           vendor='vendor'))

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.image == image_id
            assert activation.image_product is None
            assert activation.image_branch is None
            assert activation.image_arch is None
            assert activation.image_platform is None
            assert activation.image_timestamp is None
            assert activation.image_personality is None

        # Parse the image for old activation records
        self.run_subcommand('test_parse_old_images', 'parse-old-images')

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.image == image_id
            assert activation.image_product == 'eos'
            assert activation.image_branch == 'eos3.7'
            assert activation.image_arch == 'amd64'
            assert activation.image_platform == 'amd64'
            assert activation.image_timestamp == datetime(2019,
                                                          4,
                                                          19,
                                                          22,
                                                          56,
                                                          6,
                                                          tzinfo=timezone.utc)
            assert activation.image_personality == 'base'
Пример #6
0
    def test_parse_old_unknown_images(self, capfd):
        from azafea.event_processors.endless.activation.v1.handler import Activation

        # Create the table
        self.run_subcommand('initdb')
        self.ensure_tables(Activation)

        # Insert an activation with an unknown image
        image_id = 'unknown'
        created_at = datetime.utcnow().replace(tzinfo=timezone.utc)

        with self.db as dbsession:
            dbsession.add(
                Activation(image=image_id,
                           product='product',
                           release='release',
                           country='HK',
                           created_at=created_at,
                           vendor='vendor'))

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.image == image_id
            assert activation.image_product is None
            assert activation.image_branch is None
            assert activation.image_arch is None
            assert activation.image_platform is None
            assert activation.image_timestamp is None
            assert activation.image_personality is None

        # Parse the image for old activation records
        self.run_subcommand('test_parse_old_unknown_images',
                            'parse-old-images')

        with self.db as dbsession:
            activation = dbsession.query(Activation).one()
            assert activation.image == image_id
            assert activation.image_product is None
            assert activation.image_branch is None
            assert activation.image_arch is None
            assert activation.image_platform is None
            assert activation.image_timestamp is None
            assert activation.image_personality is None

        capture = capfd.readouterr()
        assert 'No activation record with unparsed image ids' in capture.out
Пример #7
0
def test_empty_country(country):
    from azafea.event_processors.endless.activation.v1.handler import Activation

    activation = Activation(country=country)
    assert activation.country is None
Пример #8
0
def test_valid_country():
    from azafea.event_processors.endless.activation.v1.handler import Activation

    activation = Activation(country='HK')
    assert activation.country == 'HK'