Skip to content

Commit

Permalink
present recording instance config file, improve cassette testing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ezr-ondrej authored and chris1984 committed Apr 24, 2019
1 parent 3d407fe commit 93c3a22
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tmp
mkmf.log
gemfiles/*.lock
.*.swp
tests/vsphere_config.yml
47 changes: 42 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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: '<UserName>'
password: '<password>'
rev: '6.7' # optional revision of the vsphere
expected_pubkey_hash: <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/<your_request>
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:
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions tests/fixtures/vcr_cassettes/6_7/get_vm_first_scsi_controller.yml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions tests/requests/compute/get_vm_first_scsi_controller_tests.rb
Original file line number Diff line number Diff line change
@@ -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
73 changes: 46 additions & 27 deletions tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,71 @@
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
config.default_cassette_options = { match_requests_on: [:body] }
end

# Makes these parameters available to the cassettes for easier recording
SHARED_VSPHERE_PARAMS = {
vsphere_server: '127.0.0.1',
vsphere_username: '[email protected]',
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: '[email protected]',
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
Expand Down

0 comments on commit 93c3a22

Please sign in to comment.