This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
419 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
func addListCommand(mainOptions *mainOptions) (*flags.Command, error) { | ||
parser := mainOptions.Parser() | ||
var containsOptions MatchingOptions | ||
containsOptions.mainOptions = mainOptions | ||
containsOptions.mode = MATCH_AND_PRINT | ||
|
||
return parser.AddCommand("list", | ||
"List image references with matching predicates.", | ||
"Returns exit code 0 when the given input contains at least one image reference that satisfy the given conditions, non-null otherwise", | ||
&containsOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"bytes" | ||
"os" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/MeneDev/dockmoor/dockfmt" | ||
"github.com/MeneDev/dockmoor/dockref" | ||
) | ||
|
||
func ListOptionsTest() *containsOptionsTest { | ||
mainOptions := MainOptionsTest() | ||
containsOptions := containsOptionsTest{ | ||
MatchingOptions: &MatchingOptions{}, | ||
mainOptionsTest: mainOptions, | ||
} | ||
|
||
containsOptions.mainOptions = mainOptions.mainOptions | ||
containsOptions.mode = MATCH_AND_PRINT | ||
|
||
return &containsOptions | ||
} | ||
|
||
|
||
func TestFilenameRequiredWithList(t *testing.T) { | ||
_, _, exitCode, stdout := testMain([]string{"list"}, addListCommand) | ||
assert.NotEqual(t, 0, exitCode) | ||
assert.Contains(t, stdout.String(), "level=error") | ||
assert.Contains(t, stdout.String(), "the required argument `InputFile` was not provided") | ||
} | ||
|
||
func TestListCallsFindExecute(t *testing.T) { | ||
cmd, _, _, _ := testMain([]string{"list", "fileName"}, addListCommand) | ||
|
||
_, ok := cmd.(*MatchingOptions) | ||
assert.True(t, ok) | ||
} | ||
|
||
func TestMainMarkdownWithList(t *testing.T) { | ||
|
||
os.Args = []string {"exe", "--markdown"} | ||
|
||
mainOptions := MainOptionsTestNew(addListCommand) | ||
buffer := bytes.NewBuffer(nil) | ||
mainOptions.SetStdout(buffer) | ||
exitCode := doMain(mainOptions) | ||
|
||
assert.Contains(t, buffer.String(), "list command") | ||
|
||
assert.Equal(t, EXIT_SUCCESS, exitCode) | ||
} | ||
|
||
func TestMainAsciiDocWithList(t *testing.T) { | ||
|
||
os.Args = []string {"exe", "--asciidoc-usage"} | ||
|
||
mainOptions := MainOptionsTestNew(addListCommand) | ||
buffer := bytes.NewBuffer(nil) | ||
mainOptions.SetStdout(buffer) | ||
exitCode := doMain(mainOptions) | ||
|
||
assert.Contains(t, buffer.String(), "list command") | ||
|
||
assert.Equal(t, EXIT_SUCCESS, exitCode) | ||
} | ||
|
||
func TestListHelpIsNotAnError(t *testing.T) { | ||
|
||
os.Args = []string {"exe", "list", "--help"} | ||
|
||
mainOptions := MainOptionsTestNew(addListCommand) | ||
buffer := bytes.NewBuffer(nil) | ||
mainOptions.SetStdout(buffer) | ||
exitCode := doMain(mainOptions) | ||
|
||
assert.Contains(t, buffer.String(), "list command") | ||
|
||
assert.Equal(t, EXIT_SUCCESS, exitCode) | ||
} | ||
|
||
func TestListHelpContainsImplementedPredicates(t *testing.T) { | ||
|
||
os.Args = []string {"exe", "list", "--help"} | ||
|
||
mainOptions := MainOptionsTestNew(addListCommand) | ||
buffer := bytes.NewBuffer(nil) | ||
mainOptions.SetStdout(buffer) | ||
exitCode := doMain(mainOptions) | ||
|
||
assert.Contains(t, buffer.String(), "--any") | ||
assert.Contains(t, buffer.String(), "--latest") | ||
assert.Contains(t, buffer.String(), "--unpinned") | ||
|
||
assert.Equal(t, EXIT_SUCCESS, exitCode) | ||
} | ||
|
||
func TestListHelpHidesUnimplementedPredicates(t *testing.T) { | ||
|
||
os.Args = []string {"exe", "list", "--help"} | ||
|
||
mainOptions := MainOptionsTestNew(addListCommand) | ||
buffer := bytes.NewBuffer(nil) | ||
mainOptions.SetStdout(buffer) | ||
exitCode := doMain(mainOptions) | ||
|
||
assert.NotContains(t, buffer.String(), "--outdated") | ||
assert.NotContains(t, buffer.String(), "--name") | ||
assert.NotContains(t, buffer.String(), "--domain") | ||
|
||
assert.Equal(t, EXIT_SUCCESS, exitCode) | ||
} | ||
|
||
var _ dockfmt.FormatProcessor = (*FormatProcessorMock)(nil) | ||
type FormatProcessorMock struct { | ||
*mock.Mock | ||
|
||
process func(imageNameProcessor dockfmt.ImageNameProcessor) error | ||
} | ||
|
||
func (d *FormatProcessorMock) Process(imageNameProcessor dockfmt.ImageNameProcessor) error { | ||
return d.process(imageNameProcessor) | ||
} | ||
|
||
func TestListCommandPrints(t *testing.T) { | ||
test := ListOptionsTest() | ||
stdout := test.MainOptions().Stdout() | ||
|
||
test.Predicates.Any = true | ||
|
||
processorMock := &FormatProcessorMock{} | ||
processorMock.process = func(imageNameProcessor dockfmt.ImageNameProcessor) error { | ||
r, _ := dockref.FromOriginal("nginx") | ||
imageNameProcessor(r) | ||
r, _ = dockref.FromOriginal("nginx:latest") | ||
imageNameProcessor(r) | ||
r, _ = dockref.FromOriginal("nginx:1.2") | ||
imageNameProcessor(r) | ||
return nil | ||
} | ||
|
||
test.matchFormatProcessor(processorMock) | ||
|
||
s := stdout.String() | ||
assert.Contains(t, s, "nginx") | ||
assert.Contains(t, s, "nginx:latest") | ||
assert.Contains(t, s, "nginx:1.2") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
== Examples | ||
include::cmdList.adoc[] | ||
include::cmdContains.adoc[] | ||
|
||
== Supported Formats | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
[#contains-command-examples] | ||
=== list command | ||
|
||
==== List all image references in file | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listAnyInFile,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listAnyInFile.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listAnyInFile.exitCode[] | ||
|
||
|
||
==== List all image references with latest or no tag in file | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listLatestInFile,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listLatestInFile.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listLatestInFile.exitCode[] | ||
|
||
|
||
==== List all unpinned image references | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listUnpinnedInFile,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listUnpinnedInFile.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listUnpinnedInFile.exitCode[] | ||
|
||
|
||
==== Use unix find to list all unpinned image references | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listUnpinnedInFolder,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listUnpinnedInFolder.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listUnpinnedInFolder.exitCode[] | ||
|
||
|
||
==== Use unix find to list all image references with latest/no tags | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listLatestInFolder,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listLatestInFolder.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listLatestInFolder.exitCode[] | ||
|
||
|
||
==== Use unix find to list all image references | ||
[source,bash] | ||
---- | ||
include::../end-to-end/test.sh[tag=listAnyInFolder,indent=0] | ||
---- | ||
stdout: | ||
__________________________ | ||
include::../end-to-end/results/listAnyInFolder.stdout[indent=1] | ||
__________________________ | ||
stderr is empty + | ||
exit code: | ||
include::../end-to-end/results/listAnyInFolder.exitCode[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM image-name | ||
FROM image-name:latest | ||
FROM image-name:latest@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
FROM image-name:1.12 | ||
FROM image-name:1.12@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
FROM image-name@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
|
||
FROM example.com/image-name | ||
FROM example.com/image-name:latest | ||
FROM example.com/image-name:latest@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
FROM example.com/image-name:1.12 | ||
FROM example.com/image-name:1.12@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
FROM example.com/image-name@sha256:2c4269d573d9fc6e9e95d5e8f3de2dd0b07c19912551f25e848415b5dd783acf | ||
|
Oops, something went wrong.