Skip to content

Commit f8064d6

Browse files
committed
Test overriding connection
1 parent 6d890fb commit f8064d6

File tree

5 files changed

+74
-27
lines changed

5 files changed

+74
-27
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ jobs:
8080
services:
8181
remotehost:
8282
image: ghcr.io/tenstad/remotehost:${{ github.sha }}
83+
remotehost2:
84+
image: ghcr.io/tenstad/remotehost:${{ github.sha }}
8385
container:
8486
image: golang:1.15
8587
steps:

GNUmakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ testacc:
88
docker network create remote
99
docker build -t remotehost tests
1010
docker run --rm -d --net remote --name remotehost remotehost
11+
docker run --rm -d --net remote --name remotehost2 remotehost
1112
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
1213
docker rm -f remotehost
14+
docker rm -f remotehost2
1315
docker network rm remote
Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
11
package provider
22

33
import (
4-
"fmt"
54
"regexp"
65
"testing"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9-
"golang.org/x/crypto/ssh"
108
)
119

1210
func TestAccDataSourceRemoteFile(t *testing.T) {
1311

1412
resource.UnitTest(t, resource.TestCase{
1513
PreCheck: func() {
1614
testAccPreCheck(t)
17-
sshClient, err := ssh.Dial("tcp", "remotehost:22", &ssh.ClientConfig{
18-
User: "root",
19-
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
20-
Auth: []ssh.AuthMethod{ssh.Password("password")},
21-
})
22-
if err != nil {
23-
panic(err)
24-
}
25-
session, err := sshClient.NewSession()
26-
if err != nil {
27-
panic(err)
28-
}
29-
30-
defer session.Close()
31-
32-
stdin, err := session.StdinPipe()
33-
if err != nil {
34-
panic(err)
35-
}
36-
go func() {
37-
stdin.Write([]byte("file-content"))
38-
stdin.Close()
39-
}()
40-
41-
session.Run(fmt.Sprintf("cat /dev/stdin | tee %s", "/tmp/bar.txt"))
15+
writeFileToHost("remotehost:22", "/tmp/bar.txt", "file-content")
4216
},
4317
ProviderFactories: providerFactories,
4418
Steps: []resource.TestStep{
@@ -53,6 +27,26 @@ func TestAccDataSourceRemoteFile(t *testing.T) {
5327
})
5428
}
5529

30+
func TestAccDataSourceRemoteFileOverridingDefaultConnection(t *testing.T) {
31+
32+
resource.UnitTest(t, resource.TestCase{
33+
PreCheck: func() {
34+
testAccPreCheck(t)
35+
writeFileToHost("remotehost2:22", "/tmp/bar.txt", "file-content")
36+
},
37+
ProviderFactories: providerFactories,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccDataSourceRemoteFileOverridingDefaultConnection,
41+
Check: resource.ComposeTestCheckFunc(
42+
resource.TestMatchResourceAttr(
43+
"data.remote_file.baz", "content", regexp.MustCompile("file-content")),
44+
),
45+
},
46+
},
47+
})
48+
}
49+
5650
const testAccDataSourceRemoteFile = `
5751
data "remote_file" "bar" {
5852
conn {
@@ -63,3 +57,16 @@ data "remote_file" "bar" {
6357
path = "/tmp/bar.txt"
6458
}
6559
`
60+
61+
const testAccDataSourceRemoteFileOverridingDefaultConnection = `
62+
data "remote_file" "baz" {
63+
provider = remotehost
64+
65+
conn {
66+
host = "remotehost2"
67+
user = "root"
68+
private_key_path = "../../tests/key"
69+
}
70+
path = "/tmp/bar.txt"
71+
}
72+
`

internal/provider/resource_remote_file_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestAccResourceRemoteFile(t *testing.T) {
2222
},
2323
})
2424
}
25+
2526
func TestAccResourceRemoteFileWithDefaultConnection(t *testing.T) {
2627
resource.UnitTest(t, resource.TestCase{
2728
PreCheck: func() { testAccPreCheck(t) },

internal/provider/utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/crypto/ssh"
7+
)
8+
9+
func writeFileToHost(host string, filename string, content string) {
10+
sshClient, err := ssh.Dial("tcp", host, &ssh.ClientConfig{
11+
User: "root",
12+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
13+
Auth: []ssh.AuthMethod{ssh.Password("password")},
14+
})
15+
if err != nil {
16+
panic(err)
17+
}
18+
19+
session, err := sshClient.NewSession()
20+
if err != nil {
21+
panic(err)
22+
}
23+
defer session.Close()
24+
25+
stdin, err := session.StdinPipe()
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
go func() {
31+
stdin.Write([]byte(content))
32+
stdin.Close()
33+
}()
34+
session.Run(fmt.Sprintf("cat /dev/stdin | tee %s", filename))
35+
}

0 commit comments

Comments
 (0)