github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/watcher_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package apiserver_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/apiserver"
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/facade/facadetest"
    15  	"github.com/juju/juju/apiserver/params"
    16  	apiservertesting "github.com/juju/juju/apiserver/testing"
    17  	"github.com/juju/juju/controller"
    18  	"github.com/juju/juju/core/migration"
    19  	"github.com/juju/juju/network"
    20  	"github.com/juju/juju/state"
    21  	"github.com/juju/juju/testing"
    22  )
    23  
    24  type watcherSuite struct {
    25  	testing.BaseSuite
    26  	resources  *common.Resources
    27  	authorizer apiservertesting.FakeAuthorizer
    28  }
    29  
    30  var _ = gc.Suite(&watcherSuite{})
    31  
    32  func (s *watcherSuite) SetUpTest(c *gc.C) {
    33  	s.BaseSuite.SetUpTest(c)
    34  	s.resources = common.NewResources()
    35  	s.AddCleanup(func(*gc.C) {
    36  		s.resources.StopAll()
    37  	})
    38  	s.authorizer = apiservertesting.FakeAuthorizer{}
    39  }
    40  
    41  func (s *watcherSuite) getFacade(c *gc.C, name string, version int, id string) interface{} {
    42  	factory, err := common.Facades.GetFactory(name, version)
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	facade, err := factory(facadetest.Context{
    45  		Resources_: s.resources,
    46  		Auth_:      s.authorizer,
    47  		ID_:        id,
    48  	})
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	return facade
    51  }
    52  
    53  func (s *watcherSuite) TestVolumeAttachmentsWatcher(c *gc.C) {
    54  	ch := make(chan []string, 1)
    55  	id := s.resources.Register(&fakeStringsWatcher{ch: ch})
    56  	s.authorizer.Tag = names.NewMachineTag("123")
    57  
    58  	ch <- []string{"0:1", "1:2"}
    59  	facade := s.getFacade(c, "VolumeAttachmentsWatcher", 2, id).(machineStorageIdsWatcher)
    60  	result, err := facade.Next()
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	c.Assert(result, jc.DeepEquals, params.MachineStorageIdsWatchResult{
    64  		Changes: []params.MachineStorageId{
    65  			{MachineTag: "machine-0", AttachmentTag: "volume-1"},
    66  			{MachineTag: "machine-1", AttachmentTag: "volume-2"},
    67  		},
    68  	})
    69  }
    70  
    71  func (s *watcherSuite) TestFilesystemAttachmentsWatcher(c *gc.C) {
    72  	ch := make(chan []string, 1)
    73  	id := s.resources.Register(&fakeStringsWatcher{ch: ch})
    74  	s.authorizer.Tag = names.NewMachineTag("123")
    75  
    76  	ch <- []string{"0:1", "1:2"}
    77  	facade := s.getFacade(c, "FilesystemAttachmentsWatcher", 2, id).(machineStorageIdsWatcher)
    78  	result, err := facade.Next()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  
    81  	c.Assert(result, jc.DeepEquals, params.MachineStorageIdsWatchResult{
    82  		Changes: []params.MachineStorageId{
    83  			{MachineTag: "machine-0", AttachmentTag: "filesystem-1"},
    84  			{MachineTag: "machine-1", AttachmentTag: "filesystem-2"},
    85  		},
    86  	})
    87  }
    88  
    89  func (s *watcherSuite) TestMigrationStatusWatcher(c *gc.C) {
    90  	w := apiservertesting.NewFakeNotifyWatcher()
    91  	id := s.resources.Register(w)
    92  	s.authorizer.Tag = names.NewMachineTag("12")
    93  	apiserver.PatchGetMigrationBackend(s, new(fakeMigrationBackend))
    94  	apiserver.PatchGetControllerCACert(s, "no worries")
    95  
    96  	facade := s.getFacade(c, "MigrationStatusWatcher", 1, id).(migrationStatusWatcher)
    97  	defer c.Check(facade.Stop(), jc.ErrorIsNil)
    98  	result, err := facade.Next()
    99  	c.Assert(err, jc.ErrorIsNil)
   100  	c.Assert(result, jc.DeepEquals, params.MigrationStatus{
   101  		MigrationId:    "id",
   102  		Attempt:        2,
   103  		Phase:          "IMPORT",
   104  		SourceAPIAddrs: []string{"1.2.3.4:5", "2.3.4.5:6", "3.4.5.6:7"},
   105  		SourceCACert:   "no worries",
   106  		TargetAPIAddrs: []string{"1.2.3.4:5555"},
   107  		TargetCACert:   "trust me",
   108  	})
   109  }
   110  
   111  func (s *watcherSuite) TestMigrationStatusWatcherNoMigration(c *gc.C) {
   112  	w := apiservertesting.NewFakeNotifyWatcher()
   113  	id := s.resources.Register(w)
   114  	s.authorizer.Tag = names.NewMachineTag("12")
   115  	apiserver.PatchGetMigrationBackend(s, &fakeMigrationBackend{noMigration: true})
   116  
   117  	facade := s.getFacade(c, "MigrationStatusWatcher", 1, id).(migrationStatusWatcher)
   118  	defer c.Check(facade.Stop(), jc.ErrorIsNil)
   119  	result, err := facade.Next()
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	c.Assert(result, jc.DeepEquals, params.MigrationStatus{
   122  		Phase: "NONE",
   123  	})
   124  }
   125  
   126  func (s *watcherSuite) TestMigrationStatusWatcherNotAgent(c *gc.C) {
   127  	id := s.resources.Register(apiservertesting.NewFakeNotifyWatcher())
   128  	s.authorizer.Tag = names.NewUserTag("frogdog")
   129  
   130  	factory, err := common.Facades.GetFactory("MigrationStatusWatcher", 1)
   131  	c.Assert(err, jc.ErrorIsNil)
   132  	_, err = factory(facadetest.Context{
   133  		Resources_: s.resources,
   134  		Auth_:      s.authorizer,
   135  		ID_:        id,
   136  	})
   137  	c.Assert(err, gc.Equals, common.ErrPerm)
   138  }
   139  
   140  type machineStorageIdsWatcher interface {
   141  	Next() (params.MachineStorageIdsWatchResult, error)
   142  }
   143  
   144  type fakeStringsWatcher struct {
   145  	state.StringsWatcher
   146  	ch chan []string
   147  }
   148  
   149  func (w *fakeStringsWatcher) Changes() <-chan []string {
   150  	return w.ch
   151  }
   152  
   153  func (w *fakeStringsWatcher) Stop() error {
   154  	return nil
   155  }
   156  
   157  type fakeMigrationBackend struct {
   158  	noMigration bool
   159  }
   160  
   161  func (b *fakeMigrationBackend) LatestMigration() (state.ModelMigration, error) {
   162  	if b.noMigration {
   163  		return nil, errors.NotFoundf("migration")
   164  	}
   165  	return new(fakeModelMigration), nil
   166  }
   167  
   168  func (b *fakeMigrationBackend) APIHostPorts() ([][]network.HostPort, error) {
   169  	return [][]network.HostPort{
   170  		MustParseHostPorts("1.2.3.4:5", "2.3.4.5:6"),
   171  		MustParseHostPorts("3.4.5.6:7"),
   172  	}, nil
   173  }
   174  
   175  func (b *fakeMigrationBackend) ControllerModel() (*state.Model, error) {
   176  	return nil, nil
   177  }
   178  
   179  func (b *fakeMigrationBackend) ControllerConfig() (controller.Config, error) {
   180  	return nil, nil
   181  }
   182  
   183  func MustParseHostPorts(hostports ...string) []network.HostPort {
   184  	out, err := network.ParseHostPorts(hostports...)
   185  	if err != nil {
   186  		panic(err)
   187  	}
   188  	return out
   189  }
   190  
   191  type fakeModelMigration struct {
   192  	state.ModelMigration
   193  }
   194  
   195  func (m *fakeModelMigration) Id() string {
   196  	return "id"
   197  }
   198  
   199  func (m *fakeModelMigration) Attempt() (int, error) {
   200  	return 2, nil
   201  }
   202  
   203  func (m *fakeModelMigration) Phase() (migration.Phase, error) {
   204  	return migration.IMPORT, nil
   205  }
   206  
   207  func (m *fakeModelMigration) TargetInfo() (*migration.TargetInfo, error) {
   208  	return &migration.TargetInfo{
   209  		ControllerTag: names.NewControllerTag("uuid"),
   210  		Addrs:         []string{"1.2.3.4:5555"},
   211  		CACert:        "trust me",
   212  		AuthTag:       names.NewUserTag("admin"),
   213  		Password:      "sekret",
   214  	}, nil
   215  }
   216  
   217  type migrationStatusWatcher interface {
   218  	Next() (params.MigrationStatus, error)
   219  	Stop() error
   220  }