github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	gc "launchpad.net/gocheck"
    12  
    13  	"launchpad.net/juju-core/testing/testbase"
    14  	"launchpad.net/juju-core/worker"
    15  	"launchpad.net/juju-core/worker/terminationworker"
    16  )
    17  
    18  func TestPackage(t *stdtesting.T) {
    19  	gc.TestingT(t)
    20  }
    21  
    22  var _ = gc.Suite(&TerminationWorkerSuite{})
    23  
    24  type TerminationWorkerSuite struct {
    25  	testbase.LoggingSuite
    26  	// c is a channel that will wait for the termination
    27  	// signal, to prevent signals terminating the process.
    28  	c chan os.Signal
    29  }
    30  
    31  func (s *TerminationWorkerSuite) SetUpTest(c *gc.C) {
    32  	s.LoggingSuite.SetUpTest(c)
    33  	s.c = make(chan os.Signal, 1)
    34  	signal.Notify(s.c, terminationworker.TerminationSignal)
    35  }
    36  
    37  func (s *TerminationWorkerSuite) TearDownTest(c *gc.C) {
    38  	close(s.c)
    39  	signal.Stop(s.c)
    40  	s.LoggingSuite.TearDownTest(c)
    41  }
    42  
    43  func (s *TerminationWorkerSuite) TestStartStop(c *gc.C) {
    44  	w := terminationworker.NewWorker()
    45  	w.Kill()
    46  	err := w.Wait()
    47  	c.Assert(err, gc.IsNil)
    48  }
    49  
    50  func (s *TerminationWorkerSuite) TestSignal(c *gc.C) {
    51  	w := terminationworker.NewWorker()
    52  	proc, err := os.FindProcess(os.Getpid())
    53  	c.Assert(err, gc.IsNil)
    54  	defer proc.Release()
    55  	err = proc.Signal(terminationworker.TerminationSignal)
    56  	c.Assert(err, gc.IsNil)
    57  	err = w.Wait()
    58  	c.Assert(err, gc.Equals, worker.ErrTerminateAgent)
    59  }