github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/orderer/util_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 main
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	config "github.com/hyperledger/fabric/orderer/localconfig"
    24  )
    25  
    26  func TestCreateLedgerFactory(t *testing.T) {
    27  	testCases := []struct {
    28  		name            string
    29  		ledgerType      string
    30  		ledgerDir       string
    31  		ledgerDirPrefix string
    32  		expectPanic     bool
    33  	}{
    34  		{"RAM", "ram", "", "", false},
    35  		{"JSONwithPathSet", "json", "test-dir", "", false},
    36  		{"JSONwithPathUnset", "json", "", "test-prefix", false},
    37  		{"FilewithPathSet", "file", "test-dir", "", false},
    38  		{"FilewithPathUnset", "file", "", "test-prefix", false},
    39  	}
    40  
    41  	conf := config.Load()
    42  
    43  	for _, tc := range testCases {
    44  		t.Run(tc.name, func(t *testing.T) {
    45  			defer func() {
    46  				r := recover()
    47  				if tc.expectPanic && r == nil {
    48  					t.Fatal("Should have panicked")
    49  				}
    50  				if !tc.expectPanic && r != nil {
    51  					t.Fatal("Should not have panicked")
    52  				}
    53  			}()
    54  
    55  			conf.General.LedgerType = tc.ledgerType
    56  			conf.FileLedger.Location = tc.ledgerDir
    57  			conf.FileLedger.Prefix = tc.ledgerDirPrefix
    58  			lf, ld := createLedgerFactory(conf)
    59  
    60  			defer func() {
    61  				if ld != "" {
    62  					os.RemoveAll(ld)
    63  					t.Log("Removed temp dir:", ld)
    64  				}
    65  			}()
    66  			lf.ChainIDs()
    67  		})
    68  	}
    69  }
    70  
    71  func TestCreateSubDir(t *testing.T) {
    72  	testCases := []struct {
    73  		name          string
    74  		count         int
    75  		expectCreated bool
    76  		expectPanic   bool
    77  	}{
    78  		{"CleanDir", 1, true, false},
    79  		{"HasSubDir", 2, false, false},
    80  	}
    81  
    82  	for _, tc := range testCases {
    83  		t.Run(tc.name, func(t *testing.T) {
    84  			defer func() {
    85  				r := recover()
    86  				if tc.expectPanic && r == nil {
    87  					t.Fatal("Should have panicked")
    88  				}
    89  				if !tc.expectPanic && r != nil {
    90  					t.Fatal("Should not have panicked")
    91  				}
    92  			}()
    93  
    94  			parentDirPath := createTempDir("test-dir")
    95  
    96  			var created bool
    97  			for i := 0; i < tc.count; i++ {
    98  				_, created = createSubDir(parentDirPath, "test-sub-dir")
    99  			}
   100  
   101  			if created != tc.expectCreated {
   102  				t.Fatalf("Sub dir created = %v, but expectation was = %v", created, tc.expectCreated)
   103  			}
   104  		})
   105  	}
   106  }