@@ -6,19 +6,16 @@ package provider
6
6
import (
7
7
"bufio"
8
8
"context"
9
- "fmt"
10
9
"io"
11
- "net/http/httptest"
12
- "net/url"
13
10
"os"
14
11
"path/filepath"
15
12
"slices"
16
13
"strings"
17
14
"testing"
18
15
19
- "github.com/google/go-containerregistry/pkg/registry"
20
16
"github.com/hashicorp/terraform-plugin-framework/providerserver"
21
17
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
18
+ "github.com/mafredri/terraform-provider-envbuilder/testutil/registrytest"
22
19
23
20
"github.com/docker/docker/api/types/container"
24
21
"github.com/docker/docker/api/types/image"
@@ -60,16 +57,16 @@ func setup(t testing.TB, files map[string]string) testDependencies {
60
57
// TODO: envbuilder creates /.envbuilder/bin/envbuilder owned by root:root which we are unable to clean up.
61
58
// This causes tests to fail.
62
59
repoDir := t .TempDir ()
63
- cacheRepo := runLocalRegistry (t )
60
+ regDir := t .TempDir ()
61
+ reg := registrytest .New (t , regDir )
64
62
writeFiles (t , files , repoDir )
65
63
return testDependencies {
66
64
BuilderImage : envbuilderImageRef ,
67
- CacheRepo : cacheRepo ,
65
+ CacheRepo : reg + "/test" ,
68
66
RepoDir : repoDir ,
69
67
}
70
68
}
71
69
72
-
73
70
func seedCache (ctx context.Context , t testing.TB , deps testDependencies ) {
74
71
cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
75
72
require .NoError (t , err , "init docker client" )
@@ -92,7 +89,7 @@ func seedCache(ctx context.Context, t testing.TB, deps testDependencies) {
92
89
testContainerLabel : "true" ,
93
90
}}, & container.HostConfig {
94
91
NetworkMode : container .NetworkMode ("host" ),
95
- Binds : []string {deps .RepoDir + ":" + deps .RepoDir },
92
+ Binds : []string {deps .RepoDir + ":" + deps .RepoDir },
96
93
}, nil , nil , "" )
97
94
require .NoError (t , err , "failed to run envbuilder to seed cache" )
98
95
t .Cleanup (func () {
@@ -150,17 +147,6 @@ func writeFiles(t testing.TB, files map[string]string, destPath string) {
150
147
}
151
148
}
152
149
153
- func runLocalRegistry (t testing.TB ) string {
154
- t .Helper ()
155
- tempDir := t .TempDir ()
156
- regHandler := registry .New (registry .WithBlobHandler (registry .NewDiskBlobHandler (tempDir )))
157
- regSrv := httptest .NewServer (regHandler )
158
- t .Cleanup (func () { regSrv .Close () })
159
- regSrvURL , err := url .Parse (regSrv .URL )
160
- require .NoError (t , err )
161
- return fmt .Sprintf ("localhost:%s/test" , regSrvURL .Port ())
162
- }
163
-
164
150
func ensureImage (ctx context.Context , t testing.TB , cli * client.Client , ref string ) {
165
151
t .Helper ()
166
152
0 commit comments