Skip to content

Commit 2afd6c9

Browse files
fix(oslogin): Fixing a flaky issue with OSLogin SSH tests (#8208)
* WIP: Triggering a new PR to have a stage for running tests and experimenting. * fix(ssh-test): Trying to fix #7277 * Import reordering * fix(compute-ssh): Another fix for #7277 * Changing debian from 9 to 11 * Fixing something. * Testing things. Co-authored-by: Vadym Matsishevskyi <[email protected]>
1 parent b868f40 commit 2afd6c9

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

compute/oslogin/service_account_ssh.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ def create_ssh_key(oslogin, account, private_key_file=None, expire_time=300):
8181
# This method sometimes failed to work causing issues like #7277
8282
# Maybe retrying it with some delay will make things better
8383
oslogin.users().importSshPublicKey(parent=account, body=body).execute()
84-
except RefreshError:
84+
except RefreshError as err:
8585
if attempt_no == 3:
86-
break
86+
raise err
8787
time.sleep(attempt_no)
8888
else:
8989
break
@@ -126,10 +126,18 @@ def main(cmd, project, instance=None, zone=None,
126126
# Create a new SSH key pair and associate it with the service account.
127127
private_key_file = create_ssh_key(oslogin, account)
128128

129-
# Using the OS Login API, get the POSIX user name from the login profile
129+
# Using the OS Login API, get the POSIX username from the login profile
130130
# for the service account.
131-
profile = oslogin.users().getLoginProfile(name=account).execute()
132-
username = profile.get('posixAccounts')[0].get('username')
131+
for attempt_no in range(1, 4):
132+
try:
133+
profile = oslogin.users().getLoginProfile(name=account).execute()
134+
except RefreshError as err:
135+
if attempt_no == 3:
136+
raise err
137+
time.sleep(attempt_no)
138+
else:
139+
username = profile.get('posixAccounts')[0].get('username')
140+
break
133141

134142
# Create the hostname of the target instance using the instance name,
135143
# the zone where the instance is located, and the project that owns the

compute/oslogin/service_account_ssh_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_main(capsys):
4444
project = os.environ['GOOGLE_CLOUD_PROJECT']
4545
test_id = 'oslogin-test-{id}'.format(id=str(random.randint(0, 1000000)))
4646
zone = 'us-east1-d'
47-
image_family = 'projects/debian-cloud/global/images/family/debian-9'
47+
image_family = 'projects/debian-cloud/global/images/family/debian-11'
4848
machine_type = 'zones/{zone}/machineTypes/f1-micro'.format(zone=zone)
4949
account_email = '{test_id}@{project}.iam.gserviceaccount.com'.format(
5050
test_id=test_id, project=project)
@@ -207,6 +207,9 @@ def setup_resources(
207207
operation=operation['name']).execute()['status'] != 'DONE':
208208
time.sleep(5)
209209

210+
# Wait for the OS of the instance to be ready to accept SSH connections
211+
time.sleep(10)
212+
210213
# Grant the service account osLogin access on the test instance.
211214
compute.instances().setIamPolicy(
212215
project=project,

0 commit comments

Comments
 (0)