github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/terminationworker/worker_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package terminationworker_test
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  	stdtesting "testing"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker"
    16  	"github.com/juju/juju/worker/terminationworker"
    17  )
    18  
    19  func TestPackage(t *stdtesting.T) {
    20  	gc.TestingT(t)
    21  }
    22  
    23  var _ = gc.Suite(&TerminationWorkerSuite{})
    24  
    25  type TerminationWorkerSuite struct {
    26  	testing.BaseSuite
    27  	// c is a channel that will wait for the termination
    28  	// signal, to prevent signals terminating the process.
    29  	c chan os.Signal
    30  }
    31  
    32  func (s *TerminationWorkerSuite) SetUpTest(c *gc.C) {
    33  	s.BaseSuite.SetUpTest(c)
    34  	s.c = make(chan os.Signal, 1)
    35  	signal.Notify(s.c, terminationworker.TerminationSignal)
    36  }
    37  
    38  func (s *TerminationWorkerSuite) TearDownTest(c *gc.C) {
    39  	close(s.c)
    40  	signal.Stop(s.c)
    41  	s.BaseSuite.TearDownTest(c)
    42  }
    43  
    44  func (s *TerminationWorkerSuite) TestStartStop(c *gc.C) {
    45  	w := terminationworker.NewWorker()
    46  	w.Kill()
    47  	err := w.Wait()
    48  	c.Assert(err, jc.ErrorIsNil)
    49  }
    50  
    51  func (s *TerminationWorkerSuite) TestSignal(c *gc.C) {
    52  	w := terminationworker.NewWorker()
    53  	proc, err := os.FindProcess(os.Getpid())
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	defer proc.Release()
    56  	err = proc.Signal(terminationworker.TerminationSignal)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	err = w.Wait()
    59  	c.Assert(err, gc.Equals, worker.ErrTerminateAgent)
    60  }