databricks-cli/bundle/run/job_options_test.go

245 lines
7.4 KiB
Go
Raw Permalink Normal View History

package run
import (
"testing"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go/service/jobs"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupJobOptions(t *testing.T) (*flag.FlagSet, *JobOptions) {
var fs flag.FlagSet
var opts JobOptions
Group bundle run flags by job and pipeline types (#1174) ## Changes Group bundle run flags by job and pipeline types ## Tests ``` Run a resource (e.g. a job or a pipeline) Usage: databricks bundle run [flags] KEY Job Flags: --dbt-commands strings A list of commands to execute for jobs with DBT tasks. --jar-params strings A list of parameters for jobs with Spark JAR tasks. --notebook-params stringToString A map from keys to values for jobs with notebook tasks. (default []) --params stringToString comma separated k=v pairs for job parameters (default []) --pipeline-params stringToString A map from keys to values for jobs with pipeline tasks. (default []) --python-named-params stringToString A map from keys to values for jobs with Python wheel tasks. (default []) --python-params strings A list of parameters for jobs with Python tasks. --spark-submit-params strings A list of parameters for jobs with Spark submit tasks. --sql-params stringToString A map from keys to values for jobs with SQL tasks. (default []) Pipeline Flags: --full-refresh strings List of tables to reset and recompute. --full-refresh-all Perform a full graph reset and recompute. --refresh strings List of tables to update. --refresh-all Perform a full graph update. Flags: -h, --help help for run --no-wait Don't wait for the run to complete. Global Flags: --debug enable debug logging -o, --output type output type: text or json (default text) -p, --profile string ~/.databrickscfg profile -t, --target string bundle target to use (if applicable) --var strings set values for variables defined in bundle config. Example: --var="foo=bar" ```
2024-02-06 14:51:02 +00:00
opts.DefineJobOptions(&fs)
opts.DefineTaskOptions(&fs)
return &fs, &opts
}
func TestJobOptionsDbtCommands(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--dbt-commands=arg1,arg2,arg3`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.dbtCommands)
}
func TestJobOptionsDbtCommandsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--dbt-commands="arg1","arg2,arg3"`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2,arg3"}, opts.dbtCommands)
}
func TestJobOptionsDbtCommandsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--dbt-commands=arg1,arg2`,
`--dbt-commands=arg3`,
})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.dbtCommands)
}
func TestJobOptionsJarParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--jar-params=arg1,arg2,arg3`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.jarParams)
}
func TestJobOptionsJarParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--jar-params="arg1","arg2,arg3"`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2,arg3"}, opts.jarParams)
}
func TestJobOptionsJarParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--jar-params=arg1,arg2`,
`--jar-params=arg3`,
})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.jarParams)
}
func TestJobOptionsNotebookParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--notebook-params=arg1=1,arg2=2,arg3=3`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.notebookParams)
}
func TestJobOptionsNotebookParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--notebook-params="arg1=1","arg2=2,arg3=3"`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2,arg3=3"}, opts.notebookParams)
}
func TestJobOptionsNotebookParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--notebook-params=arg1=1,arg2=2`,
`--notebook-params=arg3=3`,
})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.notebookParams)
}
func TestJobOptionsPythonNamedParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-named-params=arg1=1,arg2=2,arg3=3`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.pythonNamedParams)
}
func TestJobOptionsPythonNamedParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-named-params="arg1=1","arg2=2,arg3=3"`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2,arg3=3"}, opts.pythonNamedParams)
}
func TestJobOptionsPythonNamedParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--python-named-params=arg1=1,arg2=2`,
`--python-named-params=arg3=3`,
})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.pythonNamedParams)
}
func TestJobOptionsPythonParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-params=arg1,arg2,arg3`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.pythonParams)
}
func TestJobOptionsPythonParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-params="arg1","arg2,arg3"`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2,arg3"}, opts.pythonParams)
}
func TestJobOptionsPythonParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--python-params=arg1,arg2`,
`--python-params=arg3`,
})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.pythonParams)
}
func TestJobOptionsSparkSubmitParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--spark-submit-params=arg1,arg2,arg3`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.sparkSubmitParams)
}
func TestJobOptionsSparkSubmitParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--spark-submit-params="arg1","arg2,arg3"`})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2,arg3"}, opts.sparkSubmitParams)
}
func TestJobOptionsSparkSubmitParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--spark-submit-params=arg1,arg2`,
`--spark-submit-params=arg3`,
})
require.NoError(t, err)
assert.Equal(t, []string{"arg1", "arg2", "arg3"}, opts.sparkSubmitParams)
}
func TestJobOptionsSqlParams(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--sql-params=arg1=1,arg2=2,arg3=3`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.sqlParams)
}
func TestJobOptionsSqlParamsWithQuotes(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--sql-params="arg1=1","arg2=2,arg3=3"`})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2,arg3=3"}, opts.sqlParams)
}
func TestJobOptionsSqlParamsMultiple(t *testing.T) {
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{
`--sql-params=arg1=1,arg2=2`,
`--sql-params=arg3=3`,
})
require.NoError(t, err)
assert.Equal(t, map[string]string{"arg1": "1", "arg2": "2", "arg3": "3"}, opts.sqlParams)
}
func TestJobOptionsValidateIfJobHasJobParameters(t *testing.T) {
job := &resources.Job{
JobSettings: &jobs.JobSettings{
Parameters: []jobs.JobParameterDefinition{
{
Name: "param",
Default: "value",
},
},
},
}
{
// Test error if task parameters are specified.
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-params=arg1`})
require.NoError(t, err)
err = opts.Validate(job)
assert.ErrorContains(t, err, "the job to run defines job parameters; specifying task parameters is not allowed")
}
{
// Test no error if job parameters are specified.
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--params=arg1=val1`})
require.NoError(t, err)
err = opts.Validate(job)
assert.NoError(t, err)
}
}
func TestJobOptionsValidateIfJobHasNoJobParameters(t *testing.T) {
job := &resources.Job{
JobSettings: &jobs.JobSettings{
Parameters: []jobs.JobParameterDefinition{},
},
}
{
// Test error if job parameters are specified.
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--params=arg1=val1`})
require.NoError(t, err)
err = opts.Validate(job)
assert.ErrorContains(t, err, "the job to run does not define job parameters; specifying job parameters is not allowed")
}
{
// Test no error if task parameters are specified.
fs, opts := setupJobOptions(t)
err := fs.Parse([]string{`--python-params=arg1`})
require.NoError(t, err)
err = opts.Validate(job)
assert.NoError(t, err)
}
}