From a8cf1d737aa8b427b3b64817c1f44ed7bf3bceed Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Mon, 8 Jun 2020 18:37:44 +0000 Subject: [PATCH 01/12] add python snippets and tests for creating, listing, and deleting queues --- tasks/create_queue.py | 73 ++++++++++++++++++++++++++++++++++++++ tasks/create_queue_test.py | 31 ++++++++++++++++ tasks/delete_queue.py | 71 ++++++++++++++++++++++++++++++++++++ tasks/delete_queue_test.py | 37 +++++++++++++++++++ tasks/list_queues.py | 67 ++++++++++++++++++++++++++++++++++ tasks/list_queues_test.py | 30 ++++++++++++++++ 6 files changed, 309 insertions(+) create mode 100644 tasks/create_queue.py create mode 100644 tasks/create_queue_test.py create mode 100644 tasks/delete_queue.py create mode 100644 tasks/delete_queue_test.py create mode 100644 tasks/list_queues.py create mode 100644 tasks/list_queues_test.py diff --git a/tasks/create_queue.py b/tasks/create_queue.py new file mode 100644 index 000000000000..90c3457030fa --- /dev/null +++ b/tasks/create_queue.py @@ -0,0 +1,73 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +import argparse + + +def create_queue(project, + queue_name, + location): + # [START cloud_tasks_create_queue] + """Create a task queue.""" + + from google.cloud import tasks_v2 + + # Create a client. + client = tasks_v2.CloudTasksClient() + + # Construct the fully qualified location path. + parent = client.location_path(project, location) + + # Construct Queue + queue = { + 'name': client.queue_path(project, location, queue_name) + } + + # Use the client to create the queue. + response = client.create_queue(parent, queue) + + print('Created queue {}'.format(response.name)) + return response +# [END cloud_tasks_create_queue] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=create_queue.__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + parser.add_argument( + '--project', + help='Project to add the queue to.', + required=True, + ) + + parser.add_argument( + '--queue', + help='ID (short name) of the queue to be created.', + required=True, + ) + + parser.add_argument( + '--location', + help='Location of the project to add the queue to.', + required=True, + ) + + args = parser.parse_args() + + create_queue( + args.project, args.queue, args.location) diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py new file mode 100644 index 000000000000..c4988eede138 --- /dev/null +++ b/tasks/create_queue_test.py @@ -0,0 +1,31 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import create_queue + +TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') +TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') + + +def test_create_queue(): + result = create_queue.create_queue( + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) + assert TEST_QUEUE_NAME in result.name + + +if __name__ == '__main__': + test_create_queue() diff --git a/tasks/delete_queue.py b/tasks/delete_queue.py new file mode 100644 index 000000000000..b3e697094d48 --- /dev/null +++ b/tasks/delete_queue.py @@ -0,0 +1,71 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +import argparse + + +def delete_queue(project, + queue_name, + location): + # [START cloud_tasks_delete_queue] + """Delete a task queue.""" + + from google.cloud import tasks_v2 + from google.api_core import exceptions + + # Create a client. + client = tasks_v2.CloudTasksClient() + + # Get the fully qualified path to queue + queue = client.queue_path(project, location, queue_name) + + # Use the client to delete the queue. + try: + client.delete_queue(queue) + print('Deleted queue') + except exceptions.NotFound: + print('Queue not found') + +# [END cloud_tasks_delete_queue] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=delete_queue.__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + parser.add_argument( + '--project', + help='Project to delete the queue from.', + required=True, + ) + + parser.add_argument( + '--queue', + help='ID (short name) of the queue to be deleted.', + required=True, + ) + + parser.add_argument( + '--location', + help='Location of the queue.', + required=True, + ) + + args = parser.parse_args() + + delete_queue( + args.project, args.queue, args.location) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py new file mode 100644 index 000000000000..24c8abc78e08 --- /dev/null +++ b/tasks/delete_queue_test.py @@ -0,0 +1,37 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import sys +import io + +import delete_queue + +TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') +TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') + + +def test_delete_queue(): + output_capture = io.StringIO() + sys.stdout = output_capture + delete_queue.delete_queue( + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) + sys.stdout = sys.__stdout__ + assert(output_capture.getvalue().rstrip() + in ['Deleted Queue', 'Queue not found']) + + +if __name__ == '__main__': + test_delete_queue() diff --git a/tasks/list_queues.py b/tasks/list_queues.py new file mode 100644 index 000000000000..f34e652f6a4c --- /dev/null +++ b/tasks/list_queues.py @@ -0,0 +1,67 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +import argparse + + +def list_queues(project, + location): + # [START cloud_tasks_list_queues] + """List all task queues.""" + + from google.cloud import tasks_v2 + + # Create a client. + client = tasks_v2.CloudTasksClient() + + # Construct the fully qualified location path. + parent = client.location_path(project, location) + + # Use the client to obtain the queues + response = client.list_queues(parent) + + # Convert the response to a list containing all queues + queue_list = list(response) + + # Print the results + for queue in queue_list: + print(queue.name) + + return response +# [END cloud_tasks_list_queues] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=list_queues.__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + parser.add_argument( + '--project', + help='Project to list queues for.', + required=True, + ) + + parser.add_argument( + '--location', + help='Location of the project to list queues for.', + required=True, + ) + + args = parser.parse_args() + + list_queues( + args.project, args.location) diff --git a/tasks/list_queues_test.py b/tasks/list_queues_test.py new file mode 100644 index 000000000000..122962497fb5 --- /dev/null +++ b/tasks/list_queues_test.py @@ -0,0 +1,30 @@ +# Copyright 2019 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import list_queues + +TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') + + +def test_list_queues(): + result = list_queues.list_queues( + TEST_PROJECT_ID, TEST_LOCATION) + assert(result) + + +if __name__ == '__main__': + test_list_queues() From 7fe9f1c0fdbb4aa38800d6bd702d8d59f7409df0 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Mon, 8 Jun 2020 20:40:04 +0000 Subject: [PATCH 02/12] fix grammar --- tasks/create_queue.py | 2 +- tasks/delete_queue.py | 2 +- tasks/list_queues.py | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tasks/create_queue.py b/tasks/create_queue.py index 90c3457030fa..802e854464c5 100644 --- a/tasks/create_queue.py +++ b/tasks/create_queue.py @@ -31,7 +31,7 @@ def create_queue(project, # Construct the fully qualified location path. parent = client.location_path(project, location) - # Construct Queue + # Construct the queue. queue = { 'name': client.queue_path(project, location, queue_name) } diff --git a/tasks/delete_queue.py b/tasks/delete_queue.py index b3e697094d48..91f89cea2ed2 100644 --- a/tasks/delete_queue.py +++ b/tasks/delete_queue.py @@ -29,7 +29,7 @@ def delete_queue(project, # Create a client. client = tasks_v2.CloudTasksClient() - # Get the fully qualified path to queue + # Get the fully qualified path to queue. queue = client.queue_path(project, location, queue_name) # Use the client to delete the queue. diff --git a/tasks/list_queues.py b/tasks/list_queues.py index f34e652f6a4c..83808f629178 100644 --- a/tasks/list_queues.py +++ b/tasks/list_queues.py @@ -30,13 +30,13 @@ def list_queues(project, # Construct the fully qualified location path. parent = client.location_path(project, location) - # Use the client to obtain the queues + # Use the client to obtain the queues. response = client.list_queues(parent) - # Convert the response to a list containing all queues + # Convert the response to a list containing all queues. queue_list = list(response) - # Print the results + # Print the results. for queue in queue_list: print(queue.name) From 8a9cff9247f4d799fd83dfe2b20ab04c5181f551 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Mon, 8 Jun 2020 20:45:04 +0000 Subject: [PATCH 03/12] update licenses --- tasks/create_queue.py | 4 ++-- tasks/create_queue_test.py | 4 ++-- tasks/delete_queue.py | 4 ++-- tasks/delete_queue_test.py | 4 ++-- tasks/list_queues.py | 4 ++-- tasks/list_queues_test.py | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tasks/create_queue.py b/tasks/create_queue.py index 802e854464c5..f142fe8d009c 100644 --- a/tasks/create_queue.py +++ b/tasks/create_queue.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py index c4988eede138..77308b9eae44 100644 --- a/tasks/create_queue_test.py +++ b/tasks/create_queue_test.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tasks/delete_queue.py b/tasks/delete_queue.py index 91f89cea2ed2..b68f468c72a6 100644 --- a/tasks/delete_queue.py +++ b/tasks/delete_queue.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 24c8abc78e08..8d25ed3bf000 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tasks/list_queues.py b/tasks/list_queues.py index 83808f629178..43ba81e39a59 100644 --- a/tasks/list_queues.py +++ b/tasks/list_queues.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tasks/list_queues_test.py b/tasks/list_queues_test.py index 122962497fb5..80299cad3d48 100644 --- a/tasks/list_queues_test.py +++ b/tasks/list_queues_test.py @@ -1,10 +1,10 @@ -# Copyright 2019 Google LLC All Rights Reserved. +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, From 4a9a2c1e5fbf5b500d956de7f722770da0183543 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Tue, 9 Jun 2020 22:11:48 +0000 Subject: [PATCH 04/12] apply suggested fixes and format with black --- tasks/create_queue.py | 46 ++++------------------------------- tasks/create_queue_test.py | 9 +++---- tasks/delete_queue.py | 49 ++++---------------------------------- tasks/delete_queue_test.py | 26 +++++++++----------- tasks/list_queues.py | 41 +++++-------------------------- tasks/list_queues_test.py | 11 +++------ 6 files changed, 31 insertions(+), 151 deletions(-) diff --git a/tasks/create_queue.py b/tasks/create_queue.py index f142fe8d009c..d8d4dfaea263 100644 --- a/tasks/create_queue.py +++ b/tasks/create_queue.py @@ -12,15 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - -import argparse - - -def create_queue(project, - queue_name, - location): - # [START cloud_tasks_create_queue] +# [START cloud_tasks_create_queue] +def create_queue(project, queue_name, location): """Create a task queue.""" from google.cloud import tasks_v2 @@ -31,10 +24,8 @@ def create_queue(project, # Construct the fully qualified location path. parent = client.location_path(project, location) - # Construct the queue. - queue = { - 'name': client.queue_path(project, location, queue_name) - } + # Construct the create queue request. + queue = {'name': client.queue_path(project, location, queue_name)} # Use the client to create the queue. response = client.create_queue(parent, queue) @@ -42,32 +33,3 @@ def create_queue(project, print('Created queue {}'.format(response.name)) return response # [END cloud_tasks_create_queue] - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description=create_queue.__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) - - parser.add_argument( - '--project', - help='Project to add the queue to.', - required=True, - ) - - parser.add_argument( - '--queue', - help='ID (short name) of the queue to be created.', - required=True, - ) - - parser.add_argument( - '--location', - help='Location of the project to add the queue to.', - required=True, - ) - - args = parser.parse_args() - - create_queue( - args.project, args.queue, args.location) diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py index 77308b9eae44..d4187dc7d781 100644 --- a/tasks/create_queue_test.py +++ b/tasks/create_queue_test.py @@ -16,16 +16,13 @@ import create_queue -TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') def test_create_queue(): result = create_queue.create_queue( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION + ) assert TEST_QUEUE_NAME in result.name - - -if __name__ == '__main__': - test_create_queue() diff --git a/tasks/delete_queue.py b/tasks/delete_queue.py index b68f468c72a6..d69514249a17 100644 --- a/tasks/delete_queue.py +++ b/tasks/delete_queue.py @@ -12,19 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - -import argparse - - -def delete_queue(project, - queue_name, - location): - # [START cloud_tasks_delete_queue] +# [START cloud_tasks_delete_queue] +def delete_queue(project, queue_name, location): """Delete a task queue.""" from google.cloud import tasks_v2 - from google.api_core import exceptions # Create a client. client = tasks_v2.CloudTasksClient() @@ -33,39 +25,6 @@ def delete_queue(project, queue = client.queue_path(project, location, queue_name) # Use the client to delete the queue. - try: - client.delete_queue(queue) - print('Deleted queue') - except exceptions.NotFound: - print('Queue not found') - + client.delete_queue(queue) + print('Deleted queue') # [END cloud_tasks_delete_queue] - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description=delete_queue.__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) - - parser.add_argument( - '--project', - help='Project to delete the queue from.', - required=True, - ) - - parser.add_argument( - '--queue', - help='ID (short name) of the queue to be deleted.', - required=True, - ) - - parser.add_argument( - '--location', - help='Location of the queue.', - required=True, - ) - - args = parser.parse_args() - - delete_queue( - args.project, args.queue, args.location) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 8d25ed3bf000..c14b918a9b9f 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -13,25 +13,21 @@ # limitations under the License. import os -import sys -import io import delete_queue +from google.api_core import exceptions -TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') -def test_delete_queue(): - output_capture = io.StringIO() - sys.stdout = output_capture - delete_queue.delete_queue( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) - sys.stdout = sys.__stdout__ - assert(output_capture.getvalue().rstrip() - in ['Deleted Queue', 'Queue not found']) - - -if __name__ == '__main__': - test_delete_queue() +def test_delete_queue(capsys): + try: + delete_queue.delete_queue( + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION + ) + out, _ = capsys.readouterr() + assert 'Deleted Queue' in out + except exceptions.NotFound: + pass diff --git a/tasks/list_queues.py b/tasks/list_queues.py index 43ba81e39a59..3746b95914bd 100644 --- a/tasks/list_queues.py +++ b/tasks/list_queues.py @@ -12,14 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import print_function - -import argparse - - -def list_queues(project, - location): - # [START cloud_tasks_list_queues] +# [START cloud_tasks_list_queues] +def list_queues(project, location): """List all task queues.""" from google.cloud import tasks_v2 @@ -33,35 +27,12 @@ def list_queues(project, # Use the client to obtain the queues. response = client.list_queues(parent) - # Convert the response to a list containing all queues. - queue_list = list(response) - # Print the results. - for queue in queue_list: + for queue in response: print(queue.name) + if response.num_results == 0: + print('No queues found!') + return response # [END cloud_tasks_list_queues] - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description=list_queues.__doc__, - formatter_class=argparse.RawDescriptionHelpFormatter) - - parser.add_argument( - '--project', - help='Project to list queues for.', - required=True, - ) - - parser.add_argument( - '--location', - help='Location of the project to list queues for.', - required=True, - ) - - args = parser.parse_args() - - list_queues( - args.project, args.location) diff --git a/tasks/list_queues_test.py b/tasks/list_queues_test.py index 80299cad3d48..98bc3ac6af16 100644 --- a/tasks/list_queues_test.py +++ b/tasks/list_queues_test.py @@ -16,15 +16,10 @@ import list_queues -TEST_PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') +TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') def test_list_queues(): - result = list_queues.list_queues( - TEST_PROJECT_ID, TEST_LOCATION) - assert(result) - - -if __name__ == '__main__': - test_list_queues() + result = list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) + assert result From 3018c4630fb0e2060c626762daab138630fb18ae Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 20:09:18 +0000 Subject: [PATCH 05/12] refine delete_queue_test with fixture for setup --- tasks/delete_queue_test.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index c14b918a9b9f..498069f0a264 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -13,21 +13,29 @@ # limitations under the License. import os +import uuid +import pytest +import create_queue import delete_queue -from google.api_core import exceptions + TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') -TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') - - -def test_delete_queue(capsys): - try: - delete_queue.delete_queue( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION - ) - out, _ = capsys.readouterr() - assert 'Deleted Queue' in out - except exceptions.NotFound: - pass + + +@pytest.fixture +def queue_name(): + queue_name = 'test-queue-{}'.format(uuid.uuid4().hex) + create_queue.create_queue( + TEST_PROJECT_ID, queue_name, TEST_LOCATION + ) + return queue_name + + +def test_delete_queue(capsys, queue_name): + delete_queue.delete_queue( + TEST_PROJECT_ID, queue_name, TEST_LOCATION + ) + out, _ = capsys.readouterr() + assert 'Deleted queue' in out From 55e3bbf5347c7e629d6588e12446ef977c212047 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 20:34:10 +0000 Subject: [PATCH 06/12] utilize fixtures and match format of create_http_task_test --- tasks/delete_queue_test.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 498069f0a264..66364f12ae1d 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -14,28 +14,35 @@ import os import uuid + +from google.cloud import tasks_v2 import pytest -import create_queue import delete_queue TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') +TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}' -@pytest.fixture -def queue_name(): - queue_name = 'test-queue-{}'.format(uuid.uuid4().hex) - create_queue.create_queue( - TEST_PROJECT_ID, queue_name, TEST_LOCATION - ) - return queue_name +@pytest.fixture() +def test_queue(): + client = tasks_v2.CloudTasksClient() + parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION) + queue = { + # The fully qualified path to the queue + 'name': client.queue_path( + TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME), + } + q = client.create_queue(parent, queue) + + return q -def test_delete_queue(capsys, queue_name): +def test_delete_queue(capsys, test_queue): delete_queue.delete_queue( - TEST_PROJECT_ID, queue_name, TEST_LOCATION + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION ) out, _ = capsys.readouterr() assert 'Deleted queue' in out From 547ac9d22348460b3a7b20f9d7175d21c69daeb0 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 21:44:11 +0000 Subject: [PATCH 07/12] utilize fixtures in list_queues_test and create_queue_test --- tasks/create_queue_test.py | 29 +++++++++++++++++++++++------ tasks/list_queues.py | 2 -- tasks/list_queues_test.py | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py index d4187dc7d781..f660063373f5 100644 --- a/tasks/create_queue_test.py +++ b/tasks/create_queue_test.py @@ -13,16 +13,33 @@ # limitations under the License. import os +import uuid + +from google.cloud import tasks_v2 +import pytest import create_queue TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') -TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-queue') +TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}' + + +@pytest.fixture() +def test_queue(): + client = tasks_v2.CloudTasksClient() + parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION) + queue = { + # The fully qualified path to the queue + 'name': client.queue_path( + TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME), + } + q = client.create_queue(parent, queue) + + yield q + + client.delete_queue(q.name) -def test_create_queue(): - result = create_queue.create_queue( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION - ) - assert TEST_QUEUE_NAME in result.name +def test_create_queue(test_queue): + assert TEST_QUEUE_NAME in test_queue.name diff --git a/tasks/list_queues.py b/tasks/list_queues.py index 3746b95914bd..fa48907fad17 100644 --- a/tasks/list_queues.py +++ b/tasks/list_queues.py @@ -33,6 +33,4 @@ def list_queues(project, location): if response.num_results == 0: print('No queues found!') - - return response # [END cloud_tasks_list_queues] diff --git a/tasks/list_queues_test.py b/tasks/list_queues_test.py index 98bc3ac6af16..df11d0bd4f9b 100644 --- a/tasks/list_queues_test.py +++ b/tasks/list_queues_test.py @@ -13,13 +13,43 @@ # limitations under the License. import os +import uuid + +from google.cloud import tasks_v2 +import pytest import list_queues TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') +TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}' + + +@pytest.fixture() +def test_queue(): + client = tasks_v2.CloudTasksClient() + parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION) + queue = { + # The fully qualified path to the queue + 'name': client.queue_path( + TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME), + } + q = client.create_queue(parent, queue) + + yield q + + client.delete_queue(q.name) + + +def test_list_queues_not_present(capsys): + list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) + out, _ = capsys.readouterr() + + assert(TEST_QUEUE_NAME not in out) + +def test_list_queues_present(capsys, test_queue): + list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) + out, _ = capsys.readouterr() -def test_list_queues(): - result = list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) - assert result + assert(TEST_QUEUE_NAME in out) From e4fd7425e3507c8564ae0d9426176c0dafbff54e Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 21:51:00 +0000 Subject: [PATCH 08/12] make create_queue_test call the right function --- tasks/create_queue_test.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py index f660063373f5..1f623a2aa6cd 100644 --- a/tasks/create_queue_test.py +++ b/tasks/create_queue_test.py @@ -28,18 +28,13 @@ @pytest.fixture() def test_queue(): client = tasks_v2.CloudTasksClient() - parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION) - queue = { - # The fully qualified path to the queue - 'name': client.queue_path( - TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME), - } - q = client.create_queue(parent, queue) + q = create_queue.create_queue(TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) yield q client.delete_queue(q.name) -def test_create_queue(test_queue): - assert TEST_QUEUE_NAME in test_queue.name +def test_create_queue(capsys, test_queue): + out, _ = capsys.readouterr() + assert 'Created queue' in out From 195183f289816b08fcd4d9cbdab1f4412d469167 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 22:13:33 +0000 Subject: [PATCH 09/12] still attempt to delete queue after test runs in case of failure --- tasks/delete_queue_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 66364f12ae1d..2eadf3bb4b78 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -37,7 +37,7 @@ def test_queue(): } q = client.create_queue(parent, queue) - return q + yield q def test_delete_queue(capsys, test_queue): From 8552a80796070ca291a647c91cfe5dd939547a9a Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 22:31:31 +0000 Subject: [PATCH 10/12] attempt to delete queue in case of failure, using try/except approach --- tasks/delete_queue_test.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 2eadf3bb4b78..10d896762089 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -15,7 +15,7 @@ import os import uuid -from google.cloud import tasks_v2 +from google.cloud import exceptions, tasks_v2 import pytest import delete_queue @@ -39,6 +39,13 @@ def test_queue(): yield q + try: + # Attempt to delete the queue in case the sample failed. + client.delete_queue(q.name) + except exceptions.NotFound: + # The queue was successfully deleted, nothing to do. + pass + def test_delete_queue(capsys, test_queue): delete_queue.delete_queue( From eca47d12ccb9f6d3d87609e1683a76fb7e2622af Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 10 Jun 2020 23:30:01 +0000 Subject: [PATCH 11/12] add print when NotFound is caught --- tasks/delete_queue_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 10d896762089..7f82a99eaa82 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -43,8 +43,8 @@ def test_queue(): # Attempt to delete the queue in case the sample failed. client.delete_queue(q.name) except exceptions.NotFound: - # The queue was successfully deleted, nothing to do. - pass + # The queue was already successfully deleted. + print('Queue already deleted successfully') def test_delete_queue(capsys, test_queue): From bd4b6a712fc479a270da91bd76912a7098a32be8 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Fri, 12 Jun 2020 20:36:03 +0000 Subject: [PATCH 12/12] fix import --- tasks/delete_queue_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py index 7f82a99eaa82..4e1acf403dac 100644 --- a/tasks/delete_queue_test.py +++ b/tasks/delete_queue_test.py @@ -15,7 +15,8 @@ import os import uuid -from google.cloud import exceptions, tasks_v2 +from google.api_core import exceptions +from google.cloud import tasks_v2 import pytest import delete_queue