|
1 | 1 | package cli
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | defaults "github.com/mcuadros/go-defaults"
|
7 | 8 | "github.com/mcuadros/ofelia/core"
|
8 | 9 | "github.com/mcuadros/ofelia/middlewares"
|
9 | 10 | . "gopkg.in/check.v1"
|
| 11 | + gcfg "gopkg.in/gcfg.v1" |
10 | 12 | )
|
11 | 13 |
|
12 | 14 | func Test(t *testing.T) { TestingT(t) }
|
@@ -68,6 +70,76 @@ func (s *SuiteConfig) TestExecJobBuild(c *C) {
|
68 | 70 | c.Assert(j.Middlewares(), HasLen, 1)
|
69 | 71 | }
|
70 | 72 |
|
| 73 | +func (s *SuiteConfig) TestConfigIni(c *C) { |
| 74 | + testcases := []struct { |
| 75 | + Ini string |
| 76 | + ExpectedConfig Config |
| 77 | + Comment string |
| 78 | + }{ |
| 79 | + { |
| 80 | + Ini: ` |
| 81 | + [job-exec "foo"] |
| 82 | + schedule = @every 10s |
| 83 | + command = echo \"foo\" |
| 84 | + `, |
| 85 | + ExpectedConfig: Config{ |
| 86 | + ExecJobs: map[string]*ExecJobConfig{ |
| 87 | + "foo": {ExecJob: core.ExecJob{BareJob: core.BareJob{ |
| 88 | + Schedule: "@every 10s", |
| 89 | + Command: `echo "foo"`, |
| 90 | + }}}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + Comment: "Test job-exec", |
| 94 | + }, |
| 95 | + { |
| 96 | + Ini: ` |
| 97 | + [job-run "foo"] |
| 98 | + schedule = @every 10s |
| 99 | + environment = "KEY1=value1" |
| 100 | + Environment = "KEY2=value2" |
| 101 | + `, |
| 102 | + ExpectedConfig: Config{ |
| 103 | + RunJobs: map[string]*RunJobConfig{ |
| 104 | + "foo": {RunJob: core.RunJob{BareJob: core.BareJob{ |
| 105 | + Schedule: "@every 10s", |
| 106 | + }, |
| 107 | + Environment: []string{"KEY1=value1", "KEY2=value2"}, |
| 108 | + }}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + Comment: "Test job-run with Env Variables", |
| 112 | + }, |
| 113 | + { |
| 114 | + Ini: ` |
| 115 | + [job-run "foo"] |
| 116 | + schedule = @every 10s |
| 117 | + volumes-from = "volume1" |
| 118 | + volumes-from = "volume2" |
| 119 | + `, |
| 120 | + ExpectedConfig: Config{ |
| 121 | + RunJobs: map[string]*RunJobConfig{ |
| 122 | + "foo": {RunJob: core.RunJob{BareJob: core.BareJob{ |
| 123 | + Schedule: "@every 10s", |
| 124 | + }, |
| 125 | + VolumesFrom: []string{"volume1", "volume2"}, |
| 126 | + }}, |
| 127 | + }, |
| 128 | + }, |
| 129 | + Comment: "Test job-run with Env Variables", |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, t := range testcases { |
| 134 | + conf := Config{} |
| 135 | + err := gcfg.ReadStringInto(&conf, t.Ini) |
| 136 | + c.Assert(err, IsNil) |
| 137 | + if !c.Check(conf, DeepEquals, t.ExpectedConfig) { |
| 138 | + c.Errorf("Test %q\nExpected %s, but got %s", t.Comment, toJSON(t.ExpectedConfig), toJSON(conf)) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
71 | 143 | func (s *SuiteConfig) TestLabelsConfig(c *C) {
|
72 | 144 | testcases := []struct {
|
73 | 145 | Labels map[string]map[string]string
|
@@ -285,12 +357,56 @@ func (s *SuiteConfig) TestLabelsConfig(c *C) {
|
285 | 357 | },
|
286 | 358 | Comment: "Test run job with environment variables",
|
287 | 359 | },
|
| 360 | + { |
| 361 | + Labels: map[string]map[string]string{ |
| 362 | + "some": { |
| 363 | + requiredLabel: "true", |
| 364 | + serviceLabel: "true", |
| 365 | + labelPrefix + "." + jobRun + ".job1.schedule": "schedule1", |
| 366 | + labelPrefix + "." + jobRun + ".job1.command": "command1", |
| 367 | + labelPrefix + "." + jobRun + ".job1.volumes-from": "test123", |
| 368 | + labelPrefix + "." + jobRun + ".job2.schedule": "schedule2", |
| 369 | + labelPrefix + "." + jobRun + ".job2.command": "command2", |
| 370 | + labelPrefix + "." + jobRun + ".job2.volumes-from": `["test321", "test456"]`, |
| 371 | + }, |
| 372 | + }, |
| 373 | + ExpectedConfig: Config{ |
| 374 | + RunJobs: map[string]*RunJobConfig{ |
| 375 | + "job1": { |
| 376 | + RunJob: core.RunJob{ |
| 377 | + BareJob: core.BareJob{ |
| 378 | + Schedule: "schedule1", |
| 379 | + Command: "command1", |
| 380 | + }, |
| 381 | + VolumesFrom: []string{"test123"}, |
| 382 | + }, |
| 383 | + }, |
| 384 | + "job2": { |
| 385 | + RunJob: core.RunJob{ |
| 386 | + BareJob: core.BareJob{ |
| 387 | + Schedule: "schedule2", |
| 388 | + Command: "command2", |
| 389 | + }, |
| 390 | + VolumesFrom: []string{"test321", "test456"}, |
| 391 | + }, |
| 392 | + }, |
| 393 | + }, |
| 394 | + }, |
| 395 | + Comment: "Test run job with volumes-from", |
| 396 | + }, |
288 | 397 | }
|
289 | 398 |
|
290 | 399 | for _, t := range testcases {
|
291 | 400 | var conf = Config{}
|
292 | 401 | err := conf.buildFromDockerLabels(t.Labels)
|
293 | 402 | c.Assert(err, IsNil)
|
294 |
| - c.Assert(conf, DeepEquals, t.ExpectedConfig) |
| 403 | + if !c.Check(conf, DeepEquals, t.ExpectedConfig) { |
| 404 | + c.Errorf("Test %q\nExpected %s, but got %s", t.Comment, toJSON(t.ExpectedConfig), toJSON(conf)) |
| 405 | + } |
295 | 406 | }
|
296 | 407 | }
|
| 408 | + |
| 409 | +func toJSON(any interface{}) string { |
| 410 | + b, _ := json.MarshalIndent(any, "", " ") |
| 411 | + return string(b) |
| 412 | +} |
0 commit comments