github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/container/vm_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 container
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/hyperledger/fabric/common/util"
    25  	"github.com/hyperledger/fabric/core/testutil"
    26  	pb "github.com/hyperledger/fabric/protos/peer"
    27  	"github.com/stretchr/testify/assert"
    28  	"golang.org/x/net/context"
    29  )
    30  
    31  func TestMain(m *testing.M) {
    32  	flag.BoolVar(&runTests, "run-controller-tests", true, "run tests")
    33  	flag.Parse()
    34  	testutil.SetupTestConfig()
    35  	os.Exit(m.Run())
    36  }
    37  
    38  func TestVM_ListImages(t *testing.T) {
    39  	vm, err := NewVM()
    40  	if err != nil {
    41  		t.Fail()
    42  		t.Logf("Error getting VM: %s", err)
    43  	}
    44  	err = vm.ListImages(context.TODO())
    45  	assert.NoError(t, err, "Error listing images")
    46  }
    47  
    48  func TestVM_BuildImage_ChaincodeLocal(t *testing.T) {
    49  	vm, err := NewVM()
    50  	if err != nil {
    51  		t.Fail()
    52  		t.Logf("Error getting VM: %s", err)
    53  		return
    54  	}
    55  	// Build the spec
    56  	chaincodePath := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01"
    57  	spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
    58  		ChaincodeId: &pb.ChaincodeID{Name: "ex01", Path: chaincodePath},
    59  		Input:       &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}}
    60  	err = vm.BuildChaincodeContainer(spec)
    61  	assert.NoError(t, err)
    62  }
    63  
    64  func TestVM_BuildImage_ChaincodeRemote(t *testing.T) {
    65  	t.Skip("Works but needs user credentials. Not suitable for automated unit tests as is")
    66  	vm, err := NewVM()
    67  	if err != nil {
    68  		t.Fail()
    69  		t.Logf("Error getting VM: %s", err)
    70  		return
    71  	}
    72  	// Build the spec
    73  	chaincodePath := "https://github.com/prjayach/chaincode_examples/chaincode_example02"
    74  	spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
    75  		ChaincodeId: &pb.ChaincodeID{Name: "ex02", Path: chaincodePath},
    76  		Input:       &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}}
    77  	err = vm.BuildChaincodeContainer(spec)
    78  	assert.NoError(t, err)
    79  }
    80  
    81  func TestVM_GetChaincodePackageBytes(t *testing.T) {
    82  	_, err := GetChaincodePackageBytes(nil)
    83  	assert.Error(t, err,
    84  		"GetChaincodePackageBytes did not return error when chaincode spec is nil")
    85  
    86  	spec := &pb.ChaincodeSpec{ChaincodeId: nil}
    87  	_, err = GetChaincodePackageBytes(spec)
    88  	assert.Error(t, err, "Error expected when GetChaincodePackageBytes is called with nil chaincode ID")
    89  	assert.Contains(t, err.Error(), "invalid chaincode spec")
    90  
    91  	spec = &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
    92  		ChaincodeId: nil,
    93  		Input:       &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}}
    94  	_, err = GetChaincodePackageBytes(spec)
    95  	assert.Error(t, err,
    96  		"GetChaincodePackageBytes did not return error when chaincode ID is nil")
    97  }
    98  
    99  func TestVM_BuildChaincodeContainer(t *testing.T) {
   100  	vm, err := NewVM()
   101  	assert.NoError(t, err)
   102  	err = vm.BuildChaincodeContainer(nil)
   103  	assert.Error(t, err)
   104  	assert.Contains(t, err.Error(), "Error getting chaincode package bytes")
   105  }
   106  
   107  func TestVM_Chaincode_Compile(t *testing.T) {
   108  	// vm, err := NewVM()
   109  	// if err != nil {
   110  	// 	t.Fail()
   111  	// 	t.Logf("Error getting VM: %s", err)
   112  	// 	return
   113  	// }
   114  
   115  	// if err := vm.BuildPeerContainer(); err != nil {
   116  	// 	t.Fail()
   117  	// 	t.Log(err)
   118  	// }
   119  	t.Skip("NOT IMPLEMENTED")
   120  }