From 93c3a220f46e40b42457f96c36a8c4d5b1df9fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Ezr?= Date: Thu, 18 Apr 2019 19:27:04 +0200 Subject: [PATCH] present recording instance config file, improve cassette testing docs --- .gitignore | 1 + CONTRIBUTING.md | 47 +++++++- README.md | 1 + .../6_7/get_vm_first_scsi_controller.yml | 111 ++++++++++++++++++ .../get_vm_first_scsi_controller_tests.rb | 19 +++ tests/test_helper.rb | 73 +++++++----- 6 files changed, 220 insertions(+), 32 deletions(-) create mode 100644 tests/fixtures/vcr_cassettes/6_7/get_vm_first_scsi_controller.yml create mode 100644 tests/requests/compute/get_vm_first_scsi_controller_tests.rb diff --git a/.gitignore b/.gitignore index 6cf002d4..c083a6d6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ tmp mkmf.log gemfiles/*.lock .*.swp +tests/vsphere_config.yml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9f943315..6d23e6a1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,18 +1,55 @@ # Getting Involved -New contributors are always welcome, when it doubt please ask questions. We strive to be an open and welcoming community. Please be nice to one another. +:+1::tada: First off, thanks for taking the time to contribute! :tada::+1: + +New contributors are always welcome, when it doubt please ask questions. +We strive to be an open and welcoming community. Please be nice to one another. + +## Non-Coding + +- Offer feedback on open [issues](https://github.com/fog/fog-vsphere/issues). +- Organize or volunteer at events. ## Coding - Pick a task: -- Offer feedback on open [pull requests](https://github.com/fog/fog-vsphere/pulls). - Review open [issues](https://github.com/fog/fog-vsphere/issues) for things to help on. - [Create an issue](https://github.com/fog/fog-vsphere/issues/new) to start a discussion on additions or features. - Fork the project, add your changes and tests to cover them in a topic branch. - Commit your changes and rebase against `fog/fog-vsphere` to ensure everything is up to date. - [Submit a pull request](https://github.com/fog/fog-vsphere/compare/) -## Non-Coding +### Testing -- Offer feedback on open [issues](https://github.com/fog/fog-vsphere/issues). -- Organize or volunteer at events. +We are in a process of a covering the functionality by integrational tests. +We would like to cover all the requests made towards the vSphere instance. +Doing so should give us more confidence and make the development easier. +We will appreciate if you are able to help. Following steps will guide you +through a process of covering new/changed funcionality. + +1. First of all you will need vSphere instance. +2. You need to add your vSphere instance login info in `tests/vsphere_config.yml` file. +```yaml +server: example.com +username: '' +password: '' +rev: '6.7' # optional revision of the vsphere +expected_pubkey_hash: +``` +*This file is gitignored to ensure you won't accidently commit it.* +*We are making sure, that the login request is not recorded, but you may want to reasure it in a newly written cassette.* + +3. write your test like this: +```ruby +# tests/requests/compute/ +it 'parses the mocked request' do + recording_webmock_cassette('unique_mocked_request') do + #stuff I want to get tested + end +end +``` +*But be aware, we do not mock the names of your datacenters and/or clusters, please be aware, you will need to take care not to commit any private server names/IP addresses.* + +4. Run the test +5. Change the `recording_webmock_cassette` to a `with_webmock_cassette` +6. Commit, open a PR :tada: diff --git a/README.md b/README.md index 9b297d71..eb044bae 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ There is a lot more you can do as well! We are working on providing better docum ## Contributing To contribute to this project, add an issue or pull request. For more info on what that means or how to do it, see [this guide](https://guides.github.com/activities/contributing-to-open-source/#contributing) from GitHub. +For more details see [CONTRIBUTING.md file](CONTRIBUTING.md) [climate-image]: https://codeclimate.com/github/fog/fog-vsphere.svg [climate-url]: https://codeclimate.com/github/fog/fog-vsphere diff --git a/tests/fixtures/vcr_cassettes/6_7/get_vm_first_scsi_controller.yml b/tests/fixtures/vcr_cassettes/6_7/get_vm_first_scsi_controller.yml new file mode 100644 index 00000000..4e6c2cd6 --- /dev/null +++ b/tests/fixtures/vcr_cassettes/6_7/get_vm_first_scsi_controller.yml @@ -0,0 +1,111 @@ +--- +http_interactions: +- request: + method: post + uri: https://<%= vsphere_server %>/sdk + body: + encoding: UTF-8 + string: <_this type="SearchIndex">SearchIndex52d810bd-077b-368d-a86f-0b2ad84269f811 + headers: + Content-Type: + - text/xml; charset=utf-8 + Soapaction: + - urn:vim25/6.7 + Cookie: + - vmware_soap_session="2da975e61be85346b62d8f5d4814878b7216244f"; Path=/; HttpOnly; + Secure; + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 18 Apr 2019 17:23:34 GMT + Cache-Control: + - no-cache + Connection: + - Keep-Alive + Content-Type: + - text/xml; charset=utf-8 + X-Frame-Options: + - DENY + Content-Length: + - '436' + body: + encoding: UTF-8 + string: |- + + + + vm-22 + + + http_version: + recorded_at: Thu, 18 Apr 2019 17:23:35 GMT +- request: + method: post + uri: https://<%= vsphere_server %>/sdk + body: + encoding: UTF-8 + string: <_this type="PropertyCollector">propertyCollectorVirtualMachineconfigvm-22 + headers: + Content-Type: + - text/xml; charset=utf-8 + Soapaction: + - urn:vim25/6.7 + Cookie: + - vmware_soap_session="2da975e61be85346b62d8f5d4814878b7216244f"; Path=/; HttpOnly; + Secure; + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Thu, 18 Apr 2019 17:23:34 GMT + Cache-Control: + - no-cache + Connection: + - Keep-Alive + Content-Type: + - text/xml; charset=utf-8 + Transfer-Encoding: + - chunked + X-Frame-Options: + - DENY + body: + encoding: UTF-8 + string: |- + + + + vm-22config2019-04-09T11:43:16.576892Z1970-01-01T00:00:00ZDC1Microsoft Windows Server 2016 (64-bit)vmx-11564d88d6-2aea-1c8e-826e-1f0dbc536ae62019-01-24T14:32:57.981331Z52d810bd-077b-368d-a86f-0b2ad84269f8true564d88d6-2aea-1c8e-826e-1f0dbc536ae6windows9Server64Guest[storage1] DC1/DC1.vmx[storage1] DC1/[storage1] DC1/[storage1] DC1/10304guestToolsTypeMSItruetruetruetruemanualfalse1falsetruefalsefalsereleaseanyfalsefalsetrueautomatichvAutopowerOfffalseunsetfalsefalsefalsesoftsoftsoftsofthardsoftcheckpoint214096falsefalse200IDE 00201IDE 11300PS2 controller 00600700100PCI controller 00500120001400010001500040004001400SIO controller 00600Keyboard3000700Pointing device; Devicefalseautodetect3001500Video card100040961falsefalseautomatic26214412000Device on the virtual machine PCI bus that provides support for the virtual machine communication interface3210017-1135383834falsetrue14000USB xHCI controller224100230false1000LSI Logic SAS160100302000truenoSharing715000AHCI331002401600016000ISO [storage1] Installations/Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.ISO[storage1] Installations/Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.ISOdatastore-10truetruetrueok150000200062,914,560 KB[storage1] DC1/DC1.vmdkdatastore-10persistentfalsefalsefalse6000C295-8cfd-1ccc-e886-475a84e1f37d73427c5fcbe18b98431ee5fe63097b0cfalsesharingNone1000062914560644245094401000normal-11000normal03-2000false4000VM NetworkVM Networkfalsenetwork-13unsettruetruetrueok1921007generated00:0c:29:53:6a:e6false050normal-1false4001InternalNetworkInternalNetworkfalsenetwork-15unsettruetruetrueok2561008assigned00:50:56:90:6d:7atrue050normal-1false0-12000normal0-140960normalnormalfalsefalsefalse40960nvramDC1.nvrampciBridge0.presentTRUEsvga.presentTRUEpciBridge4.presentTRUEpciBridge4.virtualDevpcieRootPortpciBridge4.functions8pciBridge5.presentTRUEpciBridge5.virtualDevpcieRootPortpciBridge5.functions8pciBridge6.presentTRUEpciBridge6.virtualDevpcieRootPortpciBridge6.functions8pciBridge7.presentTRUEpciBridge7.virtualDevpcieRootPortpciBridge7.functions8hpet0.presentTRUEsched.cpu.latencySensitivitynormaldisk.EnableUUIDTRUEnuma.autosize.cookie20001numa.autosize.vcpu.maxPerVirtualNode2sched.swap.derivedName/vmfs/volumes/5c49ca9b-a707a914-97d0-6805ca0c6705/DC1/DC1-750e2789.vswpscsi0:0.redopciBridge0.pciSlotNumber17pciBridge4.pciSlotNumber21pciBridge5.pciSlotNumber22pciBridge6.pciSlotNumber23pciBridge7.pciSlotNumber24scsi0.pciSlotNumber160ethernet0.pciSlotNumber192usb_xhci.pciSlotNumber224vmci0.pciSlotNumber32sata0.pciSlotNumber33scsi0.sasWWID50 05 05 66 2a ea 1c 80ethernet0.generatedAddressOffset0vm.genid305336515256366739vm.genidX6674865700987541297monitor.phys_bits_used42vmotion.checkpointFBSize4194304vmotion.checkpointSVGAPrimarySize4194304softPowerOffFALSEtools.remindInstallFALSEtoolsInstallManager.lastInstallError0svga.guestBackedPrimaryAwareTRUEtoolsInstallManager.updateCounter1ethernet1.pciSlotNumber256usb_xhci:4.presentTRUEusb_xhci:4.deviceTypehidusb_xhci:4.port4usb_xhci:4.parent-1vmware.tools.internalversion10304vmware.tools.requiredversion10304migrate.hostLogStatenonemigrate.migrationId0migrate.hostLog./DC1-750e2789.hlogguestinfo.vmtools.buildNumber7253323guestinfo.vmtools.descriptionVMware Tools 10.2.0 build 7253323guestinfo.vmtools.versionNumber10304guestinfo.vmtools.versionString10.2.0storage1/vmfs/volumes/5c49ca9b-a707a914-97d0-6805ca0c6705inherit0falsefalsefalse10ipv4falsefalsebios40falsefalse61550592205807616falsefalsenevernone0Kb5if01Xz293NlhNv4J4+4x6Zng=falsefalseopportunistic + + + http_version: + recorded_at: Thu, 18 Apr 2019 17:23:35 GMT +recorded_with: VCR 4.0.0 diff --git a/tests/requests/compute/get_vm_first_scsi_controller_tests.rb b/tests/requests/compute/get_vm_first_scsi_controller_tests.rb new file mode 100644 index 00000000..29f6edae --- /dev/null +++ b/tests/requests/compute/get_vm_first_scsi_controller_tests.rb @@ -0,0 +1,19 @@ +require_relative '../../test_helper' + +describe Fog::Vsphere::Compute::Real do + include Fog::Vsphere::TestHelper + + before { Fog.unmock! } + after { Fog.mock! } + + let(:compute) { prepare_compute } + + describe '#get_vm_first_scsi_controller' do + it 'gets virtual machine by uuid' do + with_webmock_cassette('get_vm_first_scsi_controller') do + controller = compute.get_vm_first_scsi_controller('52d810bd-077b-368d-a86f-0b2ad84269f8') + assert_equal(controller.type, 'VirtualLsiLogicSASController') + end + end + end +end diff --git a/tests/test_helper.rb b/tests/test_helper.rb index f5e9876c..7be36b30 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -2,8 +2,6 @@ require 'vcr' require 'webmock/minitest' -# WebMock::Config.instance.net_http_connect_on_start = true - VCR.configure do |config| config.cassette_library_dir = "tests/fixtures/vcr_cassettes/6_7" config.hook_into :webmock @@ -11,43 +9,64 @@ end # Makes these parameters available to the cassettes for easier recording -SHARED_VSPHERE_PARAMS = { - vsphere_server: '127.0.0.1', - vsphere_username: 'Administrator@example.com', - vsphere_password: 'StrongAdminPassword' -}.freeze - -# Allows to skip writing of shared examples. -module VCRCasseteFilterShared - def record_http_interaction(interaction) - @shared_cassette ||= VCR::Cassette.new('shared', erb: SHARED_VSPHERE_PARAMS) - return if @shared_cassette.http_interactions.has_interaction_matching?(interaction.request) - interaction.request.uri = "https://<%= vsphere_server %>/sdk" - super(interaction) - end -end -VCR::Cassette.send(:prepend, VCRCasseteFilterShared) - module Fog module Vsphere + # Allows to skip writing of shared examples. + module VCRCasseteFilterShared + def record_http_interaction(interaction) + @shared_cassette ||= VCR::Cassette.new('shared', erb: TestConfig.load.to_h) + return if @shared_cassette.http_interactions.has_interaction_matching?(interaction.request) + interaction.request.uri = "https://<%= vsphere_server %>/sdk" + super(interaction) + end + end + ::VCR::Cassette.send(:prepend, VCRCasseteFilterShared) + module TestHelper def prepare_compute - Fog::Compute.new( - SHARED_VSPHERE_PARAMS.merge( - provider: 'vsphere', - vsphere_rev: '6.7' - ) - ) + Fog::Compute.new(TestConfig.load.to_h.merge(provider: 'vsphere')) end # Records new cassette, but before writing removes the examples already in *shared* cassette. def recording_webmock_cassette(name, options = {}, &block) + WebMock::Config.instance.net_http_connect_on_start = true VCR.use_cassette(name, options, &block) + ensure + WebMock::Config.instance.net_http_connect_on_start = false end def with_webmock_cassette(name, options = {}, &block) - VCR.use_cassette('shared', erb: SHARED_VSPHERE_PARAMS, allow_playback_repeats: true) do - VCR.use_cassette(name, options.merge(erb: SHARED_VSPHERE_PARAMS), &block) + VCR.use_cassette('shared', erb: TestConfig.load.to_h, allow_playback_repeats: true) do + VCR.use_cassette(name, options.merge(erb: TestConfig.load.to_h), &block) + end + end + end + + class TestConfig + FILE = File.join(File.dirname(__FILE__), 'vsphere_config.yml') + ATTRIBUTES = %w[server username password rev expected_pubkey_hash].freeze + DEFAULTS = { + vsphere_server: '127.0.0.1', + vsphere_username: 'Administrator@example.com', + vsphere_password: 'StrongAdminPassword' + }.freeze + + def self.load + @loaded ||= new(File.exist?(FILE) ? YAML.load_file(FILE) : DEFAULTS) + end + + def initialize(config = {}) + @config = parse(config) + end + + def to_h + @config.dup + end + + def parse(config, prefix = 'vsphere') + ATTRIBUTES.each_with_object({}) do |attr_name, parsed| + full_name = "#{prefix}_#{attr_name}" + parsed[full_name.to_sym] = config[full_name.to_sym] || config[full_name] || config[attr_name] end end end