github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/core/config/configtest/config.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtest
     8  
     9  import (
    10  	"bytes"
    11  	"errors"
    12  	"fmt"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/spf13/viper"
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  // AddDevConfigPath adds the DevConfigDir to the viper path.
    25  func AddDevConfigPath(v *viper.Viper) {
    26  	devPath := GetDevConfigDir()
    27  	if v != nil {
    28  		v.AddConfigPath(devPath)
    29  	} else {
    30  		viper.AddConfigPath(devPath)
    31  	}
    32  }
    33  
    34  func dirExists(path string) bool {
    35  	fi, err := os.Stat(path)
    36  	if err != nil {
    37  		return false
    38  	}
    39  	return fi.IsDir()
    40  }
    41  
    42  // GetDevConfigDir gets the path to the default configuration that is
    43  // maintained with the source tree. This should only be used in a
    44  // test/development context.
    45  func GetDevConfigDir() string {
    46  	path, err := gomodDevConfigDir()
    47  	if err != nil {
    48  		path, err = gopathDevConfigDir()
    49  		if err != nil {
    50  			panic(err)
    51  		}
    52  	}
    53  	return path
    54  }
    55  
    56  func gopathDevConfigDir() (string, error) {
    57  	buf := bytes.NewBuffer(nil)
    58  	cmd := exec.Command("go", "env", "GOPATH")
    59  	cmd.Stdout = buf
    60  	if err := cmd.Run(); err != nil {
    61  		return "", err
    62  	}
    63  
    64  	gopath := strings.TrimSpace(buf.String())
    65  	for _, p := range filepath.SplitList(gopath) {
    66  		devPath := filepath.Join(p, "src/github.com/osdi23p228/fabric/sampleconfig")
    67  		if dirExists(devPath) {
    68  			return devPath, nil
    69  		}
    70  	}
    71  
    72  	return "", fmt.Errorf("unable to find sampleconfig directory on GOPATH")
    73  }
    74  
    75  func gomodDevConfigDir() (string, error) {
    76  	buf := bytes.NewBuffer(nil)
    77  	cmd := exec.Command("go", "env", "GOMOD")
    78  	cmd.Stdout = buf
    79  
    80  	if err := cmd.Run(); err != nil {
    81  		return "", err
    82  	}
    83  
    84  	modFile := strings.TrimSpace(buf.String())
    85  	if modFile == "" {
    86  		return "", errors.New("not a module or not in module mode")
    87  	}
    88  
    89  	devPath := filepath.Join(filepath.Dir(modFile), "sampleconfig")
    90  	if !dirExists(devPath) {
    91  		return "", fmt.Errorf("%s does not exist", devPath)
    92  	}
    93  
    94  	return devPath, nil
    95  }
    96  
    97  // GetDevMspDir gets the path to the sampleconfig/msp tree that is maintained
    98  // with the source tree.  This should only be used in a test/development
    99  // context.
   100  func GetDevMspDir() string {
   101  	devDir := GetDevConfigDir()
   102  	return filepath.Join(devDir, "msp")
   103  }
   104  
   105  func SetDevFabricConfigPath(t *testing.T) (cleanup func()) {
   106  	t.Helper()
   107  
   108  	oldFabricCfgPath, resetFabricCfgPath := os.LookupEnv("FABRIC_CFG_PATH")
   109  	devConfigDir := GetDevConfigDir()
   110  
   111  	err := os.Setenv("FABRIC_CFG_PATH", devConfigDir)
   112  	require.NoError(t, err, "failed to set FABRIC_CFG_PATH")
   113  	if resetFabricCfgPath {
   114  		return func() {
   115  			err := os.Setenv("FABRIC_CFG_PATH", oldFabricCfgPath)
   116  			assert.NoError(t, err)
   117  		}
   118  	}
   119  
   120  	return func() {
   121  		err := os.Unsetenv("FABRIC_CFG_PATH")
   122  		assert.NoError(t, err)
   123  	}
   124  }