| 1 | package rundeck |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestUnmarshalJobDetail(t *testing.T) { |
| 9 | testUnmarshalXML(t, []unmarshalTest{ |
| 10 | unmarshalTest{ |
| 11 | "with-config", |
| 12 | `<job><uuid>baz</uuid><dispatch><rankOrder>ascending</rankOrder></dispatch></job>`, |
| 13 | &JobDetail{}, |
| 14 | func (rv interface {}) error { |
| 15 | v := rv.(*JobDetail) |
| 16 | if v.ID != "baz" { |
| 17 | return fmt.Errorf("got ID %s, but expecting baz", v.ID) |
| 18 | } |
| 19 | if v.Dispatch.RankOrder != "ascending" { |
| 20 | return fmt.Errorf("Dispatch.RankOrder = \"%v\", but expecting \"ascending\"", v.Dispatch.RankOrder) |
| 21 | } |
| 22 | return nil |
| 23 | }, |
| 24 | }, |
| 25 | unmarshalTest{ |
| 26 | "with-empty-config", |
| 27 | `<JobPlugin type="foo-plugin"><configuration/></JobPlugin>`, |
| 28 | &JobPlugin{}, |
| 29 | func (rv interface {}) error { |
| 30 | v := rv.(*JobPlugin) |
| 31 | if v.Type != "foo-plugin" { |
| 32 | return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type) |
| 33 | } |
| 34 | if len(v.Config) != 0 { |
| 35 | return fmt.Errorf("got %i Config values, but expecting 0", len(v.Config)) |
| 36 | } |
| 37 | return nil |
| 38 | }, |
| 39 | }, |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | func TestMarshalJobPlugin(t *testing.T) { |
| 44 | testMarshalXML(t, []marshalTest{ |
| 45 | marshalTest{ |
| 46 | "with-config", |
| 47 | JobPlugin{ |
| 48 | Type: "foo-plugin", |
| 49 | Config: map[string]string{ |
| 50 | "woo": "foo", |
| 51 | "bar": "baz", |
| 52 | }, |
| 53 | }, |
| 54 | `<JobPlugin type="foo-plugin"><configuration><entry key="bar" value="baz"></entry><entry key="woo" value="foo"></entry></configuration></JobPlugin>`, |
| 55 | }, |
| 56 | marshalTest{ |
| 57 | "with-empty-config", |
| 58 | JobPlugin{ |
| 59 | Type: "foo-plugin", |
| 60 | Config: map[string]string{}, |
| 61 | }, |
| 62 | `<JobPlugin type="foo-plugin"></JobPlugin>`, |
| 63 | }, |
| 64 | marshalTest{ |
| 65 | "with-zero-value-config", |
| 66 | JobPlugin{ |
| 67 | Type: "foo-plugin", |
| 68 | }, |
| 69 | `<JobPlugin type="foo-plugin"></JobPlugin>`, |
| 70 | }, |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | func TestUnmarshalJobPlugin(t *testing.T) { |
| 75 | testUnmarshalXML(t, []unmarshalTest{ |
| 76 | unmarshalTest{ |
| 77 | "with-config", |
| 78 | `<JobPlugin type="foo-plugin"><configuration><entry key="woo" value="foo"/><entry key="bar" value="baz"/></configuration></JobPlugin>`, |
| 79 | &JobPlugin{}, |
| 80 | func (rv interface {}) error { |
| 81 | v := rv.(*JobPlugin) |
| 82 | if v.Type != "foo-plugin" { |
| 83 | return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type) |
| 84 | } |
| 85 | if len(v.Config) != 2 { |
| 86 | return fmt.Errorf("got %v Config values, but expecting 2", len(v.Config)) |
| 87 | } |
| 88 | if v.Config["woo"] != "foo" { |
| 89 | return fmt.Errorf("Config[\"woo\"] = \"%s\", but expecting \"foo\"", v.Config["woo"]) |
| 90 | } |
| 91 | if v.Config["bar"] != "baz" { |
| 92 | return fmt.Errorf("Config[\"bar\"] = \"%s\", but expecting \"baz\"", v.Config["bar"]) |
| 93 | } |
| 94 | return nil |
| 95 | }, |
| 96 | }, |
| 97 | unmarshalTest{ |
| 98 | "with-empty-config", |
| 99 | `<JobPlugin type="foo-plugin"><configuration/></JobPlugin>`, |
| 100 | &JobPlugin{}, |
| 101 | func (rv interface {}) error { |
| 102 | v := rv.(*JobPlugin) |
| 103 | if v.Type != "foo-plugin" { |
| 104 | return fmt.Errorf("got Type %s, but expecting foo-plugin", v.Type) |
| 105 | } |
| 106 | if len(v.Config) != 0 { |
| 107 | return fmt.Errorf("got %i Config values, but expecting 0", len(v.Config)) |
| 108 | } |
| 109 | return nil |
| 110 | }, |
| 111 | }, |
| 112 | }) |
| 113 | } |
| 114 | |
| 115 | func TestMarshalJobCommand(t *testing.T) { |
| 116 | testMarshalXML(t, []marshalTest{ |
| 117 | marshalTest{ |
| 118 | "with-shell", |
| 119 | JobCommand{ |
| 120 | ShellCommand: "command", |
| 121 | }, |
| 122 | `<JobCommand><exec>command</exec></JobCommand>`, |
| 123 | }, |
| 124 | marshalTest{ |
| 125 | "with-script", |
| 126 | JobCommand{ |
| 127 | Script: "script", |
| 128 | }, |
| 129 | `<JobCommand><script>script</script></JobCommand>`, |
| 130 | }, |
| 131 | marshalTest{ |
| 132 | "with-script-interpreter", |
| 133 | JobCommand{ |
| 134 | FileExtension: "sh", |
| 135 | Script: "Hello World!", |
| 136 | ScriptInterpreter: &JobCommandScriptInterpreter{ |
| 137 | InvocationString: "sudo", |
| 138 | }, |
| 139 | }, |
| 140 | `<JobCommand><fileExtension>sh</fileExtension><script>Hello World!</script><scriptinterpreter>sudo</scriptinterpreter></JobCommand>`, |
| 141 | }, |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | func TestUnmarshalJobCommand(t *testing.T) { |
| 146 | testUnmarshalXML(t, []unmarshalTest{ |
| 147 | unmarshalTest{ |
| 148 | "with-shell", |
| 149 | `<JobCommand><exec>command</exec></JobCommand>`, |
| 150 | &JobCommand{}, |
| 151 | func (rv interface {}) error { |
| 152 | v := rv.(*JobCommand) |
| 153 | if v.ShellCommand != "command" { |
| 154 | return fmt.Errorf("got ShellCommand %s, but expecting command", v.ShellCommand) |
| 155 | } |
| 156 | return nil |
| 157 | }, |
| 158 | }, |
| 159 | unmarshalTest{ |
| 160 | "with-script", |
| 161 | `<JobCommand><script>script</script></JobCommand>`, |
| 162 | &JobCommand{}, |
| 163 | func (rv interface {}) error { |
| 164 | v := rv.(*JobCommand) |
| 165 | if v.Script != "script" { |
| 166 | return fmt.Errorf("got Script %s, but expecting script", v.Script) |
| 167 | } |
| 168 | return nil |
| 169 | }, |
| 170 | }, |
| 171 | unmarshalTest{ |
| 172 | "with-script-interpreter", |
| 173 | `<JobCommand><script>Hello World!</script><fileExtension>sh</fileExtension><scriptinterpreter>sudo</scriptinterpreter></JobCommand>`, |
| 174 | &JobCommand{}, |
| 175 | func (rv interface {}) error { |
| 176 | v := rv.(*JobCommand) |
| 177 | if v.FileExtension != "sh" { |
| 178 | return fmt.Errorf("got FileExtension %s, but expecting sh", v.FileExtension) |
| 179 | } |
| 180 | if v.Script != "Hello World!" { |
| 181 | return fmt.Errorf("got Script %s, but expecting Hello World!", v.Script) |
| 182 | } |
| 183 | if v.ScriptInterpreter == nil { |
| 184 | return fmt.Errorf("got %s, but expecting not nil", v.ScriptInterpreter) |
| 185 | } |
| 186 | if v.ScriptInterpreter.InvocationString != "sudo" { |
| 187 | return fmt.Errorf("got InvocationString %s, but expecting sudo", v.ScriptInterpreter.InvocationString) |
| 188 | } |
| 189 | return nil |
| 190 | }, |
| 191 | }, |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | func TestMarshalScriptInterpreter(t *testing.T) { |
| 196 | testMarshalXML(t, []marshalTest{ |
| 197 | marshalTest{ |
| 198 | "with-script-interpreter", |
| 199 | JobCommandScriptInterpreter{ |
| 200 | InvocationString: "sudo", |
| 201 | }, |
| 202 | `<scriptinterpreter>sudo</scriptinterpreter>`, |
| 203 | }, |
| 204 | marshalTest{ |
| 205 | "with-script-interpreter-quoted", |
| 206 | JobCommandScriptInterpreter{ |
| 207 | ArgsQuoted: true, |
| 208 | InvocationString: "sudo", |
| 209 | }, |
| 210 | `<scriptinterpreter argsquoted="true">sudo</scriptinterpreter>`, |
| 211 | }, |
| 212 | }) |
| 213 | } |
| 214 | |
| 215 | func TestUnmarshalScriptInterpreter(t *testing.T) { |
| 216 | testUnmarshalXML(t, []unmarshalTest{ |
| 217 | unmarshalTest{ |
| 218 | "with-script-interpreter", |
| 219 | `<scriptinterpreter>sudo</scriptinterpreter>`, |
| 220 | &JobCommandScriptInterpreter{}, |
| 221 | func (rv interface {}) error { |
| 222 | v := rv.(*JobCommandScriptInterpreter) |
| 223 | if v.InvocationString != "sudo" { |
| 224 | return fmt.Errorf("got InvocationString %s, but expecting sudo", v.InvocationString) |
| 225 | } |
| 226 | if v.ArgsQuoted { |
| 227 | return fmt.Errorf("got ArgsQuoted %s, but expecting false", v.ArgsQuoted) |
| 228 | } |
| 229 | return nil |
| 230 | }, |
| 231 | }, |
| 232 | unmarshalTest{ |
| 233 | "with-script-interpreter-quoted", |
| 234 | `<scriptinterpreter argsquoted="true">sudo</scriptinterpreter>`, |
| 235 | &JobCommandScriptInterpreter{}, |
| 236 | func (rv interface {}) error { |
| 237 | v := rv.(*JobCommandScriptInterpreter) |
| 238 | if v.InvocationString != "sudo" { |
| 239 | return fmt.Errorf("got InvocationString %s, but expecting sudo", v.InvocationString) |
| 240 | } |
| 241 | if ! v.ArgsQuoted { |
| 242 | return fmt.Errorf("got ArgsQuoted %s, but expecting true", v.ArgsQuoted) |
| 243 | } |
| 244 | return nil |
| 245 | }, |
| 246 | }, |
| 247 | }) |
| 248 | } |
| 249 | |
| 250 | func TestMarshalErrorHanlder(t *testing.T) { |
| 251 | testMarshalXML(t, []marshalTest{ |
| 252 | marshalTest{ |
| 253 | "with-errorhandler", |
| 254 | JobCommandSequence{ |
| 255 | ContinueOnError: true, |
| 256 | OrderingStrategy: "step-first", |
| 257 | Commands: []JobCommand{ |
| 258 | JobCommand{ |
| 259 | Script: "inline_script", |
| 260 | ErrorHandler: &JobCommand{ |
| 261 | ContinueOnError: true, |
| 262 | Script: "error_script", |
| 263 | }, |
| 264 | }, |
| 265 | }, |
| 266 | }, |
| 267 | `<sequence keepgoing="true" strategy="step-first"><command><errorhandler keepgoingOnSuccess="true"><script>error_script</script></errorhandler><script>inline_script</script></command></sequence>`, |
| 268 | }, |
| 269 | }) |
| 270 | } |
| 271 | |
| 272 | |
| 273 | func TestMarshalJobOption(t *testing.T) { |
| 274 | testMarshalXML(t, []marshalTest{ |
| 275 | marshalTest{ |
| 276 | "with-option-basic", |
| 277 | JobOption{ |
| 278 | Name: "basic", |
| 279 | }, |
| 280 | `<option name="basic"></option>`, |
| 281 | }, |
| 282 | marshalTest{ |
| 283 | "with-option-multivalued", |
| 284 | JobOption{ |
| 285 | Name: "Multivalued", |
| 286 | MultiValueDelimiter: "|", |
| 287 | RequirePredefinedChoice: true, |
| 288 | AllowsMultipleValues: true, |
| 289 | IsRequired: true, |
| 290 | ValueChoices: JobValueChoices([]string{"myValues"}), |
| 291 | }, |
| 292 | `<option delimiter="|" enforcedvalues="true" multivalued="true" name="Multivalued" required="true" values="myValues"></option>`, |
| 293 | }, |
| 294 | marshalTest{ |
| 295 | "with-all-attributes", |
| 296 | JobOption{ |
| 297 | Name: "advanced", |
| 298 | MultiValueDelimiter: "|", |
| 299 | RequirePredefinedChoice: true, |
| 300 | AllowsMultipleValues: true, |
| 301 | ValidationRegex: ".+", |
| 302 | IsRequired: true, |
| 303 | ObscureInput: true, |
| 304 | StoragePath: "myKey", |
| 305 | DefaultValue: "myValue", |
| 306 | ValueIsExposedToScripts: true, |
| 307 | ValueChoices: JobValueChoices([]string{"myValues"}), |
| 308 | ValueChoicesURL: "myValuesUrl", |
| 309 | }, |
| 310 | `<option delimiter="|" enforcedvalues="true" multivalued="true" name="advanced" regex=".+" required="true" secure="true" storagePath="myKey" value="myValue" valueExposed="true" values="myValues" valuesUrl="myValuesUrl"></option>`, |
| 311 | }, |
| 312 | }) |
| 313 | } |
| 314 |