oci: introduce WithSpecFromFile combinator

We introduce a WithSpecFromFile option combinator to allow creation
simpler creation of OCI specs from a file name. Often used as the first
option in a `SpecOpts` slice, it simplifies choosing between a local
file and the built-in default.

The code in `ctr run` has been updated to use the new option, with out
changing the order of operations or functionality present there.

Signed-off-by: Stephen Day <stephen.day@getcruise.com>
This commit is contained in:
Stephen Day
2018-07-19 12:38:00 -07:00
parent c8017d0275
commit 2a1bd7414b
9 changed files with 162 additions and 44 deletions

View File

@@ -17,8 +17,16 @@
package oci
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/namespaces"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
@@ -87,3 +95,75 @@ func TestWithMounts(t *testing.T) {
t.Fatal("invaid mount")
}
}
func TestWithDefaultSpec(t *testing.T) {
t.Parallel()
var (
s Spec
c = containers.Container{ID: "TestWithDefaultSpec"}
ctx = namespaces.WithNamespace(context.Background(), "test")
)
if err := ApplyOpts(ctx, nil, &c, &s, WithDefaultSpec()); err != nil {
t.Fatal(err)
}
expected, err := createDefaultSpec(ctx, c.ID)
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(s, Spec{}) {
t.Fatalf("spec should not be empty")
}
if !reflect.DeepEqual(&s, expected) {
t.Fatalf("spec from option differs from default: \n%#v != \n%#v", &s, expected)
}
}
func TestWithSpecFromFile(t *testing.T) {
t.Parallel()
var (
s Spec
c = containers.Container{ID: "TestWithDefaultSpec"}
ctx = namespaces.WithNamespace(context.Background(), "test")
)
fp, err := ioutil.TempFile("", "testwithdefaultspec.json")
if err != nil {
t.Fatal(err)
}
defer fp.Close()
defer func() {
if err := os.Remove(fp.Name()); err != nil {
log.Printf("failed to remove tempfile %v: %v", fp.Name(), err)
}
}()
expected, err := GenerateSpec(ctx, nil, &c)
if err != nil {
t.Fatal(err)
}
p, err := json.Marshal(expected)
if err != nil {
t.Fatal(err)
}
if _, err := fp.Write(p); err != nil {
t.Fatal(err)
}
if err := ApplyOpts(ctx, nil, &c, &s, WithSpecFromFile(fp.Name())); err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(s, Spec{}) {
t.Fatalf("spec should not be empty")
}
if !reflect.DeepEqual(&s, expected) {
t.Fatalf("spec from option differs from default: \n%#v != \n%#v", &s, expected)
}
}