Skip to content

Commit

Permalink
Test overriding connection
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Aug 29, 2021
1 parent 6d890fb commit f8064d6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 34 additions & 27 deletions internal/provider/data_source_remote_file_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
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) {

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{
Expand All @@ -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 {
Expand All @@ -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"
}
`
1 change: 1 addition & 0 deletions internal/provider/resource_remote_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccResourceRemoteFile(t *testing.T) {
},
})
}

func TestAccResourceRemoteFileWithDefaultConnection(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit f8064d6

Please sign in to comment.