Skip to content

Commit

Permalink
add test to index
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Jul 17, 2017
1 parent 0140f7d commit a622eaf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 32 deletions.
118 changes: 87 additions & 31 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,54 @@ const (
)

var (
indexJSON = `{
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.index.v1+json",
"size": <index_size>,
"digest": "<index_digest>",
"annotations": {
"org.opencontainers.ref.name": "v1.0"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": <manifest_size>,
"digest": "<manifest_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": <manifest_size>,
"digest": "<manifest_digest>",
"annotations": {
"org.opencontainers.ref.name": "v1.0"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": <manifest_size>,
"digest": "<manifest_digest>",
"platform": {
"architecture": "ppc64le",
"os": "linux"
},
"annotations": {
"org.opencontainers.ref.name": "latest"
}
},
{
"mediaType": "application/xml",
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": <manifest_size>,
"digest": "<manifest_digest>",
"annotations": {
"org.freedesktop.specifications.metainfo.version": "1.0",
"org.freedesktop.specifications.metainfo.type": "AppStream"
"platform": {
"architecture": "amd64",
"os": "linux"
}
}
],
Expand Down Expand Up @@ -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) {
Expand All @@ -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")},
},
Expand All @@ -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 {
Expand Down Expand Up @@ -226,8 +253,22 @@ func createImageLayoutBundle(il imageLayout) error {
if err != nil {
return err
}
il.index = strings.Replace(il.index, "<manifest_digest>", string(desc.Digest), -1)
il.index = strings.Replace(il.index, "<manifest_size>", strconv.FormatInt(desc.Size, 10), -1)

return createIndexFile(il.rootDir, desc)
il.indexjson = strings.Replace(il.indexjson, "<manifest_digest>", string(desc.Digest), -1)
il.indexjson = strings.Replace(il.indexjson, "<manifest_size>", 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, "<index_digest>", string(desc.Digest), -1)
il.indexjson = strings.Replace(il.indexjson, "<index_size>", strconv.FormatInt(desc.Size, 10), -1)

// create index.json file
return createIndexJSON(il.rootDir, il.indexjson)
}

func createLayoutFile(root string) error {
Expand All @@ -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, "<manifest_digest>", string(mft.Digest), -1)
indexStr = strings.Replace(indexStr, "<manifest_size>", 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)
Expand Down
3 changes: 2 additions & 1 deletion image/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit a622eaf

Please sign in to comment.