From 410853dc94909c91a4e787e967c6773834728199 Mon Sep 17 00:00:00 2001 From: Rajat Gupta <37516416+r4rajat@users.noreply.github.com> Date: Wed, 27 Sep 2023 19:07:00 +0530 Subject: [PATCH] Refactor Duplicate Functions and Tests from kando package (#2234) * Remove test files from kando package Signed-off-by: Rajat Gupta * - Remove Unused Functions - Refactored helper function Signed-off-by: Rajat Gupta --------- Signed-off-by: Rajat Gupta --- pkg/kando/chronicle_pull.go | 16 +++++++- pkg/kando/kando_test.go | 24 ------------ pkg/kando/location_delete.go | 9 ----- pkg/kando/location_pull.go | 18 --------- pkg/kando/location_push.go | 12 ------ pkg/kando/location_test.go | 71 ------------------------------------ 6 files changed, 14 insertions(+), 136 deletions(-) delete mode 100644 pkg/kando/kando_test.go delete mode 100644 pkg/kando/location_test.go diff --git a/pkg/kando/chronicle_pull.go b/pkg/kando/chronicle_pull.go index 5f4338fae3..941ee4b335 100644 --- a/pkg/kando/chronicle_pull.go +++ b/pkg/kando/chronicle_pull.go @@ -17,11 +17,14 @@ package kando import ( "context" "encoding/json" + "io" + "os" - "github.com/kanisterio/kanister/pkg/chronicle" - "github.com/kanisterio/kanister/pkg/param" "github.com/pkg/errors" "github.com/spf13/cobra" + + "github.com/kanisterio/kanister/pkg/chronicle" + "github.com/kanisterio/kanister/pkg/param" ) func newChroniclePullCommand() *cobra.Command { @@ -64,3 +67,12 @@ func runChroniclePull(cmd *cobra.Command, p locationParams, arg string) error { ctx := context.Background() return chronicle.Pull(ctx, target, *prof, p.suffix) } + +const usePipeParam = `-` + +func targetWriter(target string) (io.Writer, error) { + if target != usePipeParam { + return os.OpenFile(target, os.O_RDWR|os.O_CREATE, 0755) + } + return os.Stdout, nil +} diff --git a/pkg/kando/kando_test.go b/pkg/kando/kando_test.go deleted file mode 100644 index 5d0ccb6092..0000000000 --- a/pkg/kando/kando_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2019 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 kando - -import ( - "testing" - - . "gopkg.in/check.v1" -) - -// Hook up gocheck into the "go test" runner. -func Test(t *testing.T) { TestingT(t) } diff --git a/pkg/kando/location_delete.go b/pkg/kando/location_delete.go index cbd1f40410..e93d793029 100644 --- a/pkg/kando/location_delete.go +++ b/pkg/kando/location_delete.go @@ -15,12 +15,7 @@ package kando import ( - "context" - "github.com/spf13/cobra" - - "github.com/kanisterio/kanister/pkg/location" - "github.com/kanisterio/kanister/pkg/param" ) func newLocationDeleteCommand() *cobra.Command { @@ -42,7 +37,3 @@ func newLocationDeleteCommand() *cobra.Command { cmd.Flags().StringP(kopiaSnapshotFlagName, "k", "", "Pass the kopia snapshot information from the location push command (optional)") return cmd } - -func locationDelete(ctx context.Context, p *param.Profile, path string) error { - return location.Delete(ctx, *p, path) -} diff --git a/pkg/kando/location_pull.go b/pkg/kando/location_pull.go index 95c485a4cd..ef3172c881 100644 --- a/pkg/kando/location_pull.go +++ b/pkg/kando/location_pull.go @@ -15,14 +15,7 @@ package kando import ( - "context" - "io" - "os" - "github.com/spf13/cobra" - - "github.com/kanisterio/kanister/pkg/location" - "github.com/kanisterio/kanister/pkg/param" ) const ( @@ -49,14 +42,3 @@ func newLocationPullCommand() *cobra.Command { cmd.Flags().StringP(kopiaSnapshotFlagName, "k", "", "Pass the kopia snapshot information from the location push command (optional)") return cmd } - -func targetWriter(target string) (io.Writer, error) { - if target != usePipeParam { - return os.OpenFile(target, os.O_RDWR|os.O_CREATE, 0755) - } - return os.Stdout, nil -} - -func locationPull(ctx context.Context, p *param.Profile, path string, target io.Writer) error { - return location.Read(ctx, target, *p, path) -} diff --git a/pkg/kando/location_push.go b/pkg/kando/location_push.go index cbc3842c81..c69ce04e87 100644 --- a/pkg/kando/location_push.go +++ b/pkg/kando/location_push.go @@ -15,13 +15,7 @@ package kando import ( - "context" - "io" - "github.com/spf13/cobra" - - "github.com/kanisterio/kanister/pkg/location" - "github.com/kanisterio/kanister/pkg/param" ) const ( @@ -50,9 +44,3 @@ func newLocationPushCommand() *cobra.Command { return cmd } - -const usePipeParam = `-` - -func locationPush(ctx context.Context, p *param.Profile, path string, source io.Reader) error { - return location.Write(ctx, source, *p, path) -} diff --git a/pkg/kando/location_test.go b/pkg/kando/location_test.go deleted file mode 100644 index a0b7585aaa..0000000000 --- a/pkg/kando/location_test.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2019 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 kando - -import ( - "bytes" - "context" - "path/filepath" - - . "gopkg.in/check.v1" - - crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" - "github.com/kanisterio/kanister/pkg/objectstore" - "github.com/kanisterio/kanister/pkg/testutil" -) - -type LocationSuite struct{} - -var _ = Suite(&LocationSuite{}) - -const testContent = "test-content" - -func (s *LocationSuite) TestLocationObjectStore(c *C) { - location := crv1alpha1.Location{ - Type: crv1alpha1.LocationTypeS3Compliant, - Bucket: testutil.TestS3BucketName, - } - p := testutil.ObjectStoreProfileOrSkip(c, objectstore.ProviderTypeS3, location) - ctx := context.Background() - dir := c.MkDir() - path := filepath.Join(dir, "test-object1.txt") - - source := bytes.NewBufferString(testContent) - err := locationPush(ctx, p, path, source) - c.Assert(err, IsNil) - - target := bytes.NewBuffer(nil) - err = locationPull(ctx, p, path, target) - c.Assert(err, IsNil) - c.Assert(target.String(), Equals, testContent) - - // test deleting single artifact - err = locationDelete(ctx, p, path) - c.Assert(err, IsNil) - - //test deleting dir with multiple artifacts - source = bytes.NewBufferString(testContent) - err = locationPush(ctx, p, path, source) - c.Assert(err, IsNil) - - path = filepath.Join(dir, "test-object2.txt") - - source = bytes.NewBufferString(testContent) - err = locationPush(ctx, p, path, source) - c.Assert(err, IsNil) - - err = locationDelete(ctx, p, dir) - c.Assert(err, IsNil) -}