|
| 1 | +require_migration |
| 2 | + |
| 3 | +describe AddImageNameToOperatingSystems do |
| 4 | + let(:host_stub) { migration_stub(:Host) } |
| 5 | + let(:hardware_stub) { migration_stub(:Hardware) } |
| 6 | + let(:vm_or_template_stub) { migration_stub(:VmOrTemplate) } |
| 7 | + let(:computer_system_stub) { migration_stub(:ComputerSystem) } |
| 8 | + let(:operating_system_stub) { migration_stub(:OperatingSystem) } |
| 9 | + |
| 10 | + # rubocop:disable Layout/SpaceInsideArrayPercentLiteral |
| 11 | + let(:test_os_values) do |
| 12 | + [ |
| 13 | + %w(an_amazing_undiscovered_os unknown), |
| 14 | + %w(centos-7 linux_centos), |
| 15 | + %w(debian-8 linux_debian), |
| 16 | + %w(opensuse-13 linux_suse), |
| 17 | + %w(sles-12 linux_suse), |
| 18 | + %w(rhel-7 linux_redhat), |
| 19 | + %w(ubuntu-15-10 linux_ubuntu), |
| 20 | + %w(windows-2012-r2 windows_generic), |
| 21 | + %w(vmnix-x86 linux_esx), |
| 22 | + %w(vista windows_generic), |
| 23 | + %w(coreos-cloud linux_coreos) |
| 24 | + ] |
| 25 | + end |
| 26 | + # rubocop:enable Layout/SpaceInsideArrayPercentLiteral |
| 27 | + |
| 28 | + def record_with_os(klass, os_attributes = nil, record_attributes = {:name => ""}, hardware_attributes = nil) |
| 29 | + os_record = operating_system_stub.new(os_attributes) if os_attributes |
| 30 | + |
| 31 | + if klass == operating_system_stub |
| 32 | + os_record.save! |
| 33 | + os_record |
| 34 | + else |
| 35 | + record = klass.new |
| 36 | + record_attributes.each do |attr, val| |
| 37 | + record.send("#{attr}=", val) if record.respond_to?(attr) |
| 38 | + end |
| 39 | + |
| 40 | + record.operating_system = os_record |
| 41 | + record.hardware = hardware_stub.new(hardware_attributes) if hardware_attributes |
| 42 | + record.save! |
| 43 | + record |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + # Runs tests for class type to confirm they |
| 48 | + def test_for_klass(klass) |
| 49 | + begin |
| 50 | + # This callback is necessary after the migration, but fails when the |
| 51 | + # column doesn't eixst (prior to the migration). Removing it and |
| 52 | + # re-enabling it after the migration. |
| 53 | + operating_system_stub.skip_callback(:save, :before, :update_platform_and_image_name) |
| 54 | + |
| 55 | + distribution_based = [] |
| 56 | + product_type_based = [] |
| 57 | + product_name_based = [] |
| 58 | + fallback_records = [] |
| 59 | + |
| 60 | + test_os_values.each do |(value, _)| |
| 61 | + distribution_based << record_with_os(klass, :distribution => value) |
| 62 | + product_type_based << record_with_os(klass, :product_type => value) |
| 63 | + product_name_based << record_with_os(klass, :product_name => value) |
| 64 | + end |
| 65 | + |
| 66 | + # favor distribution over product_type |
| 67 | + fallback_records << record_with_os(klass, :distribution => "rhel-7", :product_type => "centos-7") |
| 68 | + # falls back to os.product_type if invalid os.distribution |
| 69 | + fallback_records << record_with_os(klass, :distribution => "undiscovered-7", :product_type => "rhel-7") |
| 70 | + # falls back to os.product_name |
| 71 | + fallback_records << record_with_os(klass, :distribution => "undiscovered-7", :product_name => "rhel-7") |
| 72 | + # falls back to hardware.guest_os |
| 73 | + fallback_records << record_with_os(klass, {:distribution => "undiscovered-7"}, {}, {:guest_os => "rhel-7"}) |
| 74 | + # falls back to Host#user_assigned_os |
| 75 | + fallback_records << record_with_os(klass, {:distribution => "undiscovered-7"}, {:user_assigned_os => "rhel-7"}) |
| 76 | + ensure |
| 77 | + # If the any of the above fails, make sure we re-enable callbacks so |
| 78 | + # subsequent specs don't fail trying to skip this callback when it |
| 79 | + # doesn't exist. |
| 80 | + operating_system_stub.set_callback(:save, :before, :update_platform_and_image_name) |
| 81 | + end |
| 82 | + |
| 83 | + migrate |
| 84 | + |
| 85 | + test_os_values.each.with_index do |(_, image_name), index| |
| 86 | + [distribution_based, product_type_based, product_name_based].each do |record_list| |
| 87 | + os_record = record_list[index] |
| 88 | + os_record.reload |
| 89 | + os_record = os_record.operating_system if os_record.respond_to?(:operating_system) |
| 90 | + |
| 91 | + expect(os_record.image_name).to eq(image_name) |
| 92 | + expect(os_record.platform).to eq(image_name.split("_").first) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + fallback_records.each(&:reload) |
| 97 | + |
| 98 | + platform, image_name = %w(linux linux_redhat) |
| 99 | + fallback_records.each.with_index do |record, index| |
| 100 | + os_record = record |
| 101 | + os_record = os_record.operating_system if os_record.respond_to?(:operating_system) |
| 102 | + |
| 103 | + # OperatingSystem records don't have a hardware relation, so this will be |
| 104 | + # a "unknown" OS |
| 105 | + platform, image_name = %w(unknown unknown) if index == 3 && klass == operating_system_stub |
| 106 | + |
| 107 | + # Both ComputerSystem and VmOrTemplate don't have :user_assigned_os, so |
| 108 | + # these will return "unknown" instead of what we (tried to) set. |
| 109 | + platform, image_name = %w(unknown unknown) if index == 4 && klass != host_stub |
| 110 | + |
| 111 | + expect(os_record.image_name).to eq(image_name) |
| 112 | + expect(os_record.platform).to eq(platform) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + migration_context :up do |
| 117 | + it "adds the columns" do |
| 118 | + before_columns = operating_system_stub.columns.map(&:name) |
| 119 | + expect(before_columns).to_not include("platform") |
| 120 | + expect(before_columns).to_not include("image_name") |
| 121 | + |
| 122 | + migrate |
| 123 | + |
| 124 | + after_columns = operating_system_stub.columns.map(&:name) |
| 125 | + expect(after_columns).to include("platform") |
| 126 | + expect(after_columns).to include("image_name") |
| 127 | + end |
| 128 | + |
| 129 | + it "updates OperatingSystem for Host records" do |
| 130 | + test_for_klass host_stub |
| 131 | + end |
| 132 | + |
| 133 | + it "updates OperatingSystem for VmOrTemplate records" do |
| 134 | + test_for_klass vm_or_template_stub |
| 135 | + end |
| 136 | + |
| 137 | + it "updates OperatingSystem for ComputerSystem records" do |
| 138 | + test_for_klass computer_system_stub |
| 139 | + end |
| 140 | + |
| 141 | + it "updates orphaned OperatingSystem records" do |
| 142 | + test_for_klass operating_system_stub |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + migration_context :down do |
| 147 | + it "adds the columns" do |
| 148 | + before_columns = operating_system_stub.columns.map(&:name) |
| 149 | + expect(before_columns).to include("platform") |
| 150 | + expect(before_columns).to include("image_name") |
| 151 | + |
| 152 | + migrate |
| 153 | + |
| 154 | + after_columns = operating_system_stub.columns.map(&:name) |
| 155 | + expect(after_columns).to_not include("platform") |
| 156 | + expect(after_columns).to_not include("image_name") |
| 157 | + end |
| 158 | + end |
| 159 | +end |
0 commit comments