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

Commit 6c24107

Browse files
committed
Formatter e2e tests
Signed-off-by: Joffrey F <joffrey@docker.com>
1 parent 928fd45 commit 6c24107

9 files changed

Lines changed: 233 additions & 112 deletions

File tree

cmd/docker-app/render.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func renderCmd(dockerCli command.Cli) *cobra.Command {
4848
return err
4949
}
5050
if renderOutput == "-" {
51-
fmt.Fprint(dockerCli.Out(), string(res))
51+
fmt.Fprint(dockerCli.Out(), res)
5252
} else {
5353
f, err := os.Create(renderOutput)
5454
if err != nil {
5555
return err
5656
}
57-
fmt.Fprint(f, string(res))
57+
fmt.Fprint(f, res)
5858
}
5959
return nil
6060
},
@@ -69,6 +69,6 @@ func renderCmd(dockerCli command.Cli) *cobra.Command {
6969
cmd.Flags().StringArrayVarP(&renderSettingsFile, "settings-files", "f", []string{}, "Override settings files")
7070
cmd.Flags().StringArrayVarP(&renderEnv, "set", "s", []string{}, "Override settings values")
7171
cmd.Flags().StringVarP(&renderOutput, "output", "o", "-", "Output file")
72-
cmd.Flags().StringVarP(&formatDriver, "presenter", "p", "yaml", "Configure the output format")
72+
cmd.Flags().StringVarP(&formatDriver, "presenter", "p", "yaml", "Configure the output format (yaml|json)")
7373
return cmd
7474
}

e2e/commands_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ func testRenderApp(appPath string, env ...string) func(*testing.T) {
7676
}
7777
}
7878

