github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/server/util_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package server
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	config "github.com/hyperledger/fabric/orderer/common/localconfig"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCreateLedgerFactory(t *testing.T) {
    18  	testCases := []struct {
    19  		name            string
    20  		ledgerType      string
    21  		ledgerDir       string
    22  		ledgerDirPrefix string
    23  		expectPanic     bool
    24  	}{
    25  		{"RAM", "ram", "", "", false},
    26  		{"JSONwithPathSet", "json", "test-dir", "", false},
    27  		{"JSONwithPathUnset", "json", "", "test-prefix", false},
    28  		{"FilewithPathSet", "file", "test-dir", "", false},
    29  		{"FilewithPathUnset", "file", "", "test-prefix", false},
    30  	}
    31  
    32  	conf := config.Load()
    33  
    34  	for _, tc := range testCases {
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			defer func() {
    37  				r := recover()
    38  				if tc.expectPanic && r == nil {
    39  					t.Fatal("Should have panicked")
    40  				}
    41  				if !tc.expectPanic && r != nil {
    42  					t.Fatal("Should not have panicked")
    43  				}
    44  			}()
    45  
    46  			conf.General.LedgerType = tc.ledgerType
    47  			conf.FileLedger.Location = tc.ledgerDir
    48  			conf.FileLedger.Prefix = tc.ledgerDirPrefix
    49  			lf, ld := createLedgerFactory(conf)
    50  
    51  			defer func() {
    52  				if ld != "" {
    53  					os.RemoveAll(ld)
    54  					t.Log("Removed temp dir:", ld)
    55  				}
    56  			}()
    57  			lf.ChainIDs()
    58  		})
    59  	}
    60  }
    61  
    62  func TestCreateSubDir(t *testing.T) {
    63  	testCases := []struct {
    64  		name          string
    65  		count         int
    66  		expectCreated bool
    67  		expectPanic   bool
    68  	}{
    69  		{"CleanDir", 1, true, false},
    70  		{"HasSubDir", 2, false, false},
    71  	}
    72  
    73  	for _, tc := range testCases {
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			defer func() {
    76  				r := recover()
    77  				if tc.expectPanic && r == nil {
    78  					t.Fatal("Should have panicked")
    79  				}
    80  				if !tc.expectPanic && r != nil {
    81  					t.Fatal("Should not have panicked")
    82  				}
    83  			}()
    84  
    85  			parentDirPath := createTempDir("test-dir")
    86  
    87  			var created bool
    88  			for i := 0; i < tc.count; i++ {
    89  				_, created = createSubDir(parentDirPath, "test-sub-dir")
    90  			}
    91  
    92  			if created != tc.expectCreated {
    93  				t.Fatalf("Sub dir created = %v, but expectation was = %v", created, tc.expectCreated)
    94  			}
    95  		})
    96  	}
    97  	t.Run("ParentDirNotExists", func(t *testing.T) {
    98  		assert.Panics(t, func() { createSubDir(os.TempDir(), "foo/name") })
    99  	})
   100  }
   101  
   102  func TestCreateTempDir(t *testing.T) {
   103  	t.Run("Good", func(t *testing.T) {
   104  		tempDir := createTempDir("foo")
   105  		if _, err := os.Stat(tempDir); err != nil {
   106  			t.Fatal(err)
   107  		}
   108  	})
   109  
   110  	t.Run("Bad", func(t *testing.T) {
   111  		assert.Panics(t, func() {
   112  			createTempDir("foo/bar")
   113  		})
   114  	})
   115  
   116  }