github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/migrationtarget/client_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationtarget_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jujutesting "github.com/juju/testing"
     9  	"github.com/juju/version"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	apitesting "github.com/juju/juju/api/base/testing"
    14  	"github.com/juju/juju/api/migrationtarget"
    15  	"github.com/juju/juju/apiserver/params"
    16  	coremigration "github.com/juju/juju/core/migration"
    17  )
    18  
    19  type ClientSuite struct {
    20  	jujutesting.IsolationSuite
    21  }
    22  
    23  var _ = gc.Suite(&ClientSuite{})
    24  
    25  func (s *ClientSuite) getClientAndStub(c *gc.C) (*migrationtarget.Client, *jujutesting.Stub) {
    26  	var stub jujutesting.Stub
    27  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    28  		stub.AddCall(objType+"."+request, id, arg)
    29  		return errors.New("boom")
    30  	})
    31  	client := migrationtarget.NewClient(apiCaller)
    32  	return client, &stub
    33  }
    34  
    35  func (s *ClientSuite) TestPrechecks(c *gc.C) {
    36  	client, stub := s.getClientAndStub(c)
    37  
    38  	ownerTag := names.NewUserTag("owner")
    39  	vers := version.MustParse("1.2.3")
    40  
    41  	err := client.Prechecks(coremigration.ModelInfo{
    42  		UUID:         "uuid",
    43  		Owner:        ownerTag,
    44  		Name:         "name",
    45  		AgentVersion: vers,
    46  	})
    47  	c.Assert(err, gc.ErrorMatches, "boom")
    48  
    49  	expectedArg := params.MigrationModelInfo{
    50  		UUID:         "uuid",
    51  		Name:         "name",
    52  		OwnerTag:     ownerTag.String(),
    53  		AgentVersion: vers,
    54  	}
    55  	stub.CheckCalls(c, []jujutesting.StubCall{
    56  		{"MigrationTarget.Prechecks", []interface{}{"", expectedArg}},
    57  	})
    58  }
    59  
    60  func (s *ClientSuite) TestImport(c *gc.C) {
    61  	client, stub := s.getClientAndStub(c)
    62  
    63  	err := client.Import([]byte("foo"))
    64  
    65  	expectedArg := params.SerializedModel{Bytes: []byte("foo")}
    66  	stub.CheckCalls(c, []jujutesting.StubCall{
    67  		{"MigrationTarget.Import", []interface{}{"", expectedArg}},
    68  	})
    69  	c.Assert(err, gc.ErrorMatches, "boom")
    70  }
    71  
    72  func (s *ClientSuite) TestAbort(c *gc.C) {
    73  	client, stub := s.getClientAndStub(c)
    74  
    75  	uuid := "fake"
    76  	err := client.Abort(uuid)
    77  	s.AssertModelCall(c, stub, names.NewModelTag(uuid), "Abort", err)
    78  }
    79  
    80  func (s *ClientSuite) TestActivate(c *gc.C) {
    81  	client, stub := s.getClientAndStub(c)
    82  
    83  	uuid := "fake"
    84  	err := client.Activate(uuid)
    85  	s.AssertModelCall(c, stub, names.NewModelTag(uuid), "Activate", err)
    86  }
    87  
    88  func (s *ClientSuite) AssertModelCall(c *gc.C, stub *jujutesting.Stub, tag names.ModelTag, call string, err error) {
    89  	expectedArg := params.ModelArgs{ModelTag: tag.String()}
    90  	stub.CheckCalls(c, []jujutesting.StubCall{
    91  		{"MigrationTarget." + call, []interface{}{"", expectedArg}},
    92  	})
    93  	c.Assert(err, gc.ErrorMatches, "boom")
    94  }