Repository navigation
Expand file tree
/
Copy pathregistry_test.go
More file actions
161 lines (138 loc) · 5.37 KB
/
Copy pathregistry_test.go
File metadata and controls
161 lines (138 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2025 Harald Albrecht.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package morbyd
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/moby/moby/api/types/registry"
"golang.org/x/crypto/bcrypt"
"github.com/thediveo/morbyd/v2/pull"
"github.com/thediveo/morbyd/v2/push"
"github.com/thediveo/morbyd/v2/run"
"github.com/thediveo/morbyd/v2/session"
"github.com/thediveo/morbyd/v2/timestamper"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gleak"
. "github.com/thediveo/success"
)
const (
registryPort = 5999 // port where we expose our local registry on loopback.
originalImage = "busybox:latest" // the upstream original image to get from the Docker registry.
canaryImage = "morbyd-busybox:weirdest" // the tag we'll use in the tests
user = "silly-user"
password = "silly-password"
)
// full image reference to our local registry-located testing image
var localCanaryImage = fmt.Sprintf("127.0.0.1:%d/%s", registryPort, canaryImage)
var _ = Describe("given a (local) registry", Ordered, Serial, func() {
var magicauth string
BeforeAll(func(ctx context.Context) {
sess := Successful(NewSession(ctx,
session.WithAutoCleaning("test=morbyd.registry")))
DeferCleanup(func(ctx context.Context) {
By("removing the local container registry")
sess.Close(ctx)
})
By("creating a silly htpasswd")
hash := Successful(bcrypt.GenerateFromPassword([]byte(password), 10))
tmpDir := Successful(os.MkdirTemp("", "registry-silly-*"))
DeferCleanup(os.RemoveAll, tmpDir)
htpasswdPath := filepath.Join(tmpDir, "htpasswd")
Expect(os.WriteFile(htpasswdPath,
fmt.Appendf(nil, "%s:%s\n", user, hash),
0644)).To(Succeed())
By("starting a local container registry")
Expect(sess.Run(ctx, "registry:3",
run.WithName("local-registry"),
run.WithPublishedPort(fmt.Sprintf("127.0.0.1:%d:5000", registryPort)),
run.WithVolume(htpasswdPath+":/auth/htpasswd:ro"),
run.WithEnvVars("REGISTRY_LOG_LEVEL=info",
"REGISTRY_AUTH=htpasswd",
"REGISTRY_AUTH_HTPASSWD_REALM=local-registry",
"REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd",
"REGISTRY_HTTP_SECRET=not-secret"),
run.WithAutoRemove(),
run.WithCombinedOutput(GinkgoWriter),
)).Error().NotTo(HaveOccurred())
By("waiting for the registry to become operational")
Eventually(func() error {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/v2/", registryPort))
if err != nil {
return err
}
defer resp.Body.Close() //nolint:errcheck // any error is irrelevant at this point
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusUnauthorized {
return fmt.Errorf("HTTP status code %d", resp.StatusCode)
}
return nil
}).Within(5 * time.Second).ProbeEvery(250 * time.Millisecond).
Should(Succeed())
magicauth = base64.URLEncoding.EncodeToString(
Successful(json.Marshal(registry.AuthConfig{
Username: user,
Password: password,
})))
})
BeforeEach(func(ctx context.Context) {
goodgos := Goroutines()
DeferCleanup(func() {
Eventually(Goroutines).Within(2 * time.Second).ProbeEvery(250 * time.Millisecond).
ShouldNot(HaveLeaked(goodgos))
})
})
It("pushes an image, needing auth", func(ctx context.Context) {
sess := Successful(NewSession(ctx))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
By("pulling the canary image into the local Docker, if not already available")
// normal PullImage will always first check instead of skipping
// immediately, so we need to check explicitly before pulling.
if !Successful(sess.HasImage(ctx, originalImage)) {
Expect(sess.PullImage(ctx,
originalImage,
//pull.WithRegistryAuth(magicauth),
pull.WithOutput(timestamper.New(GinkgoWriter)))).To(Succeed())
}
By("tagging the canary image for local registry")
Expect(sess.TagImage(ctx, originalImage, localCanaryImage)).To(Succeed())
By("pushing the canary image into the local registry, without auth")
Expect(sess.PushImage(ctx, localCanaryImage,
push.WithOutput(timestamper.New(GinkgoWriter)))).NotTo(Succeed())
By("pushing the canary image into the local registry, with silly auth")
Expect(sess.PushImage(ctx, localCanaryImage,
push.WithRegistryAuth(magicauth),
push.WithOutput(timestamper.New(GinkgoWriter)))).To(Succeed())
})
It("pulls an image without auth", func(ctx context.Context) {
sess := Successful(NewSession(ctx))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
By("ensuring the image isn't available locally (anymore)")
Expect(sess.RemoveImage(ctx, localCanaryImage)).Error().NotTo(HaveOccurred())
By("pulling the image")
Expect(sess.PullImage(ctx, localCanaryImage,
pull.WithRegistryAuth(magicauth),
pull.WithOutput(timestamper.New(GinkgoWriter)))).To(Succeed())
Expect(sess.HasImage(ctx, localCanaryImage)).To(BeTrue())
})
})