diff --git a/image/image_test.go b/image/image_test.go index 61ec39e..390d839 100644 --- a/image/image_test.go +++ b/image/image_test.go @@ -90,17 +90,38 @@ const ( ) var ( + indexJSON = `{ + "schemaVersion": 2, + "manifests": [ + { + "mediaType": "application/vnd.oci.image.index.v1+json", + "size": , + "digest": "", + "annotations": { + "org.opencontainers.ref.name": "v1.0" + } + }, + { + "mediaType": "application/vnd.oci.image.manifest.v1+json", + "size": , + "digest": "", + "platform": { + "architecture": "ppc64le", + "os": "linux" + }, + "annotations": { + "org.opencontainers.ref.name": "latest" + } + } + ], + "annotations": { + "com.example.index.revision": "r124356" + } +} + ` indexStr = `{ "schemaVersion": 2, "manifests": [ - { - "mediaType": "application/vnd.oci.image.index.v1+json", - "size": , - "digest": "", - "annotations": { - "org.opencontainers.ref.name": "v1.0" - } - }, { "mediaType": "application/vnd.oci.image.manifest.v1+json", "size": , @@ -108,18 +129,15 @@ var ( "platform": { "architecture": "ppc64le", "os": "linux" - }, - "annotations": { - "org.opencontainers.ref.name": "latest" } }, { - "mediaType": "application/xml", + "mediaType": "application/vnd.oci.image.manifest.v1+json", "size": , "digest": "", - "annotations": { - "org.freedesktop.specifications.metainfo.version": "1.0", - "org.freedesktop.specifications.metainfo.type": "AppStream" + "platform": { + "architecture": "amd64", + "os": "linux" } } ], @@ -156,12 +174,14 @@ type tarContent struct { } type imageLayout struct { - rootDir string - layout string - ref string - manifest string - config string - tarList []tarContent + rootDir string + layout string + ref string + manifest string + index string + config string + indexjson string + tarList []tarContent } func TestValidateLayout(t *testing.T) { @@ -172,11 +192,13 @@ func TestValidateLayout(t *testing.T) { defer os.RemoveAll(root) il := imageLayout{ - rootDir: root, - layout: layoutStr, - ref: refTag, - manifest: manifestStr, - config: configStr, + rootDir: root, + layout: layoutStr, + ref: refTag, + manifest: manifestStr, + index: indexStr, + indexjson: indexJSON, + config: configStr, tarList: []tarContent{ {&tar.Header{Name: "test", Size: 4, Mode: 0600}, []byte("test")}, }, @@ -192,6 +214,11 @@ func TestValidateLayout(t *testing.T) { if err != nil { t.Fatal(err) } + + err = ValidateLayout(root, []string{"v1.0"}, nil) + if err != nil { + t.Fatal(err) + } } func createImageLayoutBundle(il imageLayout) error { @@ -226,8 +253,22 @@ func createImageLayoutBundle(il imageLayout) error { if err != nil { return err } + il.index = strings.Replace(il.index, "", string(desc.Digest), -1) + il.index = strings.Replace(il.index, "", strconv.FormatInt(desc.Size, 10), -1) - return createIndexFile(il.rootDir, desc) + il.indexjson = strings.Replace(il.indexjson, "", string(desc.Digest), -1) + il.indexjson = strings.Replace(il.indexjson, "", strconv.FormatInt(desc.Size, 10), -1) + + // create index blob file + desc, err = createIndexFile(il.rootDir, il.index) + if err != nil { + return err + } + il.indexjson = strings.Replace(il.indexjson, "", string(desc.Digest), -1) + il.indexjson = strings.Replace(il.indexjson, "", strconv.FormatInt(desc.Size, 10), -1) + + // create index.json file + return createIndexJSON(il.rootDir, il.indexjson) } func createLayoutFile(root string) error { @@ -241,19 +282,34 @@ func createLayoutFile(root string) error { return err } -func createIndexFile(root string, mft v1.Descriptor) error { +func createIndexJSON(root string, str string) error { indexpath := filepath.Join(root, "index.json") f, err := os.Create(indexpath) if err != nil { return err } defer f.Close() - indexStr = strings.Replace(indexStr, "", string(mft.Digest), -1) - indexStr = strings.Replace(indexStr, "", strconv.FormatInt(mft.Size, 10), -1) - _, err = io.Copy(f, bytes.NewBuffer([]byte(indexStr))) + _, err = io.Copy(f, bytes.NewBuffer([]byte(str))) + return err } +func createIndexFile(root, str string) (v1.Descriptor, error) { + name := filepath.Join(root, "blobs", "sha256", "test-index") + f, err := os.Create(name) + if err != nil { + return v1.Descriptor{}, err + } + defer f.Close() + + _, err = io.Copy(f, bytes.NewBuffer([]byte(str))) + if err != nil { + return v1.Descriptor{}, err + } + + return createHashedBlob(name) +} + func createManifestFile(root, str string) (v1.Descriptor, error) { name := filepath.Join(root, "blobs", "sha256", "test-manifest") f, err := os.Create(name) diff --git a/image/index.go b/image/index.go index d41f8f0..1a20d76 100644 --- a/image/index.go +++ b/image/index.go @@ -30,9 +30,10 @@ import ( func findIndex(w walker, d *v1.Descriptor) (*v1.Index, error) { var index v1.Index + ipath := filepath.Join("blobs", string(d.Digest.Algorithm()), d.Digest.Hex()) switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error { - if info.IsDir() || filepath.Clean(path) != indexPath { + if info.IsDir() || filepath.Clean(path) != ipath { return nil }