|
408 | 408 | end |
409 | 409 | end |
410 | 410 | end |
| 411 | + |
| 412 | +describe OctocatalogDiff::Catalog do |
| 413 | + describe '#remove_existing_resources' do |
| 414 | + let(:catalog) do |
| 415 | + opts = { |
| 416 | + compare_file_text: false, |
| 417 | + node: 'my.rspec.node', |
| 418 | + json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json')) |
| 419 | + } |
| 420 | + OctocatalogDiff::Catalog.new(opts) |
| 421 | + end |
| 422 | + |
| 423 | + it 'should raise error if resource is not in expected format' do |
| 424 | + test_arg = ['Foo-Bar'] |
| 425 | + expect { catalog.send(:remove_existing_resources, test_arg) }.to raise_error(ArgumentError, /Resource Foo-Bar /) |
| 426 | + end |
| 427 | + |
| 428 | + it 'should return full array when no matches' do |
| 429 | + allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil) |
| 430 | + allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(nil) |
| 431 | + test_arg = ['Foo[bar]', 'Baz[biff]'] |
| 432 | + result = catalog.send(:remove_existing_resources, test_arg) |
| 433 | + expect(result).to eq(['Foo[bar]', 'Baz[biff]']) |
| 434 | + end |
| 435 | + |
| 436 | + it 'should remove matching entries' do |
| 437 | + allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(nil) |
| 438 | + allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true) |
| 439 | + test_arg = ['Foo[bar]', 'Baz[biff]'] |
| 440 | + result = catalog.send(:remove_existing_resources, test_arg) |
| 441 | + expect(result).to eq(['Foo[bar]']) |
| 442 | + end |
| 443 | + |
| 444 | + it 'should return empty array with all matches' do |
| 445 | + allow(catalog).to receive(:resource).with(type: 'Foo', title: 'bar').and_return(true) |
| 446 | + allow(catalog).to receive(:resource).with(type: 'Baz', title: 'biff').and_return(true) |
| 447 | + test_arg = ['Foo[bar]', 'Baz[biff]'] |
| 448 | + result = catalog.send(:remove_existing_resources, test_arg) |
| 449 | + expect(result).to eq([]) |
| 450 | + end |
| 451 | + end |
| 452 | + |
| 453 | + describe '#validate_references' do |
| 454 | + it 'should return nil if no reference validation is requested' do |
| 455 | + opts = { |
| 456 | + compare_file_text: false, |
| 457 | + node: 'my.rspec.node', |
| 458 | + json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json')) |
| 459 | + } |
| 460 | + catalog = OctocatalogDiff::Catalog.new(opts) |
| 461 | + result = catalog.validate_references |
| 462 | + expect(result).to be_nil |
| 463 | + end |
| 464 | + |
| 465 | + it 'should raise error if reference validation is requested' do |
| 466 | + opts = { |
| 467 | + compare_file_text: false, |
| 468 | + validate_references: %w(before notify require subscribe), |
| 469 | + node: 'my.rspec.node', |
| 470 | + json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/reference-validation-broken.json')) |
| 471 | + } |
| 472 | + catalog = OctocatalogDiff::Catalog.new(opts) |
| 473 | + error_str = [ |
| 474 | + 'Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]]', |
| 475 | + 'exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]', |
| 476 | + 'exec[subscribe caller 2] -> subscribe[Exec[subscribe target 2]]', |
| 477 | + 'exec[subscribe caller 3] -> subscribe[Exec[subscribe target]]' |
| 478 | + ].join('; ') |
| 479 | + expect { catalog.validate_references }.to raise_error(OctocatalogDiff::Catalog::ReferenceValidationError, error_str) |
| 480 | + end |
| 481 | + end |
| 482 | +end |
0 commit comments