Skip to content

Commit

Permalink
Implement stop for runtime launcher
Browse files Browse the repository at this point in the history
Extend the chaincode runtime launcher to support
stopping externally built/launched chaincodes and
docker chaincodes.

FAB-17047

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored and sykesm committed Jan 15, 2020
1 parent b578a96 commit 8aab447
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Runtime interface {
// Launcher is used to launch chaincode runtimes.
type Launcher interface {
Launch(ccid string, streamHandler extcc.StreamHandler) error
Stop(ccid string) error
}

// Lifecycle provides a way to retrieve chaincode definitions and the packages necessary to run them
Expand Down
14 changes: 14 additions & 0 deletions core/chaincode/runtime_launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ func (r *RuntimeLauncher) Launch(ccid string, streamHandler extcc.StreamHandler)
chaincodeLogger.Debug("launch complete")
return err
}

func (r *RuntimeLauncher) Stop(ccid string) error {
err := r.Runtime.Stop(ccid)
if err != nil {
return errors.WithMessagef(err, "failed to stop chaincode %s", ccid)
}

err = r.Registry.Deregister(ccid)
if err != nil {
return errors.WithMessagef(err, "failed to deregister chaincode %s", ccid)
}

return nil
}
39 changes: 37 additions & 2 deletions core/chaincode/runtime_launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ var _ = Describe("RuntimeLauncher", func() {
})

It("starts the runtime for the chaincode with no TLS", func() {

err := runtimeLauncher.Launch("chaincode-name:chaincode-version", fakeStreamHandler)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -445,7 +444,7 @@ var _ = Describe("RuntimeLauncher", func() {
})
})

Context("when stopping the runtime fails", func() {
Context("when stopping the runtime fails while launching", func() {
BeforeEach(func() {
fakeRuntime.StartReturns(errors.New("whirled-peas"))
fakeRuntime.StopReturns(errors.New("applesauce"))
Expand All @@ -456,4 +455,40 @@ var _ = Describe("RuntimeLauncher", func() {
Expect(err).To(MatchError("error starting container: whirled-peas"))
})
})

It("stops the runtime for the chaincode", func() {
err := runtimeLauncher.Stop("chaincode-name:chaincode-version")
Expect(err).NotTo(HaveOccurred())

Expect(fakeRuntime.StopCallCount()).To(Equal(1))
ccidArg := fakeRuntime.StopArgsForCall(0)
Expect(ccidArg).To(Equal("chaincode-name:chaincode-version"))

Expect(fakeRegistry.DeregisterCallCount()).To(Equal(1))
ccidArg = fakeRegistry.DeregisterArgsForCall(0)
Expect(ccidArg).To(Equal("chaincode-name:chaincode-version"))
})

Context("when stopping the runtime fails while stopping", func() {
BeforeEach(func() {
fakeRuntime.StopReturns(errors.New("liver-mush"))
})

It("preserves the initial error", func() {
err := runtimeLauncher.Stop("chaincode-name:chaincode-version")
Expect(err).To(MatchError("failed to stop chaincode chaincode-name:chaincode-version: liver-mush"))
Expect(fakeRegistry.DeregisterCallCount()).To(Equal(0))
})
})

Context("when deregistering fails while stopping", func() {
BeforeEach(func() {
fakeRegistry.DeregisterReturns(errors.New("pickled-okra"))
})

It("preserves the initial error", func() {
err := runtimeLauncher.Stop("chaincode-name:chaincode-version")
Expect(err).To(MatchError("failed to deregister chaincode chaincode-name:chaincode-version: pickled-okra"))
})
})
})

0 comments on commit 8aab447

Please sign in to comment.