add test for WithImageConfigArgs
Signed-off-by: Andrey Kolomentsev <andrey.kolomentsev@docker.com>
This commit is contained in:
parent
f2344db40a
commit
1be86af108
@ -19,6 +19,9 @@ package oci
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -26,11 +29,103 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/content"
|
||||||
|
"github.com/opencontainers/go-digest"
|
||||||
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
|
||||||
"github.com/containerd/containerd/containers"
|
"github.com/containerd/containerd/containers"
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type blob []byte
|
||||||
|
|
||||||
|
func (b blob) ReadAt(p []byte, off int64) (int, error) {
|
||||||
|
if off >= int64(len(b)) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
return copy(p, b[off:]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b blob) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b blob) Size() int64 {
|
||||||
|
return int64(len(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
type fakeImage struct {
|
||||||
|
config ocispec.Descriptor
|
||||||
|
blobs map[string]blob
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFakeImage(config ocispec.Image) (Image, error) {
|
||||||
|
configBlob, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
configDescriptor := ocispec.Descriptor{
|
||||||
|
MediaType: ocispec.MediaTypeImageConfig,
|
||||||
|
Digest: digest.NewDigestFromBytes(digest.SHA256, configBlob),
|
||||||
|
}
|
||||||
|
|
||||||
|
return fakeImage{
|
||||||
|
config: configDescriptor,
|
||||||
|
blobs: map[string]blob{
|
||||||
|
configDescriptor.Digest.String(): configBlob,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Config(ctx context.Context) (ocispec.Descriptor, error) {
|
||||||
|
return i.config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) ContentStore() content.Store {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) ReaderAt(ctx context.Context, dec ocispec.Descriptor) (content.ReaderAt, error) {
|
||||||
|
blob, found := i.blobs[dec.Digest.String()]
|
||||||
|
if !found {
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
return blob, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
|
||||||
|
return content.Info{}, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
|
||||||
|
return content.Info{}, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
|
||||||
|
return errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Delete(ctx context.Context, dgst digest.Digest) error {
|
||||||
|
return errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Status(ctx context.Context, ref string) (content.Status, error) {
|
||||||
|
return content.Status{}, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
|
||||||
|
return nil, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Abort(ctx context.Context, ref string) error {
|
||||||
|
return errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fakeImage) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
|
||||||
|
return nil, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
func TestReplaceOrAppendEnvValues(t *testing.T) {
|
func TestReplaceOrAppendEnvValues(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@ -51,26 +146,15 @@ func TestReplaceOrAppendEnvValues(t *testing.T) {
|
|||||||
|
|
||||||
results := replaceOrAppendEnvValues(defaults, overrides)
|
results := replaceOrAppendEnvValues(defaults, overrides)
|
||||||
|
|
||||||
for i, x := range defaultsOrig {
|
if err := assertEqualsStringArrays(defaults, defaultsOrig); err != nil {
|
||||||
if defaults[i] != x {
|
t.Fatal(err)
|
||||||
t.Fatal("defaults slice was mutated")
|
}
|
||||||
}
|
if err := assertEqualsStringArrays(overrides, overridesOrig); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, x := range overridesOrig {
|
if err := assertEqualsStringArrays(results, expected); err != nil {
|
||||||
if overrides[i] != x {
|
t.Fatal(err)
|
||||||
t.Fatal("overrides slice was mutated")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(expected) != len(results) {
|
|
||||||
t.Fatalf("expected %s, but found %s", expected, results)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, x := range expected {
|
|
||||||
if results[i] != x {
|
|
||||||
t.Fatalf("expected %s, but found %s", expected, results)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,3 +359,66 @@ func TestWithMemoryLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isEqualStringArrays(values, expected []string) bool {
|
||||||
|
if len(values) != len(expected) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, x := range expected {
|
||||||
|
if values[i] != x {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertEqualsStringArrays(values, expected []string) error {
|
||||||
|
if !isEqualStringArrays(values, expected) {
|
||||||
|
return fmt.Errorf("expected %s, but found %s", expected, values)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithImageConfigArgs(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
img, err := newFakeImage(ocispec.Image{
|
||||||
|
Config: ocispec.ImageConfig{
|
||||||
|
Env: []string{"z=bar", "y=baz"},
|
||||||
|
Entrypoint: []string{"create", "--namespace=test"},
|
||||||
|
Cmd: []string{"", "--debug"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := Spec{
|
||||||
|
Version: specs.Version,
|
||||||
|
Root: &specs.Root{},
|
||||||
|
Windows: &specs.Windows{},
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := []SpecOpts{
|
||||||
|
WithEnv([]string{"x=foo", "y=boo"}),
|
||||||
|
WithProcessArgs("run", "--foo", "xyz", "--bar"),
|
||||||
|
WithImageConfigArgs(img, []string{"--boo", "bar"}),
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedEnv := []string{"x=foo", "y=baz", "z=bar"}
|
||||||
|
expectedArgs := []string{"create", "--namespace=test", "--boo", "bar"}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(nil, nil, nil, &s); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := assertEqualsStringArrays(s.Process.Env, expectedEnv); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := assertEqualsStringArrays(s.Process.Args, expectedArgs); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user