Skip to content

Commit

Permalink
FAB-9807 Remove unused VM methods
Browse files Browse the repository at this point in the history
Killing yet more dead code.

Change-Id: Ia2d94def5d51e1c2b3891d2a58420526f9d815a5
Signed-off-by: Jason Yellick <[email protected]>
  • Loading branch information
Jason Yellick committed May 10, 2018
1 parent 7bc73ea commit 90281f4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 217 deletions.
6 changes: 2 additions & 4 deletions core/container/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ type Builder interface {

//VM is an abstract virtual image for supporting arbitrary virual machines
type VM interface {
Deploy(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, reader io.Reader) error
Start(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, filesToUpload map[string][]byte, builder Builder) error
Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, dontkill bool, dontremove bool) error
Destroy(ctxt context.Context, ccid ccintf.CCID, force bool, noprune bool) error
GetVMName(ccID ccintf.CCID) string
}

type refCountedLock struct {
Expand Down Expand Up @@ -165,7 +162,8 @@ func (vmc *VMController) Process(ctxt context.Context, vmtype string, req VMCReq

c := make(chan error)
go func() {
id := v.GetVMName(req.GetCCID())
ccid := req.GetCCID()
id := ccid.GetName()
vmc.lockContainer(id)
err := req.Do(ctxt, v)
vmc.unlockContainer(id)
Expand Down
44 changes: 0 additions & 44 deletions core/container/dockercontroller/dockercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,6 @@ func (vm *DockerVM) deployImage(client dockerClient, ccid ccintf.CCID,
return nil
}

//Deploy use the reader containing targz to create a docker image
//for docker inputbuf is tar reader ready for use by docker.Client
//the stream from end client to peer could directly be this tar stream
//talk to docker daemon using docker Client and build the image
func (vm *DockerVM) Deploy(ctxt context.Context, ccid ccintf.CCID,
args []string, env []string, reader io.Reader) error {

client, err := vm.getClientFnc()
switch err {
case nil:
if err = vm.deployImage(client, ccid, args, env, reader); err != nil {
return err
}
default:
return fmt.Errorf("Error creating docker client: %s", err)
}
return nil
}

//Start starts a container using a previously created docker image
func (vm *DockerVM) Start(ctxt context.Context, ccid ccintf.CCID,
args []string, env []string, filesToUpload map[string][]byte, builder container.Builder) error {
Expand Down Expand Up @@ -443,31 +424,6 @@ func (vm *DockerVM) stopInternal(ctxt context.Context, client dockerClient,
return err
}

//Destroy destroys an image
func (vm *DockerVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool, noprune bool) error {
id, err := vm.GetVMNameForDocker(ccid)
if err != nil {
return err
}

client, err := vm.getClientFnc()
if err != nil {
dockerLogger.Errorf("destroy-cannot create client %s", err)
return err
}
id = strings.Replace(id, ":", "_", -1)

err = client.RemoveImageExtended(id, docker.RemoveImageOptions{Force: force, NoPrune: noprune})

if err != nil {
dockerLogger.Errorf("error while destroying image: %s", err)
} else {
dockerLogger.Debugf("Destroyed image %s", id)
}

return err
}

// GetVMName generates the VM name from peer information. It accepts a format
// function parameter to allow different formatting based on the desired use of
// the name.
Expand Down
66 changes: 6 additions & 60 deletions core/container/dockercontroller/dockercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ import (
)

// This test used to be part of an integration style test in core/container, moved to here
func TestRealPath(t *testing.T) {
func TestIntegrationPath(t *testing.T) {
coreutil.SetupTestConfig()
ctxt := context.Background()
dc := NewDockerVM("", "")
ccid := ccintf.CCID{Name: "simple"}
reader := getCodeChainBytesInMem()

err := dc.Deploy(ctxt, ccid, nil, nil, reader)
require.NoError(t, err)

err = dc.Start(ctxt, ccid, nil, nil, nil, nil)
err := dc.Start(ctxt, ccid, nil, nil, nil, InMemBuilder{})
require.NoError(t, err)

// Stop, killing, and deleting
Expand Down Expand Up @@ -85,34 +81,6 @@ func TestGetDockerHostConfig(t *testing.T) {
testutil.AssertEquals(t, hostConfig.CPUShares, int64(1024*1024*1024*2))
}

func Test_Deploy(t *testing.T) {
dvm := DockerVM{}
ccid := ccintf.CCID{Name: "simple"}
//get the tarball for codechain
tarRdr := getCodeChainBytesInMem()
args := make([]string, 1)
env := make([]string, 1)
ctx := context.Background()

// getMockClient returns error
getClientErr = true
dvm.getClientFnc = getMockClient
err := dvm.Deploy(ctx, ccid, args, env, tarRdr)
testerr(t, err, false)
getClientErr = false

// Failure case: dockerClient.BuildImage returns error
buildErr = true
dvm.getClientFnc = getMockClient
err = dvm.Deploy(ctx, ccid, args, env, tarRdr)
testerr(t, err, false)
buildErr = false

// Success case
err = dvm.Deploy(ctx, ccid, args, env, tarRdr)
testerr(t, err, true)
}

func Test_Start(t *testing.T) {
dvm := DockerVM{}
ccid := ccintf.CCID{Name: "simple"}
Expand Down Expand Up @@ -213,30 +181,6 @@ func Test_Stop(t *testing.T) {
testerr(t, err, true)
}

func Test_Destroy(t *testing.T) {
dvm := DockerVM{}
ccid := ccintf.CCID{Name: "simple"}
ctx := context.Background()

// Failure cases
// Case 1: getMockClient returns error
getClientErr = true
dvm.getClientFnc = getMockClient
err := dvm.Destroy(ctx, ccid, true, true)
testerr(t, err, false)
getClientErr = false

// Case 2: dockerClient.RemoveImageExtended returns error
removeImgErr = true
err = dvm.Destroy(ctx, ccid, true, true)
testerr(t, err, false)
removeImgErr = false

// Success case
err = dvm.Destroy(ctx, ccid, true, true)
testerr(t, err, true)
}

type testCase struct {
name string
vm *DockerVM
Expand Down Expand Up @@ -314,7 +258,9 @@ func TestGetVMName(t *testing.T) {
assert.NotNil(t, err, "Expected error")
}*/

func getCodeChainBytesInMem() io.Reader {
type InMemBuilder struct{}

func (imb InMemBuilder) Build() (io.Reader, error) {
startTime := time.Now()
inputbuf := bytes.NewBuffer(nil)
gw := gzip.NewWriter(inputbuf)
Expand All @@ -327,7 +273,7 @@ func getCodeChainBytesInMem() io.Reader {
tr.Write([]byte(dockerFileContents))
tr.Close()
gw.Close()
return inputbuf
return inputbuf, nil
}

func testerr(t *testing.T, err error, succ bool) {
Expand Down
30 changes: 0 additions & 30 deletions core/container/inproccontroller/inproccontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package inproccontroller

import (
"fmt"
"io"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/shim"
Expand Down Expand Up @@ -107,29 +106,6 @@ func (vm *InprocVM) getInstance(ctxt context.Context, ipctemplate *inprocContain
return ipc, nil
}

//Deploy verifies chaincode is registered and creates an instance for it. Currently only one instance can be created
func (vm *InprocVM) Deploy(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, reader io.Reader) error {
path := ccid.GetName()
inprocLogger.Debugf("Deploying chaincode instance: %s", path)

ipctemplate := vm.registry.typeRegistry[path]
if ipctemplate == nil {
return fmt.Errorf(fmt.Sprintf("%s not registered. Please register the system chaincode in inprocinstances.go", path))
}

if ipctemplate.chaincode == nil {
return fmt.Errorf(fmt.Sprintf("%s system chaincode does not contain chaincode instance", path))
}

instName := vm.GetVMName(ccid)
_, err := vm.getInstance(ctxt, ipctemplate, instName, args, env)

//FUTURE ... here is where we might check code for safety
inprocLogger.Debugf("registered : %s", path)

return err
}

func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args []string, env []string, ccSupport ccintf.CCSupport) error {
peerRcvCCSend := make(chan *pb.ChaincodeMessage)
ccRcvPeerSend := make(chan *pb.ChaincodeMessage)
Expand Down Expand Up @@ -251,12 +227,6 @@ func (vm *InprocVM) Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, d
return nil
}

//Destroy destroys an image
func (vm *InprocVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool, noprune bool) error {
//not implemented
return nil
}

// GetVMName ignores the peer and network name as it just needs to be unique in
// process. It accepts a format function parameter to allow different
// formatting based on the desired use of the name.
Expand Down
79 changes: 0 additions & 79 deletions core/container/inproccontroller/inproccontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,74 +131,6 @@ func (r MockReader) Read(p []byte) (n int, err error) {
return 1, nil
}

func TestDeployNotRegistered(t *testing.T) {
mockContext := MockContext{}
r := NewRegistry()
vm := NewInprocVM(r)

ccid := ccintf.CCID{Name: "name"}

mockInprocContainer := &inprocContainer{
chaincode: MockShim{},
}

args := []string{"a", "b"}
env := []string{"a", "b"}
mockReader := MockReader{}
ipc := &inprocContainer{args: args, env: env, chaincode: mockInprocContainer.chaincode, stopChan: make(chan struct{})}
r.instRegistry["instName"] = ipc

err := vm.Deploy(mockContext, ccid, args, env, mockReader)

assert.NotNil(t, err, "err should not be nil")
assert.Equal(t, err.Error(), "name not registered. Please register the system chaincode in inprocinstances.go", "error message should be correct")
}

func TestDeployNoChaincodeInstance(t *testing.T) {
mockContext := MockContext{}
r := NewRegistry()
vm := NewInprocVM(r)

ccid := ccintf.CCID{Name: "name"}

mockInprocContainer := &inprocContainer{}

args := []string{"a", "b"}
env := []string{"a", "b"}
mockReader := MockReader{}

ipc := &inprocContainer{args: args, env: env, chaincode: mockInprocContainer.chaincode, stopChan: make(chan struct{})}

r.typeRegistry["name"] = ipc

err := vm.Deploy(mockContext, ccid, args, env, mockReader)
assert.NotNil(t, err, "err should not be nil")
assert.Equal(t, err.Error(), "name system chaincode does not contain chaincode instance")
}

func TestDeployChaincode(t *testing.T) {
mockContext := MockContext{}
r := NewRegistry()
vm := NewInprocVM(r)

ccid := ccintf.CCID{Name: "name"}

mockInprocContainer := &inprocContainer{
chaincode: MockShim{},
}

args := []string{"a", "b"}
env := []string{"a", "b"}
mockReader := MockReader{}

ipc := &inprocContainer{args: args, env: env, chaincode: mockInprocContainer.chaincode, stopChan: make(chan struct{})}

r.typeRegistry["name"] = ipc

err := vm.Deploy(mockContext, ccid, args, env, mockReader)
assert.Nil(t, err, "err should be nil")
}

type MockCCSupport struct {
}

Expand Down Expand Up @@ -497,14 +429,3 @@ func TestStopIPCNotRunning(t *testing.T) {
assert.NotNil(t, err, "err should not be nil")
assert.Equal(t, err.Error(), "name-1 not running", "error should be correct")
}

func TestDestroy(t *testing.T) {
vm := NewInprocVM(NewRegistry())
mockContext := MockContext{}
ccid := ccintf.CCID{
Name: "name",
Version: "1",
}
err := vm.Destroy(mockContext, ccid, true, true)
assert.Nil(t, err, "err should be nil")
}

0 comments on commit 90281f4

Please sign in to comment.