From 5c6c2d23330890442bcabec0bf34fe81b12279e1 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Sirot Date: Fri, 8 Nov 2019 12:41:27 +0100 Subject: [PATCH] Add App inspect unit tests Signed-off-by: Jean-Christophe Sirot --- internal/inspect/inspect_test.go | 75 ++++++++++++++++++- .../inspect/testdata/inspect-app-json.golden | 20 +++++ .../testdata/inspect-app-pretty.golden | 18 +++++ 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 internal/inspect/testdata/inspect-app-json.golden create mode 100644 internal/inspect/testdata/inspect-app-pretty.golden diff --git a/internal/inspect/inspect_test.go b/internal/inspect/inspect_test.go index 0b5fbaa58..11f1e2bee 100644 --- a/internal/inspect/inspect_test.go +++ b/internal/inspect/inspect_test.go @@ -5,8 +5,12 @@ import ( "fmt" "os" "testing" + "time" + "github.com/deislabs/cnab-go/bundle" + "github.com/deislabs/cnab-go/claim" "github.com/docker/app/internal" + "github.com/docker/app/internal/store" "github.com/docker/app/types" "gotest.tools/assert" "gotest.tools/fs" @@ -22,7 +26,7 @@ type inspectTestCase struct { args map[string]string } -func TestInspect(t *testing.T) { +func TestImageInspect(t *testing.T) { dir := fs.NewDir(t, "inspect", fs.WithDir("no-maintainers", fs.WithFile(internal.ComposeFileName, composeYAML), @@ -121,14 +125,14 @@ text: hello`), {name: "full"}, } { os.Setenv(internal.DockerInspectFormatEnvVar, "pretty") - testInspect(t, dir, testcase, "") + testImageInspect(t, dir, testcase, "") os.Setenv(internal.DockerInspectFormatEnvVar, "json") - testInspect(t, dir, testcase, "-json") + testImageInspect(t, dir, testcase, "-json") } }) } -func testInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix string) { +func testImageInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix string) { app, err := types.NewAppFromDefaultFiles(dir.Join(testcase.name)) assert.NilError(t, err) // 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 }) } } + +func getInstallation() store.Installation { + created := time.Now().Add(time.Hour * -24) + modified := time.Now().Add(time.Hour * -17) + b := bundle.Bundle{ + SchemaVersion: "1.0.0", + Name: "hello-world", + Version: "0.1.0", + Description: "Hello, World!", + } + i := store.Installation{ + Claim: claim.Claim{ + Name: "hello-world", + Revision: "01DS2ZW4QKPXHTZXZ8YAP6S9W2", + Created: created, + Modified: modified, + Bundle: &b, + Result: claim.Result{ + Action: "upgrade", + Status: "success", + }, + Parameters: map[string]interface{}{ + "com.docker.app.args": "{}", + "com.docker.app.inspect-format": "json", + "com.docker.app.kubernetes-namespace": "default", + "com.docker.app.orchestrator": "", + "com.docker.app.render-format": "yaml", + "com.docker.app.share-registry-creds": false, + "port": "8080", + "text": "Hello, World!", + }, + }, + Reference: "docker.io/sirot/hello-world:0.1.0", + } + return i +} + +func TestInspect(t *testing.T) { + i := getInstallation() + + testCases := []struct { + name string + format string + }{ + { + name: "pretty", + format: "pretty", + }, + { + name: "json", + format: "json", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + var out bytes.Buffer + err := Inspect(&out, &i, testCase.format, "swarm") + assert.NilError(t, err) + golden.Assert(t, out.String(), fmt.Sprintf("inspect-app-%s.golden", testCase.name)) + }) + } +} diff --git a/internal/inspect/testdata/inspect-app-json.golden b/internal/inspect/testdata/inspect-app-json.golden new file mode 100644 index 000000000..15ba5104b --- /dev/null +++ b/internal/inspect/testdata/inspect-app-json.golden @@ -0,0 +1,20 @@ +{ + "Installation": { + "Name": "hello-world", + "Created": "1 day ago", + "Modified": "17 hours ago", + "Revision": "01DS2ZW4QKPXHTZXZ8YAP6S9W2", + "Last Action": "upgrade", + "Result": "success", + "Orchestrator": "swarm" + }, + "Application": { + "Name": "hello-world", + "Version": "0.1.0", + "ImageReference": "docker.io/sirot/hello-world:0.1.0" + }, + "Parameters": { + "port": "8080", + "text": "Hello, World!" + } +} diff --git a/internal/inspect/testdata/inspect-app-pretty.golden b/internal/inspect/testdata/inspect-app-pretty.golden new file mode 100644 index 000000000..e9d8973f5 --- /dev/null +++ b/internal/inspect/testdata/inspect-app-pretty.golden @@ -0,0 +1,18 @@ +Installation: + Name: hello-world + Created: 1 day ago + Modified: 17 hours ago + Revision: 01DS2ZW4QKPXHTZXZ8YAP6S9W2 + Last Action: upgrade + Result: success + Ochestrator: swarm + +Application: + Name: hello-world + Version: 0.1.0 + Image Reference: docker.io/sirot/hello-world:0.1.0 + +Parameters: + port: "8080" + text: Hello, World! +