diff --git a/store/local/job_spec_adapter.go b/store/local/job_spec_adapter.go index 7ecaa87fd2..a5a155c6f2 100644 --- a/store/local/job_spec_adapter.go +++ b/store/local/job_spec_adapter.go @@ -463,9 +463,17 @@ func (adapt JobSpecAdapter) ToSpec(conf Job) (models.JobSpec, error) { taskConf := models.JobSpecConfigs{} for _, c := range conf.Task.Config { + name, ok := c.Key.(string) + if !ok { + return models.JobSpec{}, fmt.Errorf("spec reading error, failed to convert key %+v to string", c.Key) + } + value, ok := c.Value.(string) + if !ok { + return models.JobSpec{}, fmt.Errorf("spec reading error, failed to convert value %+v on key %s to string", c.Value, name) + } taskConf = append(taskConf, models.JobSpecConfigItem{ - Name: c.Key.(string), - Value: c.Value.(string), // TODO: panics when value not valid, error with macros + Name: name, + Value: value, }) } diff --git a/store/local/job_spec_adapter_test.go b/store/local/job_spec_adapter_test.go index baa8fee2ac..8f3d36641b 100644 --- a/store/local/job_spec_adapter_test.go +++ b/store/local/job_spec_adapter_test.go @@ -236,6 +236,80 @@ hooks: []` assert.Equal(t, expErr, actualErr) assert.Equal(t, models.JobSpec{}, modelJob) }) + t.Run("should not convert job if task config is not string", func(t *testing.T) { + yamlSpec := ` +version: 1 +name: test_job +owner: test@example.com +schedule: + start_date: "2021-02-03" + interval: 0 2 * * * +behavior: + depends_on_past: true + catch_up: false + notify: + - on: test + channel: + - test://hello +task: + name: bq2bq + config: + PROJECT: example + DATASET: dataset + TABLE: table + SQL_TYPE: STANDARD + LOAD_METHOD: REPLACE + MAX_LINE: 100 + window: + size: 168h + offset: 0 + truncate_to: w +labels: + orchestrator: optimus +dependencies: + - + http: + name: http-sensor-1 + headers: + Authentication: Token-1 + Content-type: application/json + params: + key-1: value-1 + key-2: value-2 + url: "https://optimus-host:80/serve/1/" + - + http: + name: http-sensor-2 + headers: + Authentication: Token-2 + Content-type: application/json + params: + key-3: value-3 + key-4: value-4 + url: "https://optimus-host:80/serve/2/" + +hooks: []` + var localJobParsed local.Job + err := yaml.Unmarshal([]byte(yamlSpec), &localJobParsed) + assert.Nil(t, err) + + execUnit := new(mock.BasePlugin) + execUnit.On("PluginInfo").Return(&models.PluginInfoResponse{ + Name: "bq2bq", + }, nil) + + pluginRepo := new(mock.SupportedPluginRepo) + pluginRepo.On("GetByName", "bq2bq").Return(&models.Plugin{ + Base: execUnit, + }, nil) + adapter := local.NewJobSpecAdapter(pluginRepo) + + modelJob, actualErr := adapter.ToSpec(localJobParsed) + + expErr := fmt.Errorf("spec reading error, failed to convert value 100 on key MAX_LINE to string") + assert.Equal(t, expErr, actualErr) + assert.Equal(t, models.JobSpec{}, modelJob) + }) } func TestJob_MergeFrom(t *testing.T) {