How to use fixture method of ConfigTestHelper Package

Best Inspec_ruby code snippet using ConfigTestHelper.fixture

config_test.rb

Source:config_test.rb Github

copy

Full Screen

...44 # File Validation45 # ========================================================================== #46 describe 'when validating a file' do47 let(:cfg) { Inspec::Config.new({}, cfg_io) }48 let(:cfg_io) { StringIO.new(ConfigTestHelper.fixture(fixture_name)) }49 let(:seen_fields) { cfg.final_options.keys.sort }50 describe 'when the file is a legacy file' do51 let(:fixture_name) { 'legacy' }52 it 'should read the file successfully' do53 expected = ['color', 'reporter', 'target_id', 'type'].sort54 seen_fields.must_equal expected55 end56 end57 describe 'when the file is a valid v1.1 file' do58 let(:fixture_name) { 'basic' }59 it 'should read the file successfully' do60 expected = ['create_lockfile', 'reporter', 'type'].sort61 seen_fields.must_equal expected62 end63 end64 describe 'when the file is minimal' do65 let(:fixture_name) { 'minimal' }66 it 'should read the file successfully' do67 expected = ['reporter', 'type'].sort68 seen_fields.must_equal expected69 end70 end71 describe 'when the file has malformed json' do72 let(:fixture_name) { 'malformed_json' }73 it 'should throw an exception' do74 ex = proc { cfg }.must_raise(Inspec::ConfigError::MalformedJson)75 # Failed to load JSON configuration: 765: unexpected token at '{ "hot_garbage": "a", "version": "1.1",76 # '77 # Config was: "{ \"hot_garbage\": \"a\", \"version\": \"1.1\", \n"78 ex.message.must_include 'Failed to load JSON config' # The message79 ex.message.must_include 'unexpected token' # The specific parser error80 ex.message.must_include 'hot_garbage' # A sample of the unacceptable contents81 end82 end83 describe 'when the file has a bad file version' do84 let(:fixture_name) { 'bad_version' }85 it 'should throw an exception' do86 ex = proc { cfg }.must_raise(Inspec::ConfigError::Invalid)87 ex.message.must_include 'Unsupported config file version'88 ex.message.must_include '99.99'89 ex.message.must_include '1.1'90 end91 end92 describe 'when a 1.1 file has an invalid top-level entry' do93 let(:fixture_name) { 'bad_top_level' }94 it 'should throw an exception' do95 ex = proc { cfg }.must_raise(Inspec::ConfigError::Invalid)96 ex.message.must_include 'Unrecognized top-level'97 ex.message.must_include 'unsupported_field'98 ex.message.must_include 'compliance'99 end100 end101 end102 # ========================================================================== #103 # Defaults104 # ========================================================================== #105 describe 'reading defaults' do106 let(:cfg) { Inspec::Config.new({}, nil, command) }107 let(:final_options) { cfg.final_options }108 let(:seen_fields) { cfg.final_options.keys.sort }109 describe 'when the exec command is used' do110 let(:command) { :exec }111 it 'should have the correct defaults' do112 expected = ['color', 'create_lockfile', 'backend_cache', 'reporter', 'show_progress', 'type'].sort113 seen_fields.must_equal expected114 final_options['reporter'].must_be_kind_of Hash115 final_options['reporter'].count.must_equal 1116 final_options['reporter'].keys.must_include 'cli'117 final_options['show_progress'].must_equal false118 final_options['color'].must_equal true119 final_options['create_lockfile'].must_equal true120 final_options['backend_cache'].must_equal true121 end122 end123 describe 'when the shell command is used' do124 let(:command) { :shell }125 it 'should have the correct defaults' do126 expected = ['reporter', 'type'].sort127 seen_fields.must_equal expected128 final_options['reporter'].must_be_kind_of Hash129 final_options['reporter'].count.must_equal 1130 final_options['reporter'].keys.must_include 'cli'131 end132 end133 end134 # ========================================================================== #135 # Reading CLI Options136 # ========================================================================== #137 # The config facility supports passing in CLI options in the constructor, so138 # that it can handle merging internally. That is tested here.139 #140 # This is different than storing options141 # in the config file with the same name as the CLI options, which is142 # tested under 'CLI Options Stored in File'143 describe 'reading CLI options' do144 let(:cfg) { Inspec::Config.new(cli_opts) }145 let(:final_options) { cfg.final_options }146 let(:seen_fields) { cfg.final_options.keys.sort }147 describe 'when the CLI opts are present' do148 let(:cli_opts) do149 {150 color: true,151 'string_key' => 'string_value',152 array_value: [1,2,3],153 }154 end155 it 'should transparently round-trip the options' do156 expected = ['color', 'array_value', 'reporter', 'string_key', 'type'].sort157 seen_fields.must_equal expected158 final_options[:color].must_equal true159 final_options['color'].must_equal true160 final_options['string_key'].must_equal 'string_value'161 final_options[:string_key].must_equal 'string_value'162 final_options['array_value'].must_equal [1,2,3]163 final_options[:array_value].must_equal [1,2,3]164 end165 end166 end167 # ========================================================================== #168 # CLI Options Stored in File169 # ========================================================================== #170 describe 'reading CLI options stored in the config file' do171 let(:cfg) { Inspec::Config.new({}, cfg_io) }172 let(:final_options) { cfg.final_options }173 let(:cfg_io) { StringIO.new(ConfigTestHelper.fixture(fixture_name)) }174 let(:seen_fields) { cfg.final_options.keys.sort }175 # These two test cases have the same options but in different file versions.176 describe 'when the CLI opts are present in a 1.1 file' do177 let(:fixture_name) { :like_legacy }178 it 'should read the options' do179 expected = ['color', 'reporter', 'target_id', 'type'].sort180 seen_fields.must_equal expected181 final_options['color'].must_equal "true" # Dubious - should this be String or TrueClass?182 final_options['target_id'].must_equal 'mynode'183 end184 end185 describe 'when the CLI opts are present in a legacy file' do186 let(:fixture_name) { :legacy }187 it 'should read the options' do188 expected = ['color', 'reporter', 'target_id', 'type'].sort189 seen_fields.must_equal expected190 final_options['color'].must_equal "true" # Dubious - should this be String or TrueClass?191 final_options['target_id'].must_equal 'mynode'192 end193 end194 end195 # ========================================================================== #196 # Parsing and Validating Reporters197 # ========================================================================== #198 # TODO: this should be moved into plugins for the reporters199 describe 'when parsing reporters' do200 let(:cfg) { Inspec::Config.new(cli_opts) }201 let(:seen_reporters) { cfg['reporter'] }202 describe 'when paring CLI reporter' do203 let(:cli_opts) { { 'reporter' => ['cli'] } }204 it 'parse cli reporters' do205 expected_value = { 'cli' => { 'stdout' => true }}206 seen_reporters.must_equal expected_value207 end208 end209 describe 'when paring CLI reporter' do210 let(:cli_opts) { { 'reporter' => ['cli'], 'target_id' => '1d3e399f-4d71-4863-ac54-84d437fbc444' } }211 it 'parses cli report and attaches target_id' do212 expected_value = {"cli"=>{"stdout"=>true, "target_id"=>"1d3e399f-4d71-4863-ac54-84d437fbc444"}}213 seen_reporters.must_equal expected_value214 end215 end216 end217 describe 'when validating reporters' do218 # validate_reporters is private, so we use .send219 let(:cfg) { Inspec::Config.new }220 it 'valid reporter' do221 reporters = { 'json' => { 'stdout' => true } }222 cfg.send(:validate_reporters!, reporters)223 end224 it 'invalid reporter type' do225 reporters = ['json', 'magenta']226 proc { cfg.send(:validate_reporters!, reporters) }.must_raise NotImplementedError227 end228 it 'two reporters outputting to stdout' do229 stdout = { 'stdout' => true }230 reporters = { 'json' => stdout, 'cli' => stdout }231 proc { cfg.send(:validate_reporters!, reporters) }.must_raise ArgumentError232 end233 end234 # ========================================================================== #235 # Miscellaneous Option Finalization236 # ========================================================================== #237 describe 'option finalization' do238 it 'raises if `--password/--sudo-password` are used without value' do239 # When you invoke `inspec shell --password` (with no value for password,240 # though it is setup to expect a string) Thor will set the key with value -1241 ex = proc { Inspec::Config.new({'sudo_password' => -1}) }.must_raise(ArgumentError)242 ex.message.must_match(/Please provide a value for --sudo-password/)243 end244 it 'assumes `--sudo` if `--sudo-password` is used without it' do245 @mock_logger = Minitest::Mock.new246 @mock_logger.expect(:warn, nil, [/Adding `--sudo`./])247 Inspec::Log.stub :warn, proc { |message| @mock_logger.warn(message) } do248 cfg = Inspec::Config.new('sudo_password' => 'somepass')249 cfg.key?('sudo').must_equal true250 end251 @mock_logger.verify252 end253 it 'calls `Compliance::API.login` if `opts[:compliance] is passed`' do254 InspecPlugins::Compliance::API.expects(:login)255 cfg_io = StringIO.new(ConfigTestHelper.fixture('with_compliance'))256 Inspec::Config.new({ backend: 'mock' }, cfg_io)257 end258 end259 # ========================================================================== #260 # Fetching Credentials261 # ========================================================================== #262 describe 'when fetching creds' do263 let(:cfg) { Inspec::Config.new(cli_opts, cfg_io) }264 let(:cfg_io) { StringIO.new(ConfigTestHelper.fixture(file_fixture_name)) }265 let(:seen_fields) { creds.keys.sort }266 let(:creds) { cfg.unpack_train_credentials }267 describe 'when generic creds are present on the cli' do268 let(:cfg_io) { nil }269 let(:cli_opts) { { sudo: true, 'shell_command': 'ksh' } }270 it 'should pass the credentials as-is' do271 expected = [:backend, :sudo, :shell_command].sort272 seen_fields.must_equal expected273 creds[:sudo].must_equal true274 creds[:shell_command].must_equal 'ksh'275 creds[:backend].must_equal 'local' # Checking for default276 end277 end278 describe 'when creds are specified on the CLI with a backend and transport prefixes' do279 let(:cfg_io) { nil }280 let(:cli_opts) { { backend: 'ssh', ssh_host: 'example.com', ssh_key_files: 'mykey' } }281 it 'should read the backend and strip prefixes' do282 expected = [:backend, :host, :key_files].sort283 seen_fields.must_equal expected284 creds[:backend].must_equal 'ssh'285 creds[:host].must_equal 'example.com'286 creds[:key_files].must_equal 'mykey'287 end288 end289 describe 'when creds are specified with a credset target_uri in a 1.1 file without transport prefixes' do290 let(:file_fixture_name) { :basic }291 let(:cli_opts) { { target: 'ssh://set1' }}292 it 'should use the credset to lookup the creds in the file' do293 expected = [:backend, :host, :user].sort294 seen_fields.must_equal expected295 creds[:backend].must_equal 'ssh'296 creds[:host].must_equal 'some.host'297 creds[:user].must_equal 'some_user'298 end299 end300 describe 'when creds are specified with a credset that contains odd characters' do301 let(:file_fixture_name) { :match_checks_in_credset_names }302 [303 'ssh://TitleCase',304 'ssh://snake_case',305 'ssh://conta1nsnumeral5',306 ].each do |target_uri|307 it "should be able to unpack #{target_uri}" do308 # let() caching breaks things here309 cfg_io = StringIO.new(ConfigTestHelper.fixture(file_fixture_name))310 cli_opts = { target: target_uri }311 cfg = Inspec::Config.new({ target: target_uri }, cfg_io)312 creds = cfg.unpack_train_credentials313 creds.count.must_equal 2314 creds[:backend].must_equal 'ssh'315 creds[:found].must_equal 'yes'316 end317 end318 [319 'ssh://contains.dots',320 ].each do |target_uri|321 it "should handoff unpacking #{target_uri} to train" do322 # let() caching breaks things here323 cfg_io = StringIO.new(ConfigTestHelper.fixture(file_fixture_name))324 cfg = Inspec::Config.new({ target: target_uri }, cfg_io)325 creds = cfg.unpack_train_credentials326 creds.count.must_equal 2327 creds[:backend].must_equal 'ssh'328 creds[:host].must_equal 'contains.dots'329 end330 end331 [332 'ssh://contains spaces',333 ].each do |target_uri|334 it "should be not able to unpack #{target_uri}" do335 # let() caching breaks things here336 cfg_io = StringIO.new(ConfigTestHelper.fixture(file_fixture_name))337 cfg = Inspec::Config.new({ target: target_uri }, cfg_io)338 assert_raises(Train::UserError) { creds = cfg.unpack_train_credentials }339 end340 end341 end342 describe 'when creds are specified with a credset target_uri in a 1.1 file and a prefixed override on the CLI' do343 let(:file_fixture_name) { :basic }344 let(:cli_opts) { { target: 'ssh://set1', ssh_user: 'bob' } }345 it 'should use the credset to lookup the creds in the file then override the single value' do346 expected = [:backend, :host, :user].sort347 seen_fields.must_equal expected348 creds[:backend].must_equal 'ssh'349 creds[:host].must_equal 'some.host'350 creds[:user].must_equal 'bob'351 end352 end353 describe 'when creds are specified with a non-credset target_uri' do354 let(:cfg_io) { nil }355 let(:cli_opts) { { target: 'ssh://bob@somehost' } }356 it 'should unpack the options using the URI parser' do357 expected = [:backend, :host, :user].sort358 seen_fields.must_equal expected359 creds[:backend].must_equal 'ssh'360 creds[:host].must_equal 'somehost'361 creds[:user].must_equal 'bob'362 end363 end364 describe 'when backcompat creds are specified on the CLI without a transport prefix' do365 let(:cfg_io) { nil }366 let(:cli_opts) { { target: 'ssh://some.host', user: 'bob' } }367 it 'should assign the options correctly' do368 expected = [:backend, :host, :user].sort369 seen_fields.must_equal expected370 creds[:backend].must_equal 'ssh'371 creds[:host].must_equal 'some.host'372 creds[:user].must_equal 'bob'373 end374 end375 end376 # ========================================================================== #377 # Merging Options378 # ========================================================================== #379 describe 'when merging options' do380 let(:cfg) { Inspec::Config.new(cli_opts, cfg_io, command) }381 let(:cfg_io) { StringIO.new(ConfigTestHelper.fixture(file_fixture_name)) }382 let(:seen_fields) { cfg.final_options.keys.sort }383 let(:command) { nil }384 describe 'when there is both a default and a config file setting' do385 let(:file_fixture_name) { :override_check }386 let(:cli_opts) { {} }387 it 'the config file setting should prevail' do388 Inspec::Config::Defaults.stubs(:default_for_command).returns('target_id'=> 'value_from_default')389 expected = ['reporter', 'target_id', 'type'].sort390 seen_fields.must_equal expected391 cfg.final_options['target_id'].must_equal 'value_from_config_file'392 cfg.final_options[:target_id].must_equal 'value_from_config_file'393 end394 end395 describe 'when there is both a default and a CLI option' do396 let(:cli_opts) { { target_id: 'value_from_cli_opts' } }397 let(:cfg_io) { nil }398 it 'the CLI option should prevail' do399 Inspec::Config::Defaults.stubs(:default_for_command).returns('target_id'=> 'value_from_default')400 expected = ['reporter', 'target_id', 'type'].sort401 seen_fields.must_equal expected402 cfg.final_options['target_id'].must_equal 'value_from_cli_opts'403 cfg.final_options[:target_id].must_equal 'value_from_cli_opts'404 end405 end406 describe 'when there is both a config file setting and a CLI option' do407 let(:file_fixture_name) { :override_check }408 let(:cli_opts) { { target_id: 'value_from_cli_opts' } }409 it 'the CLI option should prevail' do410 expected = ['reporter', 'target_id', 'type'].sort411 seen_fields.must_equal expected412 cfg.final_options['target_id'].must_equal 'value_from_cli_opts'413 cfg.final_options[:target_id].must_equal 'value_from_cli_opts'414 end415 end416 describe 'specifically check default vs config file override for "reporter" setting' do417 let(:cli_opts) { {} }418 let(:command) { :shell } # shell default is [ :cli ]419 let(:file_fixture_name) { :override_check } # This fixture sets the cfg file contents to request a json reporter420 it 'the config file setting should prevail' do421 expected = ['reporter', 'target_id', 'type'].sort422 seen_fields.must_equal expected423 cfg.final_options['reporter'].must_be_kind_of Hash424 cfg.final_options['reporter'].keys.must_equal ['json']425 cfg.final_options['reporter']['json']['path'].must_equal 'path/from/config/file'426 cfg.final_options[:reporter].must_be_kind_of Hash427 cfg.final_options[:reporter].keys.must_equal ['json']428 cfg.final_options[:reporter]['json']['path'].must_equal 'path/from/config/file'429 end430 end431 end432end433# ========================================================================== #434# Test Fixtures435# ========================================================================== #436module ConfigTestHelper437 def fixture(fixture_name)438 case fixture_name.to_sym439 when :legacy440 # TODO - this is dubious, but based on https://www.inspec.io/docs/reference/reporters/#automate-reporter441 # Things that have 'compliance' as a toplevel have also been seen442 <<~EOJ1443 {444 "color": "true",445 "target_id": "mynode",446 "reporter": {447 "automate" : {448 "url" : "https://YOUR_A2_URL/data-collector/v0/",449 "token" : "YOUR_A2_ADMIN_TOKEN"450 }451 }452 }453 EOJ1454 when :basic455 <<~EOJ2456 {457 "version": "1.1",458 "cli_options": {459 "create_lockfile": "false"460 },461 "reporter": {462 "automate" : {463 "url": "http://some.where",464 "token" : "YOUR_A2_ADMIN_TOKEN"465 }466 },467 "credentials": {468 "ssh": {469 "set1": {470 "host": "some.host",471 "user": "some_user"472 }473 }474 }475 }476 EOJ2477 when :like_legacy478 <<~EOJ3479 {480 "version": "1.1",481 "cli_options": {482 "color": "true",483 "target_id": "mynode"484 },485 "reporter": {486 "automate" : {487 "url" : "https://YOUR_A2_URL/data-collector/v0/",488 "token" : "YOUR_A2_ADMIN_TOKEN"489 }490 }491 }492 EOJ3493 when :override_check494 <<~EOJ4495 {496 "version": "1.1",497 "cli_options": {498 "target_id": "value_from_config_file"499 },500 "reporter": {501 "json": {502 "path": "path/from/config/file"503 }504 }505 }506 EOJ4507 when :minimal508 '{ "version": "1.1" }'509 when :bad_version510 '{ "version": "99.99" }'511 when :bad_top_level512 '{ "version": "1.1", "unsupported_field": "some_value" }'513 when :malformed_json514 '{ "hot_garbage": "a", "version": "1.1", '515 when :with_compliance516 # TODO - this is dubious, need to verify517 <<~EOJ5518 {519 "compliance": {520 "server":"https://some.host",521 "user":"someuser"522 }523 }524 EOJ5525 when :match_checks_in_credset_names526 <<~EOJ6527 {528 "version": "1.1",529 "credentials": {530 "ssh": {531 "TitleCase": {532 "found": "yes"533 },534 "snake_case": {535 "found": "yes"536 },537 "conta1nsnumeral5": {538 "found": "yes"539 },540 "contains.dots": {541 "found": "no"542 },543 "contains spaces": {544 "found": "no"545 }546 }547 }548 }549 EOJ6550 end551 end552 module_function :fixture553end...

