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

Commit

Permalink
Remove App-only fields from CNAB image inspect out
Browse files Browse the repository at this point in the history
Removes the App-only SERVICE fields from the `app image inspect
--pretty` output when inspecting a non-App CNAB:
* REPLICAS
* PORTS

Added unit test for image inspect of CNABs with pretty format.

Refactored parameter key sorting code to use simpler `sort.Strings()`

Signed-off-by: Nick Adcock <[email protected]>
  • Loading branch information
zappy-shu committed Nov 20, 2019
1 parent db25844 commit 8908b28
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
34 changes: 23 additions & 11 deletions internal/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func ImageInspect(out io.Writer, app *types.App, argParameters map[string]string
}

outputFormat := os.Getenv(internal.DockerInspectFormatEnvVar)
return printImageAppInfo(out, appInfo, outputFormat)
return printImageAppInfo(out, appInfo, outputFormat, true)
}

func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) error {
Expand All @@ -138,6 +138,7 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
params[v.Definition] = ""
}
}
sort.Strings(paramKeys)

services := []Service{}
for k, v := range bndl.Images {
Expand All @@ -146,6 +147,9 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
Image: v.Image,
})
}
sort.SliceStable(services, func(i, j int) bool {
return services[i].Name < services[j].Name
})

appInfo := ImageAppInfo{
Metadata: meta,
Expand All @@ -154,7 +158,7 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
Services: services,
}

return printImageAppInfo(out, appInfo, outputFormat)
return printImageAppInfo(out, appInfo, outputFormat, false)
}

func printAppInfo(out io.Writer, app AppInfo, format string) error {
Expand All @@ -168,10 +172,10 @@ func printAppInfo(out io.Writer, app AppInfo, format string) error {
}
}

func printImageAppInfo(out io.Writer, app ImageAppInfo, format string) error {
func printImageAppInfo(out io.Writer, app ImageAppInfo, format string, isApp bool) error {
switch format {
case "pretty":
return printTable(out, app)
return printTable(out, app, isApp)
case "json":
return printJSON(out, app)
default:
Expand Down Expand Up @@ -209,16 +213,24 @@ func printAppTable(out io.Writer, info AppInfo) error {
return nil
}

func printTable(out io.Writer, appInfo ImageAppInfo) error {
func printTable(out io.Writer, appInfo ImageAppInfo, isApp bool) error {
// Add Meta data
printYAML(out, appInfo.Metadata)

// Add Service section
printSection(out, len(appInfo.Services), func(w io.Writer) {
for _, service := range appInfo.Services {
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, service.Replicas, service.Ports, service.Image)
}
}, "SERVICE", "REPLICAS", "PORTS", "IMAGE")
if isApp {
printSection(out, len(appInfo.Services), func(w io.Writer) {
for _, service := range appInfo.Services {
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, service.Replicas, service.Ports, service.Image)
}
}, "SERVICE", "REPLICAS", "PORTS", "IMAGE")
} else {
printSection(out, len(appInfo.Services), func(w io.Writer) {
for _, service := range appInfo.Services {
fmt.Fprintf(w, "%s\t%s\n", service.Name, service.Image)
}
}, "SERVICE", "IMAGE")
}

// Add Network section
printSection(out, len(appInfo.Networks), func(w io.Writer) {
Expand Down Expand Up @@ -369,7 +381,7 @@ func extractParameters(app *types.App, argParameters map[string]string) ([]strin
for k := range allParameters {
parametersKeys = append(parametersKeys, k)
}
sort.Slice(parametersKeys, func(i, j int) bool { return parametersKeys[i] < parametersKeys[j] })
sort.Strings(parametersKeys)
return parametersKeys, allParameters, nil
}

Expand Down
14 changes: 11 additions & 3 deletions internal/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,24 @@ text: hello`),
})
}

func TestImageInspectCNAB(t *testing.T) {
func TestImageInspectCNABFormatJSON(t *testing.T) {
testImageInspectCNAB(t, "json")
}

func TestImageInspectCNABFormatPretty(t *testing.T) {
testImageInspectCNAB(t, "pretty")
}

func testImageInspectCNAB(t *testing.T, format string) {
s := golden.Get(t, "bundle-json.golden")
var bndl bundle.Bundle
err := json.Unmarshal(s, &bndl)
assert.NilError(t, err)

expected := golden.Get(t, "inspect-bundle-json.golden")
expected := golden.Get(t, fmt.Sprintf("inspect-bundle-%s.golden", format))

outBuffer := new(bytes.Buffer)
err = ImageInspectCNAB(outBuffer, &bndl, "json")
err = ImageInspectCNAB(outBuffer, &bndl, format)
assert.NilError(t, err)

result := outBuffer.String()
Expand Down
24 changes: 24 additions & 0 deletions internal/inspect/testdata/inspect-bundle-pretty.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 0.1.0
name: packing
description: hello
maintainers:
- name: dev1
email: [email protected]
- name: dev2
email: [email protected]


SERVICE IMAGE
app-watcher watcher
debug busybox:latest
front nginx
monitor busybox:latest

PARAMETER VALUE
com.docker.app.args
com.docker.app.inspect-format json
com.docker.app.kubernetes-namespace
com.docker.app.orchestrator
com.docker.app.render-format yaml
com.docker.app.share-registry-creds false
watcher.cmd foo

0 comments on commit 8908b28

Please sign in to comment.