func syncFileToRemoteServer(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair, sshUsername string, srcPath string, destPath string, chmod string) {
  publicInstanceIP := terraform.Output(t, terraformOptions, "public_instance_ip")
  host := ssh.Host{
    Hostname:    publicInstanceIP,
    SshKeyPair:  keyPair.KeyPair,
    SshUserName: sshUsername,
  }
  maxRetries := 15
  timeBetweenRetries, err := time.ParseDuration("5s")
  require.NoError(t, err)
  _, err = retry.DoWithRetryE(t, "Wait for the instance to be ready", maxRetries, timeBetweenRetries, func() (string, error){
    _, err := ssh.CheckSshCommandE(t, host, "whoami")
    if err != nil {
      return "", err
    }
    return "", nil
  })
  require.NoError(t, err)
  output, err := ssh.CheckSshCommandE(t, host,fmt.Sprintf("bash -c 'install -m 644 -D /dev/null \"%s\"'", destPath))
  require.NoError(t, err, output)
  f, err := os.Open(srcPath)
  require.NoError(t, err)
  reader := bufio.NewReader(f)
  content, err := ioutil.ReadAll(reader)
  require.NoError(t, err)
  encodedContent := base64.StdEncoding.EncodeToString(content)
  err = ssh.ScpFileToE(t, host, 0600, fmt.Sprintf("%s.b64", destPath), encodedContent)
  require.NoError(t, err)
  output, err = ssh.CheckSshCommandE(t, host, fmt.Sprintf("base64 -d \"%s.b64\" > \"%s\" && chmod \"%s\" \"%s\"", destPath, destPath, chmod, destPath))
  require.NoError(t, err, output)
}
syncFileToRemoteServer uses SCP to sync a file from source to destination. `destPath` can be absolute or relative to
the SSH user's home directory. It has to be in a directory that the SSH user is allowed to write to.
package test
import (
  "bufio"
  "encoding/base64"
  "fmt"
  "github.com/gruntwork-io/terratest/modules/random"
  "github.com/gruntwork-io/terratest/modules/retry"
  teststructure "github.com/gruntwork-io/terratest/modules/test-structure"
  "github.com/stretchr/testify/require"
  "io/ioutil"
  "os"
  "testing"
  "time"
  "github.com/gruntwork-io/terratest/modules/aws"
  "github.com/gruntwork-io/terratest/modules/ssh"
  "github.com/gruntwork-io/terratest/modules/terraform"
)
func teardown(t *testing.T, tmpFolder string) {
  keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)
  aws.DeleteEC2KeyPair(t, keyPair)
  terraformOptions := teststructure.LoadTerraformOptions(t, tmpFolder)
  terraform.Destroy(t, terraformOptions)
}
func setup(t *testing.T, tmpFolder string) {
  terraformOptions, keyPair, err := configureTerraformOptions(t, tmpFolder)
  require.NoError(t, err)
  teststructure.SaveTerraformOptions(t, tmpFolder, terraformOptions)
  teststructure.SaveEc2KeyPair(t, tmpFolder, keyPair)
  terraform.InitAndApply(t, terraformOptions)
}
func configureTerraformOptions(t *testing.T, tmpFolder string) (*terraform.Options, *aws.Ec2Keypair, error) {
  uniqueID := random.UniqueId()
  namespace := "zarf"
  stage := "terratest"
  name := fmt.Sprintf("e2e-%s", uniqueID)
  awsRegion, err := getAwsRegion()
  if err != nil {
    return nil, nil, err
  }
  instanceType := "t3a.large"
  keyPairName := fmt.Sprintf("%s-%s-%s", namespace, stage, name)
  keyPair := aws.CreateAndImportEC2KeyPair(t, awsRegion, keyPairName)
  terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
    TerraformDir: tmpFolder,
    Vars: map[string]interface{}{
      "aws_region":    awsRegion,
      "namespace":     namespace,
      "stage":         stage,
      "name":          name,
      "instance_type": instanceType,
      "key_pair_name": keyPairName,
    },
  })
  return terraformOptions, keyPair, nil
}