github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/toolsversionchecker/worker_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package toolsversionchecker_test
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/environs"
    13  	"github.com/juju/juju/environs/config"
    14  	"github.com/juju/juju/provider/dummy"
    15  	coretesting "github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker/toolsversionchecker"
    17  )
    18  
    19  var _ = gc.Suite(&ToolsCheckerSuite{})
    20  
    21  type ToolsCheckerSuite struct {
    22  	coretesting.BaseSuite
    23  }
    24  
    25  type dummyEnviron struct {
    26  	environs.Environ
    27  }
    28  
    29  func (dummyEnviron) Config() *config.Config {
    30  	sConfig := dummy.SampleConfig()
    31  	sConfig["agent-version"] = "2.5.0"
    32  	c, err := config.New(config.NoDefaults, sConfig)
    33  	if err != nil {
    34  		panic("cannot make a proper config")
    35  	}
    36  	return c
    37  }
    38  
    39  type facade struct {
    40  	called chan string
    41  }
    42  
    43  func (f *facade) UpdateToolsVersion() error {
    44  	f.called <- "UpdateToolsVersion"
    45  	return nil
    46  }
    47  
    48  func newFacade() *facade {
    49  	f := &facade{
    50  		called: make(chan string, 1),
    51  	}
    52  	return f
    53  }
    54  
    55  func (s *ToolsCheckerSuite) TestWorker(c *gc.C) {
    56  	f := newFacade()
    57  	params := &toolsversionchecker.VersionCheckerParams{
    58  		CheckInterval: coretesting.ShortWait,
    59  	}
    60  
    61  	checker := toolsversionchecker.NewPeriodicWorkerForTests(
    62  		f,
    63  		params,
    64  	)
    65  	s.AddCleanup(func(*gc.C) {
    66  		checker.Kill()
    67  		c.Assert(checker.Wait(), jc.ErrorIsNil)
    68  	})
    69  
    70  	select {
    71  	case called := <-f.called:
    72  		c.Assert(called, gc.Equals, "UpdateToolsVersion")
    73  	case <-time.After(coretesting.LongWait):
    74  		c.Fatalf("timed out waiting worker to seek new tool versions")
    75  	}
    76  
    77  }