def test_buckets_filter():
    """Test listing the buckets"""
    client = boto3.client('s3')
    for name in ['c', 'a', 'aa']:
        client.create_bucket(Bucket=name)
    bucket_list = list_buckets()
    assert len(bucket_list) == 3
    assert bucket_list[0]['Name'] == 'a'
    assert bucket_list[0]['Region'] == 'us-east-1'
    assert bucket_list[0]['CreationDate'] is not None

    bucket_list = list_buckets(prefix='s3://a*')
    assert len(bucket_list) == 2
def test_set_gauges(monkeypatch):
    """Test setting the prometheus gaugaes from the datapoints"""
    _setup(monkeypatch)
    buckets = list_buckets()
    metrics = list_metrics(buckets)
    datapoints = get_metrics_data(metrics, buckets)
    update_gauges(datapoints)
def test_get_metrics(monkeypatch):
    """Test get the metrics"""
    _setup(monkeypatch)
    # improve the coverage: test without pool as the coverage pool ignores the workers
    s3_storage_analyser._POOL_SIZE[0] = 1
    buckets = list_buckets()
    metrics = list_metrics(buckets)
    assert len(metrics) == 3
    assert metrics[0]['_region'] == 'us-east-1'
    s3_storage_analyser._POOL_SIZE[0] = None
def test_get_metrics_data(monkeypatch):
    """Check the sanity of the shape of the datapoint"""
    _setup(monkeypatch)
    buckets = list_buckets()
    metrics = list_metrics(buckets)
    data = get_metrics_data(metrics, buckets)
    assert len(data) == 3
    for prop in [
            'BucketName', 'CreationDate', 'Name', 'Region', 'StorageType',
            'Value'
    ]:
        for datapoint in data:
            assert prop in datapoint
    assert data[0]['BucketName'] == 'hm.samples'
    assert data[0]['Region'] == 'us-east-1'
def test_fold_metrics_data(monkeypatch):
    """Test folding the datapoints"""
    _setup(monkeypatch)
    buckets = list_buckets()
    metrics = list_metrics(buckets)
    datapoints = get_metrics_data(metrics, buckets)
    folded = fold_metrics_data(datapoints)
    for index_type in ['bybucket', 'byregion']:
        index = folded[index_type]
        assert len(index.keys()) == 1
        key, value = index.popitem()
        if index_type == 'bybucket':
            assert key == 'hm.samples'
            assert value['Bucket'] == 'hm.samples'
        else:
            assert key == 'us-east-1'
            assert value['Buckets'] == 1
        assert value['Region'] == 'us-east-1'
        assert value['Files'] == 4
        assert value['Bytes'] == 24.0
        assert value['Bytes-ST'] == 24.0
        assert value['Bytes-RR'] == 0
        assert value['Bytes-IA'] == 0