github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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  	"testing"
    23  
    24  	"github.com/hyperledger/fabric/common/ledger/testutil"
    25  )
    26  
    27  var DbPathTest = "/tmp/v2/test/util"
    28  var DbFileTest = DbPathTest + "/testUtilFileDat1"
    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  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", DbPathTestWSeparator))
    39  	testutil.AssertEquals(t, dirEmpty, true) //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  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", DbPathTest))
    50  	testutil.AssertEquals(t, dirEmpty, true)
    51  
    52  	//test creating directory AGAIN, that is the directory already exists
    53  	dirEmpty2, err2 := CreateDirIfMissing(DbPathTest)
    54  	testutil.AssertNoError(t, err2, fmt.Sprintf("Error not handling existing directory when trying to create a test db directory at [%s]", DbPathTest))
    55  	testutil.AssertEquals(t, dirEmpty2, true)
    56  
    57  }
    58  
    59  func TestDirNotEmptyAndFileExists(t *testing.T) {
    60  
    61  	cleanup(DbPathTest)
    62  	defer cleanup(DbPathTest)
    63  
    64  	//create the directory
    65  	dirEmpty, err := CreateDirIfMissing(DbPathTest)
    66  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to create a test db directory at [%s]", DbPathTest))
    67  	testutil.AssertEquals(t, dirEmpty, true)
    68  
    69  	//test file does not exists and size is returned correctly
    70  	exists2, size2, err2 := FileExists(DbFileTest)
    71  	testutil.AssertNoError(t, err2, fmt.Sprintf("Error when trying to determine if file exist when it does not at [%s]", DbFileTest))
    72  	testutil.AssertEquals(t, size2, int64(0))
    73  	testutil.AssertEquals(t, exists2, false) //test file that does not exists reports false
    74  
    75  	//create file
    76  	testStr := "This is some test data in a file"
    77  	sizeOfFileCreated, err3 := createAndWriteAFile(testStr)
    78  	testutil.AssertNoError(t, err3, fmt.Sprintf("Error when trying to create and write to file at [%s]", DbFileTest))
    79  	testutil.AssertEquals(t, sizeOfFileCreated, len(testStr)) //test file size returned is correct
    80  
    81  	//test that the file exists and size is returned correctly
    82  	exists, size, err4 := FileExists(DbFileTest)
    83  	testutil.AssertNoError(t, err4, fmt.Sprintf("Error when trying to determine if file exist at [%s]", DbFileTest))
    84  	testutil.AssertEquals(t, size, int64(sizeOfFileCreated))
    85  	testutil.AssertEquals(t, exists, true) //test file that does exists reports true
    86  
    87  	//test that if the directory is not empty
    88  	dirEmpty5, err5 := DirEmpty(DbPathTest)
    89  	testutil.AssertNoError(t, err5, fmt.Sprintf("Error when detecting if empty at db directory [%s]", DbPathTest))
    90  	testutil.AssertEquals(t, dirEmpty5, false) //test directory is empty is returning false
    91  }
    92  
    93  func createAndWriteAFile(sentence string) (int, error) {
    94  	//create a file in the direcotry
    95  	f, err2 := os.Create(DbFileTest)
    96  	if err2 != nil {
    97  		return 0, err2
    98  	}
    99  	defer f.Close()
   100  
   101  	//write to the file
   102  	return f.WriteString(sentence)
   103  }
   104  
   105  func cleanup(path string) {
   106  	os.RemoveAll(path)
   107  }