Skip to content

Commit

Permalink
UT for metaservice (#122)
Browse files Browse the repository at this point in the history
Signed-off-by: vineela1999 <[email protected]>
  • Loading branch information
vineela1999 authored Dec 5, 2022
1 parent cdddc44 commit feac4ed
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 0 deletions.
108 changes: 108 additions & 0 deletions providerframework/metaservice/archiver/compressors/gzip_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
159 changes: 159 additions & 0 deletions providerframework/metaservice/archiver/manager/manager_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
120 changes: 120 additions & 0 deletions providerframework/metaservice/archiver/tar/tar_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading

0 comments on commit feac4ed

Please sign in to comment.