configtoolkit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ ---
2
+ opt_integer: 56
3
+ req_uri: http://blogs.designingpatterns.com
4
+ req_float: 3.14
5
+ req_pathname: /usr/designingpatterns/bin
6
+ req_integer: 6
7
+ req_string: sample string2
8
+ req_boolean: true
9
+ req_symbol: sample_symbol2
10
+ req_big_integer: 90000000000
11
+ nested_config:
12
+ req_nested_array:
13
+ - req_second_nested_pathname: /usr/designingpatterns/Applications/pattmake
14
+ req_second_nested_array:
15
+ - 6
16
+ - 20
17
+ - req_second_nested_pathname: /usr
18
+ req_second_nested_array:
19
+ - 205
20
+ - 206
21
+ req_nested_integer: 34
@@ -0,0 +1,292 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'common'
4
+
5
+ require 'configtoolkit'
6
+ require 'configtoolkit/yamlreader'
7
+ require 'configtoolkit/yamlwriter'
8
+
9
+ require 'pathname'
10
+ require 'test/unit'
11
+
12
+ class ConstrainedArrayTest < Test::Unit::TestCase
13
+ def verify_class_attributes(array_class, expected_attribute_values)
14
+ expected_attribute_values.each do |attribute, attribute_value|
15
+ assert_equal(attribute_value, array_class.send(attribute))
16
+ end
17
+ end
18
+
19
+ def test_new
20
+ verify_class_attributes(ConfigToolkit::ConstrainedArray.new(Integer, 1, 2),
21
+ {
22
+ :element_class => Integer,
23
+ :min_num_elements => 1,
24
+ :max_num_elements => 2
25
+ })
26
+
27
+ verify_class_attributes(ConfigToolkit::ConstrainedArray.new(Integer),
28
+ {
29
+ :element_class => Integer,
30
+ :min_num_elements => nil,
31
+ :max_num_elements => nil
32
+ })
33
+
34
+ verify_class_attributes(ConfigToolkit::ConstrainedArray.new(String, 1),
35
+ {
36
+ :element_class => String,
37
+ :min_num_elements => 1,
38
+ :max_num_elements => nil
39
+ })
40
+ end
41
+ end
42
+
43
+ class BaseConfigTest < Test::Unit::TestCase
44
+ #
45
+ # This config class will be used in the tests.
46
+ # Note that it has a:
47
+ # 1.) optional parameter (:behind_firewall)
48
+ # 2.) A ConfigToolkit::Boolean parameter
49
+ # 3.) A ConstrainedArray parameter
50
+ #
51
+ class MachineConfig < ConfigToolkit::BaseConfig
52
+ add_required_param(:name, String)
53
+
54
+ add_required_param(:architecture, String)
55
+
56
+ add_required_param(:os, String)
57
+
58
+ add_required_param(:num_cpus, Integer) do |value|
59
+ if(value <= 0)
60
+ raise_error("num_cpus must be greater than zero")
61
+ end
62
+ end
63
+
64
+ add_optional_param(:behind_firewall, ConfigToolkit::Boolean, true)
65
+
66
+ add_required_param(:contains_sensitive_data, ConfigToolkit::Boolean)
67
+
68
+ add_required_param(:addresses, ConfigToolkit::ConstrainedArray.new(URI, 2, 3))
69
+
70
+ def validate_all_values
71
+ if(contains_sensitive_data && !behind_firewall)
72
+ raise_error("a machine cannot contain sensitive data and not be behind the firewall")
73
+ end
74
+ end
75
+ end
76
+
77
+ MACHINES_CONFIG_FILE_NAME = get_test_config_file_name("machines.yaml")
78
+
79
+ #
80
+ # Test whether the methods for MachineConfig's attributes
81
+ # were generated properly.
82
+ #
83
+ def test_attribute_method_generation
84
+ config = MachineConfig.new()
85
+
86
+ config_param_values = {
87
+ :name => "pear",
88
+ :architecture => "x86_64",
89
+ :os => "FreeBSD",
90
+ :num_cpus => 8,
91
+ :behind_firewall => true,
92
+ :contains_sensitive_data => true,
93
+ :addresses => [
94
+ URI("http://release.designingpatterns.com"),
95
+ URI("http://pear.designingpatterns.com")
96
+ ]
97
+ }
98
+
99
+ #
100
+ # Iterate through the hash and check that the setters and
101
+ # getters for each parameter work properly.
102
+ #
103
+ config_param_values.each do |param_name, param_value|
104
+ assert_equal(nil, config.send(param_name))
105
+ setter = "#{param_name}=".to_sym()
106
+ config.send(setter, param_value)
107
+ assert_equal(param_value, config.send(param_name))
108
+ end
109
+
110
+ puts config
111
+ end
112
+
113
+ def verify_param_values(config, expected_config_param_values)
114
+ expected_config_param_values.each do |param_name, param_value|
115
+ assert_equal(param_value, config.send(param_name))
116
+ end
117
+ end
118
+
119
+ def verify_correct_dump(config_file_name, containing_object_name)
120
+ reader = ConfigToolkit::YAMLReader.new(config_file_name)
121
+ config = MachineConfig.load(reader, containing_object_name)
122
+
123
+ dump_stream = StringIO.new()
124
+ writer = ConfigToolkit::YAMLWriter.new(dump_stream, true)
125
+ config.dump(writer)
126
+
127
+ puts dump_stream.string()
128
+
129
+ dump_stream.rewind()
130
+ dump_reader = ConfigToolkit::YAMLReader.new(dump_stream)
131
+ assert_equal(config, MachineConfig.load(dump_reader, containing_object_name))
132
+ end
133
+
134
+ def verify_dump_error(config, message)
135
+ dump_stream = StringIO.new()
136
+ writer = ConfigToolkit::YAMLWriter.new(dump_stream, true)
137
+
138
+ rescued_exception = false
139
+ begin
140
+ config.dump(writer)
141
+ rescue ConfigToolkit::Error => e
142
+ assert_equal(message, e.message)
143
+ rescued_exception = true
144
+ end
145
+
146
+ assert(rescued_exception)
147
+ end
148
+
149
+ def test_dump
150
+ verify_correct_dump(get_test_config_file_name("firewall.yaml"), "firewall")
151
+ verify_correct_dump(get_test_config_file_name("webserver.yaml"), "")
152
+
153
+ #
154
+ # Verify that dumping a config sets the config's optional
155
+ # parameters to their default values.
156
+ #
157
+ config = MachineConfig.new()
158
+ config.name = "jackfruit"
159
+ config.architecture = "powerpc"
160
+ config.os = "AIX"
161
+ config.contains_sensitive_data = true
162
+ config.num_cpus = 2
163
+ config.addresses = ["http://jackfruit.designingpatterns.com", "http://www3.designingpatterns.com"]
164
+ dump_stream = StringIO.new()
165
+ writer = ConfigToolkit::YAMLWriter.new(dump_stream, true)
166
+ assert_equal(nil, config.behind_firewall)
167
+ config.dump(dump_stream)
168
+ assert_equal(true, config.behind_firewall)
169
+
170
+ config = MachineConfig.new()
171
+ config.name = "uniq"
172
+ verify_dump_error(config, "missing parameter(s): architecture, os, num_cpus, contains_sensitive_data, addresses")
173
+
174
+ config.architecture = "powerpc"
175
+ config.os = "AIX"
176
+ config.contains_sensitive_data = true
177
+ config.behind_firewall = false
178
+ config.num_cpus = 2
179
+ config.addresses = ["http://uniq.designingpatterns.com", "http://ftp.designingpatterns.com"]
180
+ verify_dump_error(config, "BaseConfigTest::MachineConfig#validate_all_values dump error: a machine cannot contain sensitive data and not be behind the firewall")
181
+ end
182
+
183
+ def verify_correct_load(config_file_name,
184
+ containing_object_name,
185
+ expected_param_values)
186
+ reader = ConfigToolkit::YAMLReader.new(config_file_name)
187
+ config = MachineConfig.load(reader, containing_object_name)
188
+ verify_param_values(config, expected_param_values)
189
+ puts config
190
+ end
191
+
192
+ def verify_load_error(config_file_name, containing_object_name, message)
193
+ reader = ConfigToolkit::YAMLReader.new(config_file_name)
194
+
195
+ rescued_exception = false
196
+ begin
197
+ config = MachineConfig.load(reader, containing_object_name)
198
+ rescue ConfigToolkit::Error => e
199
+ assert_equal(message, e.message)
200
+ rescued_exception = true
201
+ end
202
+
203
+ assert(rescued_exception)
204
+ end
205
+
206
+ #
207
+ # Test whether a canned config file can be load correctly (whether
208
+ # the config instances ends up having the expected values).
209
+ #
210
+ def test_load
211
+ verify_correct_load(MACHINES_CONFIG_FILE_NAME, "", {
212
+ :name => "apple",
213
+ :architecture => "powerpc",
214
+ :os => "AIX",
215
+ :num_cpus => 32,
216
+ :behind_firewall => false,
217
+ :contains_sensitive_data => false,
218
+ :addresses =>
219
+ [
220
+ URI("http://default.designingpatterns.com"),
221
+ URI("http://apple.designingpatterns.com")
222
+ ]
223
+ })
224
+
225
+ verify_correct_load(MACHINES_CONFIG_FILE_NAME, "production", {
226
+ :name => "orange",
227
+ :architecture => "ultrasparc",
228
+ :os => "Solaris",
229
+ :num_cpus => 64,
230
+ :behind_firewall => true,
231
+ :contains_sensitive_data => true,
232
+ :addresses =>
233
+ [
234
+ URI("http://production.designingpatterns.com"),
235
+ URI("http://orange.designingpatterns.com")
236
+ ]
237
+ })
238
+
239
+ verify_correct_load(MACHINES_CONFIG_FILE_NAME, "test", {
240
+ :name => "bananna",
241
+ :architecture => "itanium",
242
+ :os => "Linux",
243
+ :num_cpus => 16,
244
+ :behind_firewall => true,
245
+ :contains_sensitive_data => false,
246
+ :addresses =>
247
+ [
248
+ URI("http://test.designingpatterns.com"),
249
+ URI("http://bananna.designingpatterns.com")
250
+ ]
251
+ })
252
+ end
253
+
254
+ def test_load_errors
255
+ verify_load_error(get_test_config_file_name("bad_config.yaml"),
256
+ "",
257
+ "ConfigToolkit::YAMLReader#read returned Array rather than Hash")
258
+
259
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
260
+ "bad_containing_object",
261
+ "error: BaseConfigTest::MachineConfig#load found Fixnum rather than Hash when reading the bad_containing_object containing object")
262
+
263
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
264
+ "missing_num_cpus",
265
+ "missing parameter(s): missing_num_cpus.num_cpus")
266
+
267
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
268
+ "missing_name_and_num_cpus",
269
+ "missing parameter(s): missing_name_and_num_cpus.name, missing_name_and_num_cpus.num_cpus")
270
+
271
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
272
+ "bad_num_cpus",
273
+ "error setting parameter bad_num_cpus.num_cpus with value -2: num_cpus must be greater than zero.")
274
+
275
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
276
+ "bad_addresses_element_type",
277
+ "error setting parameter bad_addresses_element_type.addresses[0] with value 2: cannot convert from value class Fixnum to specified class URI.")
278
+
279
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
280
+ "bad_addresses_max_num_elements",
281
+ "error setting parameter bad_addresses_max_num_elements.addresses with value [http://test.designingpatterns.com, http://bananna.designingpatterns.com, http://fruitsalad.designingpatterns.com, http://parfait.designingpatterns.com]: the number of elements (4) is greater than the specified maximum number of elements (3).")
282
+
283
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
284
+ "bad_addresses_min_num_elements",
285
+ "error setting parameter bad_addresses_min_num_elements.addresses with value [http://bananna.designingpatterns.com]: the number of elements (1) is less than the specified minimum number of elements (2).")
286
+
287
+ verify_load_error(MACHINES_CONFIG_FILE_NAME,
288
+ "bad_contains_sensitive_data_behind_firewall_combo",
289
+ "BaseConfigTest::MachineConfig#validate_all_values load error for bad_contains_sensitive_data_behind_firewall_combo: a machine cannot contain sensitive data and not be behind the firewall")
290
+
291
+ end
292
+ end
data/test/test_yaml.rb ADDED
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'common'
4
+ require 'readerwritertest'
5
+
6
+ require 'configtoolkit/yamlreader'
7
+ require 'configtoolkit/yamlwriter'
8
+
9
+ class YAMLTest < Test::Unit::TestCase
10
+ include ReaderWriterTest
11
+
12
+ def test_readers
13
+ run_reader_test(ConfigToolkit::YAMLReader,
14
+ "",
15
+ REFERENCE_SAMPLE_CONFIG,
16
+ get_test_config_file_name("sample.standard_yaml_types.yaml"))
17
+
18
+ run_reader_test(ConfigToolkit::YAMLReader,
19
+ "",
20
+ REFERENCE_SAMPLE_CONFIG,
21
+ get_test_config_file_name("sample.ruby_yaml_types.yaml"))
22
+
23
+ run_reader_test(ConfigToolkit::YAMLReader,
24
+ "",
25
+ REFERENCE_SAMPLE_CONFIG,
26
+ File.new(get_test_config_file_name("sample.ruby_yaml_types.yaml"), "r"))
27
+
28
+ run_reader_test(ConfigToolkit::YAMLReader,
29
+ "contained_sample",
30
+ REFERENCE_CONTAINED_SAMPLE_CONFIG,
31
+ get_test_config_file_name("contained_sample.yaml"))
32
+ end
33
+
34
+ def test_writers
35
+ run_writer_test(ConfigToolkit::YAMLWriter,
36
+ REFERENCE_SAMPLE_CONFIG,
37
+ get_test_config_file_name("sample.standard_yaml_types.yaml"),
38
+ get_test_config_file_name("sample.standard_yaml_types.dumped.yaml"),
39
+ get_test_config_file_name("sample.standard_yaml_types.dumped.yaml"),
40
+ true)
41
+
42
+ run_writer_test(ConfigToolkit::YAMLWriter,
43
+ REFERENCE_SAMPLE_CONFIG,
44
+ get_test_config_file_name("sample.ruby_yaml_types.yaml"),
45
+ get_test_config_file_name("sample.ruby_yaml_types.dumped.yaml"),
46
+ get_test_config_file_name("sample.ruby_yaml_types.dumped.yaml"),
47
+ false)
48
+
49
+ run_writer_test(ConfigToolkit::YAMLWriter,
50
+ REFERENCE_SAMPLE_CONFIG,
51
+ get_test_config_file_name("sample.standard_yaml_types.yaml"),
52
+ get_test_config_file_name("sample.standard_yaml_types.dumped.yaml"),
53
+ File.new(get_test_config_file_name("sample.standard_yaml_types.dumped.yaml"), "w"),
54
+ true)
55
+
56
+ run_writer_test(ConfigToolkit::YAMLWriter,
57
+ REFERENCE_CONTAINED_SAMPLE_CONFIG,
58
+ get_test_config_file_name("contained_sample.yaml"),
59
+ get_test_config_file_name("contained_sample.dumped.yaml"),
60
+ get_test_config_file_name("contained_sample.dumped.yaml"),
61
+ true)
62
+ end
63
+
64
+ def test_readers_writers
65
+ run_reader_writer_test(ConfigToolkit::YAMLReader,
66
+ ConfigToolkit::YAMLWriter,
67
+ REFERENCE_SAMPLE_CONFIG,
68
+ [get_test_config_file_name("sample.standard_yaml_types.dumped.yaml")],
69
+ [get_test_config_file_name("sample.standard_yaml_types.dumped.yaml"), false])
70
+ run_reader_writer_test(ConfigToolkit::YAMLReader,
71
+ ConfigToolkit::YAMLWriter,
72
+ REFERENCE_CONTAINED_SAMPLE_CONFIG,
73
+ [get_test_config_file_name("contained_sample.yaml")],
74
+ [get_test_config_file_name("contained_sample.dumped.yaml"), true])
75
+ end
76
+ end
@@ -0,0 +1,9 @@
1
+ name: papaya
2
+ architecture: powerpc
3
+ os: AIX
4
+ num_cpus: 4
5
+ contains_sensitive_data: false
6
+ behind_firewall: false
7
+ addresses:
8
+ - http://www.designingpatterns.com
9
+ - http://papaya.designingpatterns.com
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configtoolkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - DesigningPatterns
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-13 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ version:
24
+ description: "This package makes sourcing information from configuration files robust and easy! It: * Allows programmers to specify the type of data that should be returned from a configuration file. The toolkit automatically will validate the file's data against this specification when loading the file, ensuring that the specifications always are met and saving the programmer the tedious chore of writing validation code. * Allows programmers to easily programatically create and dump new configuration files. * Provides classes that can load from and dump to different kinds of configuration files, including YAML configuration files."
25
+ email:
26
+ - technical.inquiries@designingpatterns.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.txt
33
+ - History.txt
34
+ files:
35
+ - LICENSE
36
+ - README.txt
37
+ - History.txt
38
+ - Rakefile
39
+ - lib/configtoolkit.rb
40
+ - lib/configtoolkit/baseconfig.rb
41
+ - lib/configtoolkit/toolkit.rb
42
+ - lib/configtoolkit/types.rb
43
+ - lib/configtoolkit/yamlreader.rb
44
+ - lib/configtoolkit/yamlwriter.rb
45
+ - test/common.rb
46
+ - test/readerwritertest.rb
47
+ - test/test_baseconfig.rb
48
+ - test/test_yaml.rb
49
+ - test/bad_config.yaml
50
+ - test/contained_sample.yaml
51
+ - test/firewall.yaml
52
+ - test/machines.yaml
53
+ - test/sample.ruby_yaml_types.yaml
54
+ - test/sample.standard_yaml_types.yaml
55
+ - test/webserver.yaml
56
+ has_rdoc: true
57
+ homepage: http://configtoolkit.rubyforge.org/
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --main
61
+ - README.txt
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: configtoolkit
79
+ rubygems_version: 1.1.1
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: "This package makes sourcing information from configuration files robust and easy! It: * Allows programmers to specify the type of data that should be returned from a configuration file"
83
+ test_files:
84
+ - test/test_baseconfig.rb
85
+ - test/test_yaml.rb