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.
This commit is contained in:
Simone Gotti
2015-01-21 09:49:21 +01:00
parent 267d4e24ce
commit af073571fc
2 changed files with 37 additions and 8 deletions
+35 -7
View File
@@ -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
}
+2 -1
View File
@@ -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)
}