github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/sparsefile_test.go (about) 1 /* 2 Copyright 2018 The OpenEBS Author 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestSparseFileCreate(t *testing.T) { 26 tests := map[string]struct { 27 path string 28 size int64 29 err bool 30 }{ 31 "Create a sparse file": {path: "/tmp/test.img", size: 1024, err: false}, 32 "Retry with same file": {path: "/tmp/test.img", size: 1024, err: false}, 33 "Fail to create sub-dir file": {path: "/tmp/0/test.img", size: 1024, err: true}, 34 } 35 for name, test := range tests { 36 t.Run(name, func(t *testing.T) { 37 createErr := SparseFileCreate(test.path, test.size) 38 assert.Equal(t, test.err, createErr != nil) 39 }) 40 } 41 } 42 43 func TestSparseFileDelete(t *testing.T) { 44 tests := map[string]struct { 45 path string 46 }{ 47 "Delete the sparse file ": {path: "/tmp/test.img"}, 48 "Retry Delete on deleted file ": {path: "/tmp/test.img"}, 49 } 50 for name, test := range tests { 51 t.Run(name, func(t *testing.T) { 52 err := SparseFileDelete(test.path) 53 assert.Equal(t, nil, err) 54 }) 55 } 56 } 57 58 func TestSparseFileInfo(t *testing.T) { 59 60 testFile := "/tmp/test.img" 61 testFileSize := int64(1024) 62 63 _ = SparseFileCreate(testFile, testFileSize) 64 65 tests := map[string]struct { 66 path string 67 size int64 68 err bool 69 }{ 70 "Valid FileInfo": {path: testFile, err: false}, 71 "Invalid FileInfo": {path: "invalid", err: true}, 72 } 73 74 for name, test := range tests { 75 t.Run(name, func(t *testing.T) { 76 info, infoErr := SparseFileInfo(test.path) 77 if infoErr == nil { 78 assert.Equal(t, testFileSize, info.Size()) 79 } 80 assert.Equal(t, test.err, infoErr != nil) 81 }) 82 } 83 84 _ = SparseFileDelete(testFile) 85 }