|
| 1 | +require 'pry' |
| 2 | +# Verify if a physical volume, volume group, logical volume, or filesystem resource type is created |
| 3 | +# |
| 4 | +# ==== Attributes |
| 5 | +# |
| 6 | +# * +resource_type+ - resorce type, i.e 'physical_volume', 'volume_group', 'logical_volume', 'filesystem', |
| 7 | +# * 'aix_physical_volume', 'aix_volume_group', or 'aix_logical_volume'. |
| 8 | +# * +resource_name+ - The name of resource type, i.e '/dev/sdb' for physical volume, vg_1234 for volume group |
| 9 | +# * +vg+ - volume group name associated with logical volume (if any) |
| 10 | +# * +properties+ - a matching string or regular expression in logical volume properties |
| 11 | +# ==== Returns |
| 12 | +# |
| 13 | +# +nil+ |
| 14 | +# |
| 15 | +# ==== Raises |
| 16 | +# assert_match failure message |
| 17 | +# ==== Examples |
| 18 | +# |
| 19 | +# verify_if_created?(agent, 'physical_volume', /dev/sdb', VolumeGroup_123, "Size 7GB") |
| 20 | +def verify_if_created?(resource_type, resource_name, vg=nil, properties=nil) |
| 21 | + case resource_type |
| 22 | + when 'physical_volume' |
| 23 | + run_shell("pvdisplay") do |result| |
| 24 | + assert_match(/#{resource_name}/, result.stdout, 'Unexpected error was detected') |
| 25 | + end |
| 26 | + when 'volume_group' |
| 27 | + run_shell("vgdisplay") do |result| |
| 28 | + assert_match(/#{resource_name}/, result.stdout, 'Unexpected error was detected') |
| 29 | + end |
| 30 | + when 'logical_volume' |
| 31 | + raise ArgumentError, 'Missing volume group that the logical volume is associated with' unless vg |
| 32 | + run_shell("lvdisplay /dev/#{vg}/#{resource_name}") do |result| |
| 33 | + assert_match(/#{resource_name}/, result.stdout, 'Unexpected error was detected') |
| 34 | + if properties |
| 35 | + assert_match(/#{properties}/, result.stdout, 'Unexpected error was detected') |
| 36 | + end |
| 37 | + end |
| 38 | + when 'aix_physical_volume' |
| 39 | + run_shell("lspv #{resource_name}") do |result| |
| 40 | + assert_match(/Physical volume #{resource_name} is not assigned to/, result.stdout, 'Unexpected error was detected') |
| 41 | + end |
| 42 | + when 'aix_volume_group' |
| 43 | + run_shell("lsvg") do |result| |
| 44 | + assert_match(/#{resource_name}/, result.stdout, 'Unexpected error was detected') |
| 45 | + end |
| 46 | + when 'aix_logical_volume' |
| 47 | + raise ArgumentError, 'Missing volume group that the logical volume is associated with' unless vg |
| 48 | + run_shell("lslv #{resource_name}") do |result| |
| 49 | + assert_match(/#{resource_name}/, result.stdout, 'Unexpected error was detected') |
| 50 | + if properties |
| 51 | + assert_match(/#{properties}/, result.stdout, 'Unexpected error was detected') |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +# Clean the box after each test, make sure the newly created logical volumes, volume groups, |
| 58 | +# and physical volumes are removed at the end of each test to make the server ready for the |
| 59 | +# next test case. |
| 60 | +# |
| 61 | +# ==== Attributes |
| 62 | +# |
| 63 | +# * +pv+ - physical volume, can be one volume or an array of multiple volumes |
| 64 | +# * +vg+ - volume group, can be one group or an array of multiple volume groups |
| 65 | +# * +lv+ - logical volume, can be one volume or an array of multiple volumes |
| 66 | +# * +aix+ - if the agent is an AIX server. |
| 67 | +# |
| 68 | +# ==== Returns |
| 69 | +# |
| 70 | +# +nil+ |
| 71 | +# |
| 72 | +# ==== Raises |
| 73 | +# +nil+ |
| 74 | +# ==== Examples |
| 75 | +# |
| 76 | +# remove_all('/dev/sdb', 'VolumeGroup_1234', 'LogicalVolume_fa13') |
| 77 | +def remove_all(pv=nil, vg=nil, lv=nil, aix=false) |
| 78 | + if aix |
| 79 | + run_shell("reducevg -d -f #{vg} #{pv}") |
| 80 | + run_shell("rm -rf /dev/#{vg} /dev/#{lv}") |
| 81 | + else |
| 82 | + if lv |
| 83 | + if lv.kind_of?(Array) |
| 84 | + lv.each do |logical_volume| |
| 85 | + run_shell("umount /dev/#{vg}/#{logical_volume}", expect_failures: true) |
| 86 | + run_shell("lvremove /dev/#{vg}/#{logical_volume} --force", expect_failures: true) |
| 87 | + end |
| 88 | + else |
| 89 | + #note: in some test cases, for example, the test case 'create_vg_property_logical_volume' |
| 90 | + # the logical volume must be unmount before being able to delete it |
| 91 | + run_shell("umount /dev/#{vg}/#{lv}", expect_failures: true) |
| 92 | + run_shell("lvremove /dev/#{vg}/#{lv} --force", expect_failures: true) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + if vg |
| 97 | + if vg.kind_of?(Array) |
| 98 | + vg.each do |volume_group| |
| 99 | + run_shell("vgremove /dev/#{volume_group}") |
| 100 | + end |
| 101 | + else |
| 102 | + run_shell("vgremove /dev/#{vg}") |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + if pv |
| 107 | + if pv.kind_of?(Array) |
| 108 | + pv.each do |physical_volume| |
| 109 | + run_shell("pvremove #{physical_volume}") |
| 110 | + end |
| 111 | + else |
| 112 | + run_shell("pvremove #{pv}") |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +RSpec.configure do |c| |
| 119 | + c.before :suite do |
| 120 | + auth_tok = 'pvxejsxwstwhsy0u2tjolfovg9wfzg2e' |
| 121 | + fail_test "AUTH_TOKEN must be set" unless auth_tok |
| 122 | + machine = ENV['TARGET_HOST'] |
| 123 | + command = "curl -H X-AUTH-TOKEN:#{auth_tok} -X POST --url vcloud.delivery.puppetlabs.net/api/v1/vm/#{machine}/disk/1" |
| 124 | + fdisk = run_shell('fdisk -l').stdout |
| 125 | + if fdisk !~ /sdb/ |
| 126 | + stdout, _stderr, _status = Open3.capture3(command) |
| 127 | + sleep(30) |
| 128 | + run_shell("echo \"- - -\" >/sys/class/scsi_host/host2/scan") |
| 129 | + end |
| 130 | + if fdisk !~ /sdc/ |
| 131 | + stdout, _stderr, _status = Open3.capture3(command) |
| 132 | + sleep(30) |
| 133 | + run_shell("echo \"- - -\" >/sys/class/scsi_host/host2/scan") |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments