Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Remove CREATED from bundle.json #778

Merged
merged 1 commit into from
Dec 5, 2019
Merged
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
88 changes: 44 additions & 44 deletions e2e/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func TestImageList(t *testing.T) {

insertBundles(t, cmd)

expected := `REPOSITORY TAG APP IMAGE ID APP NAME CREATED
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
my.registry:5000/c-myapp latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
expected := `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app latest [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
my.registry:5000/c-myapp latest [a-f0-9]{12} push-pull
`

expectImageListOutput(t, cmd, expected)
Expand All @@ -87,10 +87,10 @@ func TestImageListDigests(t *testing.T) {
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
cmd := info.configuredCmd
insertBundles(t, cmd)
expected := `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME CREATED
a-simple-app latest <none> [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest <none> [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
my.registry:5000/c-myapp latest <none> [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
expected := `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME
a-simple-app latest <none> [a-f0-9]{12} simple
b-simple-app latest <none> [a-f0-9]{12} simple
my.registry:5000/c-myapp latest <none> [a-f0-9]{12} push-pull
`
expectImageListDigestsOutput(t, cmd, expected)
})
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestImageRmForce(t *testing.T) {
cmd.Command = dockerCli.Command("app", "image", "rm", "--force", imageID)
icmd.RunCmd(cmd).Assert(t, icmd.Success)

expectedOutput := "REPOSITORY TAG APP IMAGE ID APP NAME CREATED \n"
expectedOutput := "REPOSITORY TAG APP IMAGE ID APP NAME \n"
expectImageListOutput(t, cmd, expectedOutput)
})
}
Expand Down Expand Up @@ -151,7 +151,7 @@ Deleted: b-simple-app:latest`,
Err: `b-simple-app:latest: reference not found`,
})

expectedOutput := "REPOSITORY TAG APP IMAGE ID APP NAME CREATED \n"
expectedOutput := "REPOSITORY TAG APP IMAGE ID APP NAME \n"
expectImageListOutput(t, cmd, expectedOutput)
})
}
Expand All @@ -169,8 +169,8 @@ func TestImageTag(t *testing.T) {
cmd.Command = dockerCli.Command("app", "build", "--tag", "a-simple-app", filepath.Join("testdata", "simple"))
icmd.RunCmd(cmd).Assert(t, icmd.Success)

singleImageExpectation := `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
singleImageExpectation := `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app latest [a-f0-9]{12} simple
`
expectImageListOutput(t, cmd, singleImageExpectation)

Expand Down Expand Up @@ -219,63 +219,63 @@ a-simple-app latest [a-f0-9]{12} simple
// tag image with only names
dockerAppImageTag("a-simple-app", "b-simple-app")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app latest [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
`)

// target tag
dockerAppImageTag("a-simple-app", "a-simple-app:0.1")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app 0.1 [a-f0-9]{12} simple
a-simple-app latest [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
`)

// source tag
dockerAppImageTag("a-simple-app:0.1", "c-simple-app")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app 0.1 [a-f0-9]{12} simple
a-simple-app latest [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
c-simple-app latest [a-f0-9]{12} simple
`)

// source and target tags
dockerAppImageTag("a-simple-app:0.1", "b-simple-app:0.2")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app 0.2 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app 0.1 [a-f0-9]{12} simple
a-simple-app latest [a-f0-9]{12} simple
b-simple-app 0.2 [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
c-simple-app latest [a-f0-9]{12} simple
`)

// given a new application
cmd.Command = dockerCli.Command("app", "build", "--tag", "push-pull", filepath.Join("testdata", "push-pull"))
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app 0.2 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app 0.1 [a-f0-9]{12} simple
a-simple-app latest [a-f0-9]{12} simple
b-simple-app 0.2 [a-f0-9]{12} simple
b-simple-app latest [a-f0-9]{12} simple
c-simple-app latest [a-f0-9]{12} simple
push-pull latest [a-f0-9]{12} push-pull
`)

// can be tagged to an existing tag
dockerAppImageTag("push-pull", "b-simple-app:0.2")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME CREATED[ ]*
b-simple-app 0.2 [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
push-pull latest [a-f0-9]{12} push-pull [La-z0-9 ]+ ago[ ]*
a-simple-app 0.1 [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
a-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
b-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
c-simple-app latest [a-f0-9]{12} simple [La-z0-9 ]+ ago[ ]*
expectImageListOutput(t, cmd, `REPOSITORY TAG APP IMAGE ID APP NAME
a-simple-app 0.1 [a-f0-9]{12} simple
a-simple-app latest [a-f0-9]{12} simple
b-simple-app 0.2 [a-f0-9]{12} push-pull
b-simple-app latest [a-f0-9]{12} simple
c-simple-app latest [a-f0-9]{12} simple
push-pull latest [a-f0-9]{12} push-pull
`)
})
}
25 changes: 7 additions & 18 deletions internal/commands/image/formatter.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package image

import (
"time"

"github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-units"
)

const (
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Name}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t"
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.Name}}\t\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t"
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Name}}\t"
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.Name}}\t\t"

imageIDHeader = "APP IMAGE ID"
repositoryHeader = "REPOSITORY"
Expand Down Expand Up @@ -69,12 +66,11 @@ type imageContext struct {
func newImageContext() *imageContext {
imageCtx := imageContext{}
imageCtx.Header = formatter.SubHeaderContext{
"ID": imageIDHeader,
"Name": imageNameHeader,
"Repository": repositoryHeader,
"Tag": tagHeader,
"Digest": digestHeader,
"CreatedSince": formatter.CreatedSinceHeader,
"ID": imageIDHeader,
"Name": imageNameHeader,
"Repository": repositoryHeader,
"Tag": tagHeader,
"Digest": digestHeader,
}
return &imageCtx
}
Expand Down Expand Up @@ -117,10 +113,3 @@ func (c *imageContext) Digest() string {
}
return c.i.Digest
}

func (c *imageContext) CreatedSince() string {
if c.i.Created.IsZero() {
return ""
}
return units.HumanDuration(time.Now().UTC().Sub(c.i.Created)) + " ago"
}
35 changes: 5 additions & 30 deletions internal/commands/image/list.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package image

import (
"sort"
"time"

"github.com/docker/app/internal/packager"
"github.com/docker/app/internal/relocated"
"github.com/docker/app/internal/store"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -61,7 +57,6 @@ func runList(dockerCli command.Cli, options imageListOption, bundleStore store.B
Format: NewImageFormat(options.format, options.quiet, options.digests),
}

sortImages(images)
return Write(ctx, images)
}

Expand Down Expand Up @@ -94,25 +89,12 @@ func getImageID(bundle *relocated.Bundle, ref reference.Reference) (string, erro
return stringid.TruncateID(id.String()), nil
}

func sortImages(images []imageDesc) {
sort.SliceStable(images, func(i, j int) bool {
if images[i].Created.IsZero() {
return false
}
if images[j].Created.IsZero() {
return true
}
return images[i].Created.After(images[j].Created)
})
}

type imageDesc struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Repository string `json:"repository,omitempty"`
Tag string `json:"tag,omitempty"`
Digest string `json:"digest,omitempty"`
Created time.Time `json:"created,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Repository string `json:"repository,omitempty"`
Tag string `json:"tag,omitempty"`
Digest string `json:"digest,omitempty"`
}

