File: job_test.go

package info (click to toggle)
golang-github-apparentlymart-go-rundeck-api 0.0.1%2Bgit20170705.2c962ac-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (314 lines) | stat: -rw-r--r-- 8,711 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package rundeck

import (
	"fmt"
	"testing"
)

func TestUnmarshalJobDetail(t *testing.T) {
	testUnmarshalXML(t, []unmarshalTest{
		unmarshalTest{
			"with-config",
			`<job><uuid>baz</uuid><dispatch><rankOrder>ascending</rankOrder></dispatch></job>`,
			&JobDetail{},
			func (rv interface {}) error {
				v := rv.(*JobDetail)
				if v.ID != "baz" {
					return fmt.Errorf("got ID %s, but expecting baz", v.ID)
				}
				if v.Dispatch.RankOrder != "ascending" {
					return fmt.Errorf("Dispatch.RankOrder = \"%v\", but expecting \"ascending\"", v.Dispatch.RankOrder)
				}
				return nil
			},
		},
		unmarshalTest{
			"with-empty-config",
			`<JobPlugin type="foo-plugin"><configuration/></JobPlugin>`,
			&JobPlugin{},
			func (rv interface {}) error {
				v := rv.(*JobPlugin)
				if v.Type != "foo-plugin" {
					return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type)
				}
				if len(v.Config) != 0 {
					return fmt.Errorf("got %i Config values, but expecting 0", len(v.Config))
				}
				return nil
			},
		},
	})
}

func TestMarshalJobPlugin(t *testing.T) {
	testMarshalXML(t, []marshalTest{
		marshalTest{
			"with-config",
			JobPlugin{
				Type: "foo-plugin",
				Config: map[string]string{
					"woo": "foo",
					"bar": "baz",
				},
			},
			`<JobPlugin type="foo-plugin"><configuration><entry key="bar" value="baz"></entry><entry key="woo" value="foo"></entry></configuration></JobPlugin>`,
		},
		marshalTest{
			"with-empty-config",
			JobPlugin{
				Type: "foo-plugin",
				Config: map[string]string{},
			},
			`<JobPlugin type="foo-plugin"></JobPlugin>`,
		},
		marshalTest{
			"with-zero-value-config",
			JobPlugin{
				Type: "foo-plugin",
			},
			`<JobPlugin type="foo-plugin"></JobPlugin>`,
		},
	})
}

func TestUnmarshalJobPlugin(t *testing.T) {
	testUnmarshalXML(t, []unmarshalTest{
		unmarshalTest{
			"with-config",
			`<JobPlugin type="foo-plugin"><configuration><entry key="woo" value="foo"/><entry key="bar" value="baz"/></configuration></JobPlugin>`,
			&JobPlugin{},
			func (rv interface {}) error {
				v := rv.(*JobPlugin)
				if v.Type != "foo-plugin" {
					return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type)
				}
				if len(v.Config) != 2 {
					return fmt.Errorf("got %v Config values, but expecting 2", len(v.Config))
				}
				if v.Config["woo"] != "foo" {
					return fmt.Errorf("Config[\"woo\"] = \"%s\", but expecting \"foo\"", v.Config["woo"])
				}
				if v.Config["bar"] != "baz" {
					return fmt.Errorf("Config[\"bar\"] = \"%s\", but expecting \"baz\"", v.Config["bar"])
				}
				return nil
			},
		},
		unmarshalTest{
			"with-empty-config",
			`<JobPlugin type="foo-plugin"><configuration/></JobPlugin>`,
			&JobPlugin{},
			func (rv interface {}) error {
				v := rv.(*JobPlugin)
				if v.Type != "foo-plugin" {
					return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type)
				}
				if len(v.Config) != 0 {
					return fmt.Errorf("got %i Config values, but expecting 0", len(v.Config))
				}
				return nil
			},
		},
	})
}

func TestMarshalJobCommand(t *testing.T) {
	testMarshalXML(t, []marshalTest{
		marshalTest{
			"with-shell",
			JobCommand{
				ShellCommand: "command",
			},
			`<JobCommand><exec>command</exec></JobCommand>`,
		},
		marshalTest{
			"with-script",
			JobCommand{
				Script: "script",
			},
			`<JobCommand><script>script</script></JobCommand>`,
		},
		marshalTest{
			"with-script-interpreter",
			JobCommand{
				FileExtension: "sh",
				Script: "Hello World!",
			  ScriptInterpreter: &JobCommandScriptInterpreter{
						InvocationString: "sudo",
				},
			},
			`<JobCommand><fileExtension>sh</fileExtension><script>Hello World!</script><scriptinterpreter>sudo</scriptinterpreter></JobCommand>`,
		},
	})
}

