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

Commit

Permalink
Add App inspect unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Sirot <[email protected]>
  • Loading branch information
Jean-Christophe Sirot committed Nov 8, 2019
1 parent b58651e commit e6522c7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
75 changes: 71 additions & 4 deletions internal/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand All @@ -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))
})
}
}
20 changes: 20 additions & 0 deletions internal/inspect/testdata/inspect-app-json.golden
Original file line number Diff line number Diff line change
@@ -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!"
}
}
18 changes: 18 additions & 0 deletions internal/inspect/testdata/inspect-app-pretty.golden
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit e6522c7

Please sign in to comment.