func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
Expand All @@ -130,18 +112,11 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
if t, ok := ref.(reference.Digested); ok {
digest = t.Digest().String()
}
var created time.Time
if payload, err := packager.CustomPayload(bundle.Bundle); err == nil {
if createdPayload, ok := payload.(packager.CustomPayloadCreated); ok {
created = createdPayload.CreatedTime()
}
}
return imageDesc{
ID: id,
Name: bundle.Name,
Repository: repository,
Tag: tag,
Digest: digest,
Created: created,
}
}
33 changes: 8 additions & 25 deletions internal/commands/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"testing"
"time"

"github.com/docker/app/internal/relocated"

Expand Down Expand Up @@ -84,10 +83,10 @@ func TestListCmd(t *testing.T) {
}{
{
name: "TestList",
expectedOutput: `REPOSITORY TAG APP IMAGE ID APP NAME CREATED
foo/bar <none> 3f825b2d0657 Digested App N/A
foo/bar 1.0 9aae408ee04f Foo App N/A
<none> <none> a855ac937f2e Quiet App N/A
expectedOutput: `REPOSITORY TAG APP IMAGE ID APP NAME
foo/bar <none> 3f825b2d0657 Digested App
foo/bar 1.0 9aae408ee04f Foo App
<none> <none> a855ac937f2e Quiet App
`,
options: imageListOption{format: "table"},
},
Expand All @@ -103,10 +102,10 @@ a855ac937f2e sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc
{
name: "TestListWithDigests",
//nolint:lll
expectedOutput: `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME CREATED
foo/bar <none> sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865 3f825b2d0657 Digested App N/A
foo/bar 1.0 <none> 9aae408ee04f Foo App N/A
<none> <none> sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087 a855ac937f2e Quiet App N/A
expectedOutput: `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME
foo/bar <none> sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865 3f825b2d0657 Digested App
foo/bar 1.0 <none> 9aae408ee04f Foo App
<none> <none> sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087 a855ac937f2e Quiet App
`,
options: imageListOption{format: "table", digests: true},
},
Expand All @@ -127,22 +126,6 @@ a855ac937f2e
}
}

