Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions dsc/tests/dsc_parameters.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,128 @@ Describe 'Parameters tests' {
$LASTEXITCODE | Should -Be 4
$testError | Should -Match 'Circular dependency or unresolvable parameter references detected in parameters'
}

It 'Default value violates <constraint> constraint' -TestCases @(
@{ constraint = 'minLength'; type = 'string'; value = 'ab'; min = 3; max = 20; errorMatch = 'minimum length' }
@{ constraint = 'maxLength'; type = 'string'; value = 'verylongusernamethatexceedslimit'; min = 3; max = 20; errorMatch = 'maximum length' }
@{ constraint = 'minValue'; type = 'int'; value = 0; min = 1; max = 65535; errorMatch = 'minimum value' }
@{ constraint = 'maxValue'; type = 'int'; value = 99999; min = 1; max = 65535; errorMatch = 'maximum value' }
) {
param($constraint, $type, $value, $min, $max, $errorMatch)

if ($type -eq 'string') {
$value = "'$value'"
}

$minConstraint = if ($type -eq 'string') { "minLength: $min" } else { "minValue: $min" }
$maxConstraint = if ($type -eq 'string') { "maxLength: $max" } else { "maxValue: $max" }

$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
param1:
type: $type
$minConstraint
$maxConstraint
defaultValue: $value
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: '[parameters(''param1'')]'
"@

$testError = & {$config_yaml | dsc config get -f - 2>&1}
$LASTEXITCODE | Should -Be 4
$testError | Should -Match $errorMatch
}

It 'Default value violates allowedValues constraint for <type>' -TestCases @(
@{ type = 'string'; value = 'staging'; allowed = @('dev', 'test', 'prod') }
@{ type = 'int'; value = 7; allowed = @(1, 5, 10) }
) {
param($type, $value, $allowed)

if ($type -eq 'string') {
$value = "'$value'"
}

$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
param1:
type: $type
allowedValues: $($allowed | ConvertTo-Json -Compress)
defaultValue: $value
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: '[parameters(''param1'')]'
"@

$testError = & {$config_yaml | dsc config get -f - 2>&1}
$LASTEXITCODE | Should -Be 4
$testError | Should -Match 'allowed values'
}

It 'Default values pass constraint validation for <type>' -TestCases @(
@{ type = 'string'; value = 'admin'; min = 3; max = 20 }
@{ type = 'string'; value = 'abc'; min = 3; max = 20 }
@{ type = 'string'; value = 'abcdefghijklmnopqrst'; min = 3; max = 20 }
@{ type = 'int'; value = 8080; min = 1; max = 65535 }
@{ type = 'int'; value = 1; min = 1; max = 65535 }
@{ type = 'int'; value = 65535; min = 1; max = 65535 }
) {
param($type, $value, $min, $max)

$minConstraint = if ($type -eq 'string') { "minLength: $min" } else { "minValue: $min" }
$maxConstraint = if ($type -eq 'string') { "maxLength: $max" } else { "maxValue: $max" }

$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
param1:
type: $type
$minConstraint
$maxConstraint
defaultValue: $value
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: '[parameters(''param1'')]'
"@

$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -BeExactly $value
}

It 'Default values with allowedValues pass validation for <type>' -TestCases @(
@{ type = 'string'; value = 'dev'; allowed = @('dev', 'test', 'prod') }
@{ type = 'string'; value = 'prod'; allowed = @('dev', 'test', 'prod') }
@{ type = 'int'; value = 5; allowed = @(1, 5, 10) }
@{ type = 'int'; value = 10; allowed = @(1, 5, 10) }
) {
param($type, $value, $allowed)

$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
param1:
type: $type
allowedValues: $($allowed | ConvertTo-Json -Compress)
defaultValue: $value
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: '[parameters(''param1'')]'
"@

$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -BeExactly $value
}
}
3 changes: 3 additions & 0 deletions lib/dsc-lib/src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ impl Configurator {
};

if let Ok(value) = value_result {
check_length(name, &value, parameter)?;
check_allowed_values(name, &value, parameter)?;
check_number_limits(name, &value, parameter)?;
validate_parameter_type(name, &value, &parameter.parameter_type)?;
self.context.parameters.insert(name.to_string(), (value, parameter.parameter_type.clone()));
resolved_in_this_pass.push(name.clone());
Expand Down