diff --git a/opencloud/pkg/command/decomposedfs.go b/opencloud/pkg/command/decomposedfs.go index 4807d968da..5285dcbb3b 100644 --- a/opencloud/pkg/command/decomposedfs.go +++ b/opencloud/pkg/command/decomposedfs.go @@ -183,10 +183,11 @@ func metadataCmd(cfg *config.Config) *cobra.Command { Short: `cli tools to inspect and manipulate node metadata`, } metaCmd.AddCommand(dumpCmd(cfg), getCmd(cfg), setCmd(cfg)) - metaCmd.Flags().StringP("root", "r", "", "Path to the root directory of the decomposedfs") - _ = metaCmd.MarkFlagRequired("root") - metaCmd.Flags().StringP("node", "n", "", "Path to or ID of the node to inspect") - _ = metaCmd.MarkFlagRequired("node") + // persistent, so that the subcommands dump, get and set, which read them, have them + metaCmd.PersistentFlags().StringP("root", "r", "", "Path to the root directory of the decomposedfs") + _ = metaCmd.MarkPersistentFlagRequired("root") + metaCmd.PersistentFlags().StringP("node", "n", "", "Path to or ID of the node to inspect") + _ = metaCmd.MarkPersistentFlagRequired("node") return metaCmd } diff --git a/opencloud/pkg/command/decomposedfs_test.go b/opencloud/pkg/command/decomposedfs_test.go index f2e5985305..632a0d8e4c 100644 --- a/opencloud/pkg/command/decomposedfs_test.go +++ b/opencloud/pkg/command/decomposedfs_test.go @@ -1,6 +1,7 @@ package command import ( + "io" "testing" "github.com/opencloud-eu/reva/v3/pkg/storage/pkg/decomposedfs/metadata" @@ -36,3 +37,63 @@ func TestGetBackend(t *testing.T) { require.Nil(t, b) }) } + +// runMetadataCmd runs "opencloud decomposedfs metadata" with the given arguments +func runMetadataCmd(args ...string) error { + cmd := metadataCmd(nil) + cmd.SetArgs(args) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + return cmd.Execute() +} + +func TestMetadataCommands(t *testing.T) { + // the arguments each subcommand needs besides --root and --node + subcommands := map[string][]string{ + "dump": nil, + "get": {"--attribute", "user.*"}, + "set": {"--attribute", "user.foo", "--value", "bar"}, + } + + for name, extra := range subcommands { + t.Run(name, func(t *testing.T) { + // The root is an empty directory, so the command can't read any metadata + // there, but it only gets that far with the value of --root. + root := t.TempDir() + readRoot := `could not read the metadata in "` + root + `"` + + t.Run("takes --root and --node after the subcommand", func(t *testing.T) { + args := append([]string{name, "--root", root, "--node", "some-node"}, extra...) + err := runMetadataCmd(args...) + require.Error(t, err) + require.Contains(t, err.Error(), readRoot) + }) + + t.Run("takes --root and --node before the subcommand", func(t *testing.T) { + args := append([]string{"--root", root, "--node", "some-node", name}, extra...) + err := runMetadataCmd(args...) + require.Error(t, err) + require.Contains(t, err.Error(), readRoot) + }) + + t.Run("takes the short flags -r and -n", func(t *testing.T) { + args := append([]string{name, "-r", root, "-n", "some-node"}, extra...) + err := runMetadataCmd(args...) + require.Error(t, err) + require.Contains(t, err.Error(), readRoot) + }) + + t.Run("requires --root", func(t *testing.T) { + args := append([]string{name, "--node", "some-node"}, extra...) + err := runMetadataCmd(args...) + require.EqualError(t, err, `required flag(s) "root" not set`) + }) + + t.Run("requires --node", func(t *testing.T) { + args := append([]string{name, "--root", root}, extra...) + err := runMetadataCmd(args...) + require.EqualError(t, err, `required flag(s) "node" not set`) + }) + }) + } +}