From 72371c1c42a64b78daa337ed068d1368a5d5e104 Mon Sep 17 00:00:00 2001 From: Alexander Kuleshov Date: Fri, 23 Dec 2016 23:11:56 +0600 Subject: [PATCH] rkt/cat-manifest: add support for --uuid-file So we will be able to use the value saved on rkt run with --uuid-file-save. --- rkt/cat_manifest.go | 24 ++++++++++++++++++----- tests/rkt_cat_manifest_test.go | 35 +++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/rkt/cat_manifest.go b/rkt/cat_manifest.go index 2c734cd975..567b5279d0 100644 --- a/rkt/cat_manifest.go +++ b/rkt/cat_manifest.go @@ -23,7 +23,7 @@ import ( var ( cmdCatManifest = &cobra.Command{ - Use: "cat-manifest UUID", + Use: "cat-manifest --uuid-file=FILE | UUID ...", Short: "Inspect and print the pod manifest", Long: `UUID should be the UUID of a pod`, Run: runWrapper(runCatManifest), @@ -34,19 +34,33 @@ var ( func init() { cmdRkt.AddCommand(cmdCatManifest) cmdCatManifest.Flags().BoolVar(&flagPMPrettyPrint, "pretty-print", true, "apply indent to format the output") + cmdCatManifest.Flags().StringVar(&flagUUIDFile, "uuid-file", "", "read pod UUID from file instead of argument") } func runCatManifest(cmd *cobra.Command, args []string) (exit int) { - if len(args) != 1 { - cmd.Usage() - return 254 + var podUUID string + + if flagUUIDFile != "" { + uuid, err := pkgPod.ReadUUIDFromFile(flagUUIDFile) + if err != nil { + stderr.PrintE("unable to resolve UUID from file", err) + return 254 + } + podUUID = uuid + } else { + if len(args) != 1 { + cmd.Usage() + return 254 + } + podUUID = args[0] } - pod, err := pkgPod.PodFromUUIDString(getDataDir(), args[0]) + pod, err := pkgPod.PodFromUUIDString(getDataDir(), podUUID) if err != nil { stderr.PrintE("problem retrieving pod", err) return 254 } + defer pod.Close() _, manifest, err := pod.PodManifest() diff --git a/tests/rkt_cat_manifest_test.go b/tests/rkt_cat_manifest_test.go index 8f74c90a79..130228e85e 100644 --- a/tests/rkt_cat_manifest_test.go +++ b/tests/rkt_cat_manifest_test.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "io/ioutil" "os" "testing" @@ -45,26 +46,50 @@ func TestCatManifest(t *testing.T) { defer os.RemoveAll(tmpDir) tests := []struct { - uuid string - match string + uuid string + match string + uuidFile bool }{ { podUuid, imgName, + false, }, { podUuid, imageHash[:20], + false, }, { "1234567890abcdef", "no matches found for", + false, + }, + { + "", + imageHash[:20], + true, }, } for i, tt := range tests { - runCmd := fmt.Sprintf("%s cat-manifest --pretty-print=false %s", ctx.Cmd(), tt.uuid) - t.Logf("Running test #%d", i) - runRktAndCheckRegexOutput(t, runCmd, tt.match) + if tt.uuidFile == true { + podUUID := runRktAndGetUUID(t, cmd) + uuidFile, err := ioutil.TempFile(tmpDir, "uuid-file") + if err != nil { + panic(fmt.Sprintf("Cannot create uuid-file: %v", err)) + } + uuidFilePath := uuidFile.Name() + if err := ioutil.WriteFile(uuidFilePath, []byte(podUUID), 0600); err != nil { + panic(fmt.Sprintf("Cannot write pod UUID to uuid-file: %v", err)) + } + runCmd := fmt.Sprintf("%s cat-manifest --uuid-file=%s --pretty-print=false %s", ctx.Cmd(), uuidFilePath) + t.Logf("Running test #%d", i) + runRktAndCheckRegexOutput(t, runCmd, tt.match) + } else { + runCmd := fmt.Sprintf("%s cat-manifest --pretty-print=false %s", ctx.Cmd(), tt.uuid) + t.Logf("Running test #%d", i) + runRktAndCheckRegexOutput(t, runCmd, tt.match) + } } }