Skip to content

Commit

Permalink
FAB-9604 Move container/vm.go to car test
Browse files Browse the repository at this point in the history
The container/vm.go file is only used by the car platform test.  There
is no reason to export it in the production code.

Change-Id: I519c75fe09bba51b165694e329241fee23f4268c
Signed-off-by: Jason Yellick <[email protected]>
  • Loading branch information
Jason Yellick committed May 2, 2018
1 parent 3d11489 commit ef600ce
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 258 deletions.
3 changes: 1 addition & 2 deletions core/chaincode/platforms/car/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/container"
"github.com/hyperledger/fabric/core/testutil"
pb "github.com/hyperledger/fabric/protos/peer"
)
Expand All @@ -33,7 +32,7 @@ func TestMain(m *testing.M) {
}

func TestCar_BuildImage(t *testing.T) {
vm, err := container.NewVM()
vm, err := NewVM()
if err != nil {
t.Errorf("Error getting VM: %s", err)
return
Expand Down
60 changes: 60 additions & 0 deletions core/chaincode/platforms/car/vm_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package car_test

import (
"bytes"
"fmt"

"github.com/fsouza/go-dockerclient"
"github.com/hyperledger/fabric/core/chaincode/platforms"
"github.com/hyperledger/fabric/core/container"
cutil "github.com/hyperledger/fabric/core/container/util"
pb "github.com/hyperledger/fabric/protos/peer"
)

// VM implementation of VM management functionality.
type VM struct {
Client *docker.Client
}

// NewVM creates a new VM instance.
func NewVM() (*VM, error) {
client, err := cutil.NewDockerClient()
if err != nil {
return nil, err
}
VM := &VM{Client: client}
return VM, nil
}

// BuildChaincodeContainer builds the container for the supplied chaincode specification
func (vm *VM) BuildChaincodeContainer(spec *pb.ChaincodeSpec) error {
codePackage, err := container.GetChaincodePackageBytes(spec)
if err != nil {
return fmt.Errorf("Error getting chaincode package bytes: %s", err)
}

cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackage}
dockerSpec, err := platforms.GenerateDockerBuild(cds)
if err != nil {
return fmt.Errorf("Error getting chaincode docker image: %s", err)
}

output := bytes.NewBuffer(nil)

err = vm.Client.BuildImage(docker.BuildImageOptions{
Name: spec.ChaincodeId.Name,
InputStream: dockerSpec,
OutputStream: output,
})
if err != nil {
return fmt.Errorf("Error building docker: %s (output = %s)", err, output.String())
}

return nil
}
14 changes: 14 additions & 0 deletions core/container/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (

"golang.org/x/net/context"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/platforms"
"github.com/hyperledger/fabric/core/container/api"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/container/dockercontroller"
"github.com/hyperledger/fabric/core/container/inproccontroller"
pb "github.com/hyperledger/fabric/protos/peer"
)

type refCountedLock struct {
Expand All @@ -33,6 +36,8 @@ type VMController struct {
containerLocks map[string]*refCountedLock
}

var vmLogger = flogging.MustGetLogger("container")

var vmcontroller = &VMController{
containerLocks: make(map[string]*refCountedLock),
}
Expand Down Expand Up @@ -198,3 +203,12 @@ func VMCProcess(ctxt context.Context, vmtype string, req VMCReqIntf) (VMCResp, e
return VMCResp{}, ctxt.Err()
}
}

// GetChaincodePackageBytes creates bytes for docker container generation using the supplied chaincode specification
func GetChaincodePackageBytes(spec *pb.ChaincodeSpec) ([]byte, error) {
if spec == nil || spec.ChaincodeId == nil {
return nil, fmt.Errorf("invalid chaincode spec")
}

return platforms.GetDeploymentPayload(spec)
}
60 changes: 25 additions & 35 deletions core/container/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package container
Expand All @@ -23,13 +13,16 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"time"

"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/container/api"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/container/dockercontroller"
"github.com/hyperledger/fabric/core/container/inproccontroller"
"github.com/hyperledger/fabric/core/testutil"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
Expand Down Expand Up @@ -164,26 +157,13 @@ func createImage() {
}
}

//set to true by providing "-run-controller-tests" command line option... Tests will create a docker image called "simple"
var runTests bool

// tests will fail if we don't build an image to start and stop, so check to see if this has been done
var imageCreated bool

func testForSkip(t *testing.T) {
if !imageCreated {
createImage()
}

//run tests
if !runTests {
t.SkipNow()
}
func TestMain(m *testing.M) {
testutil.SetupTestConfig()
createImage()
os.Exit(m.Run())
}

func TestVMCStartContainer(t *testing.T) {
testForSkip(t)

var ctxt = context.Background()

c := make(chan struct{})
Expand All @@ -208,8 +188,6 @@ func TestVMCStartContainer(t *testing.T) {
}

func TestVMCCreateAndStartContainer(t *testing.T) {
testForSkip(t)

var ctxt = context.Background()

c := make(chan struct{})
Expand Down Expand Up @@ -244,8 +222,6 @@ func TestVMCCreateAndStartContainer(t *testing.T) {
}

func TestVMCSyncStartContainer(t *testing.T) {
testForSkip(t)

var ctxt = context.Background()

//creat a StartImageReq obj and send it to VMCProcess
Expand All @@ -261,8 +237,6 @@ func TestVMCSyncStartContainer(t *testing.T) {
}

func TestVMCStopContainer(t *testing.T) {
testForSkip(t)

var ctxt = context.Background()

c := make(chan struct{})
Expand Down Expand Up @@ -295,3 +269,19 @@ func TestNewVM(t *testing.T) {

assert.Panics(t, func() { vmcontroller.newVM("") }, "Requested unknown VM but did not panic")
}

func TestVM_GetChaincodePackageBytes(t *testing.T) {
_, err := GetChaincodePackageBytes(nil)
assert.Error(t, err,
"GetChaincodePackageBytes did not return error when chaincode spec is nil")
spec := &pb.ChaincodeSpec{ChaincodeId: nil}
_, err = GetChaincodePackageBytes(spec)
assert.Error(t, err, "Error expected when GetChaincodePackageBytes is called with nil chaincode ID")
assert.Contains(t, err.Error(), "invalid chaincode spec")
spec = &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG,
ChaincodeId: nil,
Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("f")}}
_, err = GetChaincodePackageBytes(spec)
assert.Error(t, err,
"GetChaincodePackageBytes did not return error when chaincode ID is nil")
}
101 changes: 0 additions & 101 deletions core/container/vm.go

This file was deleted.

Loading

0 comments on commit ef600ce

Please sign in to comment.