github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/migrationmaster/client_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationmaster_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names"
    11  	jujutesting "github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/utils"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	apitesting "github.com/juju/juju/api/base/testing"
    17  	"github.com/juju/juju/api/migrationmaster"
    18  	"github.com/juju/juju/apiserver/params"
    19  	"github.com/juju/juju/core/migration"
    20  	coretesting "github.com/juju/juju/testing"
    21  	"github.com/juju/juju/worker"
    22  )
    23  
    24  type ClientSuite struct {
    25  	jujutesting.IsolationSuite
    26  }
    27  
    28  var _ = gc.Suite(&ClientSuite{})
    29  
    30  func (s *ClientSuite) TestWatch(c *gc.C) {
    31  	var stub jujutesting.Stub
    32  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    33  		stub.AddCall(objType+"."+request, id, arg)
    34  		switch request {
    35  		case "Watch":
    36  			*(result.(*params.NotifyWatchResult)) = params.NotifyWatchResult{
    37  				NotifyWatcherId: "abc",
    38  			}
    39  		case "Next":
    40  			// The full success case is tested in api/watcher.
    41  			return errors.New("boom")
    42  		case "Stop":
    43  		}
    44  		return nil
    45  	})
    46  
    47  	client := migrationmaster.NewClient(apiCaller)
    48  	w, err := client.Watch()
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	defer worker.Stop(w)
    51  
    52  	errC := make(chan error)
    53  	go func() {
    54  		errC <- w.Wait()
    55  	}()
    56  
    57  	select {
    58  	case err := <-errC:
    59  		c.Assert(err, gc.ErrorMatches, "boom")
    60  		expectedCalls := []jujutesting.StubCall{
    61  			{"MigrationMaster.Watch", []interface{}{"", nil}},
    62  			{"NotifyWatcher.Next", []interface{}{"abc", nil}},
    63  			{"NotifyWatcher.Stop", []interface{}{"abc", nil}},
    64  		}
    65  		// The Stop API call happens in a separate goroutine which
    66  		// might execute after the worker has exited so wait for the
    67  		// expected calls to arrive.
    68  		for a := coretesting.LongAttempt.Start(); a.Next(); {
    69  			if len(stub.Calls()) >= len(expectedCalls) {
    70  				return
    71  			}
    72  		}
    73  		stub.CheckCalls(c, expectedCalls)
    74  	case <-time.After(coretesting.LongWait):
    75  		c.Fatal("timed out waiting for watcher to die")
    76  	}
    77  }
    78  
    79  func (s *ClientSuite) TestWatchErr(c *gc.C) {
    80  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    81  		return errors.New("boom")
    82  	})
    83  	client := migrationmaster.NewClient(apiCaller)
    84  	_, err := client.Watch()
    85  	c.Assert(err, gc.ErrorMatches, "boom")
    86  }
    87  
    88  func (s *ClientSuite) TestGetMigrationStatus(c *gc.C) {
    89  	modelUUID := utils.MustNewUUID().String()
    90  	controllerUUID := utils.MustNewUUID().String()
    91  	apiCaller := apitesting.APICallerFunc(func(_ string, _ int, _, _ string, _, result interface{}) error {
    92  		out := result.(*params.FullMigrationStatus)
    93  		*out = params.FullMigrationStatus{
    94  			Spec: params.ModelMigrationSpec{
    95  				ModelTag: names.NewModelTag(modelUUID).String(),
    96  				TargetInfo: params.ModelMigrationTargetInfo{
    97  					ControllerTag: names.NewModelTag(controllerUUID).String(),
    98  					Addrs:         []string{"2.2.2.2:2"},
    99  					CACert:        "cert",
   100  					AuthTag:       names.NewUserTag("admin").String(),
   101  					Password:      "secret",
   102  				},
   103  			},
   104  			Attempt: 3,
   105  			Phase:   "READONLY",
   106  		}
   107  		return nil
   108  	})
   109  	client := migrationmaster.NewClient(apiCaller)
   110  
   111  	status, err := client.GetMigrationStatus()
   112  	c.Assert(err, jc.ErrorIsNil)
   113  	c.Assert(status, gc.DeepEquals, migrationmaster.MigrationStatus{
   114  		ModelUUID: modelUUID,
   115  		Attempt:   3,
   116  		Phase:     migration.READONLY,
   117  		TargetInfo: migration.TargetInfo{
   118  			ControllerTag: names.NewModelTag(controllerUUID),
   119  			Addrs:         []string{"2.2.2.2:2"},
   120  			CACert:        "cert",
   121  			AuthTag:       names.NewUserTag("admin"),
   122  			Password:      "secret",
   123  		},
   124  	})
   125  }
   126  
   127  func (s *ClientSuite) TestSetPhase(c *gc.C) {
   128  	var stub jujutesting.Stub
   129  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   130  		stub.AddCall(objType+"."+request, id, arg)
   131  		return nil
   132  	})
   133  	client := migrationmaster.NewClient(apiCaller)
   134  	err := client.SetPhase(migration.QUIESCE)
   135  	c.Assert(err, jc.ErrorIsNil)
   136  	expectedArg := params.SetMigrationPhaseArgs{Phase: "QUIESCE"}
   137  	stub.CheckCalls(c, []jujutesting.StubCall{
   138  		{"MigrationMaster.SetPhase", []interface{}{"", expectedArg}},
   139  	})
   140  }
   141  
   142  func (s *ClientSuite) TestSetPhaseError(c *gc.C) {
   143  	apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error {
   144  		return errors.New("boom")
   145  	})
   146  	client := migrationmaster.NewClient(apiCaller)
   147  	err := client.SetPhase(migration.QUIESCE)
   148  	c.Assert(err, gc.ErrorMatches, "boom")
   149  }
   150  
   151  func (s *ClientSuite) TestExport(c *gc.C) {
   152  	var stub jujutesting.Stub
   153  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   154  		stub.AddCall(objType+"."+request, id, arg)
   155  		out := result.(*params.SerializedModel)
   156  		*out = params.SerializedModel{Bytes: []byte("foo")}
   157  		return nil
   158  	})
   159  	client := migrationmaster.NewClient(apiCaller)
   160  	bytes, err := client.Export()
   161  	c.Assert(err, jc.ErrorIsNil)
   162  	stub.CheckCalls(c, []jujutesting.StubCall{
   163  		{"MigrationMaster.Export", []interface{}{"", nil}},
   164  	})
   165  	c.Assert(string(bytes), gc.Equals, "foo")
   166  }
   167  
   168  func (s *ClientSuite) TestExportError(c *gc.C) {
   169  	apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error {
   170  		return errors.New("blam")
   171  	})
   172  	client := migrationmaster.NewClient(apiCaller)
   173  	_, err := client.Export()
   174  	c.Assert(err, gc.ErrorMatches, "blam")
   175  }