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