Skip to content

Commit

Permalink
tests for *Service
Browse files Browse the repository at this point in the history
  • Loading branch information
opalmer committed Apr 27, 2017
1 parent 6ff49f3 commit 545579b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
12 changes: 5 additions & 7 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Service struct {
// called. This function is called by Run() before returning.
Ping Ping

// Image is the container image you wish to run.
Image string
// Input is used to control the inputs to Run()
Input *ClientInput

// Timeout defines a duration that's used to prevent operations
// related to docker from running forever. If this value is not
Expand All @@ -54,16 +54,14 @@ func (s *Service) timeout() time.Duration {

// Run will run the container.
func (s *Service) Run() error {
if s.Image == "" {
return errors.New("No image provided")
if s.Input == nil {
return errors.New("Input field not provided")
}

ctx, cancel := context.WithTimeout(context.Background(), s.timeout())
defer cancel()

runInput := NewClientInput(s.Image)

info, err := s.Client.RunContainer(ctx, runInput)
info, err := s.Client.RunContainer(ctx, s.Input)
if err != nil {
return err
}
Expand Down
28 changes: 22 additions & 6 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dockertest

import (
. "gopkg.in/check.v1"
"time"
"net"
"fmt"
"net"
"time"

"github.com/pkg/errors"
. "gopkg.in/check.v1"
)

type ServiceTest struct{}
Expand Down Expand Up @@ -33,8 +35,8 @@ func (*ServiceTest) TestRunWithPing(c *C) {

input := NewClientInput(testImage)
input.Ports.Add(&Port{
Private: 80,
Public: RandomPort,
Private: 80,
Public: RandomPort,
Protocol: ProtocolTCP,
})
svc := dc.Service(input)
Expand All @@ -52,4 +54,18 @@ func (*ServiceTest) TestRunWithPing(c *C) {
}
c.Assert(svc.Run(), IsNil)
c.Assert(svc.Terminate(), IsNil)
}
}

func (*ServiceTest) TestErrorOnPingCallsTerminate(c *C) {
dc, err := NewClient()
c.Assert(err, IsNil)
defer dc.Client.Close()

input := NewClientInput(testImage)
svc := dc.Service(input)
svc.Ping = func(input *PingInput) error {
return errors.New("Some error")
}
c.Assert(svc.Run(), ErrorMatches, "Some error")
c.Assert(svc.Terminate(), IsNil)
}

0 comments on commit 545579b

Please sign in to comment.