Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
✨ Add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
MeneDev committed Sep 14, 2018
1 parent a4d8a25 commit 0f5163b
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/dockmoor/00_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func osExit(exitCode ExitCode) { osExitInternal(int(exitCode)) }
func main() {
mainOptions := MainOptionsNew()
addContainsCommand(mainOptions)
addListCommand(mainOptions)

exitCode := doMain(mainOptions)
osExit(exitCode)
Expand Down
20 changes: 20 additions & 0 deletions cmd/dockmoor/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ func TestExitCodeIsZeroForContainsLatestAndDockerfile(t *testing.T) {
assert.Equal(t, EXIT_SUCCESS, code, "Exits with code 0")
}

func TestExitCodeIsZeroForListLatestAndDockerfile(t *testing.T) {
dir, _ := ioutil.TempDir("", "dockmoor")
defer os.RemoveAll(dir)

tmpfn := filepath.Join(dir, "Dockerfile")
dockerfile :=
`FROM nginx`

if err := ioutil.WriteFile(tmpfn, []byte(dockerfile), 0666); err != nil {
log.Fatal(err)
}

stdout, code := shell(t, `dockmoor list --latest {{.Dockerfile}}`, struct {
Dockerfile string
}{tmpfn})

assert.Equal(t, "nginx\n", stdout)
assert.Equal(t, EXIT_SUCCESS, code, "Exits with code 0")
}

func TestExitCodeIsNotZeroForInvalidDockerfile(t *testing.T) {
dir, _ := ioutil.TempDir("", "dockmoor")
defer os.RemoveAll(dir)
Expand Down
17 changes: 17 additions & 0 deletions cmd/dockmoor/cmd_list.go
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)
}
149 changes: 149 additions & 0 deletions cmd/dockmoor/cmd_list_test.go
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")
}
1 change: 1 addition & 0 deletions cmd/dockmoor/doc/_readme.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
== Examples
include::cmdList.adoc[]
include::cmdContains.adoc[]

== Supported Formats
Expand Down
86 changes: 86 additions & 0 deletions cmd/dockmoor/doc/cmdList.adoc
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[]

14 changes: 14 additions & 0 deletions cmd/dockmoor/end-to-end/Dockerfile
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

Loading

0 comments on commit 0f5163b

Please sign in to comment.