~sidnei/juju-core/lxc-clone-with-overlayfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package uniter_test

import (
	"path/filepath"

	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/charm"
	"launchpad.net/juju-core/charm/hooks"
	"launchpad.net/juju-core/utils"
	"launchpad.net/juju-core/worker/uniter"
	"launchpad.net/juju-core/worker/uniter/hook"
)

type StateFileSuite struct{}

var _ = gc.Suite(&StateFileSuite{})

var stcurl = charm.MustParseURL("cs:quantal/service-name-123")
var relhook = &hook.Info{
	Kind:       hooks.RelationJoined,
	RemoteUnit: "some-thing/123",
}

var stateTests = []struct {
	st  uniter.State
	err string
}{
	// Invalid op/step.
	{
		st:  uniter.State{Op: uniter.Op("bloviate")},
		err: `unknown operation "bloviate"`,
	}, {
		st: uniter.State{
			Op:     uniter.Continue,
			OpStep: uniter.OpStep("dudelike"),
			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
		},
		err: `unknown operation step "dudelike"`,
	},
	// Install operation.
	{
		st: uniter.State{
			Op:     uniter.Install,
			OpStep: uniter.Pending,
			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
		},
		err: `unexpected hook info`,
	}, {
		st: uniter.State{
			Op:     uniter.Install,
			OpStep: uniter.Pending,
		},
		err: `missing charm URL`,
	}, {
		st: uniter.State{
			Op:       uniter.Install,
			OpStep:   uniter.Pending,
			CharmURL: stcurl,
		},
	},
	// RunHook operation.
	{
		st: uniter.State{
			Op:     uniter.RunHook,
			OpStep: uniter.Pending,
			Hook:   &hook.Info{Kind: hooks.Kind("machine-exploded")},
		},
		err: `unknown hook kind "machine-exploded"`,
	}, {
		st: uniter.State{
			Op:     uniter.RunHook,
			OpStep: uniter.Pending,
			Hook:   &hook.Info{Kind: hooks.RelationJoined},
		},
		err: `"relation-joined" hook requires a remote unit`,
	}, {
		st: uniter.State{
			Op:       uniter.RunHook,
			OpStep:   uniter.Pending,
			Hook:     &hook.Info{Kind: hooks.ConfigChanged},
			CharmURL: stcurl,
		},
		err: `unexpected charm URL`,
	}, {
		st: uniter.State{
			Op:     uniter.RunHook,
			OpStep: uniter.Pending,
			Hook:   &hook.Info{Kind: hooks.ConfigChanged},
		},
	}, {
		st: uniter.State{
			Op:     uniter.RunHook,
			OpStep: uniter.Pending,
			Hook:   relhook,
		},
	},
	// Upgrade operation.
	{
		st: uniter.State{
			Op:     uniter.Upgrade,
			OpStep: uniter.Pending,
		},
		err: `missing charm URL`,
	}, {
		st: uniter.State{
			Op:       uniter.Upgrade,
			OpStep:   uniter.Pending,
			CharmURL: stcurl,
		},
	}, {
		st: uniter.State{
			Op:       uniter.Upgrade,
			OpStep:   uniter.Pending,
			Hook:     relhook,
			CharmURL: stcurl,
		},
	},
	// Continue operation.
	{
		st: uniter.State{
			Op:     uniter.Continue,
			OpStep: uniter.Pending,
		},
		err: `missing hook info`,
	}, {
		st: uniter.State{
			Op:       uniter.Continue,
			OpStep:   uniter.Pending,
			Hook:     relhook,
			CharmURL: stcurl,
		},
		err: `unexpected charm URL`,
	}, {
		st: uniter.State{
			Op:     uniter.Continue,
			OpStep: uniter.Pending,
			Hook:   relhook,
		},
	},
}

func (s *StateFileSuite) TestStates(c *gc.C) {
	for i, t := range stateTests {
		c.Logf("test %d", i)
		path := filepath.Join(c.MkDir(), "uniter")
		file := uniter.NewStateFile(path)
		_, err := file.Read()
		c.Assert(err, gc.Equals, uniter.ErrNoStateFile)
		write := func() {
			err := file.Write(t.st.Started, t.st.Op, t.st.OpStep, t.st.Hook, t.st.CharmURL)
			c.Assert(err, gc.IsNil)
		}
		if t.err != "" {
			c.Assert(write, gc.PanicMatches, "invalid uniter state: "+t.err)
			err := utils.WriteYaml(path, &t.st)
			c.Assert(err, gc.IsNil)
			_, err = file.Read()
			c.Assert(err, gc.ErrorMatches, "cannot read charm state at .*: invalid uniter state: "+t.err)
			continue
		}
		write()
		st, err := file.Read()
		c.Assert(err, gc.IsNil)
		c.Assert(*st, gc.DeepEquals, t.st)
	}
}