Skip to content

Commit

Permalink
fix: err gracefully when config job spec is non string (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman authored Aug 16, 2022
1 parent 7d24318 commit 7253693
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
12 changes: 10 additions & 2 deletions store/local/job_spec_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
74 changes: 74 additions & 0 deletions store/local/job_spec_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
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) {
Expand Down

0 comments on commit 7253693

Please sign in to comment.