From f8064d6949fe8c113f6b4ab5dab72fce36768f8a Mon Sep 17 00:00:00 2001 From: Amund Tenstad Date: Sun, 29 Aug 2021 13:45:52 +0200 Subject: [PATCH] Test overriding connection --- .github/workflows/test.yml | 2 + GNUmakefile | 2 + .../provider/data_source_remote_file_test.go | 61 +++++++++++-------- .../provider/resource_remote_file_test.go | 1 + internal/provider/utils.go | 35 +++++++++++ 5 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 internal/provider/utils.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c3bbd16..cb0f263 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,8 @@ jobs: services: remotehost: image: ghcr.io/tenstad/remotehost:${{ github.sha }} + remotehost2: + image: ghcr.io/tenstad/remotehost:${{ github.sha }} container: image: golang:1.15 steps: diff --git a/GNUmakefile b/GNUmakefile index 676d2f0..bce3b4a 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -8,6 +8,8 @@ testacc: docker network create remote docker build -t remotehost tests docker run --rm -d --net remote --name remotehost remotehost + docker run --rm -d --net remote --name remotehost2 remotehost docker run --rm --net remote -v $(PWD):/app --workdir /app -e "TF_ACC=1" -e "TF_ACC_TERRAFORM_VERSION=0.13.4" golang:1.15 go test ./... -v $(TESTARGS) -timeout 120m docker rm -f remotehost + docker rm -f remotehost2 docker network rm remote diff --git a/internal/provider/data_source_remote_file_test.go b/internal/provider/data_source_remote_file_test.go index 6e33808..7f789cc 100644 --- a/internal/provider/data_source_remote_file_test.go +++ b/internal/provider/data_source_remote_file_test.go @@ -1,12 +1,10 @@ package provider import ( - "fmt" "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "golang.org/x/crypto/ssh" ) func TestAccDataSourceRemoteFile(t *testing.T) { @@ -14,31 +12,7 @@ func TestAccDataSourceRemoteFile(t *testing.T) { resource.UnitTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - sshClient, err := ssh.Dial("tcp", "remotehost:22", &ssh.ClientConfig{ - User: "root", - HostKeyCallback: ssh.InsecureIgnoreHostKey(), - Auth: []ssh.AuthMethod{ssh.Password("password")}, - }) - if err != nil { - panic(err) - } - session, err := sshClient.NewSession() - if err != nil { - panic(err) - } - - defer session.Close() - - stdin, err := session.StdinPipe() - if err != nil { - panic(err) - } - go func() { - stdin.Write([]byte("file-content")) - stdin.Close() - }() - - session.Run(fmt.Sprintf("cat /dev/stdin | tee %s", "/tmp/bar.txt")) + writeFileToHost("remotehost:22", "/tmp/bar.txt", "file-content") }, ProviderFactories: providerFactories, Steps: []resource.TestStep{ @@ -53,6 +27,26 @@ func TestAccDataSourceRemoteFile(t *testing.T) { }) } +func TestAccDataSourceRemoteFileOverridingDefaultConnection(t *testing.T) { + + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + writeFileToHost("remotehost2:22", "/tmp/bar.txt", "file-content") + }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceRemoteFileOverridingDefaultConnection, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.remote_file.baz", "content", regexp.MustCompile("file-content")), + ), + }, + }, + }) +} + const testAccDataSourceRemoteFile = ` data "remote_file" "bar" { conn { @@ -63,3 +57,16 @@ data "remote_file" "bar" { path = "/tmp/bar.txt" } ` + +const testAccDataSourceRemoteFileOverridingDefaultConnection = ` +data "remote_file" "baz" { + provider = remotehost + + conn { + host = "remotehost2" + user = "root" + private_key_path = "../../tests/key" + } + path = "/tmp/bar.txt" +} +` diff --git a/internal/provider/resource_remote_file_test.go b/internal/provider/resource_remote_file_test.go index df936ae..faf505c 100644 --- a/internal/provider/resource_remote_file_test.go +++ b/internal/provider/resource_remote_file_test.go @@ -22,6 +22,7 @@ func TestAccResourceRemoteFile(t *testing.T) { }, }) } + func TestAccResourceRemoteFileWithDefaultConnection(t *testing.T) { resource.UnitTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/internal/provider/utils.go b/internal/provider/utils.go new file mode 100644 index 0000000..afa2f86 --- /dev/null +++ b/internal/provider/utils.go @@ -0,0 +1,35 @@ +package provider + +import ( + "fmt" + + "golang.org/x/crypto/ssh" +) + +func writeFileToHost(host string, filename string, content string) { + sshClient, err := ssh.Dial("tcp", host, &ssh.ClientConfig{ + User: "root", + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + Auth: []ssh.AuthMethod{ssh.Password("password")}, + }) + if err != nil { + panic(err) + } + + session, err := sshClient.NewSession() + if err != nil { + panic(err) + } + defer session.Close() + + stdin, err := session.StdinPipe() + if err != nil { + panic(err) + } + + go func() { + stdin.Write([]byte(content)) + stdin.Close() + }() + session.Run(fmt.Sprintf("cat /dev/stdin | tee %s", filename)) +}