(QENG-3276) Add more parser tests for failures
This commit adds tests that validate the proper exceptions are thrown
when invalid arbitrary host settings are provided.
This also expands host-setting support to include arbitrary whitespace
to allow for more human-readable input.
Nate Wolfe
7 years ago
39 | 39 |
# * agent
|
40 | 40 |
# * database
|
41 | 41 |
#
|
42 | |
NODE_REGEX=/\A(?<bits>\d+)((?<arbitrary_roles>([[:lower:]_]*|\,)*)\.)?(?<roles>[uacldfm]*)(?<host_settings>\{[[:graph:]]*\})?\Z/
|
|
42 |
NODE_REGEX=/\A(?<bits>\d+)((?<arbitrary_roles>([[:lower:]_]*|\,)*)\.)?(?<roles>[uacldfm]*)(?<host_settings>\{[[:print:]]*\})?\Z/
|
43 | 43 |
|
44 | 44 |
module_function
|
45 | 45 |
|
|
163 | 163 |
# configuration.
|
164 | 164 |
#
|
165 | 165 |
# The string is expected to be of the form "{key1=value1,key2=value2,...}".
|
|
166 |
# Any whitespace found in the string will be stripped and ignored.
|
|
167 |
#
|
|
168 |
# Throws an exception of the string is malformed in any way.
|
166 | 169 |
#
|
167 | 170 |
# @param host_settings [String] Non-nil user input string that defines host
|
168 | 171 |
# specific settings.
|
169 | 172 |
#
|
170 | 173 |
# @returns [Hash{String=>String}] The host_settings string as a map.
|
171 | 174 |
def settings_string_to_map(host_settings)
|
172 | |
Hash[
|
|
175 |
# Strip it down to a list of pairs
|
|
176 |
settings_pairs =
|
173 | 177 |
host_settings.
|
174 | 178 |
delete('{}').
|
|
179 |
gsub(' ', '').
|
175 | 180 |
split(',').
|
176 | 181 |
map { |keyvalue| keyvalue.split('=') }
|
177 | |
]
|
|
182 |
|
|
183 |
# Validate they're actually pairs, and that all keys are non-empty
|
|
184 |
settings_pairs.each do |pair|
|
|
185 |
if pair.length != 2
|
|
186 |
raise BeakerHostGenerator::Exceptions::InvalidNodeSpecError,
|
|
187 |
"Malformed host settings: #{host_settings}"
|
|
188 |
end
|
|
189 |
if pair.first.nil? || pair.first.empty?
|
|
190 |
raise BeakerHostGenerator::Exceptions::InvalidNodeSpecError,
|
|
191 |
"Malformed host settings: #{host_settings}"
|
|
192 |
end
|
|
193 |
end
|
|
194 |
|
|
195 |
Hash[settings_pairs]
|
178 | 196 |
end
|
179 | 197 |
end
|
180 | 198 |
end
|
47 | 47 |
})
|
48 | 48 |
end
|
49 | 49 |
end
|
|
50 |
|
|
51 |
context 'When using arbitrary host settings' do
|
|
52 |
it 'Supports arbitrary whitespace' do
|
|
53 |
expect( parse_node_info_token("64{ k1=v1, k2=v2, k3 = v3 , k4 =v4 }") ).
|
|
54 |
to eq({
|
|
55 |
"roles" => "",
|
|
56 |
"arbitrary_roles" => [],
|
|
57 |
"bits" => "64",
|
|
58 |
"host_settings" => {
|
|
59 |
"k1" => "v1",
|
|
60 |
"k2" => "v2",
|
|
61 |
"k3" => "v3",
|
|
62 |
"k4" => "v4"
|
|
63 |
}
|
|
64 |
})
|
|
65 |
end
|
|
66 |
|
|
67 |
it 'Raises InvalidNodeSpecError for malformed key-value pairs' do
|
|
68 |
expect { parse_node_info_token("64{foo=bar=baz}") }.
|
|
69 |
to raise_error(BeakerHostGenerator::Exceptions::InvalidNodeSpecError)
|
|
70 |
|
|
71 |
expect { parse_node_info_token("64{foo=}") }.
|
|
72 |
to raise_error(BeakerHostGenerator::Exceptions::InvalidNodeSpecError)
|
|
73 |
|
|
74 |
expect { parse_node_info_token("64{=bar}") }.
|
|
75 |
to raise_error(BeakerHostGenerator::Exceptions::InvalidNodeSpecError)
|
|
76 |
|
|
77 |
expect { parse_node_info_token("64{=}") }.
|
|
78 |
to raise_error(BeakerHostGenerator::Exceptions::InvalidNodeSpecError)
|
|
79 |
end
|
|
80 |
end
|
50 | 81 |
end
|
51 | 82 |
end
|
52 | 83 |
end
|