From 93615dd967b7b88371207973e55c138ee8cb21d4 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 20 Dec 2017 17:54:31 -0500 Subject: [PATCH] Update some assertions. and fix some tests Signed-off-by: Daniel Nephin --- cli/command/image/history_test.go | 26 ++++++++----------- .../history-command-success.non-human.golden | 2 ++ cli/command/stack/remove_test.go | 6 ++--- cli/command/system/prune_test.go | 10 +++++-- cli/compose/loader/loader_test.go | 13 +++++----- cli/compose/template/template_test.go | 16 ++++++------ 6 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 cli/command/image/testdata/history-command-success.non-human.golden diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index eb656e5cf4..c58ed151a2 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -47,7 +47,6 @@ func TestNewHistoryCommandSuccess(t *testing.T) { testCases := []struct { name string args []string - outputRegex string imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error) }{ { @@ -64,16 +63,17 @@ func TestNewHistoryCommandSuccess(t *testing.T) { name: "quiet", args: []string{"--quiet", "image:tag"}, }, - // TODO: This test is failing since the output does not contain an RFC3339 date - //{ - // name: "non-human", - // args: []string{"--human=false", "image:tag"}, - // outputRegex: "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}", // RFC3339 date format match - //}, { - name: "non-human-header", - args: []string{"--human=false", "image:tag"}, - outputRegex: "CREATED\\sAT", + name: "non-human", + args: []string{"--human=false", "image:tag"}, + imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) { + return []image.HistoryResponseItem{{ + ID: "abcdef", + Created: time.Date(2017, 1, 1, 12, 0, 3, 0, time.UTC).Unix(), + CreatedBy: "rose", + Comment: "new history item!", + }}, nil + }, }, { name: "quiet-no-trunc", @@ -94,10 +94,6 @@ func TestNewHistoryCommandSuccess(t *testing.T) { err := cmd.Execute() assert.NoError(t, err) actual := cli.OutBuffer().String() - if tc.outputRegex == "" { - golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) - } else { - assert.Regexp(t, tc.outputRegex, actual) - } + golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) } } diff --git a/cli/command/image/testdata/history-command-success.non-human.golden b/cli/command/image/testdata/history-command-success.non-human.golden new file mode 100644 index 0000000000..4a83a3d837 --- /dev/null +++ b/cli/command/image/testdata/history-command-success.non-human.golden @@ -0,0 +1,2 @@ +IMAGE CREATED AT CREATED BY SIZE COMMENT +abcdef 2017-01-01T12:00:03Z rose 0 new history item! diff --git a/cli/command/stack/remove_test.go b/cli/command/stack/remove_test.go index 0ce5cdacfc..2d59d4c343 100644 --- a/cli/command/stack/remove_test.go +++ b/cli/command/stack/remove_test.go @@ -48,8 +48,8 @@ func TestRemoveStackVersion124DoesNotRemoveConfigsOrSecrets(t *testing.T) { assert.NoError(t, cmd.Execute()) assert.Equal(t, buildObjectIDs(client.services), client.removedServices) assert.Equal(t, buildObjectIDs(client.networks), client.removedNetworks) - assert.Nil(t, client.removedSecrets) - assert.Nil(t, client.removedConfigs) + assert.Len(t, client.removedSecrets, 0) + assert.Len(t, client.removedConfigs, 0) } func TestRemoveStackVersion125DoesNotRemoveConfigs(t *testing.T) { @@ -61,7 +61,7 @@ func TestRemoveStackVersion125DoesNotRemoveConfigs(t *testing.T) { assert.Equal(t, buildObjectIDs(client.services), client.removedServices) assert.Equal(t, buildObjectIDs(client.networks), client.removedNetworks) assert.Equal(t, buildObjectIDs(client.secrets), client.removedSecrets) - assert.Nil(t, client.removedConfigs) + assert.Len(t, client.removedConfigs, 0) } func TestRemoveStackVersion130RemovesEverything(t *testing.T) { diff --git a/cli/command/system/prune_test.go b/cli/command/system/prune_test.go index 0d694ce066..bb6ee4b1c3 100644 --- a/cli/command/system/prune_test.go +++ b/cli/command/system/prune_test.go @@ -7,9 +7,15 @@ import ( "github.com/stretchr/testify/assert" ) -func TestPrunePromptPre131(t *testing.T) { +func TestPrunePromptPre131DoesNotIncludeBuildCache(t *testing.T) { cli := test.NewFakeCli(&fakeClient{version: "1.30"}) cmd := newPruneCommand(cli) assert.NoError(t, cmd.Execute()) - assert.NotContains(t, cli.OutBuffer().String(), "all build cache") + expected := `WARNING! This will remove: + - all stopped containers + - all networks not used by at least one container + - all dangling images +Are you sure you want to continue? [y/N] ` + assert.Equal(t, expected, cli.OutBuffer().String()) + } diff --git a/cli/compose/loader/loader_test.go b/cli/compose/loader/loader_test.go index 09d3744e84..0470c9b300 100644 --- a/cli/compose/loader/loader_test.go +++ b/cli/compose/loader/loader_test.go @@ -2,7 +2,6 @@ package loader import ( "bytes" - "fmt" "io/ioutil" "os" "sort" @@ -738,13 +737,13 @@ services: `) require.Error(t, err) - assert.IsType(t, &ForbiddenPropertiesError{}, err) - fmt.Println(err) - forbidden := err.(*ForbiddenPropertiesError).Properties + forbidden, ok := err.(*ForbiddenPropertiesError) + assert.True(t, ok, "error type is %T instead of ForbiddenPropertiesError", err) - assert.Len(t, forbidden, 2) - assert.Contains(t, forbidden, "volume_driver") - assert.Contains(t, forbidden, "extends") + props := forbidden.Properties + assert.Len(t, props, 2) + assert.Contains(t, props, "volume_driver") + assert.Contains(t, props, "extends") } func TestInvalidResource(t *testing.T) { diff --git a/cli/compose/template/template_test.go b/cli/compose/template/template_test.go index 4fec57e8e8..93557bfff7 100644 --- a/cli/compose/template/template_test.go +++ b/cli/compose/template/template_test.go @@ -20,7 +20,7 @@ func defaultMapping(name string) (string, bool) { func TestEscaped(t *testing.T) { result, err := Substitute("$${foo}", defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "${foo}", result) } @@ -44,14 +44,14 @@ func TestInvalid(t *testing.T) { for _, template := range invalidTemplates { _, err := Substitute(template, defaultMapping) assert.Error(t, err) - assert.IsType(t, &InvalidTemplateError{}, err) + assert.Contains(t, err.Error(), "Invalid template") } } func TestNoValueNoDefault(t *testing.T) { for _, template := range []string{"This ${missing} var", "This ${BAR} var"} { result, err := Substitute(template, defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "This var", result) } } @@ -59,7 +59,7 @@ func TestNoValueNoDefault(t *testing.T) { func TestValueNoDefault(t *testing.T) { for _, template := range []string{"This $FOO var", "This ${FOO} var"} { result, err := Substitute(template, defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "This first var", result) } } @@ -67,26 +67,26 @@ func TestValueNoDefault(t *testing.T) { func TestNoValueWithDefault(t *testing.T) { for _, template := range []string{"ok ${missing:-def}", "ok ${missing-def}"} { result, err := Substitute(template, defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "ok def", result) } } func TestEmptyValueWithSoftDefault(t *testing.T) { result, err := Substitute("ok ${BAR:-def}", defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "ok def", result) } func TestEmptyValueWithHardDefault(t *testing.T) { result, err := Substitute("ok ${BAR-def}", defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "ok ", result) } func TestNonAlphanumericDefault(t *testing.T) { result, err := Substitute("ok ${BAR:-/non:-alphanumeric}", defaultMapping) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "ok /non:-alphanumeric", result) }