Skip to content

Commit

Permalink
Add kopia policy show command and output parser (#2440)
Browse files Browse the repository at this point in the history
* Add kopia policy show command and output parser

* Add/fix comments

* Remove redundant check import

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 1, 2023
1 parent 3051116 commit a5b4e6c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
restoreSubCommand = "restore"
runSubCommand = "run"
setSubCommand = "set"
showSubCommand = "show"
snapshotSubCommand = "snapshot"
statsSubCommand = "stats"

Expand Down
12 changes: 12 additions & 0 deletions pkg/kopia/command/parse_command_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/snapshot"
"github.com/kopia/kopia/snapshot/policy"
"github.com/pkg/errors"

"github.com/kanisterio/kanister/pkg/field"
Expand Down Expand Up @@ -415,3 +416,14 @@ func ErrorsFromOutput(output string) []error {

return err
}

// ParsePolicyShow parses the output of a kopia policy show command.
func ParsePolicyShow(output string) (policy.Policy, error) {
policy := policy.Policy{}

if err := json.Unmarshal([]byte(output), &policy); err != nil {
return policy, errors.Wrap(err, "Failed to unmarshal snapshot manifest list")
}

return policy, nil
}
50 changes: 50 additions & 0 deletions pkg/kopia/command/parse_command_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/kopia/kopia/fs"
"github.com/kopia/kopia/snapshot"
"github.com/kopia/kopia/snapshot/policy"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -658,6 +659,48 @@ func (kParse *KopiaParseUtilsTestSuite) TestErrorsFromOutput(c *C) {
}
}

func (kParse *KopiaParseUtilsTestSuite) TestParsePolicyShow(c *C) {
for _, tc := range []struct {
description string
outputGenFunc func(*C, policy.Policy) string
expPolicyShow policy.Policy
errChecker Checker
}{
{
description: "empty policy show",
outputGenFunc: marshalPolicy,
expPolicyShow: policy.Policy{},
errChecker: IsNil,
},
{
description: "default policy show",
outputGenFunc: marshalPolicy,
expPolicyShow: *policy.DefaultPolicy,
errChecker: IsNil,
},
{
description: "error: parse empty output",
outputGenFunc: func(*C, policy.Policy) string {
return ""
},
errChecker: NotNil,
},
{
description: "error: unmarshal fails",
outputGenFunc: func(*C, policy.Policy) string {
return "asdf"
},
errChecker: NotNil,
},
} {
outputToParse := tc.outputGenFunc(c, tc.expPolicyShow)
gotPolicy, err := ParsePolicyShow(outputToParse)
c.Check(err, tc.errChecker, Commentf("Failed for output: %q", outputToParse))
c.Log(err)
c.Check(gotPolicy, DeepEquals, tc.expPolicyShow)
}
}

func marshalManifestList(c *C, manifestList []*snapshot.Manifest) string {
c.Assert(manifestList, NotNil)

Expand All @@ -666,3 +709,10 @@ func marshalManifestList(c *C, manifestList []*snapshot.Manifest) string {

return string(b)
}

func marshalPolicy(c *C, policy policy.Policy) string {
b, err := json.Marshal(policy)
c.Assert(err, IsNil)

return string(b)
}
31 changes: 31 additions & 0 deletions pkg/kopia/command/policy_show_global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package command

type PolicyShowGlobalCommandArgs struct {
*CommandArgs
GetJsonOutput bool
}

// PolicyShowGlobal returns the kopia command for showing the global policy.
func PolicyShowGlobal(cmdArgs PolicyShowGlobalCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(policySubCommand, showSubCommand, globalFlag)
if cmdArgs.GetJsonOutput {
args = args.AppendLoggable(jsonFlag)
}

return stringSliceCommand(args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type KopiaPolicyTestSuite struct{}

var _ = Suite(&KopiaPolicyTestSuite{})

func (kPolicy *KopiaPolicyTestSuite) TestPolicyCommands(c *C) {
func (kPolicy *KopiaPolicyTestSuite) TestPolicySetCommands(c *C) {
for _, tc := range []struct {
f func() []string
expectedLog string
Expand All @@ -48,3 +48,28 @@ func (kPolicy *KopiaPolicyTestSuite) TestPolicyCommands(c *C) {
c.Check(cmd, Equals, tc.expectedLog)
}
}

func (kPolicy *KopiaPolicyTestSuite) TestPolicyShowCommands(c *C) {
for _, tc := range []struct {
f func() []string
expectedLog string
}{
{
f: func() []string {
args := PolicyShowGlobalCommandArgs{
CommandArgs: &CommandArgs{
RepoPassword: "encr-key",
ConfigFilePath: "path/kopia.config",
LogDirectory: "cache/log",
},
GetJsonOutput: true,
}
return PolicyShowGlobal(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key policy show --global --json",
},
} {
cmd := strings.Join(tc.f(), " ")
c.Check(cmd, Equals, tc.expectedLog)
}
}

0 comments on commit a5b4e6c

Please sign in to comment.