diff --git a/internal/helper/OS_test.go b/internal/helper/OS_test.go index 3acbf2e..8d57be0 100644 --- a/internal/helper/OS_test.go +++ b/internal/helper/OS_test.go @@ -3,10 +3,8 @@ package helper import ( "errors" "github.com/forceu/gokapi/internal/test" - "io/ioutil" "os" "testing" - _ "unsafe" ) func TestIsInArray(t *testing.T) { @@ -20,7 +18,7 @@ func TestFolderCreation(t *testing.T) { test.IsEqualBool(t, FileExists("invalid/file"), false) CreateDir("invalid") test.IsEqualBool(t, FolderExists("invalid"), true) - err := ioutil.WriteFile("invalid/file", []byte("test"), 0644) + err := os.WriteFile("invalid/file", []byte("test"), 0644) if err != nil { t.Error(err) } diff --git a/internal/storage/filesystem/FileSystem.go b/internal/storage/filesystem/FileSystem.go index 254054f..ec4b8cf 100644 --- a/internal/storage/filesystem/FileSystem.go +++ b/internal/storage/filesystem/FileSystem.go @@ -26,16 +26,21 @@ func Init(pathData string) { // SetAws sets the AWS filesystem as the default storage func SetAws() { - s3FileSystem = s3filesystem.GetDriver() - ok := s3FileSystem.Init(s3filesystem.Config{Bucket: aws.GetDefaultBucketName()}) - if !ok { - log.Println("Unable to set AWS S3 as filesystem") - return + if aws.IsIncludedInBuild { + s3FileSystem = s3filesystem.GetDriver() + ok := s3FileSystem.Init(s3filesystem.Config{Bucket: aws.GetDefaultBucketName()}) + if !ok && !isUnitTesting { + log.Println("Unable to set AWS S3 as filesystem") + return + } + ActiveStorageSystem = s3FileSystem } - ActiveStorageSystem = s3FileSystem } // SetLocal sets the local filesystem as the default storage func SetLocal() { ActiveStorageSystem = dataFilesystem } + +// isUnitTesting is only set to true when testing, to avoid login with aws +var isUnitTesting = false diff --git a/internal/storage/filesystem/fileSystem_test.go b/internal/storage/filesystem/fileSystem_test.go new file mode 100644 index 0000000..35d664a --- /dev/null +++ b/internal/storage/filesystem/fileSystem_test.go @@ -0,0 +1,46 @@ +package filesystem + +import ( + "github.com/forceu/gokapi/internal/models" + fileInterfaces "github.com/forceu/gokapi/internal/storage/filesystem/interfaces" + "github.com/forceu/gokapi/internal/storage/filesystem/s3filesystem/aws" + "github.com/forceu/gokapi/internal/test" + "testing" +) + +func TestInit(t *testing.T) { + Init("./test") + test.IsEqualBool(t, ActiveStorageSystem == dataFilesystem, true) + test.IsEqualBool(t, ActiveStorageSystem == s3FileSystem, false) + test.IsEqualString(t, ActiveStorageSystem.GetSystemName(), fileInterfaces.DriverLocal) +} + +func TestSetLocal(t *testing.T) { + ActiveStorageSystem = nil + SetLocal() + test.IsEqualBool(t, ActiveStorageSystem == dataFilesystem, true) +} + +func TestSetAws(t *testing.T) { + ActiveStorageSystem = nil + if !aws.IsIncludedInBuild { + SetAws() + test.IsNil(t, ActiveStorageSystem) + return + } + aws.Init(models.AwsConfig{ + Bucket: "test1", + Region: "test2", + KeyId: "test3", + KeySecret: "test4", + Endpoint: "test5", + }) + SetAws() + test.IsNil(t, ActiveStorageSystem) + isUnitTesting = true + SetAws() + test.IsEqualBool(t, ActiveStorageSystem == s3FileSystem, true) + test.IsEqualBool(t, ActiveStorageSystem == dataFilesystem, false) + test.IsEqualString(t, ActiveStorageSystem.GetSystemName(), fileInterfaces.DriverAws) + +} diff --git a/internal/storage/filesystem/localstorage/Localstorage.go b/internal/storage/filesystem/localstorage/Localstorage.go index c93a47d..ad546ff 100644 --- a/internal/storage/filesystem/localstorage/Localstorage.go +++ b/internal/storage/filesystem/localstorage/Localstorage.go @@ -1,6 +1,7 @@ package localstorage import ( + "errors" "github.com/forceu/gokapi/internal/helper" "github.com/forceu/gokapi/internal/models" fileInterfaces "github.com/forceu/gokapi/internal/storage/filesystem/interfaces" @@ -32,12 +33,12 @@ func (d *localStorageDriver) MoveToFilesystem(sourceFile *os.File, metaData mode if err != nil { return err } - return os.Rename(sourceFile.Name(), d.getDataPath()+metaData.SHA1) + if metaData.SHA1 == "" { + return errors.New("empty metadata passed") + } + return os.Rename(sourceFile.Name(), d.getPath()+d.filePrefix+metaData.SHA1) } -func (d *localStorageDriver) getDataPath() string { - return d.dataPath -} // Init sets the driver configurations and returns true if successful // Requires a Config struct as input diff --git a/internal/storage/filesystem/localstorage/Localstorage_test.go b/internal/storage/filesystem/localstorage/Localstorage_test.go new file mode 100644 index 0000000..abe3007 --- /dev/null +++ b/internal/storage/filesystem/localstorage/Localstorage_test.go @@ -0,0 +1,130 @@ +package localstorage + +import ( + "github.com/forceu/gokapi/internal/models" + "github.com/forceu/gokapi/internal/test" + "os" + "testing" +) + +func TestMain(m *testing.M) { + os.Mkdir("test/", 0777) + os.Mkdir("test/data", 0777) + exitVal := m.Run() + os.RemoveAll("test") + os.Exit(exitVal) +} + +func getTestDriver(t *testing.T) *localStorageDriver { + t.Helper() + driver := GetDriver() + result, ok := driver.(*localStorageDriver) + test.IsEqualBool(t, ok, true) + return result +} + +func initDriver(t *testing.T, d *localStorageDriver) { + ok := d.Init(Config{ + DataPath: "test/data", + FilePrefix: "123", + }) + test.IsEqualBool(t, ok, true) +} + +func TestGetDriver(t *testing.T) { + getTestDriver(t) +} + +func TestLocalStorageDriver_Init(t *testing.T) { + driver := getTestDriver(t) + ok := driver.Init(Config{ + DataPath: "test", + FilePrefix: "tpref", + }) + test.IsEqualBool(t, ok, true) + test.IsEqualString(t, driver.getPath(), "test/") + ok = driver.Init(Config{ + DataPath: "test2/", + FilePrefix: "", + }) + test.IsEqualBool(t, ok, true) + test.IsEqualString(t, driver.getPath(), "test2/") + defer test.ExpectPanic(t) + driver.Init(struct { + invalid string + }{invalid: "true"}) +} + +func TestLocalStorageDriver_Init2(t *testing.T) { + driver := getTestDriver(t) + defer test.ExpectPanic(t) + driver.Init(Config{ + DataPath: "", + FilePrefix: "tpref", + }) +} + +func TestLocalStorageDriver_IsAvailable(t *testing.T) { + driver := getTestDriver(t) + test.IsEqualBool(t, driver.IsAvailable(), true) +} + +func TestGetDataPath(t *testing.T) { + driver := getTestDriver(t) + initDriver(t, driver) + test.IsEqualString(t, driver.getPath(), "test/data/") + driver.dataPath = "" + defer test.ExpectPanic(t) + driver.getPath() +} +func TestLocalStorageDriver_MoveToFilesystem(t *testing.T) { + driver := getTestDriver(t) + initDriver(t, driver) + metaData := models.File{ + SHA1: "testsha", + } + err := driver.MoveToFilesystem(nil, metaData) + test.IsNotNil(t, err) + err = os.WriteFile("test/testfile", []byte("This is a test"), 0777) + test.IsNil(t, err) + file, err := os.Open("test/testfile") + test.IsNil(t, err) + err = driver.MoveToFilesystem(file, models.File{}) + test.IsNotNil(t, err) + test.FileExists(t, "test/testfile") + file, err = os.Open("test/testfile") + test.IsNil(t, err) + err = driver.MoveToFilesystem(file, metaData) + test.IsNil(t, err) + test.FileDoesNotExist(t, "test/testfile") + test.FileExists(t, "test/data/123testsha") + +} + +func TestLocalFile_Exists(t *testing.T) { + driver := getTestDriver(t) + initDriver(t, driver) + test.FileExists(t, "test/data/123testsha") + test.FileDoesNotExist(t, "test/data/testsha") + file := driver.GetFile("testsha") + test.IsEqualBool(t, file.Exists(), true) + test.IsEqualString(t, file.GetName(), "123testsha") +} + +func TestLocalStorageDriver_FileExists(t *testing.T) { + driver := getTestDriver(t) + initDriver(t, driver) + test.FileExists(t, "test/data/123testsha") + test.FileDoesNotExist(t, "test/data/testsha") + exist, err := driver.FileExists("testsha") + test.IsNil(t, err) + test.IsEqualBool(t, exist, true) + exist, err = driver.FileExists("123testsha") + test.IsNil(t, err) + test.IsEqualBool(t, exist, false) +} + +func TestLocalStorageDriver_GetSystemName(t *testing.T) { + driver := getTestDriver(t) + test.IsEqualString(t, driver.GetSystemName(), "localstorage") +} diff --git a/internal/storage/filesystem/s3filesystem/S3.go b/internal/storage/filesystem/s3filesystem/S3filesystem.go similarity index 100% rename from internal/storage/filesystem/s3filesystem/S3.go rename to internal/storage/filesystem/s3filesystem/S3filesystem.go diff --git a/internal/storage/filesystem/s3filesystem/aws/AwsS3.go b/internal/storage/filesystem/s3filesystem/aws/Aws.go similarity index 100% rename from internal/storage/filesystem/s3filesystem/aws/AwsS3.go rename to internal/storage/filesystem/s3filesystem/aws/Aws.go diff --git a/internal/storage/filesystem/s3filesystem/aws/AwsS3_mock.go b/internal/storage/filesystem/s3filesystem/aws/Aws_mock.go similarity index 100% rename from internal/storage/filesystem/s3filesystem/aws/AwsS3_mock.go rename to internal/storage/filesystem/s3filesystem/aws/Aws_mock.go diff --git a/internal/storage/filesystem/s3filesystem/aws/AwsS3_slim.go b/internal/storage/filesystem/s3filesystem/aws/Aws_slim.go similarity index 100% rename from internal/storage/filesystem/s3filesystem/aws/AwsS3_slim.go rename to internal/storage/filesystem/s3filesystem/aws/Aws_slim.go diff --git a/internal/storage/filesystem/s3filesystem/aws/AwsS3_test.go b/internal/storage/filesystem/s3filesystem/aws/Aws_test.go similarity index 100% rename from internal/storage/filesystem/s3filesystem/aws/AwsS3_test.go rename to internal/storage/filesystem/s3filesystem/aws/Aws_test.go diff --git a/internal/test/TestHelper.go b/internal/test/TestHelper.go index 429d736..23e5cb3 100644 --- a/internal/test/TestHelper.go +++ b/internal/test/TestHelper.go @@ -95,11 +95,17 @@ func IsEmpty(t MockT, s string) { } } -// IsNil fails test if error not nil -func IsNil(t MockT, got error) { +// IsNil fails test if object is not nil. If object is an error, it will display the error message +func IsNil(t MockT, got any) { t.Helper() - if got != nil { - t.Errorf("Assertion failed, got: %s, want: nil.", got.(error).Error()) + if got == nil { + return + } + err, ok := got.(error) + if !ok { + t.Errorf("Assertion failed, got: not nil, want: nil.") + } else { + t.Errorf("Assertion failed, got: %s, want: nil.", err.Error()) } } @@ -136,8 +142,8 @@ func fileExists(name string) bool { return !info.IsDir() } -// IsNotNil fails test if error is nil -func IsNotNil(t MockT, got error) { +// IsNotNil fails test if input is nil +func IsNotNil(t MockT, got any) { t.Helper() if got == nil { t.Errorf("Assertion failed, got: nil, want: not nil.")