func TestUnmarshalJobCommand(t *testing.T) {
	testUnmarshalXML(t, []unmarshalTest{
		unmarshalTest{
			"with-shell",
			`<JobCommand><exec>command</exec></JobCommand>`,
			&JobCommand{},
			func (rv interface {}) error {
				v := rv.(*JobCommand)
				if v.ShellCommand != "command" {
					return fmt.Errorf("got ShellCommand %s, but expecting command", v.ShellCommand)
				}
				return nil
			},
		},
		unmarshalTest{
			"with-script",
			`<JobCommand><script>script</script></JobCommand>`,
			&JobCommand{},
			func (rv interface {}) error {
				v := rv.(*JobCommand)
				if v.Script != "script" {
					return fmt.Errorf("got Script %s, but expecting script", v.Script)
				}
				return nil
			},
		},
		unmarshalTest{
			"with-script-interpreter",
			`<JobCommand><script>Hello World!</script><fileExtension>sh</fileExtension><scriptinterpreter>sudo</scriptinterpreter></JobCommand>`,
			&JobCommand{},
			func (rv interface {}) error {
				v := rv.(*JobCommand)
				if v.FileExtension != "sh" {
					return fmt.Errorf("got FileExtension %s, but expecting sh", v.FileExtension)
				}
				if v.Script != "Hello World!" {
					return fmt.Errorf("got Script %s, but expecting Hello World!", v.Script)
				}
				if v.ScriptInterpreter == nil {
					return fmt.Errorf("got %s, but expecting not nil", v.ScriptInterpreter)
				}
				if v.ScriptInterpreter.InvocationString != "sudo" {
					return fmt.Errorf("got InvocationString %s, but expecting sudo", v.ScriptInterpreter.InvocationString)
				}
				return nil
			},
		},
	})
}

func TestMarshalScriptInterpreter(t *testing.T) {
	testMarshalXML(t, []marshalTest{
		marshalTest{
			"with-script-interpreter",
			JobCommandScriptInterpreter{
					InvocationString: "sudo",
			},
			`<scriptinterpreter>sudo</scriptinterpreter>`,
		},
		marshalTest{
			"with-script-interpreter-quoted",
			JobCommandScriptInterpreter{
					ArgsQuoted: true,
					InvocationString: "sudo",
			},
			`<scriptinterpreter argsquoted="true">sudo</scriptinterpreter>`,
		},
	})
}

func TestUnmarshalScriptInterpreter(t *testing.T) {
	testUnmarshalXML(t, []unmarshalTest{
		unmarshalTest{
			"with-script-interpreter",
			`<scriptinterpreter>sudo</scriptinterpreter>`,
			&JobCommandScriptInterpreter{},
			func (rv interface {}) error {
				v := rv.(*JobCommandScriptInterpreter)
				if v.InvocationString != "sudo" {
					return fmt.Errorf("got InvocationString %s, but expecting sudo", v.InvocationString)
				}
				if v.ArgsQuoted {
					return fmt.Errorf("got ArgsQuoted %s, but expecting false", v.ArgsQuoted)
				}
				return nil
			},
		},
		unmarshalTest{
			"with-script-interpreter-quoted",
			`<scriptinterpreter argsquoted="true">sudo</scriptinterpreter>`,
			&JobCommandScriptInterpreter{},
			func (rv interface {}) error {
				v := rv.(*JobCommandScriptInterpreter)
				if v.InvocationString != "sudo" {
					return fmt.Errorf("got InvocationString %s, but expecting sudo", v.InvocationString)
				}
				if ! v.ArgsQuoted {
					return fmt.Errorf("got ArgsQuoted %s, but expecting true", v.ArgsQuoted)
				}
				return nil
			},
		},
	})
}

func TestMarshalErrorHanlder(t *testing.T) {
	testMarshalXML(t, []marshalTest{
		marshalTest{
			"with-errorhandler",
			JobCommandSequence{
				ContinueOnError: true,
				OrderingStrategy: "step-first",
				Commands: []JobCommand{
					JobCommand{
						Script: "inline_script",
						ErrorHandler: &JobCommand{
							ContinueOnError: true,
							Script: "error_script",
						},
					},
				},
			},
			`<sequence keepgoing="true" strategy="step-first"><command><errorhandler keepgoingOnSuccess="true"><script>error_script</script></errorhandler><script>inline_script</script></command></sequence>`,
		},
	})
}


func TestMarshalJobOption(t *testing.T) {
	testMarshalXML(t, []marshalTest{
		marshalTest{
			"with-option-basic",
			JobOption{
				Name: "basic",
			},
			`<option name="basic"></option>`,
		},
		marshalTest{
			"with-option-multivalued",
			JobOption{
				Name: "Multivalued",
				MultiValueDelimiter: "|",
				RequirePredefinedChoice: true,
				AllowsMultipleValues: true,
				IsRequired: true,
				ValueChoices: JobValueChoices([]string{"myValues"}),
			},
			`<option delimiter="|" enforcedvalues="true" multivalued="true" name="Multivalued" required="true" values="myValues"></option>`,
		},
		marshalTest{
			"with-all-attributes",
			JobOption{
				Name: "advanced",
				MultiValueDelimiter: "|",
				RequirePredefinedChoice: true,
				AllowsMultipleValues: true,
				ValidationRegex: ".+",
				IsRequired: true,
				ObscureInput: true,
				StoragePath: "myKey",
				DefaultValue: "myValue",
				ValueIsExposedToScripts: true,
				ValueChoices: JobValueChoices([]string{"myValues"}),
				ValueChoicesURL: "myValuesUrl",
			},
			`<option delimiter="|" enforcedvalues="true" multivalued="true" name="advanced" regex=".+" required="true" secure="true" storagePath="myKey" value="myValue" valueExposed="true" values="myValues" valuesUrl="myValuesUrl"></option>`,
		},
	})
}