79+
func TestRenderFormatters(t *testing.T) {
80+
appPath := filepath.Join("testdata", "fork", "simple.dockerapp")
81+
result := icmd.RunCommand(dockerApp, "render", "-p", "json", appPath).Assert(t, icmd.Success)
82+
assert.Assert(t, golden.String(result.Stdout(), "expected-json-render.golden"))
83+
84+
result = icmd.RunCommand(dockerApp, "render", "-p", "yaml", appPath).Assert(t, icmd.Success)
85+
assert.Assert(t, golden.String(result.Stdout(), "expected-yaml-render.golden"))
86+
}
87+
7988
func TestInit(t *testing.T) {
8089
composeData := `version: "3.2"
8190
services:
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"networks": {
3+
"back": {
4+
"ipam": {},
5+
"external": false
6+
},
7+
"front": {
8+
"ipam": {},
9+
"external": false
10+
}
11+
},
12+
"services": {
13+
"api": {
14+
"build": {},
15+
"credential_spec": {},
16+
"deploy": {
17+
"resources": {},
18+
"placement": {}
19+
},
20+
"image": "python:3.6",
21+
"networks": {
22+
"back": null,
23+
"front": {
24+
"aliases": [
25+
"corp.app.api.com",
26+
"coolapp.com"
27+
]
28+
}
29+
}
30+
},
31+
"db": {
32+
"build": {},
33+
"credential_spec": {},
34+
"deploy": {
35+
"resources": {},
36+
"placement": {}
37+
},
38+
"image": "postgres:9.3",
39+
"networks": {
40+
"back": null
41+
}
42+
},
43+
"web": {
44+
"build": {},
45+
"credential_spec": {},
46+
"deploy": {
47+
"resources": {},
48+
"placement": {}
49+
},
50+
"image": "nginx:latest",
51+
"networks": {
52+
"front": null
53+
},
54+
"ports": [
55+
{
56+
"mode": "ingress",
57+
"target": 80,
58+
"published": 8082,
59+
"protocol": "tcp"
60+
}
61+
],
62+
"volumes": [
63+
{
64+
"type": "volume",
65+
"source": "static",
66+
"target": "/opt/data/static"
67+
}
68+
]
69+
}
70+
},
71+
"version": "3.6",
72+
"volumes": {
73+
"static": {
74+
"name": "corp/web-static-data",
75+
"external": true
76+
}
77+
}
78+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "3.6"
2+
services:
3+
api:
4+
image: python:3.6
5+
networks:
6+
back: null
7+
front:
8+
aliases:
9+
- corp.app.api.com
10+
- coolapp.com
11+
db:
12+
image: postgres:9.3
13+
networks:
14+
back: null
15+
web:
16+
image: nginx:latest
17+
networks:
18+
front: null
19+
ports:
20+
- mode: ingress
21+
target: 80
22+
published: 8082
23+
protocol: tcp
24+
volumes:
25+
- type: volume
26+
source: static
27+
target: /opt/data/static
28+
networks:
29+
back: {}
30+
front: {}
31+
volumes:
32+
static:
33+
name: corp/web-static-data
34+
external: true
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package driver
22

33
import (
4-
composetypes "github.com/docker/cli/cli/compose/types"
4+
composetypes "github.com/docker/cli/cli/compose/types"
55
)
66

77
// Driver is the interface that must be implemented by a formatter driver.
88
type Driver interface {
9-
// Format executes the formatter on the source config
10-
Format(config *composetypes.Config) (string, error)
9+
// Format executes the formatter on the source config
10+
Format(config *composetypes.Config) (string, error)
1111
}

internal/formatter/formatter.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
package formatter
22

33
import (
4-
"sort"
5-
"sync"
4+
"sort"
5+
"sync"
66

7-
"github.com/docker/app/internal/formatter/driver"
8-
composetypes "github.com/docker/cli/cli/compose/types"
9-
"github.com/pkg/errors"
7+
"github.com/docker/app/internal/formatter/driver"
8+
composetypes "github.com/docker/cli/cli/compose/types"
9+
"github.com/pkg/errors"
1010
)
1111

1212
var (
13-
driversMu sync.RWMutex
14-
drivers = map[string]driver.Driver{}
13+
driversMu sync.RWMutex
14+
drivers = map[string]driver.Driver{}
1515
)
1616

1717
// Register makes a formatter available by the provided name.
1818
// If Register is called twice with the same name or if driver is nil,
1919
// it panics.
2020
func Register(name string, driver driver.Driver) {
21-
driversMu.Lock()
22-
defer driversMu.Unlock()
23-
if driver == nil {
24-
panic("formatter: Register driver is nil")
25-
}
26-
if _, dup := drivers[name]; dup {
27-
panic("formatter: Register called twice for driver " + name)
28-
}
29-
drivers[name] = driver
21+
driversMu.Lock()
22+
defer driversMu.Unlock()
23+
if driver == nil {
24+
panic("formatter: Register driver is nil")
25+
}
26+
if _, dup := drivers[name]; dup {
27+
panic("formatter: Register called twice for driver " + name)
28+
}
29+
drivers[name] = driver
3030
}
3131

3232
// Format uses the specified formatter to create a printable output.
3333
// If the formatter is not registered, this errors out.
3434
func Format(config *composetypes.Config, formatter string) (string, error) {
35-
driversMu.RLock()
36-
d, present := drivers[formatter]
37-
driversMu.RUnlock()
38-
if !present {
39-
return "", errors.Errorf("unknown formatter %s", formatter)
40-
}
41-
s, err := d.Format(config)
42-
if err != nil {
43-
return "", err
44-
}
45-
return s, nil
35+
driversMu.RLock()
36+
d, present := drivers[formatter]
37+
driversMu.RUnlock()
38+
if !present {
39+
return "", errors.Errorf("unknown presenter %q", formatter)
40+
}
41+
s, err := d.Format(config)
42+
if err != nil {
43+
return "", err
44+
}
45+
return s, nil
4646
}
4747

4848
// Drivers returns a sorted list of the names of the registered drivers.
4949
func Drivers() []string {
50-
list := []string{}
51-
driversMu.RLock()
52-
for name := range drivers {
53-
list = append(list, name)
54-
}
55-
driversMu.RUnlock()
56-
sort.Strings(list)
57-
return list
50+
list := []string{}
51+
driversMu.RLock()
52+
for name := range drivers {
53+
list = append(list, name)
54+
}
55+
driversMu.RUnlock()
56+
sort.Strings(list)
57+
return list
5858
}

0 commit comments

Comments
 (0)