2020-04-24 16:30:38 -04:00
|
|
|
package googlecompute
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-06 17:28:22 -04:00
|
|
|
"runtime"
|
2020-04-24 16:30:38 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
2020-04-24 16:30:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockTunnelDriver struct {
|
2020-07-07 18:37:45 -04:00
|
|
|
StopTunnelCalled bool
|
|
|
|
StartTunnelCalled bool
|
|
|
|
StartTunnelTimeout int
|
2020-04-24 16:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockTunnelDriver) StopTunnel() {
|
|
|
|
m.StopTunnelCalled = true
|
|
|
|
}
|
|
|
|
|
2020-07-07 18:37:45 -04:00
|
|
|
func (m *MockTunnelDriver) StartTunnel(_ context.Context, _ string, timeout int) error {
|
2020-04-24 16:30:38 -04:00
|
|
|
m.StartTunnelCalled = true
|
2020-07-07 18:37:45 -04:00
|
|
|
m.StartTunnelTimeout = timeout
|
2020-04-24 16:30:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestStepStartTunnel() *StepStartTunnel {
|
|
|
|
return &StepStartTunnel{
|
|
|
|
IAPConf: &IAPConfig{
|
|
|
|
IAP: true,
|
|
|
|
IAPLocalhostPort: 0,
|
|
|
|
IAPHashBang: "/bin/bash",
|
|
|
|
IAPExt: "",
|
|
|
|
},
|
|
|
|
CommConf: &communicator.Config{
|
|
|
|
SSH: communicator.SSH{
|
|
|
|
SSHPort: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AccountFile: "/path/to/account_file.json",
|
2020-05-06 17:28:22 -04:00
|
|
|
ProjectId: "fake-project-123",
|
2020-04-24 16:30:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepStartTunnel_CreateTempScript(t *testing.T) {
|
|
|
|
s := getTestStepStartTunnel()
|
|
|
|
|
|
|
|
args := []string{"compute", "start-iap-tunnel", "fakeinstance-12345",
|
2020-07-29 04:50:11 -04:00
|
|
|
"1234", "--local-host-port=localhost:8774", "--zone", "us-central-b",
|
|
|
|
"--project", "fake-project-123"}
|
2020-04-24 16:30:38 -04:00
|
|
|
|
|
|
|
scriptPath, err := s.createTempGcloudScript(args)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Shouldn't have error building script file.")
|
|
|
|
}
|
|
|
|
defer os.Remove(scriptPath)
|
|
|
|
|
|
|
|
f, err := ioutil.ReadFile(scriptPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't read created inventoryfile: %s", err)
|
|
|
|
}
|
2020-05-06 17:28:22 -04:00
|
|
|
|
2020-04-24 16:30:38 -04:00
|
|
|
expected := `#!/bin/bash
|
2020-05-06 17:28:22 -04:00
|
|
|
|
2020-04-24 16:30:38 -04:00
|
|
|
gcloud auth activate-service-account --key-file='/path/to/account_file.json'
|
2020-07-29 04:50:11 -04:00
|
|
|
gcloud compute start-iap-tunnel fakeinstance-12345 1234 --local-host-port=localhost:8774 --zone us-central-b --project fake-project-123
|
2020-04-24 16:30:38 -04:00
|
|
|
`
|
2020-05-06 17:28:22 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2020-05-06 18:18:21 -04:00
|
|
|
// in real life you'd not be passing a HashBang here, but GIGO.
|
|
|
|
expected = `#!/bin/bash
|
|
|
|
|
2020-05-06 18:01:40 -04:00
|
|
|
call gcloud auth activate-service-account --key-file "/path/to/account_file.json"
|
2020-07-29 04:50:11 -04:00
|
|
|
call gcloud compute start-iap-tunnel fakeinstance-12345 1234 --local-host-port=localhost:8774 --zone us-central-b --project fake-project-123
|
2020-05-06 17:28:22 -04:00
|
|
|
`
|
|
|
|
}
|
2020-04-24 16:30:38 -04:00
|
|
|
if fmt.Sprintf("%s", f) != expected {
|
|
|
|
t.Fatalf("script didn't match expected:\n\n expected: \n%s\n; recieved: \n%s\n", expected, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepStartTunnel_Cleanup(t *testing.T) {
|
|
|
|
// Check IAP true
|
|
|
|
s := getTestStepStartTunnel()
|
|
|
|
td := &MockTunnelDriver{}
|
|
|
|
s.tunnelDriver = td
|
|
|
|
|
|
|
|
state := testState(t)
|
|
|
|
s.Cleanup(state)
|
|
|
|
|
|
|
|
if !td.StopTunnelCalled {
|
|
|
|
t.Fatalf("Should have called StopTunnel, since IAP is true")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check IAP false
|
|
|
|
s = getTestStepStartTunnel()
|
|
|
|
td = &MockTunnelDriver{}
|
|
|
|
s.tunnelDriver = td
|
|
|
|
|
|
|
|
s.IAPConf.IAP = false
|
|
|
|
|
|
|
|
s.Cleanup(state)
|
|
|
|
|
|
|
|
if td.StopTunnelCalled {
|
|
|
|
t.Fatalf("Should not have called StopTunnel, since IAP is false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepStartTunnel_ConfigurePort_port_set_by_user(t *testing.T) {
|
|
|
|
s := getTestStepStartTunnel()
|
|
|
|
s.IAPConf.IAPLocalhostPort = 8447
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := s.ConfigureLocalHostPort(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Shouldn't have error detecting port")
|
|
|
|
}
|
|
|
|
if s.IAPConf.IAPLocalhostPort != 8447 {
|
|
|
|
t.Fatalf("Shouldn't have found new port; one was configured.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepStartTunnel_ConfigurePort_port_not_set_by_user(t *testing.T) {
|
|
|
|
s := getTestStepStartTunnel()
|
|
|
|
s.IAPConf.IAPLocalhostPort = 0
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := s.ConfigureLocalHostPort(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Shouldn't have error detecting port")
|
|
|
|
}
|
|
|
|
if s.IAPConf.IAPLocalhostPort == 0 {
|
|
|
|
t.Fatalf("Should have found new port; none was configured.")
|
|
|
|
}
|
|
|
|
}
|