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

Validate parameters against the actual compose file #710

Merged
merged 2 commits into from
Oct 29, 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
29 changes: 29 additions & 0 deletions e2e/testdata/invalid/unused_parameter.dockerapp/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "3.6"
services:
api:
image: python:3.6
networks:
back:
front:
aliases:
- api.example.com
- ${api.host}
web:
image: nginx:latest
networks:
- front
volumes:
- static:/opt/${static_subdir}
ports:
- ${web.port}:80
db:
image: postgres:9.3
networks:
- back
networks:
front:
back:
volumes:
static:
external: true
name: corp/web-static-data
8 changes: 8 additions & 0 deletions e2e/testdata/invalid/unused_parameter.dockerapp/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 1.1.0-beta1
name: simple
description: "new fancy webapp with microservices"
maintainers:
- name: John Developer
email: [email protected]
- name: Jane Developer
email: [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
web:
port: '8082'
api:
host: example.com
static_subdir: data/static
unused:
parameter: FOO
20 changes: 20 additions & 0 deletions e2e/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package e2e

import (
"path"
"strings"
"testing"

"gotest.tools/assert"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import order looks wrong 🤣

"gotest.tools/icmd"
)

func TestOrphanedParameter(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
p := path.Join("testdata", "invalid", "unused_parameter")
cmd.Command = dockerCli.Command("app", "validate", p)
out := icmd.RunCmd(cmd).Assert(t, icmd.Expected{ExitCode: 1}).Combined()
assert.Assert(t, strings.Contains(out, "unused.parameter is declared as parameter but not used by the compose file"))
}
45 changes: 31 additions & 14 deletions internal/commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"os"

"github.com/docker/app/internal/compose"
silvin-lubecki marked this conversation as resolved.
Show resolved Hide resolved
"github.com/docker/app/internal/packager"
"github.com/docker/app/render"
"github.com/docker/app/types"
"github.com/docker/cli/cli"
cliopts "github.com/docker/cli/opts"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -23,22 +25,37 @@ func validateCmd() *cobra.Command {
Short: "Checks the rendered application is syntactically correct",
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := packager.Extract(firstOrEmpty(args),
types.WithParametersFiles(opts.parametersFiles...),
)
if err != nil {
return err
}
defer app.Cleanup()
argParameters := cliopts.ConvertKVStringsToMap(opts.overrides)
_, err = render.Render(app, argParameters, nil)
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "Validated %q\n", app.Path)
return nil
return runValidate(args, opts)
},
}
opts.parametersOptions.addFlags(cmd.Flags())
return cmd
}

func runValidate(args []string, opts validateOptions) error {
app, err := packager.Extract(firstOrEmpty(args),
types.WithParametersFiles(opts.parametersFiles...),
)
if err != nil {
return err
}
defer app.Cleanup()
argParameters := cliopts.ConvertKVStringsToMap(opts.overrides)
_, err = render.Render(app, argParameters, nil)
if err != nil {
return err
}

vars, err := compose.ExtractVariables(app.Composes()[0], compose.ExtrapolationPattern)
if err != nil {
return errors.Wrap(err, "failed to parse compose file")
}
for k := range app.Parameters().Flatten() {
if _, ok := vars[k]; !ok {
return fmt.Errorf("%s is declared as parameter but not used by the compose file", k)
silvin-lubecki marked this conversation as resolved.
Show resolved Hide resolved
}
}

fmt.Fprintf(os.Stdout, "Validated %q\n", app.Path)
return nil
}