Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 7670989

Browse files
author
Jean-Christophe Sirot
committed
Add App inspect unit tests
Signed-off-by: Jean-Christophe Sirot <[email protected]>
1 parent d230ee7 commit 7670989

File tree

6 files changed

+115
-10
lines changed

6 files changed

+115
-10
lines changed

cmd/cnab-run/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var (
5050
return reference.FamiliarString(ref)
5151
}},
5252
{"PORTS", func(s *swarmtypes.Service) string {
53-
return Ports(s.Endpoint.Ports)
53+
return ports(s.Endpoint.Ports)
5454
}},
5555
}
5656
)
@@ -84,7 +84,7 @@ func (pr portRange) String() string {
8484

8585
// Ports formats port configuration. This function is copied et adapted from docker CLI
8686
// see https://github.com/docker/cli/blob/d6edc912ce/cli/command/service/formatter.go#L655
87-
func Ports(servicePorts []swarmtypes.PortConfig) string {
87+
func ports(servicePorts []swarmtypes.PortConfig) string {
8888
if servicePorts == nil {
8989
return ""
9090
}

e2e/testdata/plugin-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Management Commands:
1212
Commands:
1313
build Build an App image from an App definition (.dockerapp)
1414
init Initialize an App definition
15+
inspect Shows status, metadata, parameters and the list of services of a running App
1516
ls List running Apps
1617
pull Pull an App image from a registry
1718
push Push an App image to a registry

internal/commands/inspect.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
2929
var opts inspectOptions
3030
cmd := &cobra.Command{
3131
Use: "inspect [OPTIONS] RUNNING_APP",
32-
Short: "Shows installation and App metadata, parameters and the service list of a running App",
32+
Short: "Shows status, metadata, parameters and the list of services of a running App",
3333
Example: `$ docker app inspect my-running-app
3434
$ docker app inspect my-running-app:1.0.0`,
35-
Args: cli.ExactArgs(1),
36-
Hidden: true,
35+
Args: cli.ExactArgs(1),
3736
RunE: func(cmd *cobra.Command, args []string) error {
3837
return runInspect(dockerCli, firstOrEmpty(args), opts)
3938
},
@@ -88,7 +87,7 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
8887
} else {
8988
return fmt.Errorf("inspect failed: status action is not supported by the App")
9089
}
91-
if err := a.Run(&installation.Claim, creds, nil); err != nil {
90+
if err := a.Run(&installation.Claim, creds); err != nil {
9291
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
9392
}
9493

internal/inspect/inspect_test.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"fmt"
66
"os"
77
"testing"
8+
"time"
89

10+
"github.com/deislabs/cnab-go/bundle"
11+
"github.com/deislabs/cnab-go/claim"
912
"github.com/docker/app/internal"
13+
"github.com/docker/app/internal/store"
1014
"github.com/docker/app/types"
1115
"gotest.tools/assert"
1216
"gotest.tools/fs"
@@ -22,7 +26,7 @@ type inspectTestCase struct {
2226
args map[string]string
2327
}
2428

25-
func TestInspect(t *testing.T) {
29+
func TestImageInspect(t *testing.T) {
2630
dir := fs.NewDir(t, "inspect",
2731
fs.WithDir("no-maintainers",
2832
fs.WithFile(internal.ComposeFileName, composeYAML),
@@ -121,14 +125,14 @@ text: hello`),
121125
{name: "full"},
122126
} {
123127
os.Setenv(internal.DockerInspectFormatEnvVar, "pretty")
124-
testInspect(t, dir, testcase, "")
128+
testImageInspect(t, dir, testcase, "")
125129
os.Setenv(internal.DockerInspectFormatEnvVar, "json")
126-
testInspect(t, dir, testcase, "-json")
130+
testImageInspect(t, dir, testcase, "-json")
127131
}
128132
})
129133
}
130134

131-
func testInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix string) {
135+
func testImageInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix string) {
132136
app, err := types.NewAppFromDefaultFiles(dir.Join(testcase.name))
133137
assert.NilError(t, err)
134138
// Inspect twice to ensure output is stable (e.g. sorting of maps)
@@ -141,3 +145,66 @@ func testInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix str
141145
})
142146
}
143147
}
148+
149+
func getInstallation() store.Installation {
150+
created := time.Now().Add(time.Hour * -24)
151+
modified := time.Now().Add(time.Hour * -17)
152+
b := bundle.Bundle{
153+
SchemaVersion: "1.0.0",
154+
Name: "hello-world",
155+
Version: "0.1.0",
156+
Description: "Hello, World!",
157+
}
158+
i := store.Installation{
159+
Claim: claim.Claim{
160+
Name: "hello-world",
161+
Revision: "01DS2ZW4QKPXHTZXZ8YAP6S9W2",
162+
Created: created,
163+
Modified: modified,
164+
Bundle: &b,
165+
Result: claim.Result{
166+
Action: "upgrade",
167+
Status: "success",
168+
},
169+
Parameters: map[string]interface{}{
170+
"com.docker.app.args": "{}",
171+
"com.docker.app.inspect-format": "json",
172+
"com.docker.app.kubernetes-namespace": "default",
173+
"com.docker.app.orchestrator": "",
174+
"com.docker.app.render-format": "yaml",
175+
"com.docker.app.share-registry-creds": false,
176+
"port": "8080",
177+
"text": "Hello, World!",
178+
},
179+
},
180+
Reference: "docker.io/sirot/hello-world:0.1.0",
181+
}
182+
return i
183+
}
184+
185+
func TestInspect(t *testing.T) {
186+
i := getInstallation()
187+
188+
testCases := []struct {
189+
name string
190+
format string
191+
}{
192+
{
193+
name: "pretty",
194+
format: "pretty",
195+
},
196+
{
197+
name: "json",
198+
format: "json",
199+
},
200+
}
201+
202+
for _, testCase := range testCases {
203+
t.Run(testCase.name, func(t *testing.T) {
204+
var out bytes.Buffer
205+
err := Inspect(&out, &i, testCase.format, "swarm")
206+
assert.NilError(t, err)
207+
golden.Assert(t, out.String(), fmt.Sprintf("inspect-app-%s.golden", testCase.name))
208+
})
209+
}
210+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"Installation": {
3+
"Name": "hello-world",
4+
"Created": "1 day ago",
5+
"Modified": "17 hours ago",
6+
"Revision": "01DS2ZW4QKPXHTZXZ8YAP6S9W2",
7+
"Last Action": "upgrade",
8+
"Result": "success",
9+
"Orchestrator": "swarm"
10+
},
11+
"Application": {
12+
"Name": "hello-world",
13+
"Version": "0.1.0",
14+
"ImageReference": "docker.io/sirot/hello-world:0.1.0"
15+
},
16+
"Parameters": {
17+
"port": "8080",
18+
"text": "Hello, World!"
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Installation:
2+
Name: hello-world
3+
Created: 1 day ago
4+
Modified: 17 hours ago
5+
Revision: 01DS2ZW4QKPXHTZXZ8YAP6S9W2
6+
Last Action: upgrade
7+
Result: success
8+
Ochestrator: swarm
9+
10+
Application:
11+
Name: hello-world
12+
Version: 0.1.0
13+
Image Reference: docker.io/sirot/hello-world:0.1.0
14+
15+
Parameters:
16+
port: "8080"
17+
text: Hello, World!
18+

0 commit comments

Comments
 (0)