Skip to content

Commit 428027f

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

File tree

2 files changed

+90
-34
lines changed

2 files changed

+90
-34
lines changed

image/image_test.go

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
)
3232

3333
const (
34-
refTag = "latest"
3534
layoutStr = `{"imageLayoutVersion": "1.0.0"}`
3635

3736
configStr = `{
@@ -90,36 +89,59 @@ const (
9089
)
9190

9291
var (
92+
refTag = []string{
93+
"latest",
94+
"v1.0",
95+
}
96+
97+
indexJSON = `{
98+
"schemaVersion": 2,
99+
"manifests": [
100+
{
101+
"mediaType": "application/vnd.oci.image.index.v1+json",
102+
"size": <index_size>,
103+
"digest": "<index_digest>",
104+
"annotations": {
105+
"org.opencontainers.ref.name": "v1.0"
106+
}
107+
},
108+
{
109+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
110+
"size": <manifest_size>,
111+
"digest": "<manifest_digest>",
112+
"platform": {
113+
"architecture": "ppc64le",
114+
"os": "linux"
115+
},
116+
"annotations": {
117+
"org.opencontainers.ref.name": "latest"
118+
}
119+
}
120+
],
121+
"annotations": {
122+
"com.example.index.revision": "r124356"
123+
}
124+
}
125+
`
93126
indexStr = `{
94127
"schemaVersion": 2,
95128
"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-
},
104129
{
105130
"mediaType": "application/vnd.oci.image.manifest.v1+json",
106131
"size": <manifest_size>,
107132
"digest": "<manifest_digest>",
108133
"platform": {
109134
"architecture": "ppc64le",
110135
"os": "linux"
111-
},
112-
"annotations": {
113-
"org.opencontainers.ref.name": "latest"
114136
}
115137
},
116138
{
117-
"mediaType": "application/xml",
139+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
118140
"size": <manifest_size>,
119141
"digest": "<manifest_digest>",
120-
"annotations": {
121-
"org.freedesktop.specifications.metainfo.version": "1.0",
122-
"org.freedesktop.specifications.metainfo.type": "AppStream"
142+
"platform": {
143+
"architecture": "amd64",
144+
"os": "linux"
123145
}
124146
}
125147
],
@@ -156,12 +178,14 @@ type tarContent struct {
156178
}
157179

158180
type imageLayout struct {
159-
rootDir string
160-
layout string
161-
ref string
162-
manifest string
163-
config string
164-
tarList []tarContent
181+
rootDir string
182+
layout string
183+
ref []string
184+
manifest string
185+
index string
186+
config string
187+
indexjson string
188+
tarList []tarContent
165189
}
166190

167191
func TestValidateLayout(t *testing.T) {
@@ -172,11 +196,13 @@ func TestValidateLayout(t *testing.T) {
172196
defer os.RemoveAll(root)
173197

174198
il := imageLayout{
175-
rootDir: root,
176-
layout: layoutStr,
177-
ref: refTag,
178-
manifest: manifestStr,
179-
config: configStr,
199+
rootDir: root,
200+
layout: layoutStr,
201+
ref: refTag,
202+
manifest: manifestStr,
203+
index: indexStr,
204+
indexjson: indexJSON,
205+
config: configStr,
180206
tarList: []tarContent{
181207
{&tar.Header{Name: "test", Size: 4, Mode: 0600}, []byte("test")},
182208
},
@@ -188,7 +214,7 @@ func TestValidateLayout(t *testing.T) {
188214
t.Fatal(err)
189215
}
190216

191-
err = ValidateLayout(root, []string{refTag}, nil)
217+
err = ValidateLayout(root, refTag, nil)
192218
if err != nil {
193219
t.Fatal(err)
194220
}
@@ -226,8 +252,22 @@ func createImageLayoutBundle(il imageLayout) error {
226252
if err != nil {
227253
return err
228254
}
255+
il.index = strings.Replace(il.index, "<manifest_digest>", string(desc.Digest), -1)
256+
il.index = strings.Replace(il.index, "<manifest_size>", strconv.FormatInt(desc.Size, 10), -1)
257+
258+
il.indexjson = strings.Replace(il.indexjson, "<manifest_digest>", string(desc.Digest), -1)
259+
il.indexjson = strings.Replace(il.indexjson, "<manifest_size>", strconv.FormatInt(desc.Size, 10), -1)
260+
261+
// create index blob file
262+
desc, err = createIndexFile(il.rootDir, il.index)
263+
if err != nil {
264+
return err
265+
}
266+
il.indexjson = strings.Replace(il.indexjson, "<index_digest>", string(desc.Digest), -1)
267+
il.indexjson = strings.Replace(il.indexjson, "<index_size>", strconv.FormatInt(desc.Size, 10), -1)
229268

230-
return createIndexFile(il.rootDir, desc)
269+
// create index.json file
270+
return createIndexJSON(il.rootDir, il.indexjson)
231271
}
232272

233273
func createLayoutFile(root string) error {
@@ -241,19 +281,34 @@ func createLayoutFile(root string) error {
241281
return err
242282
}
243283

244-
func createIndexFile(root string, mft v1.Descriptor) error {
284+
func createIndexJSON(root string, str string) error {
245285
indexpath := filepath.Join(root, "index.json")
246286
f, err := os.Create(indexpath)
247287
if err != nil {
248288
return err
249289
}
250290
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)))
291+
_, err = io.Copy(f, bytes.NewBuffer([]byte(str)))
292+
254293
return err
255294
}
256295

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