github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/caasfirewaller/worker_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasfirewaller_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/worker.v1/workertest"
    14  
    15  	"github.com/juju/juju/core/application"
    16  	"github.com/juju/juju/core/life"
    17  	"github.com/juju/juju/core/watcher/watchertest"
    18  	coretesting "github.com/juju/juju/testing"
    19  	"github.com/juju/juju/worker/caasfirewaller"
    20  )
    21  
    22  type WorkerSuite struct {
    23  	testing.IsolationSuite
    24  
    25  	config            caasfirewaller.Config
    26  	applicationGetter mockApplicationGetter
    27  	serviceExposer    mockServiceExposer
    28  	lifeGetter        mockLifeGetter
    29  
    30  	applicationChanges chan []string
    31  	appExposedChange   chan struct{}
    32  	serviceExposed     chan struct{}
    33  	serviceUnexposed   chan struct{}
    34  }
    35  
    36  var _ = gc.Suite(&WorkerSuite{})
    37  
    38  func (s *WorkerSuite) SetUpTest(c *gc.C) {
    39  	s.IsolationSuite.SetUpTest(c)
    40  
    41  	s.applicationChanges = make(chan []string)
    42  	s.appExposedChange = make(chan struct{})
    43  	s.serviceExposed = make(chan struct{})
    44  	s.serviceUnexposed = make(chan struct{})
    45  
    46  	s.applicationGetter = mockApplicationGetter{
    47  		allWatcher: watchertest.NewMockStringsWatcher(s.applicationChanges),
    48  		appWatcher: watchertest.NewMockNotifyWatcher(s.appExposedChange),
    49  	}
    50  	s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, s.applicationGetter.allWatcher) })
    51  
    52  	s.lifeGetter = mockLifeGetter{
    53  		life: life.Alive,
    54  	}
    55  	s.serviceExposer = mockServiceExposer{
    56  		exposed:   s.serviceExposed,
    57  		unexposed: s.serviceUnexposed,
    58  	}
    59  
    60  	s.config = caasfirewaller.Config{
    61  		ControllerUUID:    coretesting.ControllerTag.Id(),
    62  		ModelUUID:         coretesting.ModelTag.Id(),
    63  		ApplicationGetter: &s.applicationGetter,
    64  		ServiceExposer:    &s.serviceExposer,
    65  		LifeGetter:        &s.lifeGetter,
    66  	}
    67  }
    68  
    69  func (s *WorkerSuite) sendApplicationExposedChange(c *gc.C) {
    70  	select {
    71  	case s.appExposedChange <- struct{}{}:
    72  	case <-time.After(coretesting.LongWait):
    73  		c.Fatal("timed out sending application exposed change")
    74  	}
    75  }
    76  
    77  func (s *WorkerSuite) TestValidateConfig(c *gc.C) {
    78  	s.testValidateConfig(c, func(config *caasfirewaller.Config) {
    79  		config.ControllerUUID = ""
    80  	}, `missing ControllerUUID not valid`)
    81  
    82  	s.testValidateConfig(c, func(config *caasfirewaller.Config) {
    83  		config.ModelUUID = ""
    84  	}, `missing ModelUUID not valid`)
    85  
    86  	s.testValidateConfig(c, func(config *caasfirewaller.Config) {
    87  		config.ApplicationGetter = nil
    88  	}, `missing ApplicationGetter not valid`)
    89  
    90  	s.testValidateConfig(c, func(config *caasfirewaller.Config) {
    91  		config.ServiceExposer = nil
    92  	}, `missing ServiceExposer not valid`)
    93  
    94  	s.testValidateConfig(c, func(config *caasfirewaller.Config) {
    95  		config.LifeGetter = nil
    96  	}, `missing LifeGetter not valid`)
    97  }
    98  
    99  func (s *WorkerSuite) testValidateConfig(c *gc.C, f func(*caasfirewaller.Config), expect string) {
   100  	config := s.config
   101  	f(&config)
   102  	w, err := caasfirewaller.NewWorker(config)
   103  	if err == nil {
   104  		workertest.DirtyKill(c, w)
   105  	}
   106  	c.Check(err, gc.ErrorMatches, expect)
   107  }
   108  
   109  func (s *WorkerSuite) TestStartStop(c *gc.C) {
   110  	w, err := caasfirewaller.NewWorker(s.config)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	workertest.CheckAlive(c, w)
   113  	workertest.CleanKill(c, w)
   114  }
   115  
   116  func (s *WorkerSuite) TestExposedChange(c *gc.C) {
   117  	w, err := caasfirewaller.NewWorker(s.config)
   118  	c.Assert(err, jc.ErrorIsNil)
   119  	defer workertest.CleanKill(c, w)
   120  
   121  	select {
   122  	case s.applicationChanges <- []string{"gitlab"}:
   123  	case <-time.After(coretesting.LongWait):
   124  		c.Fatal("timed out sending applications change")
   125  	}
   126  
   127  	s.sendApplicationExposedChange(c)
   128  	// The last known state on start up was unexposed
   129  	// so we first call Unexpose().
   130  	select {
   131  	case <-s.serviceUnexposed:
   132  	case <-time.After(coretesting.LongWait):
   133  		c.Fatal("timed out waiting for service to be unexposed")
   134  	}
   135  	select {
   136  	case <-s.serviceExposed:
   137  		c.Fatal("service exposed unexpectedly")
   138  	case <-time.After(coretesting.ShortWait):
   139  	}
   140  
   141  	s.applicationGetter.exposed = true
   142  	s.sendApplicationExposedChange(c)
   143  	select {
   144  	case <-s.serviceExposed:
   145  	case <-time.After(coretesting.LongWait):
   146  		c.Fatal("timed out waiting for service to be exposed")
   147  	}
   148  	s.serviceExposer.CheckCallNames(c, "UnexposeService", "ExposeService")
   149  	s.serviceExposer.CheckCall(c, 1, "ExposeService", "gitlab",
   150  		map[string]string{
   151  			"juju-controller-uuid": coretesting.ControllerTag.Id(),
   152  			"juju-model-uuid":      coretesting.ModelTag.Id()},
   153  		application.ConfigAttributes{"juju-external-hostname": "exthost"})
   154  }
   155  
   156  func (s *WorkerSuite) TestUnexposedChange(c *gc.C) {
   157  	w, err := caasfirewaller.NewWorker(s.config)
   158  	c.Assert(err, jc.ErrorIsNil)
   159  	defer workertest.CleanKill(c, w)
   160  
   161  	select {
   162  	case s.applicationChanges <- []string{"gitlab"}:
   163  	case <-time.After(coretesting.LongWait):
   164  		c.Fatal("timed out sending applications change")
   165  	}
   166  
   167  	s.applicationGetter.exposed = true
   168  	s.sendApplicationExposedChange(c)
   169  	// The last known state on start up was exposed
   170  	// so we first call Expose().
   171  	select {
   172  	case <-s.serviceExposed:
   173  	case <-time.After(coretesting.LongWait):
   174  		c.Fatal("timed out waiting for service to be exposed")
   175  	}
   176  	select {
   177  	case <-s.serviceUnexposed:
   178  		c.Fatal("service unexposed unexpectedly")
   179  	case <-time.After(coretesting.ShortWait):
   180  	}
   181  
   182  	s.applicationGetter.exposed = false
   183  	s.sendApplicationExposedChange(c)
   184  	select {
   185  	case <-s.serviceUnexposed:
   186  	case <-time.After(coretesting.LongWait):
   187  		c.Fatal("timed out waiting for service to be unexposed")
   188  	}
   189  }
   190  
   191  func (s *WorkerSuite) TestWatchApplicationDead(c *gc.C) {
   192  	w, err := caasfirewaller.NewWorker(s.config)
   193  	c.Assert(err, jc.ErrorIsNil)
   194  	defer workertest.CleanKill(c, w)
   195  
   196  	s.lifeGetter.life = life.Dead
   197  	select {
   198  	case s.applicationChanges <- []string{"gitlab"}:
   199  	case <-time.After(coretesting.LongWait):
   200  		c.Fatal("timed out sending applications change")
   201  	}
   202  
   203  	select {
   204  	case s.appExposedChange <- struct{}{}:
   205  		c.Fatal("unexpected watch for app exposed")
   206  	case <-time.After(coretesting.ShortWait):
   207  	}
   208  
   209  	workertest.CleanKill(c, w)
   210  }
   211  
   212  func (s *WorkerSuite) TestRemoveApplicationStopsWatchingApplication(c *gc.C) {
   213  	// Set up the errors before triggering any events to avoid racing
   214  	// with the worker loop. First time around the loop the
   215  	// application's alive, then it's gone.
   216  	s.lifeGetter.SetErrors(nil, errors.NotFoundf("application"))
   217  
   218  	w, err := caasfirewaller.NewWorker(s.config)
   219  	c.Assert(err, jc.ErrorIsNil)
   220  	defer workertest.CleanKill(c, w)
   221  
   222  	select {
   223  	case s.applicationChanges <- []string{"gitlab"}:
   224  	case <-time.After(coretesting.LongWait):
   225  		c.Fatal("timed out sending applications change")
   226  	}
   227  
   228  	select {
   229  	case s.applicationChanges <- []string{"gitlab"}:
   230  	case <-time.After(coretesting.LongWait):
   231  		c.Fatal("timed out sending applications change")
   232  	}
   233  
   234  	err = workertest.CheckKilled(c, s.applicationGetter.appWatcher)
   235  	c.Assert(err, jc.ErrorIsNil)
   236  }
   237  
   238  func (s *WorkerSuite) TestWatcherErrorStopsWorker(c *gc.C) {
   239  	w, err := caasfirewaller.NewWorker(s.config)
   240  	c.Assert(err, jc.ErrorIsNil)
   241  	defer workertest.DirtyKill(c, w)
   242  
   243  	select {
   244  	case s.applicationChanges <- []string{"gitlab"}:
   245  	case <-time.After(coretesting.LongWait):
   246  		c.Fatal("timed out sending applications change")
   247  	}
   248  
   249  	s.applicationGetter.appWatcher.KillErr(errors.New("splat"))
   250  	workertest.CheckKilled(c, s.applicationGetter.appWatcher)
   251  	workertest.CheckKilled(c, s.applicationGetter.allWatcher)
   252  	err = workertest.CheckKilled(c, w)
   253  	c.Assert(err, gc.ErrorMatches, "splat")
   254  }