github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/ledger/util/ioutil_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 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 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var dbPathTest = "/tmp/ledgertests/common/ledger/util" 28 var dbFileTest = dbPathTest + "/testFile" 29 30 func TestCreatingDBDirWithPathSeperator(t *testing.T) { 31 32 //test creating a directory with path separator passed 33 dbPathTestWSeparator := dbPathTest + "/" 34 cleanup(dbPathTestWSeparator) //invoked prior to test to make sure test is executed in desired environment 35 defer cleanup(dbPathTestWSeparator) 36 37 dirEmpty, err := CreateDirIfMissing(dbPathTestWSeparator) 38 assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTestWSeparator) 39 assert.True(t, dirEmpty) //test directory is empty is returning true 40 } 41 42 func TestCreatingDBDirWhenDirDoesAndDoesNotExists(t *testing.T) { 43 44 cleanup(dbPathTest) //invoked prior to test to make sure test is executed in desired environment 45 defer cleanup(dbPathTest) 46 47 //test creating a directory without path separator passed 48 dirEmpty, err := CreateDirIfMissing(dbPathTest) 49 assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTest) 50 assert.True(t, dirEmpty) 51 52 //test creating directory AGAIN, that is the directory already exists 53 dirEmpty2, err2 := CreateDirIfMissing(dbPathTest) 54 assert.NoError(t, err2, "Error not handling existing directory when trying to create a test db directory at [%s]", dbPathTest) 55 assert.True(t, dirEmpty2) 56 } 57 58 func TestDirNotEmptyAndFileExists(t *testing.T) { 59 60 cleanup(dbPathTest) 61 defer cleanup(dbPathTest) 62 63 //create the directory 64 dirEmpty, err := CreateDirIfMissing(dbPathTest) 65 assert.NoError(t, err, "Error when trying to create a test db directory at [%s]", dbPathTest) 66 assert.True(t, dirEmpty) 67 68 //test file does not exists and size is returned correctly 69 exists2, size2, err2 := FileExists(dbFileTest) 70 assert.NoError(t, err2, "Error when trying to determine if file exist when it does not at [%s]", dbFileTest) 71 assert.Equal(t, int64(0), size2) 72 assert.False(t, exists2) //test file that does not exists reports false 73 74 //create file 75 testStr := "This is some test data in a file" 76 sizeOfFileCreated, err3 := createAndWriteAFile(testStr) 77 assert.NoError(t, err3, "Error when trying to create and write to file at [%s]", dbFileTest) 78 assert.Equal(t, len(testStr), sizeOfFileCreated) //test file size returned is correct 79 80 //test that the file exists and size is returned correctly 81 exists, size, err4 := FileExists(dbFileTest) 82 assert.NoError(t, err4, "Error when trying to determine if file exist at [%s]", dbFileTest) 83 assert.Equal(t, int64(sizeOfFileCreated), size) 84 assert.True(t, exists) //test file that does exists reports true 85 86 //test that if the directory is not empty 87 dirEmpty5, err5 := DirEmpty(dbPathTest) 88 assert.NoError(t, err5, "Error when detecting if empty at db directory [%s]", dbPathTest) 89 assert.False(t, dirEmpty5) //test directory is empty is returning false 90 } 91 92 func TestListSubdirs(t *testing.T) { 93 childFolders := []string{".childFolder1", "childFolder2", "childFolder3"} 94 cleanup(dbPathTest) 95 defer cleanup(dbPathTest) 96 for _, folder := range childFolders { 97 assert.NoError(t, os.MkdirAll(filepath.Join(dbPathTest, folder), 0755)) 98 } 99 subFolders, err := ListSubdirs(dbPathTest) 100 assert.NoError(t, err) 101 assert.Equal(t, subFolders, childFolders) 102 } 103 104 func createAndWriteAFile(sentence string) (int, error) { 105 //create a file in the directory 106 f, err2 := os.Create(dbFileTest) 107 if err2 != nil { 108 return 0, err2 109 } 110 defer f.Close() 111 112 //write to the file 113 return f.WriteString(sentence) 114 } 115 116 func cleanup(path string) { 117 os.RemoveAll(path) 118 }