func TestSortImages(t *testing.T) {
images := []imageDesc{
{ID: "1", Created: time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "2"},
{ID: "3"},
{ID: "4", Created: time.Date(2018, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "5", Created: time.Date(2017, time.August, 15, 0, 0, 0, 0, time.UTC)},
}
sortImages(images)
assert.Equal(t, "4", images[0].ID)
assert.Equal(t, "5", images[1].ID)
assert.Equal(t, "1", images[2].ID)
assert.Equal(t, "2", images[3].ID)
assert.Equal(t, "3", images[4].ID)
}

func parseReference(t *testing.T, s string) reference.Reference {
ref, err := reference.Parse(s)
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/packager/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestToCNAB(t *testing.T) {
assert.NilError(t, err)
s := golden.Get(t, "bundle-json.golden")
expectedLiteral := regexp.QuoteMeta(string(s))
expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, internal.Version, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z`)
expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, internal.Version)
matches, err := regexp.Match(expected, actualJSON)
assert.NilError(t, err)
assert.Assert(t, matches)
Expand Down
17 changes: 3 additions & 14 deletions internal/packager/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"time"

"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal"
Expand All @@ -28,39 +27,29 @@ type DockerAppCustom struct {
Payload json.RawMessage `json:"payload,omitempty"`
}

// CustomPayloadCreated is a custom payload with a created time
type CustomPayloadCreated interface {
CreatedTime() time.Time
}

// CustomPayloadAppVersion is a custom payload with a docker app version
type CustomPayloadAppVersion interface {
AppVersion() string
}

type payloadV1_0 struct {
Version string `json:"app-version"`
Created time.Time `json:"created"`
}

func (p payloadV1_0) CreatedTime() time.Time {
return p.Created
Version string `json:"app-version"`
}

func (p payloadV1_0) AppVersion() string {
return p.Version
}

func newCustomPayload() (json.RawMessage, error) {
p := payloadV1_0{Created: time.Now().UTC(), Version: internal.Version}
p := payloadV1_0{Version: internal.Version}
j, err := json.Marshal(&p)
if err != nil {
return nil, err
}
return j, nil
}

// CheckAppVersion
// CheckAppVersion prints a warning if the bundle was built with a different version of docker app
func CheckAppVersion(stderr io.Writer, bndl *bundle.Bundle) error {
payload, err := CustomPayload(bndl)
if err != nil {
Expand Down
Loading