Skip to content

Commit 161f926

Browse files
committed
Add unit tests for oslogin_ssh_key_expire_after parameter
- Enhance DriverMock to capture expiration time parameter - Add TestStepImportOSLoginSSHKey_withExpirationTime to verify expiration time is calculated and passed correctly - Add TestStepImportOSLoginSSHKey_withoutExpirationTime to verify expiration time is nil when not set - Tests follow existing test patterns in step_import_os_login_ssh_key_test.go
1 parent 60716fe commit 161f926

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

builder/googlecompute/step_import_os_login_ssh_key_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"encoding/hex"
1010
"os"
1111
"testing"
12+
"time"
1213

14+
"github.com/hashicorp/packer-plugin-googlecompute/lib/common"
1315
"github.com/hashicorp/packer-plugin-sdk/multistep"
1416
configsdk "github.com/hashicorp/packer-plugin-sdk/template/config"
1517
"google.golang.org/api/oauth2/v2"
@@ -309,3 +311,79 @@ func TestGetOSLoginUsername(t *testing.T) {
309311
})
310312
}
311313
}
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+
}

lib/common/driver_mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ type DriverMock struct {
124124
UploadToBucketData io.Reader
125125
UploadToBucketResult string
126126
UploadToBucketError error
127+
128+
ImportOSLoginSSHKeyUser string
129+
ImportOSLoginSSHKeyKey string
130+
ImportOSLoginSSHKeyExpirationTime *int64
127131
}
128132

129133
func (d *DriverMock) CreateImage(project string, imageSpec *compute.Image) (<-chan *Image, <-chan error) {
@@ -380,6 +384,9 @@ func (d *DriverMock) CreateOrResetWindowsPassword(instance, zone string, c *Wind
380384
}
381385

382386
func (d *DriverMock) ImportOSLoginSSHKey(user, key string, expirationTimeUsec *int64) (*oslogin.LoginProfile, error) {
387+
d.ImportOSLoginSSHKeyUser = user
388+
d.ImportOSLoginSSHKeyKey = key
389+
d.ImportOSLoginSSHKeyExpirationTime = expirationTimeUsec
383390
account := oslogin.PosixAccount{Primary: true, Username: "testing_packer_io"}
384391
profile := oslogin.LoginProfile{
385392
PosixAccounts: []*oslogin.PosixAccount{&account},

0 commit comments

Comments
 (0)