-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_test.go
118 lines (106 loc) · 4.18 KB
/
registry_test.go
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
// 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"
"fmt"
"net/http"
"time"
"github.com/thediveo/morbyd/pull"
"github.com/thediveo/morbyd/push"
"github.com/thediveo/morbyd/run"
"github.com/thediveo/morbyd/session"
"github.com/thediveo/morbyd/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
magic = "deadbeef" // local registry authn; arbitrary, but non-empty
)
// 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() {
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("starting a local container registry")
Expect(sess.Run(ctx, "registry:2",
run.WithName("local-registry"),
run.WithPublishedPort(fmt.Sprintf("127.0.0.1:%d:5000", registryPort)),
run.WithAutoRemove())).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()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP status code %d", resp.StatusCode)
}
return nil
}).Within(5 * time.Second).ProbeEvery(250 * time.Millisecond).
Should(Succeed())
})
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 fake auth", func(ctx context.Context) {
sess := Successful(NewSession(ctx))
DeferCleanup(func(ctx context.Context) {
sess.Close(ctx)
})
By("pulling the canary image, 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.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, once without and then with dummy auth")
Expect(sess.PushImage(ctx, localCanaryImage,
push.WithOutput(timestamper.New(GinkgoWriter)))).NotTo(Succeed())
Expect(sess.PushImage(ctx, localCanaryImage,
push.WithRegistryAuth(magic),
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.WithOutput(timestamper.New(GinkgoWriter)))).To(Succeed())
Expect(sess.HasImage(ctx, localCanaryImage)).To(BeTrue())
})
})