@@ -23,6 +23,7 @@ import (
23
23
"io"
24
24
"io/ioutil"
25
25
"math/rand"
26
+ "net"
26
27
"os"
27
28
"path/filepath"
28
29
"testing"
@@ -39,7 +40,6 @@ import (
39
40
"github.com/fluxcd/pkg/runtime/controller"
40
41
"github.com/fluxcd/pkg/runtime/testenv"
41
42
"github.com/fluxcd/pkg/testserver"
42
- "github.com/phayes/freeport"
43
43
44
44
"github.com/distribution/distribution/v3/configuration"
45
45
dockerRegistry "github.com/distribution/distribution/v3/registry"
@@ -94,68 +94,70 @@ var (
94
94
95
95
var (
96
96
testRegistryClient * registry.Client
97
- testRegistryserver * RegistryClientTestServer
97
+ testRegistryserver * registryClientTestServer
98
98
)
99
99
100
100
var (
101
- testWorkspaceDir = "registry-test"
102
- testHtpasswdFileBasename = "authtest.htpasswd"
103
- testUsername = "myuser"
104
- testPassword = "mypass"
101
+ testRegistryWorkspaceDir = "/tmp/ registry-test"
102
+ testRegistryHtpasswdFileBasename = "authtest.htpasswd"
103
+ testRegistryUsername = "myuser"
104
+ testRegistryPassword = "mypass"
105
105
)
106
106
107
107
func init () {
108
108
rand .Seed (time .Now ().UnixNano ())
109
109
}
110
110
111
- type RegistryClientTestServer struct {
112
- Out io.Writer
113
- DockerRegistryHost string
114
- WorkspaceDir string
115
- RegistryClient * registry.Client
111
+ type registryClientTestServer struct {
112
+ out io.Writer
113
+ dockerRegistryHost string
114
+ workspaceDir string
115
+ registryClient * registry.Client
116
116
}
117
117
118
- func SetupServer (server * RegistryClientTestServer ) string {
118
+ func setupRegistryServer (ctx context.Context ) (* registryClientTestServer , error ) {
119
+ server := & registryClientTestServer {}
119
120
// Create a temporary workspace directory for the registry
120
- server .WorkspaceDir = testWorkspaceDir
121
- os .RemoveAll (server .WorkspaceDir )
122
- err := os .Mkdir (server .WorkspaceDir , 0700 )
121
+ server .workspaceDir = testRegistryWorkspaceDir
122
+ os .RemoveAll (server .workspaceDir )
123
+ err := os .Mkdir (server .workspaceDir , 0o700 )
123
124
if err != nil {
124
- panic ( fmt .Sprintf ("failed to create workspace directory: %s" , err ) )
125
+ return nil , fmt .Errorf ("failed to create workspace directory: %s" , err )
125
126
}
126
127
127
128
var out bytes.Buffer
128
- server .Out = & out
129
+ server .out = & out
129
130
130
131
// init test client
131
- server .RegistryClient , err = registry .NewClient (
132
+ server .registryClient , err = registry .NewClient (
132
133
registry .ClientOptDebug (true ),
133
- registry .ClientOptWriter (server .Out ),
134
+ registry .ClientOptWriter (server .out ),
134
135
)
135
136
if err != nil {
136
- panic ( fmt .Sprintf ("failed to create registry client: %s" , err ) )
137
+ return nil , fmt .Errorf ("failed to create registry client: %s" , err )
137
138
}
138
139
139
140
// create htpasswd file (w BCrypt, which is required)
140
- pwBytes , err := bcrypt .GenerateFromPassword ([]byte (testPassword ), bcrypt .DefaultCost )
141
+ pwBytes , err := bcrypt .GenerateFromPassword ([]byte (testRegistryPassword ), bcrypt .DefaultCost )
141
142
if err != nil {
142
- panic ( fmt .Sprintf ("failed to generate password: %s" , err ) )
143
+ return nil , fmt .Errorf ("failed to generate password: %s" , err )
143
144
}
144
145
145
- htpasswdPath := filepath .Join (testWorkspaceDir , testHtpasswdFileBasename )
146
- err = ioutil .WriteFile (htpasswdPath , []byte (fmt .Sprintf ("%s:%s\n " , testUsername , string (pwBytes ))), 0644 )
146
+ htpasswdPath := filepath .Join (testRegistryWorkspaceDir , testRegistryHtpasswdFileBasename )
147
+ err = ioutil .WriteFile (htpasswdPath , []byte (fmt .Sprintf ("%s:%s\n " , testRegistryUsername , string (pwBytes ))), 0644 )
147
148
if err != nil {
148
- panic ( fmt .Sprintf ("failed to create htpasswd file: %s" , err ) )
149
+ return nil , fmt .Errorf ("failed to create htpasswd file: %s" , err )
149
150
}
150
151
151
152
// Registry config
152
153
config := & configuration.Configuration {}
153
- port , err := freeport . GetFreePort ( )
154
+ l , err := net . Listen ( "tcp" , ":0" )
154
155
if err != nil {
155
- panic ( fmt .Sprintf ("failed to get free port: %s" , err ) )
156
+ return nil , fmt .Errorf ("failed to get free port: %s" , err )
156
157
}
158
+ port := l .Addr ().(* net.TCPAddr ).Port
157
159
158
- server .DockerRegistryHost = fmt .Sprintf ("localhost:%d" , port )
160
+ server .dockerRegistryHost = fmt .Sprintf ("localhost:%d" , port )
159
161
config .HTTP .Addr = fmt .Sprintf ("127.0.0.1:%d" , port )
160
162
config .HTTP .DrainTimeout = time .Duration (10 ) * time .Second
161
163
config .Storage = map [string ]configuration.Parameters {"inmemory" : map [string ]interface {}{}}
@@ -165,15 +167,15 @@ func SetupServer(server *RegistryClientTestServer) string {
165
167
"path" : htpasswdPath ,
166
168
},
167
169
}
168
- dockerRegistry , err := dockerRegistry .NewRegistry (context . Background () , config )
170
+ dockerRegistry , err := dockerRegistry .NewRegistry (ctx , config )
169
171
if err != nil {
170
- panic ( fmt .Sprintf ("failed to create docker registry: %s" , err ) )
172
+ return nil , fmt .Errorf ("failed to create docker registry: %s" , err )
171
173
}
172
174
173
175
// Start Docker registry
174
176
go dockerRegistry .ListenAndServe ()
175
177
176
- return server . WorkspaceDir
178
+ return server , nil
177
179
}
178
180
179
181
func TestMain (m * testing.M ) {
@@ -198,8 +200,10 @@ func TestMain(m *testing.M) {
198
200
199
201
testMetricsH = controller .MustMakeMetrics (testEnv )
200
202
201
- testRegistryserver = & RegistryClientTestServer {}
202
- registryWorkspaceDir := SetupServer (testRegistryserver )
203
+ testRegistryserver , err = setupRegistryServer (ctx )
204
+ if err != nil {
205
+ panic (fmt .Sprintf ("Failed to create a test registry server: %v" , err ))
206
+ }
203
207
204
208
testRegistryClient , err = registry .NewClient (registry .ClientOptWriter (os .Stdout ))
205
209
if err != nil {
@@ -280,7 +284,7 @@ func TestMain(m *testing.M) {
280
284
panic (fmt .Sprintf ("Failed to remove storage server dir: %v" , err ))
281
285
}
282
286
283
- if err := os .RemoveAll (registryWorkspaceDir ); err != nil {
287
+ if err := os .RemoveAll (testRegistryserver . workspaceDir ); err != nil {
284
288
panic (fmt .Sprintf ("Failed to remove registry workspace dir: %v" , err ))
285
289
}
286
290
0 commit comments