From 81ed5c61aed710194560c2192cdc2334c682e65a Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 21 Mar 2016 21:48:59 +0100 Subject: [PATCH] Issue #69 Introducing vagrant-spec based acceptance tests and updating README --- .gitignore | 4 +- Gemfile | 6 +- README.md | 70 ++++++++++++++++++- vagrant-spec.config.rb | 16 +++++ vagrant-spec/cdk_spec.rb | 63 +++++++++++++++++ .../skeleton/cdk-with-vbguest/Vagrantfile | 7 ++ .../skeleton/cdk-without-vbguest/Vagrantfile | 5 ++ .../skeleton/invalid-credentials/Vagrantfile | 7 ++ .../skeleton/skipped-registration/Vagrantfile | 6 ++ .../skeleton/valid-credentials/Vagrantfile | 7 ++ 10 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 vagrant-spec.config.rb create mode 100644 vagrant-spec/cdk_spec.rb create mode 100644 vagrant-spec/skeleton/cdk-with-vbguest/Vagrantfile create mode 100644 vagrant-spec/skeleton/cdk-without-vbguest/Vagrantfile create mode 100644 vagrant-spec/skeleton/invalid-credentials/Vagrantfile create mode 100644 vagrant-spec/skeleton/skipped-registration/Vagrantfile create mode 100644 vagrant-spec/skeleton/valid-credentials/Vagrantfile diff --git a/.gitignore b/.gitignore index d5466e2..0559fc9 100644 --- a/.gitignore +++ b/.gitignore @@ -30,8 +30,8 @@ build/ # for a library or gem, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: # Gemfile.lock -# .ruby-version -# .ruby-gemset +.ruby-version +.ruby-gemset # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc diff --git a/Gemfile b/Gemfile index c2b542e..650ebad 100644 --- a/Gemfile +++ b/Gemfile @@ -3,9 +3,13 @@ source 'https://rubygems.org' gemspec group :development do - gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git' + gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git' + gem 'vagrant-spec', git: 'https://github.com/mitchellh/vagrant-spec.git' gem 'gem-compare' gem 'rake' + gem 'vagrant-vbguest' + gem 'vagrant-libvirt' + gem 'fog-libvirt', '0.0.3' # https://github.com/pradels/vagrant-libvirt/issues/568 end group :plugins do diff --git a/README.md b/README.md index e083cac..ee6e7cd 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,30 @@ # vagrant-registration + + +- [Installation](#installation) +- [Usage](#usage) + - [Plugin Configuration](#plugin-configuration) + - [Credentials Configuration](#credentials-configuration) + - [subscription-manager Configuration](#subscription-manager-configuration) + - [subscription-manager Default Options](#subscription-manager-default-options) + - [subscription-manager Options Reference](#subscription-manager-options-reference) + - [rhn-register Configuration](#rhn-register-configuration) + - [rhn-register Default Options](#rhn-register-default-options) + - [rhn-register Options Reference](#rhn-register-options-reference) +- [Development](#development) + - [Tests](#tests) + - [Shell script based tests](#shell-script-based-tests) + - [Vagrant Spec based tests](#vagrant-spec-based-tests) +- [Acknowledgements](#acknowledgements) + + + vagrant-registration plugin for Vagrant allows developers to easily register their guests for updates on systems with a subscription model (like Red Hat Enterprise Linux). This plugin would run *register* action on `vagrant up` before any provisioning and *unregister* on `vagrant halt` or `vagrant destroy`. The actions then call the registration capabilities that have to be provided for given OS. - + ## Installation Install vagrant-registration as any other Vagrant plugin: @@ -19,6 +39,7 @@ If you are on Fedora, you can install the packaged version of the plugin by runn # dnf install vagrant-registration ``` + ## Usage The plugin is designed in an registration-manager-agnostic way which means that plugin itself does not depend on any OS nor way of registration. vagrant-registration only calls registration capabilities for given guest, passes the configuration options to them and handles interactive registration. @@ -33,6 +54,7 @@ Vagrant.configure('2') do |config| end ``` + ### Plugin Configuration - **skip** skips the registration. If you wish to skip the registration process altogether, you can do so by setting a `skip` option to `true`: @@ -53,6 +75,7 @@ end config.registration.manager = 'subscription_manager' ``` + ### Credentials Configuration Setting up the credentials can be done as follows: @@ -96,6 +119,7 @@ If you do not provide credentials, you will be prompted for them in the "up proc Please note the the interactive mode asks you for the preferred registration pair only of the configured manager. + ### subscription-manager Configuration vagrant-registration will use the `subscription_manager` manager by default or can be explicitly configured by setting the `manager` option to `subscription_manager`: @@ -118,6 +142,7 @@ You can set any option easily by setting `config.registration.OPTION_NAME = 'OPT in your Vagrantfile (please see the subscription-manager's documentation for option description). + #### subscription-manager Default Options - **--force**: Subscription Manager will fail if you attempt to register an already registered machine (see the man page for explanation), therefore vagrant-registration appends the `--force` flag automatically when subscribing. If you would like to disable this feature, set `force` option to `false`: @@ -135,6 +160,7 @@ description). Note that the `auto_attach` option is set to false when using org/activationkey for registration or if pools are specified. + #### subscription-manager Options Reference ```ruby @@ -197,6 +223,7 @@ Note that the `auto_attach` option is set to false when using org/activationkey config.registration.pools ``` + ### rhn-register Configuration vagrant-registration will use the `rhn_register` manager only if explicitly configured by setting the `manager` option to `rhn_register`: @@ -217,6 +244,7 @@ vagrant-registration supports most of the options of rhnreg_ks's command. You ca `rhn_register` manager reuses the naming of `subscription-manager`'s command options where possible. + #### rhn-register Default Options - **--force**: `rhnreg_ks` command will fail if you attempt to register an already registered machine (see the man page for explanation), therefore vagrant-registration appends the `--force` flag automatically when subscribing. If you would like to disable this feature, set `force` option to `false`: @@ -225,6 +253,7 @@ vagrant-registration supports most of the options of rhnreg_ks's command. You ca config.registration.force = false ``` + #### rhn-register Options Reference ```ruby @@ -291,7 +320,23 @@ vagrant-registration supports most of the options of rhnreg_ks's command. You ca config.registration.skip ``` -## Tests + +## Development + +To install a development environment, clone the repo and prepare dependencies by + +``` +gem install bundler -v 1.7.5 +bundler install +``` + +The use of [RVM|https://rvm.io] is recommended. Verified to work with ruby 2.0.0p643. + + +### Tests + + +#### Shell script based tests Tests currently test the plugin with `subscription-manager` and `rhn_register` on RHEL 7.1 guest and Fedora host. You need an imported RHEL 7.1 Vagrant box named `rhel-7.1`. @@ -316,6 +361,27 @@ environment variable on `1` before executing the test script: export DEBUG=1 ``` + +#### Vagrant Spec based tests + +The source contains also a set of [vagrant-spec](https://github.com/mitchellh/vagrant-spec) acceptance tests. They can be run via: + +``` +vagrant-spec test --components provider/virtualbox/cdk +vagrant-spec test --components provider/libvirt/cdk +``` + +In order to successfully run the tests, you need to set the following +environment variables: + +* _VAGRANT_SPEC_BOX_VIRTUAL_BOX_ - URL to the VirtualBox box for testing +* _VAGRANT_SPEC_BOX_LIBVIRT_ - URL to the Libvirt box for testing +* _SUBSCRIPTION_USER_ - Valid Red Hat subscription user +* _SUBSCRIPTION_PASSWORD_ - Valid Red Hat subscription password + +You need to at least specify one box file! + + ## Acknowledgements The project would like to make sure we thank [purpleidea](https://github.com/purpleidea/), [humaton](https://github.com/humaton/), [strzibny](https://github.com/strzibny), [scollier](https://github.com/scollier/), [puzzle](https://github.com/puzzle), [voxik](https://github.com/voxik), [lukaszachy](https://github.com/lukaszachy), [goern](https://github.com/goern), [iconoeugen](https://github.com/iconoeugen) and [pvalena](https://github.com/pvalena) (in no particular order) for their contributions of ideas, code and testing for this project. diff --git a/vagrant-spec.config.rb b/vagrant-spec.config.rb new file mode 100644 index 0000000..e1d27a8 --- /dev/null +++ b/vagrant-spec.config.rb @@ -0,0 +1,16 @@ +Vagrant::Spec::Acceptance.configure do |c| + errors = [] + errors << 'You need to specify at least one box location via the environment variable VAGRANT_SPEC_BOX_VIRTUAL_BOX resp. VAGRANT_SPEC_BOX_LIBVIRT' unless ENV.has_key?('VAGRANT_SPEC_BOX_VIRTUAL_BOX') || ENV.has_key?('VAGRANT_SPEC_BOX_LIBVIRT') + errors << 'You need to specify a valid subscription username via the environment variable SUBSCRIPTION_USER' unless ENV.has_key?('SUBSCRIPTION_USER') + errors << 'You need to specify a valid subscription password via the environment variable SUBSCRIPTION_PASSWORD' unless ENV.has_key?('SUBSCRIPTION_PASSWORD') + + unless errors.empty? + puts errors.join("\n") + abort + end + + c.provider 'virtualbox', box: ENV['VAGRANT_SPEC_BOX_VIRTUAL_BOX'] if ENV.has_key?('VAGRANT_SPEC_BOX_VIRTUAL_BOX') + c.provider 'libvirt', box: ENV['VAGRANT_SPEC_BOX_LIBVIRT'] if ENV.has_key?('VAGRANT_SPEC_BOX_LIBVIRT') + c.component_paths << 'vagrant-spec' + c.skeleton_paths << 'vagrant-spec/skeleton' +end diff --git a/vagrant-spec/cdk_spec.rb b/vagrant-spec/cdk_spec.rb new file mode 100644 index 0000000..fd7a965 --- /dev/null +++ b/vagrant-spec/cdk_spec.rb @@ -0,0 +1,63 @@ +shared_examples "provider/cdk" do |provider, options| + if !options[:box] + raise ArgumentError, + "box_basic option must be specified for provider: #{provider}" + end + + include_context "acceptance" + + before do + assert_execute("vagrant", "box", "add", "box", options[:box]) + end + + after do + assert_execute("vagrant", "destroy", "--force") + end + + if provider.eql? 'virtualbox' + it ": with vbguest plugin installed Virtual Box guest additons will be added" do + environment.skeleton("cdk-with-vbguest") + assert_execute("vagrant", "up", "--provider=#{provider}") + + result = execute("vagrant", "ssh", "-c", "lsmod | grep -i vbox") + expect(result).to exit_with(0) + expect(result.stdout).to match(/vboxguest/) + end + + it ": without vbguest plugin installed Virtual Box guest additons will NOT be added" do + environment.skeleton("cdk-without-vbguest") + assert_execute("vagrant", "up", "--provider=#{provider}") + + result = execute("vagrant", "ssh", "-c", "lsmod | grep -i vbox") + expect(result).to exit_with(1) + expect(result.stdout).not_to match(/vboxguest/) + end + end + + it ": registration succeeds with valid credentials" do + environment.skeleton("valid-credentials") + assert_execute("vagrant", "up", "--provider=#{provider}") + + result = execute("vagrant", "ssh", "-c", "sudo subscription-manager version") + expect(result).to exit_with(0) + expect(result.stdout).to match(/Red Hat Subscription Management/) + end + + it ": registration fails with invalid credentials" do + environment.skeleton("invalid-credentials") + result = execute("vagrant", "up", "--provider=#{provider}") + + expect(result).to exit_with(1) + expect(result.stderr).to match(/Invalid username or password./) + end + + it ": skipping registration results in unregistered VM" do + environment.skeleton("skipped-registration") + assert_execute("vagrant", "up", "--provider=#{provider}") + + result = execute("vagrant", "ssh", "-c", "sudo subscription-manager version") + expect(result).to exit_with(0) + expect(result.stdout).to match(/This system is currently not registered/) + end + +end diff --git a/vagrant-spec/skeleton/cdk-with-vbguest/Vagrantfile b/vagrant-spec/skeleton/cdk-with-vbguest/Vagrantfile new file mode 100644 index 0000000..afbc8fd --- /dev/null +++ b/vagrant-spec/skeleton/cdk-with-vbguest/Vagrantfile @@ -0,0 +1,7 @@ +require 'vagrant-vbguest' + +Vagrant.configure(2) do |config| + config.vm.box = 'box' + config.registration.username = ENV['SUBSCRIPTION_USER'] + config.registration.password = ENV['SUBSCRIPTION_PASSWORD'] +end diff --git a/vagrant-spec/skeleton/cdk-without-vbguest/Vagrantfile b/vagrant-spec/skeleton/cdk-without-vbguest/Vagrantfile new file mode 100644 index 0000000..92540c2 --- /dev/null +++ b/vagrant-spec/skeleton/cdk-without-vbguest/Vagrantfile @@ -0,0 +1,5 @@ +Vagrant.configure(2) do |config| + config.vm.box = "box" + config.registration.username = ENV['SUBSCRIPTION_USER'] + config.registration.password = ENV['SUBSCRIPTION_PASSWORD'] +end diff --git a/vagrant-spec/skeleton/invalid-credentials/Vagrantfile b/vagrant-spec/skeleton/invalid-credentials/Vagrantfile new file mode 100644 index 0000000..e6ce2e6 --- /dev/null +++ b/vagrant-spec/skeleton/invalid-credentials/Vagrantfile @@ -0,0 +1,7 @@ +require 'vagrant-libvirt' + +Vagrant.configure(2) do |config| + config.vm.box = 'box' + config.registration.username = 'foo' + config.registration.password = 'bar' +end diff --git a/vagrant-spec/skeleton/skipped-registration/Vagrantfile b/vagrant-spec/skeleton/skipped-registration/Vagrantfile new file mode 100644 index 0000000..2f489d0 --- /dev/null +++ b/vagrant-spec/skeleton/skipped-registration/Vagrantfile @@ -0,0 +1,6 @@ +require 'vagrant-libvirt' + +Vagrant.configure(2) do |config| + config.vm.box = 'box' + config.registration.skip = true +end diff --git a/vagrant-spec/skeleton/valid-credentials/Vagrantfile b/vagrant-spec/skeleton/valid-credentials/Vagrantfile new file mode 100644 index 0000000..7e9152a --- /dev/null +++ b/vagrant-spec/skeleton/valid-credentials/Vagrantfile @@ -0,0 +1,7 @@ +require 'vagrant-libvirt' + +Vagrant.configure(2) do |config| + config.vm.box = 'box' + config.registration.username = ENV['SUBSCRIPTION_USER'] + config.registration.password = ENV['SUBSCRIPTION_PASSWORD'] +end