Class: Beaker::Options::CommandLineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/options/command_line_parser.rb

Overview

An object that parses arguments in the format [‘–option’, ‘value’, ‘–option2’, ‘value2’, ‘–switch’]

Instance Method Summary collapse

Constructor Details

#initializeCommandLineParser

Note:

All of Beaker’s supported command line options are defined here

Returns a new instance of CommandLineParser.

Examples:

Create a CommanLineParser

a = CommandLineParser.new


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/beaker/options/command_line_parser.rb', line 10

def initialize
  @cmd_options = Beaker::Options::OptionsHash.new

  @optparse = OptionParser.new do|opts|
    # Set a banner
    opts.banner = "Usage: #{File.basename($0)} [options...]"

    opts.on '-h', '--hosts FILE',
            'Use host configuration FILE',
            'Possible FILE values:',
            'a file path (beaker will parse file directly)',
            'a beaker-hostgenerator string (BHG generates hosts file)',
            'omitted (coordinator-only run; no SUTs provisioned)' do |file|
      @cmd_options[:hosts_file] = file
    end

    opts.on '-o', '--options-file FILE',
            'Read options from FILE',
            'This should evaluate to a ruby hash.',
            'CLI optons are given precedence.' do |file|
      @cmd_options[:options_file] =  file
    end

    opts.on '--helper PATH/TO/SCRIPT',
            'Ruby file evaluated prior to tests',
            '(a la spec_helper)' do |script|
      @cmd_options[:helper] = script
    end

    opts.on  '--load-path /PATH/TO/DIR,/ADDITIONAL/DIR/PATHS',
             'Add paths to LOAD_PATH'  do |value|
      @cmd_options[:load_path] = value
    end

    opts.on  '-t', '--tests /PATH/TO/DIR,/ADDITIONA/DIR/PATHS,/PATH/TO/FILE.rb',
             'Execute tests from paths and files' do |value|
      @cmd_options[:tests] = value
    end

    opts.on '--pre-suite /PRE-SUITE/DIR/PATH,/ADDITIONAL/DIR/PATHS,/PATH/TO/FILE.rb',
            'Path to project specific steps to be run BEFORE testing' do |value|
      @cmd_options[:pre_suite] = value
    end

    opts.on '--post-suite /POST-SUITE/DIR/PATH,/OPTIONAL/ADDITONAL/DIR/PATHS,/PATH/TO/FILE.rb',
            'Path to project specific steps to be run AFTER testing' do |value|
      @cmd_options[:post_suite] = value
    end

    opts.on '--pre-cleanup /PRE-CLEANUP/DIR/PATH,/OPTIONAL/ADDITONAL/DIR/PATHS,/PATH/TO/FILE.rb',
            'Path to project specific steps to be run before cleaning up VMs (will always run)' do |value|
      @cmd_options[:pre_cleanup] = value
    end

    opts.on '--preserve-state',
            'Preserve the state of the host vm hash for a beaker exec run',
            'This adds any additional host settings that are defined',
            'during a beaker subcommand run to .beaker/subcommand_options.yaml,',
            'allowing us to preserve state across beaker subcommand runs.' do |bool|
      @cmd_options[:preserve_state] = bool
    end

    opts.on '--[no-]provision',
            'Do not provision vm images before testing',
            '(default: true)' do |bool|
      @cmd_options[:provision] = bool
      unless bool
        @cmd_options[:validate]  = false
        @cmd_options[:configure] = false
      end
    end

    opts.on '--[no-]configure',
            'Do not configure vm images before testing',
            '(default: true)' do |bool|
      @cmd_options[:configure] = bool
    end

    opts.on '--preserve-hosts [MODE]',
            'How should SUTs be treated post test',
            'Possible values:',
            'always (keep SUTs alive)',
            'onfail (keep SUTs alive if failures occur during testing)',
            'onpass (keep SUTs alive if no failures occur during testing)',
            'never (cleanup SUTs - shutdown and destroy any changes made during testing)',
            '(default: never)'  do |mode|
      @cmd_options[:preserve_hosts] = mode || 'always'
    end

    opts.on '--debug-errors',
            'Enter a pry console if or when a test fails',
            '(default: false)' do |bool|
      @cmd_options[:debug_errors] = bool
    end

    opts.on '--exec-manual-tests',
            'Execute manual tests',
            '(default: false)' do |bool|
      @cmd_options[:exec_manual_tests] = bool
    end

    opts.on '--root-keys',
            'Install puppetlabs pubkeys for superuser',
            '(default: false)' do |bool|
      @cmd_options[:root_keys] = bool
    end

    opts.on '--keyfile /PATH/TO/SSH/KEY',
            'Specify alternate SSH key',
            '(default: ~/.ssh/id_rsa)' do |key|
      @cmd_options[:keyfile] = key
    end

    opts.on '--timeout TIMEOUT',
            '(vCloud only) Specify a provisioning timeout (in seconds)',
            '(default: 300)' do |value|
      @cmd_options[:timeout] = value
    end

    opts.on '-i URI', '--install URI',
            'Install a project repo/app on the SUTs',
            'Provide full git URI or use short form KEYWORD/name',
            'supported keywords: PUPPET, FACTER, HIERA, HIERA-PUPPET' do |value|
      @cmd_options[:install] = value
    end

    opts.on('-m', '--modules URI', 'Select puppet module git install URI') do |value|
      @cmd_options[:modules] = value
    end

    opts.on '-q', '--[no-]quiet',
            'Do not log output to STDOUT',
            '(default: false)' do |bool|
      @cmd_options[:quiet] = bool
    end

    opts.on '--[no-]color',
            'Do not display color in log output',
            '(default: true)' do |bool|
      @cmd_options[:color] = bool
    end

    opts.on '--[no-]color-host-output',
            'Ensure SUT colored output is preserved',
            '(default: false)' do |bool|
      @cmd_options[:color_host_output] = bool
      if bool
        @cmd_options[:color_host_output] = true
      end
    end

    opts.on '--log-level LEVEL',
            'Log level',
            'Supported LEVEL keywords:',
            'trace   : all messages, full stack trace of errors, file copy details',
            'debug   : all messages, plus full stack trace of errors',
            'verbose : all messages',
            'info    : info messages, notifications and warnings',
            'notify  : notifications and warnings',
            'warn    : warnings only',
            '(default: info)' do |val|
      @cmd_options[:log_level] = val
    end

    opts.on '--log-prefix PREFIX',
            'Use a custom prefix for your Beaker log files',
            'can provide nested directories (ie. face/man)',
            '(defaults to hostfile name. ie. ../i/07.yml --> "07")' do |val|
      @cmd_options[:log_prefix] = val
    end

    opts.on  '-d', '--[no-]dry-run',
             'Report what would happen on targets',
             '(default: false)' do |bool|
      @cmd_options[:dry_run] = bool
    end

    opts.on '--fail-mode [MODE]',
            'How should the harness react to errors/failures',
            'Possible values:',
            'fast (skip all subsequent tests)',
            'slow (attempt to continue run post test failure)',
            'stop (DEPRECATED, please use fast)',
            '(default: slow)'  do |mode|
      @cmd_options[:fail_mode] = mode =~ /stop/ ? 'fast' :  mode
    end

    opts.on '--test-results-file /FILE/TO/SAVE/TO.rb',
            'Path to persist errors/failures from the current run to',
            '(default: not saved)' do |value|
      @cmd_options[:test_results_file] = value
    end

    opts.on '--[no-]ntp',
            'Sync time on SUTs before testing',
            '(default: false)' do |bool|
      @cmd_options[:timesync] = bool
    end

    opts.on '--repo-proxy',
            'Proxy packaging repositories on ubuntu, debian, cumulus and solaris-11',
            '(default: false)' do
      @cmd_options[:repo_proxy] = true
    end

    opts.on '--add-el-extras',
            'Add Extra Packages for Enterprise Linux (EPEL) repository to el-* hosts',
            '(default: false)' do
      @cmd_options[:add_el_extras] = true
    end

    opts.on '--package-proxy URL', 'Set proxy url for package managers (yum and apt)' do |value|
      @cmd_options[:package_proxy] = value
    end

    opts.on '--[no-]validate',
            'Validate that SUTs are correctly provisioned before running tests',
            '(default: true)' do |bool|
      @cmd_options[:validate] = bool
    end

    opts.on '--collect-perf-data [MODE]',
            'Collect SUT performance and load data',
            'Possible values:',
            'aggressive (poll every minute)',
            'normal (poll every 10 minutes)',
            'none (do not collect perf data)',
            '(default: normal)' do |mode|
      @cmd_options[:collect_perf_data] = mode || 'normal'
    end

    opts.on('--version', 'Report currently running version of beaker' ) do
      @cmd_options[:beaker_version_print] = true
    end

    opts.on('--parse-only', 'Display beaker parsed options and exit' ) do
      @cmd_options[:parse_only] = true
    end

    opts.on('--help', 'Display this screen' ) do
      @cmd_options[:help] = true
    end

    opts.on '-c', '--config FILE',
            'DEPRECATED, use --hosts' do |file|
      @cmd_options[:hosts_file] = file
    end

    opts.on '--[no-]debug',
            'DEPRECATED, use --log-level' do |bool|
      @cmd_options[:log_level] =  bool ? 'debug' : 'info'
    end

    opts.on '-x', '--[no-]xml',
            'DEPRECATED - JUnit XML now generated by default' do
      #noop
    end

    opts.on '--type TYPE',
            'DEPRECATED - pe/foss/aio determined during runtime' do |type|
      #backwards compatability, oh how i hate you
      @cmd_options[:type] = type
    end

    opts.on '--tag TAGS',
            'DEPRECATED - use --test-tag-and instead' do |value|
      @cmd_options[:test_tag_and] = value
    end
    opts.on '--test-tag-and TAGS',
            'Run the set of tests matching ALL of the provided single or comma separated list of tags' do |value|
      @cmd_options[:test_tag_and] = value
    end

    opts.on '--test-tag-or TAGS',
            'Run the set of tests matching ANY of the provided single or comma separated list of tags' do |value|
      @cmd_options[:test_tag_or] = value
    end

    opts.on '--exclude-tag TAGS',
            'DEPRECATED - use --test-tag-exclude instead' do |value|
      @cmd_options[:test_tag_exclude] = value
    end
    opts.on '--test-tag-exclude TAGS',
            'Run the set of tests that do not contain ANY of the provided single or comma separated list of tags' do |value|
      @cmd_options[:test_tag_exclude] = value
    end

    opts.on '--xml-time-order',
            'Output an additional JUnit XML file, sorted by execution time' do |bool|
      @cmd_options[:xml_time_enabled] = bool
    end

  end

end

Instance Method Details

#parse(args = ARGV) ⇒ Hash

Parse an array of arguments into a Hash of options

Examples:

args = ['--option', 'value', '--option2', 'value2', '--switch']
parser = CommandLineParser.new
parser.parse(args) == {:option => 'value, :options2 => value, :switch => true}

Parameters:

  • args (Array) (defaults to: ARGV)

    The array of arguments to consume

Returns:

  • (Hash)

    Return the Hash of options



315
316
317
318
# File 'lib/beaker/options/command_line_parser.rb', line 315

def parse( args = ARGV )
  @optparse.parse(args)
  @cmd_options
end

#usageString

Generate a string representing the supported arguments

Examples:

parser = CommandLineParser.new
parser.usage = "Options:  ..."

Returns:

  • (String)

    Return a string representing the available arguments



327
328
329
# File 'lib/beaker/options/command_line_parser.rb', line 327

def usage
  @optparse.help
end