github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/resource/context/internal/util_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package internal_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	"github.com/juju/testing/filetesting"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/resource/context/internal"
    15  )
    16  
    17  var _ = gc.Suite(&UtilSuite{})
    18  
    19  type UtilSuite struct {
    20  	testing.IsolationSuite
    21  
    22  	stub *internalStub
    23  }
    24  
    25  func (s *UtilSuite) SetUpTest(c *gc.C) {
    26  	s.IsolationSuite.SetUpTest(c)
    27  
    28  	s.stub = newInternalStub()
    29  }
    30  
    31  func (s *UtilSuite) TestCloseAndLogNoError(c *gc.C) {
    32  	closer := &filetesting.StubCloser{Stub: s.stub.Stub}
    33  	logger := &stubLogger{Stub: s.stub.Stub}
    34  
    35  	internal.CloseAndLog(closer, "a thing", logger)
    36  
    37  	s.stub.CheckCallNames(c, "Close")
    38  }
    39  
    40  func (s *UtilSuite) TestCloseAndLog(c *gc.C) {
    41  	closer := &filetesting.StubCloser{Stub: s.stub.Stub}
    42  	logger := &stubLogger{Stub: s.stub.Stub}
    43  	failure := errors.New("<failure>")
    44  	s.stub.SetErrors(failure)
    45  
    46  	internal.CloseAndLog(closer, "a thing", logger)
    47  
    48  	s.stub.CheckCallNames(c, "Close", "Errorf")
    49  	c.Check(logger.logged, gc.Equals, "while closing a thing: <failure>")
    50  }
    51  
    52  type stubLogger struct {
    53  	*testing.Stub
    54  
    55  	logged string
    56  }
    57  
    58  func (s *stubLogger) Errorf(msg string, args ...interface{}) {
    59  	s.AddCall("Errorf", msg, args)
    60  	s.NextErr() // Pop one off.
    61  
    62  	s.logged = fmt.Sprintf(msg, args...)
    63  }