This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
/
pull_fixture.go
140 lines (125 loc) · 4.1 KB
/
pull_fixture.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
package registry
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/uber/makisu/lib/context"
"github.com/uber/makisu/lib/docker/image"
"github.com/uber/makisu/lib/utils/testutil"
)
const (
_testFileDirAlpine = "../../testdata/files/alpine"
_testFileDirAlpineDup = "../../testdata/files/alpine_dup"
)
// PullClientFixture returns a new registry client fixture that can handle image
// pull requests using a local alpine test image.
func PullClientFixtureWithAlpine(ctx *context.BuildContext) (*DockerRegistryClient, error) {
return PullClientFixture(ctx,
filepath.Join(_testFileDirAlpine, "test_distribution_manifest"),
filepath.Join(_testFileDirAlpine, "test_image_config"),
filepath.Join(_testFileDirAlpine, "test_layer.tar"))
}
// PullClientFixture returns a new registry client fixture that can handle image
// pull requests using a local alpine test image that contains duplicate layers.
func PullClientFixtureWithAlpineDup(ctx *context.BuildContext) (*DockerRegistryClient, error) {
return PullClientFixture(ctx,
filepath.Join(_testFileDirAlpineDup, "test_distribution_manifest"),
filepath.Join(_testFileDirAlpine, "test_image_config"),
filepath.Join(_testFileDirAlpine, "test_layer.tar"))
}
// PullClientFixture returns a new registry client fixture that can handle image
// pull requests.
func PullClientFixture(
ctx *context.BuildContext, manifestPath, imageConfigPath, layerTarPath string,
) (*DockerRegistryClient, error) {
imageName := image.MustParseName(
fmt.Sprintf("localhost:5055/%s:%s", testutil.SampleImageRepoName, testutil.SampleImageTag))
cli := &http.Client{
Transport: pullTransportFixture{
imageName: imageName,
manifestPath: manifestPath,
imageConfigPath: imageConfigPath,
layerTarPath: layerTarPath,
},
}
c := NewWithClient(ctx.ImageStore, imageName.GetRegistry(), imageName.GetRepository(), cli)
c.config.Security.TLS.Client.Disabled = true
return c, nil
}
type pullTransportFixture struct {
imageName image.Name
manifestPath string
imageConfigPath string
layerTarPath string
}
func (t pullTransportFixture) manifestResponse() (*http.Response, error) {
manifest, err := os.Open(t.manifestPath)
if err != nil {
return nil, err
}
header := make(http.Header)
header.Add("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
return &http.Response{
StatusCode: http.StatusOK,
Body: manifest,
Header: header,
}, nil
}
func (t pullTransportFixture) imageConfigResponse() (*http.Response, error) {
imageConfig, err := os.Open(t.imageConfigPath)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: imageConfig,
Header: make(http.Header),
}, nil
}
func (t pullTransportFixture) layerResponse() (*http.Response, error) {
layerTar, err := os.Open(t.layerTarPath)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: layerTar,
Header: make(http.Header),
}, nil
}
func (t pullTransportFixture) RoundTrip(r *http.Request) (*http.Response, error) {
repoURL := fmt.Sprintf(
"http://%s/v2/%s", t.imageName.GetRegistry(), t.imageName.GetRepository())
manifestURL := fmt.Sprintf(
"%s/manifests/%s", repoURL, t.imageName.GetTag())
imageConfigURL := repoURL + "/blobs/sha256:" + testutil.SampleImageConfigDigest
layerTarURL := repoURL + "/blobs/sha256:" + testutil.SampleLayerTarDigest
url := r.URL.String()
if r.Method == "HEAD" {
if url == manifestURL || url == imageConfigURL || url == layerTarURL {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Header: make(http.Header),
}, nil
}
return &http.Response{
StatusCode: http.StatusNotFound,
Header: make(http.Header),
}, nil
}
if r.URL.String() == manifestURL {
return t.manifestResponse()
} else if r.URL.String() == imageConfigURL {
return t.imageConfigResponse()
} else if r.URL.String() == layerTarURL {
return t.layerResponse()
}
return &http.Response{
StatusCode: http.StatusNotFound,
Header: make(http.Header),
}, nil
}