Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional image names (tags) to be saved #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Helpful utilities for working with images

## Development

To format:

```bash
$ ./bin/format
```

To run tests:

```bash
Expand Down
105 changes: 64 additions & 41 deletions fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"
"time"
"unicode"

"github.com/pkg/errors"

Expand All @@ -19,39 +20,37 @@ import (

func NewImage(name, topLayerSha, digest string) *Image {
return &Image{
alreadySaved: false,
labels: map[string]string{},
env: map[string]string{},
topLayerSha: topLayerSha,
digest: digest,
name: name,
cmd: []string{"initialCMD"},
layersMap: map[string]string{},
prevLayersMap: map[string]string{},
createdAt: time.Now(),
savedRepoNames: make(map[string]interface{}),
labels: map[string]string{},
env: map[string]string{},
topLayerSha: topLayerSha,
digest: digest,
name: name,
cmd: []string{"initialCMD"},
layersMap: map[string]string{},
prevLayersMap: map[string]string{},
createdAt: time.Now(),
savedNames: map[string]bool{},
}
}

type Image struct {
alreadySaved bool
deleted bool
layers []string
layersMap map[string]string
prevLayersMap map[string]string
reusedLayers []string
labels map[string]string
env map[string]string
topLayerSha string
digest string
name string
entryPoint []string
cmd []string
base string
createdAt time.Time
layerDir string
workingDir string
savedRepoNames map[string]interface{}
deleted bool
layers []string
layersMap map[string]string
prevLayersMap map[string]string
reusedLayers []string
labels map[string]string
env map[string]string
topLayerSha string
digest string
name string
entryPoint []string
cmd []string
base string
createdAt time.Time
layerDir string
workingDir string
savedNames map[string]bool
}

func (f *Image) CreatedAt() (time.Time, error) {
Expand Down Expand Up @@ -156,13 +155,14 @@ func (f *Image) ReuseLayer(sha string) error {
return nil
}

func (f *Image) Save() (string, error) {
f.alreadySaved = true

func (f *Image) Save(additionalNames ...string) imgutil.SaveResult {
var err error
f.layerDir, err = ioutil.TempDir("", "fake-image")
if err != nil {
return "", errors.Wrap(err, "failed to create tmpDir")
return imgutil.NewFailedResult(
append([]string{f.name}, additionalNames...),
errors.Wrap(err, "failed to create tmpDir"),
)
}

for sha, path := range f.layersMap {
Expand All @@ -176,9 +176,31 @@ func (f *Image) Save() (string, error) {
f.layers[i] = filepath.Join(f.layerDir, filepath.Base(layerPath))
}

f.savedRepoNames[f.name] = nil
allNames := append([]string{f.name}, additionalNames...)

errs := map[string]error{}
for _, n := range allNames {
if !isASCII(n) {
errs[n] = errors.New("could not parse reference")
} else {
errs[n] = nil
f.savedNames[n] = true
}
}

return imgutil.SaveResult{
Outcomes: errs,
Digest: "saved-digest-from-fake-run-image",
}
}

return "saved-digest-from-fake-run-image", nil
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}

func (f *Image) copyLayer(path, newPath string) error {
Expand Down Expand Up @@ -298,17 +320,18 @@ func (f *Image) NumberOfAddedLayers() int {
}

func (f *Image) IsSaved() bool {
return f.alreadySaved
return len(f.savedNames) > 0
}

func (f *Image) Base() string {
return f.base
}

func (f *Image) SavedRepoNames() []string {
var t []string
for k := range f.savedRepoNames {
t = append(t, k)
func (f *Image) SavedNames() []string {
var names []string
for k := range f.savedNames {
names = append(names, k)
}
return t

return names
}
67 changes: 67 additions & 0 deletions fakes/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fakes_test

import (
"github.com/buildpack/imgutil/fakes"
"math/rand"
"testing"
"time"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

h "github.com/buildpack/imgutil/testhelpers"
)

var localTestRegistry *h.DockerRegistry

func newRepoName() string {
return "test-image-" + h.RandString(10)
}

func TestFake(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())

localTestRegistry = h.NewDockerRegistry()
localTestRegistry.Start(t)
defer localTestRegistry.Stop(t)

spec.Run(t, "FakeImage", testFake, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testFake(t *testing.T, when spec.G, it spec.S) {
when("#SavedNames", func() {
when("additional names are provided during save", func() {
var (
repoName = newRepoName()
additionalNames = []string{
newRepoName(),
newRepoName(),
}
)

it("returns list of saved names", func() {
image := fakes.NewImage(repoName, "", "")

_ = image.Save(additionalNames...)

names := image.SavedNames()
h.AssertContains(t, names, append(additionalNames, repoName)...)
})

when("an image name is not valid", func() {
it("returns a list of image names with errors", func() {
badImageName := repoName + ":🧨"

image := fakes.NewImage(repoName, "", "")

result := image.Save(append([]string{badImageName}, additionalNames...)...)
h.AssertError(t, result.Outcomes[badImageName], "could not parse reference")

names := image.SavedNames()
h.AssertContains(t, names, append(additionalNames, repoName)...)
h.AssertDoesNotContain(t, names, badImageName)
})
})
})
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/sirupsen/logrus v1.4.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 // indirect
golang.org/x/sync v0.0.0-20190412183630-56d357773e84 // indirect
golang.org/x/sync v0.0.0-20190412183630-56d357773e84
golang.org/x/sys v0.0.0-20190416152802-12500544f89f // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.20.0 // indirect
Expand Down
24 changes: 22 additions & 2 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import (
"time"
)

type SaveResult struct {
// Digest is the digest of the image
Digest string
// Outcomes is a map of image name to `error` or `nil` if saved properly.
Outcomes map[string]error
}

func NewFailedResult(imageNames []string, err error) SaveResult {
errs := map[string]error{}
for _, n := range imageNames {
errs[n] = err
}

return SaveResult{
Outcomes: errs,
}
}

type Image interface {
Name() string
Rename(name string)
Expand All @@ -20,9 +38,11 @@ type Image interface {
AddLayer(path string) error
ReuseLayer(sha string) error
TopLayer() (string, error)
Save() (string, error)
// Save saves the image as `Name()` and any additional names provided to this method.
Save(additionalNames ...string) SaveResult
// Found tells whether the image exists in the repository by `Name()`.
Found() bool
GetLayer(string) (io.ReadCloser, error)
GetLayer(sha string) (io.ReadCloser, error)
Delete() error
CreatedAt() (time.Time, error)
}
Loading