Skip to content

Commit 5ffa8ab

Browse files
aarjoaverikitschTakashi Matsuo
authored
add python snippets and tests for creating, listing, and deleting queues (#4012)
* add python snippets and tests for creating, listing, and deleting queues * fix grammar * update licenses * apply suggested fixes and format with black * refine delete_queue_test with fixture for setup * utilize fixtures and match format of create_http_task_test * utilize fixtures in list_queues_test and create_queue_test * make create_queue_test call the right function * still attempt to delete queue after test runs in case of failure * attempt to delete queue in case of failure, using try/except approach * add print when NotFound is caught * fix import Co-authored-by: Averi Kitsch <[email protected]> Co-authored-by: Takashi Matsuo <[email protected]>
1 parent b521191 commit 5ffa8ab

6 files changed

+252
-0
lines changed

tasks/create_queue.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2020 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+
15+
# [START cloud_tasks_create_queue]
16+
def create_queue(project, queue_name, location):
17+
"""Create a task queue."""
18+
19+
from google.cloud import tasks_v2
20+
21+
# Create a client.
22+
client = tasks_v2.CloudTasksClient()
23+
24+
# Construct the fully qualified location path.
25+
parent = client.location_path(project, location)
26+
27+
# Construct the create queue request.
28+
queue = {'name': client.queue_path(project, location, queue_name)}
29+
30+
# Use the client to create the queue.
31+
response = client.create_queue(parent, queue)
32+
33+
print('Created queue {}'.format(response.name))
34+
return response
35+
# [END cloud_tasks_create_queue]

tasks/create_queue_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2020 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+
15+
import os
16+
import uuid
17+
18+
from google.cloud import tasks_v2
19+
import pytest
20+
21+
import create_queue
22+
23+
TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
24+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
25+
TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}'
26+
27+
28+
@pytest.fixture()
29+
def test_queue():
30+
client = tasks_v2.CloudTasksClient()
31+
q = create_queue.create_queue(TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION)
32+
33+
yield q
34+
35+
client.delete_queue(q.name)
36+
37+
38+
def test_create_queue(capsys, test_queue):
39+
out, _ = capsys.readouterr()
40+
assert 'Created queue' in out

tasks/delete_queue.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2020 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+
15+
# [START cloud_tasks_delete_queue]
16+
def delete_queue(project, queue_name, location):
17+
"""Delete a task queue."""
18+
19+
from google.cloud import tasks_v2
20+
21+
# Create a client.
22+
client = tasks_v2.CloudTasksClient()
23+
24+
# Get the fully qualified path to queue.
25+
queue = client.queue_path(project, location, queue_name)
26+
27+
# Use the client to delete the queue.
28+
client.delete_queue(queue)
29+
print('Deleted queue')
30+
# [END cloud_tasks_delete_queue]

tasks/delete_queue_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020 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+
15+
import os
16+
import uuid
17+
18+
from google.api_core import exceptions
19+
from google.cloud import tasks_v2
20+
import pytest
21+
22+
import delete_queue
23+
24+
25+
TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
26+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
27+
TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}'
28+
29+
30+
@pytest.fixture()
31+
def test_queue():
32+
client = tasks_v2.CloudTasksClient()
33+
parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION)
34+
queue = {
35+
# The fully qualified path to the queue
36+
'name': client.queue_path(
37+
TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
38+
}
39+
q = client.create_queue(parent, queue)
40+
41+
yield q
42+
43+
try:
44+
# Attempt to delete the queue in case the sample failed.
45+
client.delete_queue(q.name)
46+
except exceptions.NotFound:
47+
# The queue was already successfully deleted.
48+
print('Queue already deleted successfully')
49+
50+
51+
def test_delete_queue(capsys, test_queue):
52+
delete_queue.delete_queue(
53+
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION
54+
)
55+
out, _ = capsys.readouterr()
56+
assert 'Deleted queue' in out

tasks/list_queues.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2020 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+
15+
# [START cloud_tasks_list_queues]
16+
def list_queues(project, location):
17+
"""List all task queues."""
18+
19+
from google.cloud import tasks_v2
20+
21+
# Create a client.
22+
client = tasks_v2.CloudTasksClient()
23+
24+
# Construct the fully qualified location path.
25+
parent = client.location_path(project, location)
26+
27+
# Use the client to obtain the queues.
28+
response = client.list_queues(parent)
29+
30+
# Print the results.
31+
for queue in response:
32+
print(queue.name)
33+
34+
if response.num_results == 0:
35+
print('No queues found!')
36+
# [END cloud_tasks_list_queues]

tasks/list_queues_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2020 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+
15+
import os
16+
import uuid
17+
18+
from google.cloud import tasks_v2
19+
import pytest
20+
21+
import list_queues
22+
23+
TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
24+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
25+
TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}'
26+
27+
28+
@pytest.fixture()
29+
def test_queue():
30+
client = tasks_v2.CloudTasksClient()
31+
parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION)
32+
queue = {
33+
# The fully qualified path to the queue
34+
'name': client.queue_path(
35+
TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
36+
}
37+
q = client.create_queue(parent, queue)
38+
39+
yield q
40+
41+
client.delete_queue(q.name)
42+
43+
44+
def test_list_queues_not_present(capsys):
45+
list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION)
46+
out, _ = capsys.readouterr()
47+
48+
assert(TEST_QUEUE_NAME not in out)
49+
50+
51+
def test_list_queues_present(capsys, test_queue):
52+
list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION)
53+
out, _ = capsys.readouterr()
54+
55+
assert(TEST_QUEUE_NAME in out)

0 commit comments

Comments
 (0)