Full Screen

Full Screen

fixture

Using AI Code Generation

copy

Full Screen

1 def fixture(name)2 File.join(File.dirname(__FILE__), 'fixtures', name)3 def fixture(name)4 File.join(File.dirname(__FILE__), 'fixtures', name)5 def fixture(name)6 File.join(File.dirname(__FILE__), 'fixtures', name)7 def fixture(name)8 File.join(File.dirname(__FILE__), 'fixtures', name)

Full Screen

Full Screen

fixture

Using AI Code Generation

copy

Full Screen

1 config = fixture('config.yml')2 def fixture(file)3 YAML.load_file(File.join(File.dirname(__FILE__), 'fixtures', file))4def fixture(file)5 YAML.load_file(File.join(File.dirname(__FILE__), '..', 'fixtures', file))6def fixture(file)7 YAML.load_file(File.join(File.dirname(__FILE__), '../../fixtures', file))8def fixture(file)9 YAML.load_file(File.join(File.dirname(__FILE__), '../../../fixtures', file))10def fixture(file)11 YAML.load_file(File.join(File.dirname(__FILE__), '../../../../fixtures', file))12def fixture(file)13 YAML.load_file(File.join(File.dirname(__FILE__), '../../../../../fixtures', file))14def fixture(file)15 YAML.load_file(File.join(File.dirname(__FILE__), '../../../../../../fixtures', file))

Full Screen

Full Screen

fixture

Using AI Code Generation

copy

Full Screen

1 @config = ConfigTestHelper::fixture('config.yml')2 def self.fixture(name)3 YAML.load_file(File.dirname(__FILE__) + '/fixtures/' + name)

Full Screen

Full Screen

fixture

Using AI Code Generation

copy

Full Screen

1 @config = ConfigTestHelper::fixture('config.yml')2 def self.fixture(name)3 YAML.load_file(File.dirname(__FILE__) + '/fixtures/' + name)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Inspec_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful