From af073571fc5e6ee30752ae00a7c43c306f2b1dd3 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Wed, 14 Jan 2015 10:27:31 +0100 Subject: [PATCH] utils: aci: let NewACI create ACIs with files and custom imagemanifest. Also add a NewBasicACI functions with the previous NewACI behavior. Both functions now requires the dir where the new file should be created. Without this, the generated ACIs will be created inside the default temp dir and calling functions should remember to remove every single aci, now the tests can remove the whole test dir when finished. --- pkg/util/aci.go | 42 +++++++++++++++++++++++++++++++++++------- rkt/fetch_test.go | 3 ++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/util/aci.go b/pkg/util/aci.go index 76ae9b1..ee5b77d 100644 --- a/pkg/util/aci.go +++ b/pkg/util/aci.go @@ -8,29 +8,57 @@ import ( "io" "io/ioutil" "os" + "strings" "github.com/appc/spec/aci" "github.com/appc/spec/schema" "github.com/coreos/rocket/Godeps/_workspace/src/golang.org/x/crypto/openpgp" ) -// NewACI creates a new ACI with the given name. +type ACIEntry struct { + Header *tar.Header + Contents string +} + +// NewBasicACI creates a new ACI in the given directory with the given name. // Used for testing. -func NewACI(name string) (*os.File, error) { - tf, err := ioutil.TempFile("", "") - if err != nil { - return nil, err - } - +func NewBasicACI(dir string, name string) (*os.File, error) { manifest := fmt.Sprintf(`{"acKind":"ImageManifest","acVersion":"0.1.1","name":"%s"}`, name) + return NewACI(dir, manifest, nil) +} +// NewACI creates a new ACI in the given directory with the given image +// manifest and entries. +// Used for testing. +func NewACI(dir string, manifest string, entries []*ACIEntry) (*os.File, error) { var im schema.ImageManifest if err := im.UnmarshalJSON([]byte(manifest)); err != nil { return nil, err } + tf, err := ioutil.TempFile(dir, "") + if err != nil { + return nil, err + } + tw := tar.NewWriter(tf) aw := aci.NewImageWriter(im, tw) + + for _, entry := range entries { + // Add default mode + if entry.Header.Mode == 0 { + if entry.Header.Typeflag == tar.TypeDir { + entry.Header.Mode = 0755 + } else { + entry.Header.Mode = 0644 + } + } + sr := strings.NewReader(entry.Contents) + if err := aw.AddFile("", entry.Header, sr); err != nil { + return nil, err + } + } + if err := aw.Close(); err != nil { return nil, err } diff --git a/rkt/fetch_test.go b/rkt/fetch_test.go index 8b81b59..96ae65b 100644 --- a/rkt/fetch_test.go +++ b/rkt/fetch_test.go @@ -118,7 +118,8 @@ func TestFetchImage(t *testing.T) { if _, err := ks.StoreTrustedKeyPrefix("example.com/app", bytes.NewBufferString(key.ArmoredPublicKey)); err != nil { t.Fatalf("unexpected error %v", err) } - aci, err := util.NewACI("example.com/app") + aci, err := util.NewBasicACI(dir, "example.com/app") + defer aci.Close() if err != nil { t.Fatalf("unexpected error %v", err) }