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 20, 2017
1 parent 4829194 commit 3c7c0e7
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 63 deletions.
8 changes: 4 additions & 4 deletions cmd/oci-image-tool/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type bundleCmd struct {
typ string // the type to bundle, can be empty string
ref string
root string
platform []string
platform string
}

func createHandle(context *cli.Context) error {
Expand All @@ -44,7 +44,7 @@ func createHandle(context *cli.Context) error {
typ: context.String("type"),
ref: context.String("ref"),
root: context.String("rootfs"),
platform: context.StringSlice("platform"),
platform: context.String("platform"),
}

if v.typ == "" {
Expand Down Expand Up @@ -97,9 +97,9 @@ var createCommand = cli.Command{
Value: "rootfs",
Usage: "A directory representing the root filesystem of the container in the OCI runtime bundle. It is strongly recommended to keep the default value.",
},
cli.StringSliceFlag{
cli.StringFlag{
Name: "platform",
Usage: "The platform contains os and arch. Filter manifests according to the conditions provided. Only applicable if reftype is index.",
Usage: "Specify the os and architecture of the manifest, format is OS:Architecture. Only applicable if reftype is index.",
},
},
}
8 changes: 4 additions & 4 deletions cmd/oci-image-tool/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var unpackTypes = []string{
type unpackCmd struct {
typ string // the type to unpack, can be empty string
ref string
platform []string
platform string
}

func unpackHandle(context *cli.Context) error {
Expand All @@ -42,7 +42,7 @@ func unpackHandle(context *cli.Context) error {
v := unpackCmd{
typ: context.String("type"),
ref: context.String("ref"),
platform: context.StringSlice("platform"),
platform: context.String("platform"),
}

if v.typ == "" {
Expand Down Expand Up @@ -88,9 +88,9 @@ var unpackCommand = cli.Command{
Value: "v1.0",
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
},
cli.StringSliceFlag{
cli.StringFlag{
Name: "platform",
Usage: "The platform contains os and arch conditions. Filter manifests according to the conditions provided. Only applicable if reftype is index.",
Usage: "Specify the os and architecture of the manifest, format is OS:Architecture. Only applicable if reftype is index.",
},
},
}
30 changes: 16 additions & 14 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -140,12 +141,12 @@ func validate(w walker, refs []string, out *log.Logger) error {
// UnpackLayout walks through the file tree given by src and, using the layers
// specified in the manifest pointed to by the given ref, unpacks all layers in
// the given destination directory or returns an error if the unpacking failed.
func UnpackLayout(src, dest, ref string, platform []string) error {
func UnpackLayout(src, dest, ref string, platform string) error {
return unpack(newPathWalker(src), dest, ref, platform)
}

// UnpackFile opens the file pointed by tarFileName and calls Unpack on it.
func UnpackFile(tarFileName, dest, ref string, platform []string) error {
func UnpackFile(tarFileName, dest, ref string, platform string) error {
f, err := os.Open(tarFileName)
if err != nil {
return errors.Wrap(err, "unable to open file")
Expand All @@ -159,11 +160,11 @@ func UnpackFile(tarFileName, dest, ref string, platform []string) error {
// the manifest pointed to by the given ref, unpacks all layers in the given
// destination directory or returns an error if the unpacking failed.
// The destination will be created if it does not exist.
func Unpack(r io.ReadSeeker, dest, refName string, platform []string) error {
func Unpack(r io.ReadSeeker, dest, refName string, platform string) error {
return unpack(newTarWalker(r), dest, refName, platform)
}

func unpack(w walker, dest, refName string, platform []string) error {
func unpack(w walker, dest, refName string, platform string) error {
if err := layoutValidate(w); err != nil {
return err
}
Expand Down Expand Up @@ -216,13 +217,13 @@ func unpack(w walker, dest, refName string, platform []string) error {
// CreateRuntimeBundleLayout walks through the file tree given by src and
// creates an OCI runtime bundle in the given destination dest
// or returns an error if the unpacking failed.
func CreateRuntimeBundleLayout(src, dest, ref, root string, platform []string) error {
func CreateRuntimeBundleLayout(src, dest, ref, root string, platform string) error {
return createRuntimeBundle(newPathWalker(src), dest, ref, root, platform)
}

// CreateRuntimeBundleFile opens the file pointed by tarFile and calls
// CreateRuntimeBundle.
func CreateRuntimeBundleFile(tarFile, dest, ref, root string, platform []string) error {
func CreateRuntimeBundleFile(tarFile, dest, ref, root string, platform string) error {
f, err := os.Open(tarFile)
if err != nil {
return errors.Wrap(err, "unable to open file")
Expand All @@ -235,11 +236,11 @@ func CreateRuntimeBundleFile(tarFile, dest, ref, root string, platform []string)
// CreateRuntimeBundle walks through the given tar stream and
// creates an OCI runtime bundle in the given destination dest
// or returns an error if the unpacking failed.
func CreateRuntimeBundle(r io.ReadSeeker, dest, ref, root string, platform []string) error {
func CreateRuntimeBundle(r io.ReadSeeker, dest, ref, root string, platform string) error {
return createRuntimeBundle(newTarWalker(r), dest, ref, root, platform)
}

func createRuntimeBundle(w walker, dest, refName, rootfs string, platform []string) error {
func createRuntimeBundle(w walker, dest, refName, rootfs string, platform string) error {
if err := layoutValidate(w); err != nil {
return err
}
Expand Down Expand Up @@ -324,18 +325,19 @@ func createBundle(w walker, m *manifest, dest, rootfs string) error {
}

// filertManifest returns a filtered list of manifests
func filterManifest(w walker, Manifests []v1.Descriptor, platform []string) ([]*manifest, error) {
func filterManifest(w walker, Manifests []v1.Descriptor, platform string) ([]*manifest, error) {
var manifests []*manifest

argsParts := strings.Split(platform, ":")
if len(argsParts) != 2 {
return manifests, fmt.Errorf("platform must have os and arch when reftype is index")
}

if len(Manifests) == 0 {
fmt.Println("warning: no manifests found")
return manifests, nil
}

if len(platform) != 2 {
return manifests, fmt.Errorf("platform must have os and arch")
}

for _, manifest := range Manifests {
m, err := findManifest(w, &manifest)
if err != nil {
Expand All @@ -345,7 +347,7 @@ func filterManifest(w walker, Manifests []v1.Descriptor, platform []string) ([]*
if err := m.validate(w); err != nil {
return manifests, err
}
if manifest.Platform.OS == platform[0] && manifest.Platform.Architecture == platform[1] {
if strings.EqualFold(manifest.Platform.OS, argsParts[0]) && strings.EqualFold(manifest.Platform.Architecture, argsParts[1]) {
manifests = append(manifests, m)
}
}
Expand Down
126 changes: 90 additions & 36 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
)

const (
refTag = "latest"
layoutStr = `{"imageLayoutVersion": "1.0.0"}`

configStr = `{
Expand All @@ -40,7 +39,6 @@ const (
"architecture": "amd64",
"os": "linux",
"config": {
"User": "alice",
"ExposedPorts": {
"8080/tcp": {}
},
Expand Down Expand Up @@ -90,36 +88,59 @@ const (
)

var (
refTag = []string{
"latest",
"v1.0",
}

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 All @@ -129,10 +150,10 @@ var (
}
`
manifestStr = `{
"annotations": {
"annotations": {
"org.freedesktop.specifications.metainfo.version": "1.0",
"org.freedesktop.specifications.metainfo.type": "AppStream"
},
},
"config": {
"digest": "<config_digest>",
"mediaType": "application/vnd.oci.image.config.v1+json",
Expand All @@ -156,12 +177,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 +195,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 @@ -188,7 +213,7 @@ func TestValidateLayout(t *testing.T) {
t.Fatal(err)
}

err = ValidateLayout(root, []string{refTag}, nil)
err = ValidateLayout(root, refTag, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -226,8 +251,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)

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)

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

func createLayoutFile(root string) error {
Expand All @@ -241,19 +280,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
5 changes: 3 additions & 2 deletions 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 All @@ -54,7 +55,7 @@ func findIndex(w walker, d *v1.Descriptor) (*v1.Index, error) {
case errEOW:
return &index, nil
case nil:
return nil, fmt.Errorf("index.json not found")
return nil, fmt.Errorf("index not found")
default:
return nil, err
}
Expand Down
Loading

0 comments on commit 3c7c0e7

Please sign in to comment.