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