|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import typing |
| 15 | +import uuid |
| 16 | + |
| 17 | +import google.auth |
| 18 | +import google.cloud.storage as storage |
| 19 | +import pytest |
| 20 | + |
| 21 | +from sample_default_values import \ |
| 22 | + disable_usage_export, get_usage_export_bucket, set_usage_export_bucket |
| 23 | + |
| 24 | +PROJECT = google.auth.default()[1] |
| 25 | +BUCKET_NAME = "test" + uuid.uuid4().hex[:10] |
| 26 | +TEST_PREFIX = 'some-prefix' |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def temp_bucket(): |
| 31 | + storage_client = storage.Client() |
| 32 | + bucket = storage_client.create_bucket(BUCKET_NAME) |
| 33 | + yield bucket |
| 34 | + bucket.delete(force=True) |
| 35 | + |
| 36 | + |
| 37 | +def test_set_usage_export_bucket_default(capsys: typing.Any, |
| 38 | + temp_bucket: storage.Bucket) -> None: |
| 39 | + set_usage_export_bucket(project_id=PROJECT, bucket_name=temp_bucket.name) |
| 40 | + uel = get_usage_export_bucket(project_id=PROJECT) |
| 41 | + assert(uel.bucket_name == temp_bucket.name) |
| 42 | + assert(uel.report_name_prefix == 'usage_gce') |
| 43 | + out, _ = capsys.readouterr() |
| 44 | + assert('default prefix of `usage_gce`.' in out) |
| 45 | + |
| 46 | + disable_usage_export(project_id=PROJECT) |
| 47 | + uel = get_usage_export_bucket(project_id=PROJECT) |
| 48 | + assert(uel.bucket_name == '') |
| 49 | + assert(uel.report_name_prefix == '') |
| 50 | + |
| 51 | + |
| 52 | +def test_set_usage_export_bucket_custom(capsys: typing.Any, |
| 53 | + temp_bucket: storage.Bucket) -> None: |
| 54 | + set_usage_export_bucket(project_id=PROJECT, bucket_name=temp_bucket.name, |
| 55 | + report_name_prefix=TEST_PREFIX) |
| 56 | + uel = get_usage_export_bucket(project_id=PROJECT) |
| 57 | + assert(uel.bucket_name == temp_bucket.name) |
| 58 | + assert(uel.report_name_prefix == TEST_PREFIX) |
| 59 | + out, _ = capsys.readouterr() |
| 60 | + assert('usage_gce' not in out) |
| 61 | + |
| 62 | + disable_usage_export(project_id=PROJECT) |
| 63 | + uel = get_usage_export_bucket(project_id=PROJECT) |
| 64 | + assert(uel.bucket_name == '') |
| 65 | + assert(uel.report_name_prefix == '') |
0 commit comments