Skip to content

Commit

Permalink
Merge pull request #54 from gruntwork-io/ssh-timeout
Browse files Browse the repository at this point in the history
Add SkipStageEnvVarSet method.
  • Loading branch information
brikis98 authored Feb 21, 2018
2 parents d88d289 + 1eb0246 commit e39943e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 8 additions & 4 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,25 @@ func createSshClientConfig(hostOptions *SshConnectionOptions) *ssh.ClientConfig
User: hostOptions.Username,
Auth: hostOptions.AuthMethods,
// Do not do a host key check, as Terratest is only used for testing, not prod
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
HostKeyCallback: NoOpHostKeyCallback,
// By default, Go does not impose a timeout, so a SSH connection attempt can hang for a LONG time.
Timeout: 10 * time.Second,
}
clientConfig.SetDefaults()
return clientConfig
}

// An ssh.HostKeyCallback that does nothing. Only use this when you're sure you don't want to check the host key at all
// (e.g., only for testing and non-production use cases).
func NoOpHostKeyCallback(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}

func createAuthMethodsForHost(host Host) ([]ssh.AuthMethod, error) {
signer, err := ssh.ParsePrivateKey([]byte(host.SshKeyPair.PrivateKey))
if err != nil {
return []ssh.AuthMethod{}, err
}

return []ssh.AuthMethod{ssh.PublicKeys(signer)}, nil
}
}
2 changes: 1 addition & 1 deletion ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ func testSshToPrivateHost(terratestOptions *terratest.TerratestOptions, resource
})

return err
}
}
17 changes: 16 additions & 1 deletion test-structure/test_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"log"
"github.com/gruntwork-io/terratest/files"
"fmt"
"strings"
)

const SKIP_STAGE_ENV_VAR_PREFIX = "SKIP_"

// Execute the given test stage (e.g., setup, teardown, validation) if an environment variable of the name
// `SKIP_<stageName>` (e.g., SKIP_teardown) is not set.
func RunTestStage(stageName string, logger *log.Logger, stage func()) {
envVarName := fmt.Sprintf("SKIP_%s", stageName)
envVarName := fmt.Sprintf("%s%s", SKIP_STAGE_ENV_VAR_PREFIX, stageName)
if os.Getenv(envVarName) == "" {
logger.Printf("The '%s' environment variable is not set, so executing stage '%s'.", envVarName, stageName)
stage()
Expand All @@ -24,6 +27,18 @@ func RunTestStage(stageName string, logger *log.Logger, stage func()) {
}
}

// Returns true if an environment variable is set instructing Terratest to skip a test stage. This can be an easy way
// to tell if the tests are running in a local dev environment vs a CI server.
func SkipStageEnvVarSet() bool {
for _, environmentVariable := range os.Environ() {
if strings.HasPrefix(environmentVariable, SKIP_STAGE_ENV_VAR_PREFIX) {
return true
}
}

return false
}

// Serialize and save TerratestOptions into the given folder. This allows you to create TerratestOptions during setup
// and to reuse that TerratestOptions later during validation and teardown.
func SaveTerratestOptions(t *testing.T, testFolder string, terratestOptions *terratest.TerratestOptions, logger *log.Logger) {
Expand Down

0 comments on commit e39943e

Please sign in to comment.