5
5
"context"
6
6
"encoding/json"
7
7
"fmt"
8
+ "io"
8
9
"os"
9
10
"path/filepath"
10
11
"runtime"
@@ -15,6 +16,7 @@ import (
15
16
16
17
"github.com/docker/docker/api/types"
17
18
"github.com/docker/docker/api/types/container"
19
+ "github.com/docker/docker/api/types/image"
18
20
"github.com/docker/docker/client"
19
21
"github.com/docker/docker/pkg/stdcopy"
20
22
"github.com/stretchr/testify/assert"
@@ -44,17 +46,14 @@ func TestIntegration(t *testing.T) {
44
46
ctx , cancel := context .WithTimeout (context .Background (), time .Duration (timeoutMins )* time .Minute )
45
47
t .Cleanup (cancel )
46
48
47
- // Given: we have an existing Coder deployment running locally
48
- ctrID := setup (ctx , t )
49
-
50
49
for _ , tt := range []struct {
51
50
// Name of the folder under `integration/` containing a test template
52
- templateName string
51
+ name string
53
52
// map of string to regex to be passed to assertOutput()
54
53
expectedOutput map [string ]string
55
54
}{
56
55
{
57
- templateName : "test-data-source" ,
56
+ name : "test-data-source" ,
58
57
expectedOutput : map [string ]string {
59
58
"provisioner.arch" : runtime .GOARCH ,
60
59
"provisioner.id" : `[a-zA-Z0-9-]+` ,
@@ -82,20 +81,22 @@ func TestIntegration(t *testing.T) {
82
81
"workspace_owner.name" : `testing` ,
83
82
"workspace_owner.oidc_access_token" : `^$` , // TODO: test OIDC integration
84
83
"workspace_owner.session_token" : `.+` ,
85
- "workspace_owner.ssh_private_key" : `^$` , // Depends on coder/coder#13366
86
- "workspace_owner.ssh_public_key" : `^ $` , // Depends on coder/coder#13366
84
+ "workspace_owner.ssh_private_key" : `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$` ,
85
+ "workspace_owner.ssh_public_key" : `(?s)^ssh-ed25519.+ $` ,
87
86
},
88
87
},
89
88
} {
90
- t .Run (tt .templateName , func (t * testing.T ) {
89
+ t .Run (tt .name , func (t * testing.T ) {
90
+ // Given: we have an existing Coder deployment running locally
91
+ ctrID := setup (ctx , t , tt .name )
91
92
// Import named template
92
- _ , rc := execContainer (ctx , t , ctrID , fmt .Sprintf (`coder templates push %s --directory /src/integration/%s --var output_path=/tmp/%s.json --yes` , tt .templateName , tt .templateName , tt .templateName ))
93
+ _ , rc := execContainer (ctx , t , ctrID , fmt .Sprintf (`coder templates push %s --directory /src/integration/%s --var output_path=/tmp/%s.json --yes` , tt .name , tt .name , tt .name ))
93
94
require .Equal (t , 0 , rc )
94
95
// Create a workspace
95
- _ , rc = execContainer (ctx , t , ctrID , fmt .Sprintf (`coder create %s -t %s --yes` , tt .templateName , tt .templateName ))
96
+ _ , rc = execContainer (ctx , t , ctrID , fmt .Sprintf (`coder create %s -t %s --yes` , tt .name , tt .name ))
96
97
require .Equal (t , 0 , rc )
97
98
// Fetch the output created by the template
98
- out , rc := execContainer (ctx , t , ctrID , fmt .Sprintf (`cat /tmp/%s.json` , tt .templateName ))
99
+ out , rc := execContainer (ctx , t , ctrID , fmt .Sprintf (`cat /tmp/%s.json` , tt .name ))
99
100
require .Equal (t , 0 , rc )
100
101
actual := make (map [string ]string )
101
102
require .NoError (t , json .NewDecoder (strings .NewReader (out )).Decode (& actual ))
@@ -104,7 +105,7 @@ func TestIntegration(t *testing.T) {
104
105
}
105
106
}
106
107
107
- func setup (ctx context.Context , t * testing.T ) string {
108
+ func setup (ctx context.Context , t * testing.T , name string ) string {
108
109
var (
109
110
// For this test to work, we pass in a custom terraformrc to use
110
111
// the locally built version of the provider.
@@ -148,9 +149,17 @@ func setup(ctx context.Context, t *testing.T) string {
148
149
require .NoError (t , err , "get abs path of parent" )
149
150
t .Logf ("src path is %s\n " , srcPath )
150
151
152
+ // Ensure the image is available locally.
153
+ refStr := coderImg + ":" + coderVersion
154
+ t .Logf ("ensuring image %q" , refStr )
155
+ resp , err := cli .ImagePull (ctx , refStr , image.PullOptions {})
156
+ require .NoError (t , err )
157
+ _ , err = io .ReadAll (resp )
158
+ require .NoError (t , err )
159
+
151
160
// Stand up a temporary Coder instance
152
161
ctr , err := cli .ContainerCreate (ctx , & container.Config {
153
- Image : coderImg + ":" + coderVersion ,
162
+ Image : refStr ,
154
163
Env : []string {
155
164
"CODER_ACCESS_URL=" + localURL , // Set explicitly to avoid creating try.coder.app URLs.
156
165
"CODER_IN_MEMORY=true" , // We don't necessarily care about real persistence here.
@@ -163,7 +172,7 @@ func setup(ctx context.Context, t *testing.T) string {
163
172
tfrcPath + ":/tmp/integration.tfrc" , // Custom tfrc from above.
164
173
srcPath + ":/src" , // Bind-mount in the repo with the built binary and templates.
165
174
},
166
- }, nil , nil , "" )
175
+ }, nil , nil , "terraform-provider-coder-integration-" + name )
167
176
require .NoError (t , err , "create test deployment" )
168
177
169
178
t .Logf ("created container %s\n " , ctr .ID )
0 commit comments