diff --git a/providerframework/metaservice/archiver/compressors/gzip_test.go b/providerframework/metaservice/archiver/compressors/gzip_test.go new file mode 100644 index 00000000..ef61bd30 --- /dev/null +++ b/providerframework/metaservice/archiver/compressors/gzip_test.go @@ -0,0 +1,108 @@ +/* +Copyright 2022 The SODA 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 compressors + +import ( + "compress/gzip" + "io" + "os" + "testing" + + "github.com/soda-cdm/kahu/providerframework/metaservice/mocks" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/suite" +) + +type GzipTestSuite struct { + suite.Suite + fakeGzipWriter *gzipWriter + fakeGzipReader *gzipReader + data []byte + fileName string + fakeStr mocks.FakeStruct +} + +func (suite *GzipTestSuite) BeforeTest(suiteName, testName string) { + suite.fileName = "fakeFile" + str := "Sample data to write in TestGzipReader" + suite.data = []byte(str) + switch testName { + + case "TestWriteGzipWriter", "TestCloseGzipWriter": + suite.fakeStr.On("Write", mock.Anything).Return(1, nil) + suite.fakeStr.On("Close").Return(nil) + suite.fakeStr.On("Read", mock.Anything).Return(1, nil) + suite.fakeGzipWriter = &gzipWriter{ + writer: &suite.fakeStr, + gzip: gzip.NewWriter(&suite.fakeStr), + } + + case "TestCloseGzipReader", "TestReadGzipReader": + + out, err := os.Create(suite.fileName) + assert.Nil(suite.T(), err) + gzipWriter := gzip.NewWriter(out) + _, err = gzipWriter.Write(suite.data) + assert.Nil(suite.T(), err) + gzipWriter.Close() + out.Close() + + out, err = os.Open(suite.fileName) + assert.Nil(suite.T(), err) + + gzip, _ := gzip.NewReader(out) + suite.fakeGzipReader = &gzipReader{ + reader: out, + gzip: gzip, + } + } +} + +func (suite *GzipTestSuite) AfterTest(suiteName, testName string) { + switch testName { + case "TestCloseGzipReader", "TestReadGzipReader": + os.Remove(suite.fileName) + } +} + +func (suite *GzipTestSuite) TestCloseGzipWriter() { + err := suite.fakeGzipWriter.Close() + assert.Nil(suite.T(), err) + suite.fakeStr.AssertCalled(suite.T(), "Close", mock.Anything) +} + +func (suite *GzipTestSuite) TestWriteGzipWriter() { + _, err := suite.fakeGzipWriter.Write(suite.data) + assert.Nil(suite.T(), err) + suite.fakeStr.AssertCalled(suite.T(), "Write", mock.Anything) + suite.fakeStr.AssertCalled(suite.T(), "Close", mock.Anything) +} + +func (suite *GzipTestSuite) TestCloseGzipReader() { + err := suite.fakeGzipReader.Close() + assert.Nil(suite.T(), err) +} + +func (suite *GzipTestSuite) TestReadGzipReader() { + _, err := suite.fakeGzipReader.Read(suite.data) + assert.Equal(suite.T(), err, io.EOF) +} + +func TestGzipTestSuite(t *testing.T) { + suite.Run(t, new(GzipTestSuite)) +} diff --git a/providerframework/metaservice/archiver/manager/manager_test.go b/providerframework/metaservice/archiver/manager/manager_test.go new file mode 100644 index 00000000..faf7ed2c --- /dev/null +++ b/providerframework/metaservice/archiver/manager/manager_test.go @@ -0,0 +1,159 @@ +/* +Copyright 2022 The SODA 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 manager + +import ( + "fmt" + "os" + "testing" + + "github.com/soda-cdm/kahu/providerframework/metaservice/archiver" + "github.com/soda-cdm/kahu/providerframework/metaservice/mocks" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type ManagerTestSuite struct { + suite.Suite + mgr *archivalManager + archFileName string + typ archiver.CompressionType + fwriter *mocks.FakeStruct + fReader *mocks.FakeStruct + wrirteInvoke func(Writer archiver.Writer) (archiver.Writer, error) + readInvoke func(Reader archiver.Reader) (archiver.Reader, error) + archFilePath string + fakeType string + archieveYard string +} + +func (suite *ManagerTestSuite) BeforeTest(suiteName, testName string) { + suite.fakeType = "faketype" + suite.archieveYard = "FakeArchiveYard" + suite.archFileName = "FakeArchfileName" + suite.typ = archiver.CompressionType("fake") + suite.archFilePath = suite.archieveYard + "/" + suite.archFileName + + GetArchiverInit := func() { + suite.mgr = &archivalManager{ + archiveYard: suite.archieveYard, + } + err := os.Mkdir(suite.archieveYard, 0777) + assert.Nil(suite.T(), err) + + suite.wrirteInvoke = func(Writer archiver.Writer) (archiver.Writer, error) { + return suite.fwriter, nil + } + RegisterCompressionWriterPlugins(suite.typ, suite.wrirteInvoke) + } + + GetArchiveReaderInit := func() { + suite.mgr = &archivalManager{ + archiveYard: suite.archieveYard, + } + err := os.Mkdir(suite.archieveYard, 0777) + assert.Nil(suite.T(), err) + + suite.fReader = &mocks.FakeStruct{} + suite.readInvoke = func(Reader archiver.Reader) (archiver.Reader, error) { + return suite.fReader, nil + } + RegisterCompressionReaderPlugins(suite.typ, suite.readInvoke) + } + + switch testName { + case "TestGetArchiverFileAlreadyExists": + GetArchiverInit() + _, err := os.Create(suite.archFilePath) + assert.Nil(suite.T(), err) + case "TestGetArchiverSuccess": + GetArchiverInit() + case "TestGetArchiveReaderInvalidFilePath": + GetArchiveReaderInit() + + case "TestGetArchiveReaderSuccess": + GetArchiveReaderInit() + _, err := os.Create(suite.archFilePath) + assert.Nil(suite.T(), err) + } +} + +func (suite *ManagerTestSuite) AfterTest(suiteName, testName string) { + + switch testName { + case "TestGetArchiverFileAlreadyExists", "TestGetArchiverSuccess", "TestGetArchiveReaderSuccess": + err := os.Remove(suite.archFilePath) + assert.Nil(suite.T(), err) + + err = os.Remove(suite.archieveYard) + assert.Nil(suite.T(), err) + + case "TestGetArchiveReaderInvalidFilePath": + err := os.Remove(suite.archieveYard) + assert.Nil(suite.T(), err) + } +} + +func (suite *ManagerTestSuite) TestGetArchiverFakeCompressionType() { + + //when the compression type is fake not registered + _, _, err := suite.mgr.GetArchiver(archiver.CompressionType(suite.fakeType), suite.archFileName) + expErr := fmt.Errorf("archival plugin[%s] not available", suite.fakeType) + assert.Equal(suite.T(), expErr, err) +} + +func (suite *ManagerTestSuite) TestGetArchiverSuccess() { + //success case + _, archFile, err := suite.mgr.GetArchiver(suite.typ, suite.archFileName) + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), archFile, suite.archFilePath) +} + +func (suite *ManagerTestSuite) TestGetArchiverFileAlreadyExists() { + //when trying to give already existing file + fakefile := suite.archFileName + expErr := fmt.Errorf("archival file(%s) already exist", suite.archFilePath) + _, _, err := suite.mgr.GetArchiver(suite.typ, fakefile) + assert.Equal(suite.T(), expErr, err) +} + +func (suite *ManagerTestSuite) TestGetArchiveReaderFakeCompressionType() { + //invalid compression type + + _, err := suite.mgr.GetArchiveReader(archiver.CompressionType(suite.fakeType), suite.archFileName) + expErr := fmt.Errorf("archival plugin[%s] not available", suite.fakeType) + assert.Equal(suite.T(), expErr, err) +} + +func (suite *ManagerTestSuite) TestGetArchiveReaderInvalidFilePath() { + //invalid path + archFileName := "fakepath" + _, err := suite.mgr.GetArchiveReader(suite.typ, archFileName) + expErr := fmt.Errorf("archival file(%s) do not exist", archFileName) + assert.Equal(suite.T(), expErr, err) +} +func (suite *ManagerTestSuite) TestGetArchiveReaderSuccess() { + //success case + resArch, err := suite.mgr.GetArchiveReader(suite.typ, suite.archFilePath) + assert.NotNil(suite.T(), resArch) + assert.Nil(suite.T(), err) + +} + +func TestManagerTestSuite(t *testing.T) { + suite.Run(t, new(ManagerTestSuite)) +} diff --git a/providerframework/metaservice/archiver/tar/tar_test.go b/providerframework/metaservice/archiver/tar/tar_test.go new file mode 100644 index 00000000..bf9c5ee8 --- /dev/null +++ b/providerframework/metaservice/archiver/tar/tar_test.go @@ -0,0 +1,120 @@ +/* +Copyright 2022 The SODA 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 tar + +import ( + "archive/tar" + "os" + "testing" + + "github.com/soda-cdm/kahu/providerframework/metaservice/mocks" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/suite" +) + +type TarTestSuite struct { + suite.Suite + fakestr mocks.FakeStruct + file string + data []byte + archiver *tarArchiver + tarArchReader *tarArchiveReader +} + +func (suite *TarTestSuite) BeforeTest(suiteName, testName string) { + suite.file = "testFile" + str := "hello world!!!" + suite.data = []byte(str) + suite.fakestr.On("Write", mock.Anything).Return(1, nil) + suite.fakestr.On("Close").Return(nil) + suite.fakestr.On("Read", mock.Anything).Return(1, nil) + + switch testName { + case "TestWriteFile", "TestCloseTarArchiver": + fakeTarWriter := tar.NewWriter(&suite.fakestr) + suite.archiver = &tarArchiver{ + writer: &suite.fakestr, + tar: fakeTarWriter, + } + case "TestCloseTarArchiveReader": + fakeTarReader := tar.NewReader(&suite.fakestr) + suite.tarArchReader = &tarArchiveReader{ + reader: &suite.fakestr, + tar: fakeTarReader, + } + case "TestReadNext": + + file, err := os.Create(suite.file) + assert.Nil(suite.T(), err) + defer file.Close() + tw := tar.NewWriter(file) + defer tw.Close() + + hdr := &tar.Header{ + Name: suite.file, + Size: int64(len(suite.data)), + } + err = tw.WriteHeader(hdr) + assert.Nil(suite.T(), err) + + _, err = tw.Write(suite.data) + assert.Nil(suite.T(), err) + + file, err = os.Open(suite.file) + assert.Nil(suite.T(), err) + fakeTarReader := tar.NewReader(file) + suite.tarArchReader = &tarArchiveReader{ + reader: file, + tar: fakeTarReader, + } + } +} + +func (suite *TarTestSuite) AfterTest(suiteName, testName string) { + switch testName { + case "TestWriteFile", "TestReadNext": + os.Remove(suite.file) + } +} + +func (suite *TarTestSuite) TestWriteFile() { + err := suite.archiver.WriteFile(suite.file, suite.data) + assert.Nil(suite.T(), err) + suite.fakestr.AssertCalled(suite.T(), "Write", mock.Anything) +} + +func (suite *TarTestSuite) TestCloseTarArchiver() { + err := suite.archiver.Close() + assert.Nil(suite.T(), err) + suite.fakestr.AssertCalled(suite.T(), "Close", mock.Anything) +} + +func (suite *TarTestSuite) TestCloseTarArchiveReader() { + err := suite.tarArchReader.Close() + assert.Nil(suite.T(), err) + suite.fakestr.AssertCalled(suite.T(), "Close", mock.Anything) +} + +func (suite *TarTestSuite) TestReadNext() { + _, _, err := suite.tarArchReader.ReadNext() + assert.Nil(suite.T(), err) +} + +func TestTarTestSuite(t *testing.T) { + suite.Run(t, new(TarTestSuite)) +} diff --git a/providerframework/metaservice/backuprespository/client_test.go b/providerframework/metaservice/backuprespository/client_test.go new file mode 100644 index 00000000..de66d0d6 --- /dev/null +++ b/providerframework/metaservice/backuprespository/client_test.go @@ -0,0 +1,141 @@ +/* +Copyright 2022 The SODA 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 backuprespository + +import ( + "context" + "os" + "testing" + + pb "github.com/soda-cdm/kahu/providers/lib/go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/suite" + "google.golang.org/grpc" +) + +type ClientTestSuite struct { + suite.Suite + repo BackupRepository + filePath string +} + +type FakeMetaBackup_DownloadClient struct { + mock.Mock + grpc.ClientStream +} + +type FakeMetaBackup_UploadClient struct { + mock.Mock + grpc.ClientStream +} + +func (f *FakeMetaBackup_DownloadClient) Recv() (*pb.DownloadResponse, error) { + args := f.Called() + return args.Get(0).(*pb.DownloadResponse), args.Error(1) +} + +func (f *FakeMetaBackup_UploadClient) Send(ur *pb.UploadRequest) error { + args := f.Called(ur) + return args.Error(0) +} + +func (f *FakeMetaBackup_UploadClient) CloseAndRecv() (*pb.Empty, error) { + args := f.Called() + return args.Get(0).(*pb.Empty), args.Error(1) +} + +type FakeMetaBackupClient struct { + mock.Mock +} + +func (f *FakeMetaBackupClient) Upload(ctx context.Context, opts ...grpc.CallOption) (pb.MetaBackup_UploadClient, error) { + args := f.Called(ctx, opts) + return args.Get(0).(pb.MetaBackup_UploadClient), args.Error(1) +} + +func (f *FakeMetaBackupClient) ObjectExists(ctx context.Context, in *pb.ObjectExistsRequest, opts ...grpc.CallOption) (*pb.ObjectExistsResponse, error) { + args := f.Called(ctx, in, opts) + return args.Get(0).(*pb.ObjectExistsResponse), args.Error(1) +} + +func (f *FakeMetaBackupClient) Download(ctx context.Context, in *pb.DownloadRequest, opts ...grpc.CallOption) (pb.MetaBackup_DownloadClient, error) { + args := f.Called(ctx, in, opts) + return args.Get(0).(pb.MetaBackup_DownloadClient), args.Error(1) + +} + +func (f *FakeMetaBackupClient) Delete(ctx context.Context, in *pb.DeleteRequest, opts ...grpc.CallOption) (*pb.Empty, error) { + args := f.Called(ctx, in, opts) + return args.Get(0).(*pb.Empty), args.Error(1) +} + +func (suite *ClientTestSuite) BeforeTest(suiteName, testName string) { + suite.filePath = "FakeFile" + switch testName { + case "TestUploadInvalidPath", "TestDownloadFail": + repo, _, _ := NewBackupRepository("/tmp/nfs_test.sock") + suite.repo = repo + + case "TestUploadValid": + client := &FakeMetaBackupClient{} + fakeRespoClient := &FakeMetaBackup_UploadClient{} + client.On("Upload", mock.Anything, mock.Anything).Return(fakeRespoClient, nil) + suite.repo = &backupRepository{ + backupRepositoryAddress: "127.0.0.1:8181", + client: client, + } + fakeRespoClient.On("Send", mock.Anything).Return(nil) + fakeRespoClient.On("CloseAndRecv", mock.Anything).Return(&pb.Empty{}, nil) + file, err := os.Create(suite.filePath) + assert.Nil(suite.T(), err) + defer file.Close() + str := "this is sample data" + data := []byte(str) + err = os.WriteFile(suite.filePath, data, 0777) + } +} + +func (suite *ClientTestSuite) AfterTest(suiteName, testName string) { + switch testName { + case "TestUploadValid": + err := os.Remove(suite.filePath) + assert.Nil(suite.T(), err) + + } +} + +func (suite *ClientTestSuite) TestUploadInvalidPath() { + err := suite.repo.Upload(suite.filePath) + assert.NotNil(suite.T(), err) +} + +func (suite *ClientTestSuite) TestDownloadFail() { + attributes := map[string]string{"key": "value"} + _, err := suite.repo.Download(suite.filePath, attributes) + assert.NotNil(suite.T(), err) +} + +func (suite *ClientTestSuite) TestUploadValid() { + err := suite.repo.Upload(suite.filePath) + assert.Nil(suite.T(), err) + +} + +func TestTarTestSuite(t *testing.T) { + suite.Run(t, new(ClientTestSuite)) +} diff --git a/providerframework/metaservice/mocks/mocks.go b/providerframework/metaservice/mocks/mocks.go new file mode 100644 index 00000000..3d4b85eb --- /dev/null +++ b/providerframework/metaservice/mocks/mocks.go @@ -0,0 +1,43 @@ +/* +Copyright 2022 The SODA 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 mocks + +import "github.com/stretchr/testify/mock" + +type FakeStruct struct { + mock.Mock +} + +func (f *FakeStruct) Write(p []byte) (n int, err error) { + args := f.Called(p) + return args.Int(0), args.Error(1) +} + +func (f *FakeStruct) Close() error { + args := f.Called() + return args.Error(0) +} + +func (f *FakeStruct) Read(p []byte) (n int, err error) { + args := f.Called(p) + return args.Int(0), args.Error(1) +} + +func (f *FakeStruct) ReadByte() (byte, error) { + args := f.Called() + return args.Get(0).(byte), args.Error(1) +}