From 59f7abe7495f32af0a80b8dc8f1cb2dedd259a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Wed, 23 Mar 2016 17:40:52 +0100 Subject: [PATCH 1/2] Add infrastructure for testing cli.Command.Action handlers Also split creation of cli.App from main(), and add a test helper function. This does not change behavior at the moment, but will allow writing tests of the command handlers. --- cmd/skopeo/main.go | 8 +++++++- cmd/skopeo/main_test.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 cmd/skopeo/main_test.go diff --git a/cmd/skopeo/main.go b/cmd/skopeo/main.go index 0062ad5a..afffd63e 100644 --- a/cmd/skopeo/main.go +++ b/cmd/skopeo/main.go @@ -17,7 +17,8 @@ const ( usage = `interact with registries` ) -func main() { +// createApp returns a cli.App to be run or tested. +func createApp() *cli.App { app := cli.NewApp() app.Name = "skopeo" if gitCommit != "" { @@ -67,6 +68,11 @@ func main() { standaloneSignCmd, standaloneVerifyCmd, } + return app +} + +func main() { + app := createApp() if err := app.Run(os.Args); err != nil { logrus.Fatal(err) } diff --git a/cmd/skopeo/main_test.go b/cmd/skopeo/main_test.go new file mode 100644 index 00000000..42df5069 --- /dev/null +++ b/cmd/skopeo/main_test.go @@ -0,0 +1,14 @@ +package main + +import "bytes" + +// runSkopeo creates an app object and runs it with args, with an implied first "skopeo". +// Returns output intended for stdout and the returned error, if any. +func runSkopeo(args ...string) (string, error) { + app := createApp() + stdout := bytes.Buffer{} + app.Writer = &stdout + args = append([]string{"skopeo"}, args...) + err := app.Run(args) + return stdout.String(), err +} From df076baf565bd00e9419b6d5e09175fd35523278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Wed, 23 Mar 2016 17:42:10 +0100 Subject: [PATCH 2/2] Use cli.Context.App.Writer in the "inspect" and "standalone-verify" commands This will make the implementations testable in the future, and prevent spreading the untestable code via copy&paste. --- cmd/skopeo/inspect.go | 4 ++-- cmd/skopeo/signing.go | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/skopeo/inspect.go b/cmd/skopeo/inspect.go index 0fc0e42e..45ad9877 100644 --- a/cmd/skopeo/inspect.go +++ b/cmd/skopeo/inspect.go @@ -43,7 +43,7 @@ var inspectCmd = cli.Command{ return err } if c.Bool("raw") { - fmt.Println(string(rawManifest)) + fmt.Fprintln(c.App.Writer, string(rawManifest)) return nil } imgInspect, err := img.Inspect() @@ -77,7 +77,7 @@ var inspectCmd = cli.Command{ if err != nil { return err } - fmt.Println(string(out)) + fmt.Fprintln(c.App.Writer, string(out)) return nil }, } diff --git a/cmd/skopeo/signing.go b/cmd/skopeo/signing.go index bd4d2393..a935e2cd 100644 --- a/cmd/skopeo/signing.go +++ b/cmd/skopeo/signing.go @@ -70,7 +70,6 @@ func standaloneVerify(context *cli.Context) error { mech, err := signature.NewGPGSigningMechanism() if err != nil { - return fmt.Errorf("Error initializing GPG: %v", err) } sig, err := signature.VerifyDockerManifestSignature(unverifiedSignature, unverifiedManifest, expectedDockerReference, mech, expectedFingerprint) @@ -78,7 +77,7 @@ func standaloneVerify(context *cli.Context) error { return fmt.Errorf("Error verifying signature: %v", err) } - fmt.Printf("Signature verified, digest %s\n", sig.DockerManifestDigest) + fmt.Fprintf(context.App.Writer, "Signature verified, digest %s\n", sig.DockerManifestDigest) return nil }