Skip to content

Commit d8251f0

Browse files
committed
add integration test
1 parent eda7b52 commit d8251f0

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

integration/docker_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration_test
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"net"
910
"os"
@@ -17,6 +18,7 @@ import (
1718
"github.com/stretchr/testify/require"
1819

1920
"github.com/coder/envbox/cli"
21+
"github.com/coder/envbox/dockerutil"
2022
"github.com/coder/envbox/integration/integrationtest"
2123
)
2224

@@ -318,19 +320,38 @@ func TestDocker(t *testing.T) {
318320
regKeyPath := filepath.Join(certDir, "registry_key.pem")
319321
integrationtest.WriteCertificate(t, dockerCert, regCertPath, regKeyPath)
320322

323+
username := "coder"
324+
password := "helloworld"
325+
htpasswdPath := integrationtest.GenerateRegistryAuth(t, dir, username, password)
326+
321327
// Start up the docker registry and push an image
322328
// to it that we can reference.
323329
image := integrationtest.RunLocalDockerRegistry(t, pool, integrationtest.RegistryConfig{
324330
HostCertPath: regCertPath,
325331
HostKeyPath: regKeyPath,
326332
Image: integrationtest.UbuntuImage,
327333
TLSPort: strconv.Itoa(registryAddr.Port),
334+
PasswordPath: htpasswdPath,
328335
})
329336

337+
type authConfigs struct {
338+
Auths map[string]dockerutil.AuthConfig `json:"auths"`
339+
}
340+
341+
auths := authConfigs{
342+
Auths: map[string]dockerutil.AuthConfig{
343+
image.Registry(): {Username: username, Password: password},
344+
},
345+
}
346+
347+
authStr, err := json.Marshal(auths)
348+
require.NoError(t, err)
349+
330350
envs := []string{
331351
integrationtest.EnvVar(cli.EnvAgentToken, "faketoken"),
332352
integrationtest.EnvVar(cli.EnvAgentURL, fmt.Sprintf("https://%s:%d", "host.docker.internal", coderAddr.Port)),
333353
integrationtest.EnvVar(cli.EnvExtraCertsPath, "/tmp/certs"),
354+
integrationtest.EnvVar(cli.EnvBoxPullImageSecretEnvVar, string(authStr)),
334355
}
335356

336357
// Run the envbox container.

integration/integrationtest/docker.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/ory/dockertest/v3"
2121
"github.com/ory/dockertest/v3/docker"
2222
"github.com/stretchr/testify/require"
23+
"golang.org/x/crypto/bcrypt"
2324
"golang.org/x/xerrors"
2425

2526
"github.com/coder/envbox/buildlog"
@@ -361,6 +362,7 @@ type RegistryConfig struct {
361362
HostKeyPath string
362363
TLSPort string
363364
Image string
365+
PasswordPath string
364366
}
365367

366368
type RegistryImage string
@@ -381,14 +383,31 @@ func RunLocalDockerRegistry(t testing.TB, pool *dockertest.Pool, conf RegistryCo
381383
keyPath = "/certs/key.pem"
382384
)
383385

384-
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
385-
Repository: registryImage,
386-
Tag: registryTag,
387-
Env: []string{
386+
var (
387+
envs = []string{
388+
EnvVar("REGISTRY_HTTP_ADDR", "0.0.0.0:443"),
389+
}
390+
)
391+
392+
if conf.HostCertPath != "" && conf.HostKeyPath != "" {
393+
envs = append(envs,
388394
EnvVar("REGISTRY_HTTP_TLS_CERTIFICATE", certPath),
389395
EnvVar("REGISTRY_HTTP_TLS_KEY", keyPath),
390-
EnvVar("REGISTRY_HTTP_ADDR", "0.0.0.0:443"),
391-
},
396+
)
397+
}
398+
399+
if conf.PasswordPath != "" {
400+
envs = append(envs,
401+
EnvVar("REGISTRY_AUTH", "htpasswd"),
402+
EnvVar("REGISTRY_AUTH_HTPASSWD_REALM", "Test Registry"),
403+
EnvVar("REGISTRY_AUTH_HTPASSWD_PATH", conf.PasswordPath),
404+
)
405+
}
406+
407+
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
408+
Repository: registryImage,
409+
Tag: registryTag,
410+
Env: envs,
392411
ExposedPorts: []string{"443/tcp"},
393412
}, func(host *docker.HostConfig) {
394413
host.Binds = []string{
@@ -516,3 +535,15 @@ func BindMount(src, dst string, ro bool) docker.HostMount {
516535
Type: "bind",
517536
}
518537
}
538+
539+
func GenerateRegistryAuth(t *testing.T, directory, username, password string) string {
540+
t.Helper()
541+
542+
p, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
543+
require.NoError(t, err)
544+
545+
authFile := filepath.Join(directory, "htpasswd")
546+
WriteFile(t, authFile, fmt.Sprintf("%s:%s", username, string(p)))
547+
548+
return authFile
549+
}

0 commit comments

Comments
 (0)