github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/chaincode/platforms/node/platform_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package node
     8  
     9  import (
    10  	"archive/tar"
    11  	"bytes"
    12  	"compress/gzip"
    13  	"fmt"
    14  	"os"
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/hechain20/hechain/core/chaincode/platforms/util"
    19  	"github.com/hechain20/hechain/core/config/configtest"
    20  	"github.com/spf13/viper"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  var platform = &Platform{}
    25  
    26  type packageFile struct {
    27  	packagePath string
    28  	mode        int64
    29  }
    30  
    31  func TestValidatePath(t *testing.T) {
    32  	err := platform.ValidatePath("there/is/no/way/this/path/exists")
    33  	if err == nil {
    34  		t.Fatalf("should have returned an error on non-existent chaincode path")
    35  	} else if !strings.HasPrefix(err.Error(), "path to chaincode does not exist") {
    36  		t.Fatalf("should have returned an error about chaincode path not existent, but got '%v'", err)
    37  	}
    38  
    39  	err = platform.ValidatePath("http://something bad/because/it/has/the/space")
    40  	if err == nil {
    41  		t.Fatalf("should have returned an error on an empty chaincode path")
    42  	} else if !strings.HasPrefix(err.Error(), "invalid path") {
    43  		t.Fatalf("should have returned an error about parsing the path, but got '%v'", err)
    44  	}
    45  }
    46  
    47  func TestValidateCodePackage(t *testing.T) {
    48  	err := platform.ValidateCodePackage([]byte("dummy CodePackage content"))
    49  	if err == nil {
    50  		t.Fatalf("should have returned an error on an invalid chaincode package")
    51  	} else if !strings.HasPrefix(err.Error(), "failure opening codepackage gzip stream") {
    52  		t.Fatalf("should have returned an error about opening the invalid archive, but got '%v'", err)
    53  	}
    54  
    55  	cp, err := makeCodePackage([]*packageFile{{"filename.txt", 0o100744}})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	err = platform.ValidateCodePackage(cp)
    61  	if err == nil {
    62  		t.Fatal("should have failed to validate because file in the archive is in the root folder instead of 'src'")
    63  	} else if !strings.HasPrefix(err.Error(), "illegal file detected in payload") {
    64  		t.Fatalf("should have returned error about illegal file detected, but got '%s'", err)
    65  	}
    66  
    67  	cp, err = makeCodePackage([]*packageFile{{"src/filename.txt", 0o100744}})
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	err = platform.ValidateCodePackage(cp)
    73  	if err == nil {
    74  		t.Fatal("should have failed to validate because file in the archive is executable")
    75  	} else if !strings.HasPrefix(err.Error(), "illegal file mode detected for file") {
    76  		t.Fatalf("should have returned error about illegal file mode detected, but got '%s'", err)
    77  	}
    78  
    79  	cp, err = makeCodePackage([]*packageFile{{"src/filename.txt", 0o100666}})
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	err = platform.ValidateCodePackage(cp)
    85  	if err == nil {
    86  		t.Fatal("should have failed to validate because no 'package.json' found")
    87  	} else if !strings.HasPrefix(err.Error(), "no package.json found at the root of the chaincode package") {
    88  		t.Fatalf("should have returned error about no package.json found, but got '%s'", err)
    89  	}
    90  
    91  	cp, err = makeCodePackage([]*packageFile{{"src/package.json", 0o100666}, {"META-INF/path/to/meta", 0o100744}})
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	err = platform.ValidateCodePackage(cp)
    97  	if err == nil {
    98  		t.Fatalf("should have failed to validate because file in the archive is executable")
    99  	} else if !strings.HasPrefix(err.Error(), "illegal file mode detected for file") {
   100  		t.Fatalf("should have returned error about illegal file mode detected, but got '%s'", err)
   101  	}
   102  	cp, err = makeCodePackage([]*packageFile{{"src/package.json", 0o100666}, {"META-INF/path/to/meta", 0o100666}})
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	err = platform.ValidateCodePackage(cp)
   108  	if err != nil {
   109  		t.Fatalf("should have returned no errors, but got '%s'", err)
   110  	}
   111  }
   112  
   113  func TestGetDeploymentPayload(t *testing.T) {
   114  	_, err := platform.GetDeploymentPayload("")
   115  	if err == nil {
   116  		t.Fatal("should have failed to product deployment payload due to empty chaincode path")
   117  	} else if !strings.HasPrefix(err.Error(), "ChaincodeSpec's path cannot be empty") {
   118  		t.Fatalf("should have returned error about path being empty, but got '%s'", err)
   119  	}
   120  }
   121  
   122  func TestGenerateDockerfile(t *testing.T) {
   123  	str, _ := platform.GenerateDockerfile()
   124  	if !strings.Contains(str, "/fabric-nodeenv:") {
   125  		t.Fatalf("should have generated a docker file using the fabric-nodeenv, but got %s", str)
   126  	}
   127  
   128  	if !strings.Contains(str, "ADD binpackage.tar /usr/local/src") {
   129  		t.Fatalf("should have generated a docker file that adds code package content to /usr/local/src, but got %s", str)
   130  	}
   131  }
   132  
   133  var expectedBuildScript = `
   134  set -e
   135  if [ -x /chaincode/build.sh ]; then
   136  	/chaincode/build.sh
   137  else
   138  	cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production
   139  fi
   140  `
   141  
   142  func TestGenerateBuildOptions(t *testing.T) {
   143  	opts, err := platform.DockerBuildOptions("pathname")
   144  	require.NoError(t, err)
   145  
   146  	expectedOpts := util.DockerBuildOptions{
   147  		Image: "hyperledger/fabric-nodeenv:latest",
   148  		Cmd:   expectedBuildScript,
   149  	}
   150  	require.Equal(t, expectedOpts, opts)
   151  }
   152  
   153  func makeCodePackage(pfiles []*packageFile) ([]byte, error) {
   154  	contents := []byte("fake file's content")
   155  
   156  	payload := bytes.NewBuffer(nil)
   157  	gw := gzip.NewWriter(payload)
   158  	tw := tar.NewWriter(gw)
   159  
   160  	for _, f := range pfiles {
   161  		if err := tw.WriteHeader(&tar.Header{
   162  			Name: f.packagePath,
   163  			Mode: f.mode,
   164  			Size: int64(len(contents)),
   165  		}); err != nil {
   166  			return nil, fmt.Errorf("Error write header: %s", err)
   167  		}
   168  
   169  		if _, err := tw.Write(contents); err != nil {
   170  			return nil, fmt.Errorf("Error writing contents: %s", err)
   171  		}
   172  	}
   173  
   174  	// Write the tar file out
   175  	if err := tw.Close(); err != nil {
   176  		return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
   177  	}
   178  
   179  	gw.Close()
   180  
   181  	return payload.Bytes(), nil
   182  }
   183  
   184  func TestMain(m *testing.M) {
   185  	viper.SetConfigName("core")
   186  	viper.SetEnvPrefix("CORE")
   187  	configtest.AddDevConfigPath(nil)
   188  	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
   189  	viper.AutomaticEnv()
   190  	if err := viper.ReadInConfig(); err != nil {
   191  		fmt.Printf("could not read config %s\n", err)
   192  		os.Exit(-1)
   193  	}
   194  	os.Exit(m.Run())
   195  }