Skip to content

Commit a622eaf

Browse files
author
zhouhao
committed
add test to index
Signed-off-by: zhouhao <[email protected]>
1 parent 0140f7d commit a622eaf

File tree

2 files changed

+89
-32
lines changed

2 files changed

+89
-32
lines changed

image/image_test.go

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,54 @@ const (
9090
)
9191

9292
var (
93+
indexJSON = `{
94+
"schemaVersion": 2,
95+
"manifests": [
96+
{
97+
"mediaType": "application/vnd.oci.image.index.v1+json",
98+
"size": <index_size>,
99+
"digest": "<index_digest>",
100+
"annotations": {
101+
"org.opencontainers.ref.name": "v1.0"
102+
}
103+
},
104+
{
105+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
106+
"size": <manifest_size>,
107+
"digest": "<manifest_digest>",
108+
"platform": {
109+
"architecture": "ppc64le",
110+
"os": "linux"
111+
},
112+
"annotations": {
113+
"org.opencontainers.ref.name": "latest"
114+
}
115+
}
116+
],
117+
"annotations": {
118+
"com.example.index.revision": "r124356"
119+
}
120+
}
121+
`
93122
indexStr = `{
94123
"schemaVersion": 2,
95124
"manifests": [
96-
{
97-
"mediaType": "application/vnd.oci.image.index.v1+json",
98-
"size": <manifest_size>,
99-
"digest": "<manifest_digest>",
100-
"annotations": {
101-
"org.opencontainers.ref.name": "v1.0"
102-
}
103-
},
104125
{
105126
"mediaType": "application/vnd.oci.image.manifest.v1+json",
106127
"size": <manifest_size>,
107128
"digest": "<manifest_digest>",
108129
"platform": {
109130
"architecture": "ppc64le",
110131
"os": "linux"
111-
},
112-
"annotations": {
113-
"org.opencontainers.ref.name": "latest"
114132
}
115133
},
116134
{
117-
"mediaType": "application/xml",
135+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
118136
"size": <manifest_size>,
119137
"digest": "<manifest_digest>",
120-
"annotations": {
121-
"org.freedesktop.specifications.metainfo.version": "1.0",
122-
"org.freedesktop.specifications.metainfo.type": "AppStream"
138+
"platform": {
139+
"architecture": "amd64",
140+
"os": "linux"
123141
}
124142
}
125143
],
@@ -156,12 +174,14 @@ type tarContent struct {
156174
}
157175

158176
type imageLayout struct {
159-
rootDir string
160-
layout string
161-
ref string
162-
manifest string
163-
config string
164-
tarList []tarContent
177+
rootDir string
178+
layout string
179+
ref string
180+
manifest string
181+
index string
182+
config string
183+
indexjson string
184+
tarList []tarContent
165185
}
166186

167187
func TestValidateLayout(t *testing.T) {
@@ -172,11 +192,13 @@ func TestValidateLayout(t *testing.T) {
172192
defer os.RemoveAll(root)
173193

174194
il := imageLayout{
175-
rootDir: root,
176-
layout: layoutStr,
177-
ref: refTag,
178-
manifest: manifestStr,
179-
config: configStr,
195+
rootDir: root,
196+
layout: layoutStr,
197+
ref: refTag,
198+
manifest: manifestStr,
199+
index: indexStr,
200+
indexjson: indexJSON,
201+
config: configStr,
180202
tarList: []tarContent{
181203
{&tar.Header{Name: "test", Size: 4, Mode: 0600}, []byte("test")},
182204
},
@@ -192,6 +214,11 @@ func TestValidateLayout(t *testing.T) {
192214
if err != nil {
193215
t.Fatal(err)
194216
}
217+
218+
err = ValidateLayout(root, []string{"v1.0"}, nil)
219+
if err != nil {
220+
t.Fatal(err)
221+
}
195222
}
196223

197224
func createImageLayoutBundle(il imageLayout) error {
@@ -226,8 +253,22 @@ func createImageLayoutBundle(il imageLayout) error {
226253
if err != nil {
227254
return err
228255
}
256+
il.index = strings.Replace(il.index, "<manifest_digest>", string(desc.Digest), -1)
257+
il.index = strings.Replace(il.index, "<manifest_size>", strconv.FormatInt(desc.Size, 10), -1)
229258

230-
return createIndexFile(il.rootDir, desc)
259+
il.indexjson = strings.Replace(il.indexjson, "<manifest_digest>", string(desc.Digest), -1)
260+
il.indexjson = strings.Replace(il.indexjson, "<manifest_size>", strconv.FormatInt(desc.Size, 10), -1)
261+
262+
// create index blob file
263+
desc, err = createIndexFile(il.rootDir, il.index)
264+
if err != nil {
265+
return err
266+
}
267+
il.indexjson = strings.Replace(il.indexjson, "<index_digest>", string(desc.Digest), -1)
268+
il.indexjson = strings.Replace(il.indexjson, "<index_size>", strconv.FormatInt(desc.Size, 10), -1)
269+
270+
// create index.json file
271+
return createIndexJSON(il.rootDir, il.indexjson)
231272
}
232273

233274
func createLayoutFile(root string) error {
@@ -241,19 +282,34 @@ func createLayoutFile(root string) error {
241282
return err
242283
}
243284

244-
func createIndexFile(root string, mft v1.Descriptor) error {
285+
func createIndexJSON(root string, str string) error {
245286
indexpath := filepath.Join(root, "index.json")
246287
f, err := os.Create(indexpath)
247288
if err != nil {
248289
return err
249290
}
250291
defer f.Close()
251-
indexStr = strings.Replace(indexStr, "<manifest_digest>", string(mft.Digest), -1)
252-
indexStr = strings.Replace(indexStr, "<manifest_size>", strconv.FormatInt(mft.Size, 10), -1)
253-
_, err = io.Copy(f, bytes.NewBuffer([]byte(indexStr)))
292+
_, err = io.Copy(f, bytes.NewBuffer([]byte(str)))
293+
254294
return err
255295
}
256296

297+
func createIndexFile(root, str string) (v1.Descriptor, error) {
298+
name := filepath.Join(root, "blobs", "sha256", "test-index")
299+
f, err := os.Create(name)
300+
if err != nil {
301+
return v1.Descriptor{}, err
302+
}
303+
defer f.Close()
304+
305+
_, err = io.Copy(f, bytes.NewBuffer([]byte(str)))
306+
if err != nil {
307+
return v1.Descriptor{}, err
308+
}
309+
310+
return createHashedBlob(name)
311+
}
312+
257313
func createManifestFile(root, str string) (v1.Descriptor, error) {
258314
name := filepath.Join(root, "blobs", "sha256", "test-manifest")
259315
f, err := os.Create(name)

image/index.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030

3131
func findIndex(w walker, d *v1.Descriptor) (*v1.Index, error) {
3232
var index v1.Index
33+
ipath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex())
3334

3435
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
35-
if info.IsDir() || filepath.Clean(path) != indexPath {
36+
if info.IsDir() || filepath.Clean(path) != ipath {
3637
return nil
3738
}
3839

0 commit comments

Comments
 (0)