Codebase list ruby-beaker-hostgenerator / b80d94a
(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
2 changed file(s) with 52 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
3939 # * agent
4040 # * database
4141 #
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/
4343
4444 module_function
4545
163163 # configuration.
164164 #
165165 # 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.
166169 #
167170 # @param host_settings [String] Non-nil user input string that defines host
168171 # specific settings.
169172 #
170173 # @returns [Hash{String=>String}] The host_settings string as a map.
171174 def settings_string_to_map(host_settings)
172 Hash[
175 # Strip it down to a list of pairs
176 settings_pairs =
173177 host_settings.
174178 delete('{}').
179 gsub(' ', '').
175180 split(',').
176181 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]
178196 end
179197 end
180198 end
4747 })
4848 end
4949 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
5081 end
5182 end
5283 end