Exemplo n.º 1
0
def test_gce_to_json():
    test_instance = gce_instance_mock()
    result = gce_to_json(test_instance)

    assert result['name'] == test_instance['name']
    assert result['id'] == test_instance['id']
    assert result['machineType'] == GCE.url_to_name(test_instance['machineType'])
    assert result['zone'] == GCE.url_to_name(test_instance['zone'])
    assert result['status'] == test_instance['status']
    assert result['launch_time'] == test_instance['creationTimestamp']
    assert result['creation_time'] == test_instance['creationTimestamp']
    assert len(result['tags']) == 0
    assert 'sshKeys' not in result['tags']
Exemplo n.º 2
0
def test_parse_image_name(monkeypatch):
    monkeypatch.setattr(ConfigFile, 'get', lambda *args, **kwargs: "FOOF")
    gce = GCE('fake')

    assert gce.parse_image_name(
        'sles12-sp5-gce-x8664-0-9-1-byos-build1-56') == {
            'key': '12-sp5-gce-byos-x8664',
            'build': '0-9-1-1-56'
        }

    assert gce.parse_image_name(
        'sles15-sp2-byos-x8664-0-9-3-gce-build1-10') == {
            'key': '15-sp2-gce-byos-x8664',
            'build': '0-9-3-1-10'
        }

    assert gce.parse_image_name('sles15-sp2-x8664-0-9-3-gce-build1-10') == {
        'key': '15-sp2-gce-x8664',
        'build': '0-9-3-1-10'
    }

    assert gce.parse_image_name(
        'sles15-sp2-chost-byos-x8664-0-9-3-gce-build1-11') == {
            'key': '15-sp2-gce-chost-byos-x8664',
            'build': '0-9-3-1-11'
        }

    assert gce.parse_image_name('do not match') is None
Exemplo n.º 3
0
def cleanup_run():
    for namespace in PCWConfig.get_namespaces_for('cleanup'):
        try:
            providers = PCWConfig.get_providers_for('cleanup', namespace)
            logger.debug("[{}] Run cleanup for {}".format(namespace, ','.join(providers)))
            if 'azure' in providers:
                Azure(namespace).cleanup_all()

            if 'ec2' in providers:
                EC2(namespace).cleanup_all()

            if 'gce' in providers:
                GCE(namespace).cleanup_all()

        except Exception as e:
            logger.exception("[{}] Cleanup failed!".format(namespace))
            send_mail('{} on Cleanup in [{}]'.format(type(e).__name__, namespace), traceback.format_exc())
Exemplo n.º 4
0
def cleanup_run():
    cfg = ConfigFile()
    if cfg.has('cleanup'):
        for vault_namespace in cfg.getList(['cleanup', 'namespaces'], cfg.getList(['vault', 'namespaces'], [''])):
            try:
                providers = cfg.getList(['vault.namespace.{}'.format(vault_namespace), 'providers'],
                                        ['ec2', 'azure', 'gce'])
                logger.debug("[{}] Run cleanup for {}".format(vault_namespace, ','.join(providers)))
                if 'azure' in providers:
                    Azure(vault_namespace).cleanup_all()

                if 'ec2' in providers:
                    EC2(vault_namespace).cleanup_all()

                if 'gce' in providers:
                    GCE(vault_namespace).cleanup_all()

            except Exception as e:
                logger.exception("[{}] Cleanup failed!".format(vault_namespace))
                send_mail('{} on Cleanup in [{}]'.format(type(e).__name__, vault_namespace), traceback.format_exc())
Exemplo n.º 5
0
def test_cleanup_all(monkeypatch):
    newer_then_min_age = datetime.now(timezone.utc).isoformat()
    older_then_min_age = (
        datetime.now(timezone.utc) -
        timedelta(hours=min_image_age_hours + 1)).isoformat()
    older_then_max_age = (
        datetime.now(timezone.utc) -
        timedelta(hours=max_image_age_hours + 1)).isoformat()

    fmi = FakeMockImages([
        FakeRequest({  # on images().list()
            'items': [{
                'name': 'I will not be parsed',
                'creationTimestamp': older_then_max_age
            }, {
                'name': 'sles12-sp5-gce-x8664-0-9-1-byos-build1-54',
                'creationTimestamp': newer_then_min_age
            }, {
                'name': 'sles12-sp5-gce-x8664-0-9-1-byos-build1-56',
                'creationTimestamp': older_then_min_age
            }]
        }),
        FakeRequest({  # on images().list_next()
            'items': [{
                'name': 'sles12-sp5-gce-x8664-0-9-1-byos-build1-57',
                'creationTimestamp': older_then_min_age
            }, {
                'name': 'sles12-sp5-gce-x8664-0-9-1-byos-build1-58',
                'creationTimestamp': older_then_max_age
            }]
        }),
        None,  # on images().list_next()
        FakeRequest({
            'error': {
                'errors': [{
                    'message': 'err message'
                }]
            },
            'warnings': [{
                'message': 'warning message'
            }]
        }),
        FakeRequest(),  # on images().delete()
    ])

    def mocked_compute_client():
        pass

    mocked_compute_client.images = lambda *args, **kwargs: fmi
    monkeypatch.setattr(GCE, 'compute_client',
                        lambda self: mocked_compute_client)

    monkeypatch.setattr(Provider, 'cfgGet', mock_cfgGet)

    gce = GCE('fake')
    generators.max_images_per_flavor = 2
    gce.cleanup_all()
    assert fmi.deleted == [
        'sles12-sp5-gce-x8664-0-9-1-byos-build1-56',
        'sles12-sp5-gce-x8664-0-9-1-byos-build1-58'
    ]

    fmi = FakeMockImages([FakeRequest({})])
    gce.cleanup_all()
    assert fmi.deleted == []