github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestCreateLedgerFactory(t *testing.T) {
    28  	testCases := []struct {
    29  		name            string
    30  		ledgerType      string
    31  		ledgerDir       string
    32  		ledgerDirPrefix string
    33  		expectPanic     bool
    34  	}{
    35  		{"RAM", "ram", "", "", false},
    36  		{"JSONwithPathSet", "json", "test-dir", "", false},
    37  		{"JSONwithPathUnset", "json", "", "test-prefix", false},
    38  		{"FilewithPathSet", "file", "test-dir", "", false},
    39  		{"FilewithPathUnset", "file", "", "test-prefix", false},
    40  	}
    41  
    42  	conf := config.Load()
    43  
    44  	for _, tc := range testCases {
    45  		t.Run(tc.name, func(t *testing.T) {
    46  			defer func() {
    47  				r := recover()
    48  				if tc.expectPanic && r == nil {
    49  					t.Fatal("Should have panicked")
    50  				}
    51  				if !tc.expectPanic && r != nil {
    52  					t.Fatal("Should not have panicked")
    53  				}
    54  			}()
    55  
    56  			conf.General.LedgerType = tc.ledgerType
    57  			conf.FileLedger.Location = tc.ledgerDir
    58  			conf.FileLedger.Prefix = tc.ledgerDirPrefix
    59  			lf, ld := createLedgerFactory(conf)
    60  
    61  			defer func() {
    62  				if ld != "" {
    63  					os.RemoveAll(ld)
    64  					t.Log("Removed temp dir:", ld)
    65  				}
    66  			}()
    67  			lf.ChainIDs()
    68  		})
    69  	}
    70  }
    71  
    72  func TestCreateSubDir(t *testing.T) {
    73  	testCases := []struct {
    74  		name          string
    75  		count         int
    76  		expectCreated bool
    77  		expectPanic   bool
    78  	}{
    79  		{"CleanDir", 1, true, false},
    80  		{"HasSubDir", 2, false, false},
    81  	}
    82  
    83  	for _, tc := range testCases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			defer func() {
    86  				r := recover()
    87  				if tc.expectPanic && r == nil {
    88  					t.Fatal("Should have panicked")
    89  				}
    90  				if !tc.expectPanic && r != nil {
    91  					t.Fatal("Should not have panicked")
    92  				}
    93  			}()
    94  
    95  			parentDirPath := createTempDir("test-dir")
    96  
    97  			var created bool
    98  			for i := 0; i < tc.count; i++ {
    99  				_, created = createSubDir(parentDirPath, "test-sub-dir")
   100  			}
   101  
   102  			if created != tc.expectCreated {
   103  				t.Fatalf("Sub dir created = %v, but expectation was = %v", created, tc.expectCreated)
   104  			}
   105  		})
   106  	}
   107  	t.Run("ParentDirNotExists", func(t *testing.T) {
   108  		assert.Panics(t, func() { createSubDir(os.TempDir(), "foo/name") })
   109  	})
   110  }
   111  
   112  func TestCreateTempDir(t *testing.T) {
   113  	t.Run("Good", func(t *testing.T) {
   114  		tempDir := createTempDir("foo")
   115  		if _, err := os.Stat(tempDir); err != nil {
   116  			t.Fatal(err)
   117  		}
   118  	})
   119  
   120  	t.Run("Bad", func(t *testing.T) {
   121  		assert.Panics(t, func() {
   122  			createTempDir("foo/bar")
   123  		})
   124  	})
   125  
   126  }