github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/worker/uniter/state_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/juju/charm"
    10  	"github.com/juju/charm/hooks"
    11  	"github.com/juju/utils"
    12  	gc "launchpad.net/gocheck"
    13  
    14  	"github.com/juju/juju/worker/uniter"
    15  	"github.com/juju/juju/worker/uniter/hook"
    16  )
    17  
    18  type StateFileSuite struct{}
    19  
    20  var _ = gc.Suite(&StateFileSuite{})
    21  
    22  var stcurl = charm.MustParseURL("cs:quantal/service-name-123")
    23  var relhook = &hook.Info{
    24  	Kind:       hooks.RelationJoined,
    25  	RemoteUnit: "some-thing/123",
    26  }
    27  
    28  var stateTests = []struct {
    29  	st  uniter.State
    30  	err string
    31  }{
    32  	// Invalid op/step.
    33  	{
    34  		st:  uniter.State{Op: uniter.Op("bloviate")},
    35  		err: `unknown operation "bloviate"`,
    36  	}, {
    37  		st: uniter.State{
    38  			Op:     uniter.Continue,
    39  			OpStep: uniter.OpStep("dudelike"),
    40  			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
    41  		},
    42  		err: `unknown operation step "dudelike"`,
    43  	},
    44  	// Install operation.
    45  	{
    46  		st: uniter.State{
    47  			Op:     uniter.Install,
    48  			OpStep: uniter.Pending,
    49  			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
    50  		},
    51  		err: `unexpected hook info`,
    52  	}, {
    53  		st: uniter.State{
    54  			Op:     uniter.Install,
    55  			OpStep: uniter.Pending,
    56  		},
    57  		err: `missing charm URL`,
    58  	}, {
    59  		st: uniter.State{
    60  			Op:       uniter.Install,
    61  			OpStep:   uniter.Pending,
    62  			CharmURL: stcurl,
    63  		},
    64  	},
    65  	// RunHook operation.
    66  	{
    67  		st: uniter.State{
    68  			Op:     uniter.RunHook,
    69  			OpStep: uniter.Pending,
    70  			Hook:   &hook.Info{Kind: hooks.Kind("machine-exploded")},
    71  		},
    72  		err: `unknown hook kind "machine-exploded"`,
    73  	}, {
    74  		st: uniter.State{
    75  			Op:     uniter.RunHook,
    76  			OpStep: uniter.Pending,
    77  			Hook:   &hook.Info{Kind: hooks.RelationJoined},
    78  		},
    79  		err: `"relation-joined" hook requires a remote unit`,
    80  	}, {
    81  		st: uniter.State{
    82  			Op:       uniter.RunHook,
    83  			OpStep:   uniter.Pending,
    84  			Hook:     &hook.Info{Kind: hooks.ConfigChanged},
    85  			CharmURL: stcurl,
    86  		},
    87  		err: `unexpected charm URL`,
    88  	}, {
    89  		st: uniter.State{
    90  			Op:     uniter.RunHook,
    91  			OpStep: uniter.Pending,
    92  			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
    93  		},
    94  	}, {
    95  		st: uniter.State{
    96  			Op:     uniter.RunHook,
    97  			OpStep: uniter.Pending,
    98  			Hook:   relhook,
    99  		},
   100  	},
   101  	// Upgrade operation.
   102  	{
   103  		st: uniter.State{
   104  			Op:     uniter.Upgrade,
   105  			OpStep: uniter.Pending,
   106  		},
   107  		err: `missing charm URL`,
   108  	}, {
   109  		st: uniter.State{
   110  			Op:       uniter.Upgrade,
   111  			OpStep:   uniter.Pending,
   112  			CharmURL: stcurl,
   113  		},
   114  	}, {
   115  		st: uniter.State{
   116  			Op:       uniter.Upgrade,
   117  			OpStep:   uniter.Pending,
   118  			Hook:     relhook,
   119  			CharmURL: stcurl,
   120  		},
   121  	},
   122  	// Continue operation.
   123  	{
   124  		st: uniter.State{
   125  			Op:     uniter.Continue,
   126  			OpStep: uniter.Pending,
   127  		},
   128  		err: `missing hook info`,
   129  	}, {
   130  		st: uniter.State{
   131  			Op:       uniter.Continue,
   132  			OpStep:   uniter.Pending,
   133  			Hook:     relhook,
   134  			CharmURL: stcurl,
   135  		},
   136  		err: `unexpected charm URL`,
   137  	}, {
   138  		st: uniter.State{
   139  			Op:     uniter.Continue,
   140  			OpStep: uniter.Pending,
   141  			Hook:   relhook,
   142  		},
   143  	},
   144  }
   145  
   146  func (s *StateFileSuite) TestStates(c *gc.C) {
   147  	for i, t := range stateTests {
   148  		c.Logf("test %d", i)
   149  		path := filepath.Join(c.MkDir(), "uniter")
   150  		file := uniter.NewStateFile(path)
   151  		_, err := file.Read()
   152  		c.Assert(err, gc.Equals, uniter.ErrNoStateFile)
   153  		write := func() {
   154  			err := file.Write(t.st.Started, t.st.Op, t.st.OpStep, t.st.Hook, t.st.CharmURL)
   155  			c.Assert(err, gc.IsNil)
   156  		}
   157  		if t.err != "" {
   158  			c.Assert(write, gc.PanicMatches, "invalid uniter state: "+t.err)
   159  			err := utils.WriteYaml(path, &t.st)
   160  			c.Assert(err, gc.IsNil)
   161  			_, err = file.Read()
   162  			c.Assert(err, gc.ErrorMatches, "cannot read charm state at .*: invalid uniter state: "+t.err)
   163  			continue
   164  		}
   165  		write()
   166  		st, err := file.Read()
   167  		c.Assert(err, gc.IsNil)
   168  		c.Assert(*st, gc.DeepEquals, t.st)
   169  	}
   170  }