Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions iam/api-client/service_accounts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@
# limitations under the License.

import os
import random
import uuid

from googleapiclient.errors import HttpError

import service_accounts


def test_service_accounts(capsys):
project_id = os.environ['GCLOUD_PROJECT']
rand = str(random.randint(0, 1000))
name = 'python-test-' + rand
name = 'python-test-{}'.format(str(uuid.uuid4()).split('-')[0])

@gguuss gguuss Apr 28, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like a deterministic value based on machine ID / mac address would be ideal - are you just using UUID4 for a random value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Due to the length limitation, I needed to use only the first part, but it's still far better than random.randint(0, 1000).

email = name + '@' + project_id + '.iam.gserviceaccount.com'

service_accounts.create_service_account(
project_id, name, 'Py Test Account')
service_accounts.list_service_accounts(
project_id)
service_accounts.rename_service_account(
email, 'Updated Py Test Account')
service_accounts.disable_service_account(
email)
service_accounts.enable_service_account(
email)
service_accounts.delete_service_account(
email)
try:
service_accounts.create_service_account(
project_id, name, 'Py Test Account')
service_accounts.list_service_accounts(project_id)
service_accounts.rename_service_account(
email, 'Updated Py Test Account')
service_accounts.disable_service_account(email)
service_accounts.enable_service_account(email)
service_accounts.delete_service_account(email)
finally:
try:
service_accounts.delete_service_account(email)
except HttpError as e:
# When the service account doesn't exist, the service returns 403.
if '403' in str(e):
print("Ignoring 403 error upon cleanup.")
else:
raise