This repository has been archived by the owner on May 9, 2023. It is now read-only.
forked from seatgeek/docker-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror_test.go
143 lines (115 loc) · 3.7 KB
/
mirror_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
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
package main
import (
"strconv"
"testing"
"time"
docker "github.com/fsouza/go-dockerclient"
)
func TestGetSleepTime(t *testing.T) {
fakeNow := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
// Zero
result := getSleepTime(getTimeAsString(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)), fakeNow)
expected := 0 * time.Second
if result != expected {
t.Errorf("Expected %s got %s", expected, result)
}
// Default
result = getSleepTime(getTimeAsString(time.Date(2021, 1, 1, 0, 0, 10, 0, time.UTC)), fakeNow)
expected = 10 * time.Second
if result != expected {
t.Errorf("Expected %s got %s", expected, result)
}
// Random junk
result = getSleepTime("random-string-of-rubbish", fakeNow)
expected = 60 * time.Second
if result != expected {
t.Errorf("Expected %s got %s", expected, result)
}
// Negative
result = getSleepTime(getTimeAsString(time.Date(2020, 12, 30, 0, 0, 10, 0, time.UTC)), fakeNow)
expected = 0 * time.Second
if result != expected {
t.Errorf("Expected %s got %s", expected, result)
}
}
type ResponseContainer struct {
TagImageName string
TagImageOptions docker.TagImageOptions
PullImageOptions docker.PullImageOptions
PullImageAuthConfiguration docker.AuthConfiguration
PushImageOptions docker.PushImageOptions
PushImageAuthConfiguration docker.AuthConfiguration
RemoveImageName string
}
type TestDockerClient struct {
ResponseContainer *ResponseContainer
}
func (t *TestDockerClient) Info() (*docker.DockerInfo, error) {
return &docker.DockerInfo{}, nil
}
func (t *TestDockerClient) TagImage(name string, opts docker.TagImageOptions) error {
t.ResponseContainer.TagImageName = name
t.ResponseContainer.TagImageOptions = opts
return nil
}
func (t *TestDockerClient) PullImage(opts docker.PullImageOptions, authConfig docker.AuthConfiguration) error {
t.ResponseContainer.PullImageOptions = opts
t.ResponseContainer.PullImageAuthConfiguration = authConfig
return nil
}
func (t *TestDockerClient) PushImage(opts docker.PushImageOptions, auth docker.AuthConfiguration) error {
t.ResponseContainer.PushImageOptions = opts
t.ResponseContainer.PushImageAuthConfiguration = auth
return nil
}
func (t *TestDockerClient) RemoveImage(name string) error {
t.ResponseContainer.RemoveImageName = name
return nil
}
func CreateTestDockerClient(responseContainer *ResponseContainer) *TestDockerClient {
return &TestDockerClient{ResponseContainer: responseContainer}
}
func TestPullImage(t *testing.T) {
t.Run("tests to ensure that PrivateRegistry creates the proper repo name", func(t *testing.T) {
responseContainer := &ResponseContainer{}
var client DockerClient
client = CreateTestDockerClient(responseContainer)
repo := Repository{
PrivateRegistry: "private-registry-name",
Name: "elasticsearch",
Host: "hub.docker.com",
}
m := mirror{
dockerClient: &client,
}
m.setup(repo)
m.pullImage("latest")
got := responseContainer.PullImageOptions.Repository
want := "private-registry-name/elasticsearch"
if got != want {
t.Errorf("Expected %q, got %q", want, got)
}
})
t.Run("tests to ensure that without PrivateRegistry, a repo name is correct", func(t *testing.T) {
responseContainer := &ResponseContainer{}
var client DockerClient
client = CreateTestDockerClient(responseContainer)
repo := Repository{
Name: "elasticsearch",
Host: "hub.docker.com",
}
m := mirror{
dockerClient: &client,
}
m.setup(repo)
m.pullImage("latest")
got := responseContainer.PullImageOptions.Repository
want := "elasticsearch"
if got != want {
t.Errorf("Expected %q, got %q", want, got)
}
})
}
func getTimeAsString(date time.Time) string {
return strconv.FormatInt(date.Unix(), 10)
}