Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit d7f6d09

Browse files
committed
feat: Adding tests to default values samples.
1 parent 1b9ef96 commit d7f6d09

3 files changed

Lines changed: 78 additions & 3 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pytest==6.2.4
1+
pytest==6.2.4
2+
google-cloud-storage==1.39.0

samples/snippets/sample_default_values.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ def set_usage_export_bucket(project_id: str, bucket_name: str,
5555
"to have the default prefix of `usage_gce`.")
5656

5757
projects_client = compute_v1.ProjectsClient()
58-
projects_client.set_usage_export_bucket(
58+
operation = projects_client.set_usage_export_bucket(
5959
project=project_id, usage_export_location_resource=usage_export_location)
60+
61+
if operation.status == compute_v1.Operation.Status.RUNNING:
62+
op_client = compute_v1.GlobalOperationsClient()
63+
op_client.wait(project=project_id, operation=operation.name)
6064
return
6165
# [END compute_usage_report_set]
6266

@@ -82,6 +86,7 @@ def get_usage_export_bucket(project_id: str) -> compute_v1.UsageExportLocation:
8286
if not uel.bucket_name:
8387
# The Usage Reports are disabled.
8488
return uel
89+
8590
if not uel.report_name_prefix:
8691
# Although the server sent the empty value, the next usage report
8792
# generated with these settings still has the default prefix value
@@ -106,7 +111,11 @@ def disable_usage_export(project_id: str) -> None:
106111

107112
# Updating the setting with None will disable the
108113
# usage report generation.
109-
projects_client.set_usage_export_bucket(
114+
operation = projects_client.set_usage_export_bucket(
110115
project=project_id, usage_export_location_resource=None)
116+
117+
if operation.status == compute_v1.Operation.Status.RUNNING:
118+
op_client = compute_v1.GlobalOperationsClient()
119+
op_client.wait(project=project_id, operation=operation.name)
111120
return
112121
# [END compute_usage_report_disable]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)