|
9 | 9 | "encoding/hex" |
10 | 10 | "os" |
11 | 11 | "testing" |
| 12 | + "time" |
12 | 13 |
|
| 14 | + "github.com/hashicorp/packer-plugin-googlecompute/lib/common" |
13 | 15 | "github.com/hashicorp/packer-plugin-sdk/multistep" |
14 | 16 | configsdk "github.com/hashicorp/packer-plugin-sdk/template/config" |
15 | 17 | "google.golang.org/api/oauth2/v2" |
@@ -309,3 +311,79 @@ func TestGetOSLoginUsername(t *testing.T) { |
309 | 311 | }) |
310 | 312 | } |
311 | 313 | } |
| 314 | + |
| 315 | +func TestStepImportOSLoginSSHKey_withExpirationTime(t *testing.T) { |
| 316 | + state := testState(t) |
| 317 | + fakeAccountEmail := "raffi-compute@developer.gserviceaccount.com" |
| 318 | + driver := state.Get("driver").(*common.DriverMock) |
| 319 | + step := &StepImportOSLoginSSHKey{ |
| 320 | + TokeninfoFunc: func() (*oauth2.Tokeninfo, error) { |
| 321 | + return &oauth2.Tokeninfo{Email: fakeAccountEmail}, nil |
| 322 | + }, |
| 323 | + } |
| 324 | + defer step.Cleanup(state) |
| 325 | + |
| 326 | + config := state.Get("config").(*Config) |
| 327 | + config.UseOSLogin = configsdk.TriTrue |
| 328 | + config.Comm.SSHPublicKey = []byte{'k', 'e', 'y'} |
| 329 | + config.OSLoginSSHKeyExpireAfter = 1 * time.Hour |
| 330 | + |
| 331 | + beforeTime := time.Now() |
| 332 | + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { |
| 333 | + t.Fatalf("bad action: %#v", action) |
| 334 | + } |
| 335 | + afterTime := time.Now() |
| 336 | + |
| 337 | + if driver.ImportOSLoginSSHKeyExpirationTime == nil { |
| 338 | + t.Fatal("expected expiration time to be set, but got nil") |
| 339 | + } |
| 340 | + |
| 341 | + expectedExpirationMin := beforeTime.Add(config.OSLoginSSHKeyExpireAfter).UnixMicro() |
| 342 | + expectedExpirationMax := afterTime.Add(config.OSLoginSSHKeyExpireAfter).UnixMicro() |
| 343 | + actualExpiration := *driver.ImportOSLoginSSHKeyExpirationTime |
| 344 | + |
| 345 | + if actualExpiration < expectedExpirationMin || actualExpiration > expectedExpirationMax { |
| 346 | + t.Errorf("expected expiration time to be between %d and %d, but got %d", expectedExpirationMin, expectedExpirationMax, actualExpiration) |
| 347 | + } |
| 348 | + |
| 349 | + if driver.ImportOSLoginSSHKeyUser != fakeAccountEmail { |
| 350 | + t.Errorf("expected user to be %q, but got %q", fakeAccountEmail, driver.ImportOSLoginSSHKeyUser) |
| 351 | + } |
| 352 | + |
| 353 | + if driver.ImportOSLoginSSHKeyKey != "key" { |
| 354 | + t.Errorf("expected key to be %q, but got %q", "key", driver.ImportOSLoginSSHKeyKey) |
| 355 | + } |
| 356 | +} |
| 357 | + |
| 358 | +func TestStepImportOSLoginSSHKey_withoutExpirationTime(t *testing.T) { |
| 359 | + state := testState(t) |
| 360 | + fakeAccountEmail := "raffi-compute@developer.gserviceaccount.com" |
| 361 | + driver := state.Get("driver").(*common.DriverMock) |
| 362 | + step := &StepImportOSLoginSSHKey{ |
| 363 | + TokeninfoFunc: func() (*oauth2.Tokeninfo, error) { |
| 364 | + return &oauth2.Tokeninfo{Email: fakeAccountEmail}, nil |
| 365 | + }, |
| 366 | + } |
| 367 | + defer step.Cleanup(state) |
| 368 | + |
| 369 | + config := state.Get("config").(*Config) |
| 370 | + config.UseOSLogin = configsdk.TriTrue |
| 371 | + config.Comm.SSHPublicKey = []byte{'k', 'e', 'y'} |
| 372 | + config.OSLoginSSHKeyExpireAfter = 0 // Not set |
| 373 | + |
| 374 | + if action := step.Run(context.Background(), state); action != multistep.ActionContinue { |
| 375 | + t.Fatalf("bad action: %#v", action) |
| 376 | + } |
| 377 | + |
| 378 | + if driver.ImportOSLoginSSHKeyExpirationTime != nil { |
| 379 | + t.Errorf("expected expiration time to be nil when not set, but got %v", *driver.ImportOSLoginSSHKeyExpirationTime) |
| 380 | + } |
| 381 | + |
| 382 | + if driver.ImportOSLoginSSHKeyUser != fakeAccountEmail { |
| 383 | + t.Errorf("expected user to be %q, but got %q", fakeAccountEmail, driver.ImportOSLoginSSHKeyUser) |
| 384 | + } |
| 385 | + |
| 386 | + if driver.ImportOSLoginSSHKeyKey != "key" { |
| 387 | + t.Errorf("expected key to be %q, but got %q", "key", driver.ImportOSLoginSSHKeyKey) |
| 388 | + } |
| 389 | +} |
0 commit comments