From cf34be8f33098d247af74a6fe36c0d0621820120 Mon Sep 17 00:00:00 2001 From: Bao Nguyen Date: Fri, 25 Jul 2014 08:45:00 -0700 Subject: [PATCH] adding tests and foodcritic --- .rubocop.yml | 23 ++++++ .travis.yml | 9 +++ Gemfile | 11 ++- README.md | 19 +++++ Rakefile | 73 +++++++++++++++++++ attributes/bgpd.rb | 4 +- attributes/default.rb | 10 +-- attributes/ospfd.rb | 4 +- metadata.rb | 16 ++-- providers/bgp.rb | 24 +++--- providers/ospf.rb | 28 +++---- providers/zebra.rb | 18 ++--- recipes/bgpd.rb | 2 +- recipes/default.rb | 32 ++++---- recipes/ospfd.rb | 4 +- resources/bgp.rb | 8 +- resources/ospf.rb | 12 +-- resources/zebra.rb | 6 +- spec/spec_helper.rb | 0 .../default/tests/minitest/default_test.rb | 5 ++ test/cookbooks/quagga_test/metadata.rb | 7 ++ .../cookbooks/quagga_test/recipes/bgp_test.rb | 0 test/cookbooks/quagga_test/recipes/default.rb | 2 + .../quagga_test/recipes/ospf_test.rb | 0 test/intergration/default/bats/vtysh.bats | 3 + 25 files changed, 233 insertions(+), 87 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .travis.yml create mode 100644 Rakefile create mode 100644 spec/spec_helper.rb create mode 100644 test/cookbooks/quagga_test/files/default/tests/minitest/default_test.rb create mode 100644 test/cookbooks/quagga_test/metadata.rb rename recipes/bgp_sample.rb => test/cookbooks/quagga_test/recipes/bgp_test.rb (100%) create mode 100644 test/cookbooks/quagga_test/recipes/default.rb rename recipes/ospf_sample.rb => test/cookbooks/quagga_test/recipes/ospf_test.rb (100%) create mode 100644 test/intergration/default/bats/vtysh.bats diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..4d4f030 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,23 @@ +AllCops: + Include: + - metadata.rb + - Gemfile + - attributes/** + - recipes/** + - libraries/** + - providers/** + - resources/** + Exclude: + - example/** + - test/** + - vendor/** + +Encoding: + Enabled: false + +LineLength: + Enabled: false + +WordArray: + MinSize: 3 + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0cf161a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +rvm: + - 1.9.3 + - 2.0.0 + - 2.1.0 +before_script: + - bundle exec berks install +script: + - bundle exec rake test:quick + diff --git a/Gemfile b/Gemfile index 99f227e..21eb6d0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,16 @@ source 'https://rubygems.org' gem 'test-kitchen', '~> 1.2.1' -gem 'kitchen-vagrant', '= 0.15.0', :group => :integration +gem 'kitchen-vagrant', '= 0.15.0', group: :integration gem 'librarian-chef' gem 'berkshelf' gem 'chef-zero' + +group 'develop' do + gem 'kitchen-docker-api' + gem 'rake' + gem 'foodcritic', git: 'https://github.com/mlafeldt/foodcritic.git', branch: 'improve-rake-task' + gem 'rubocop' + gem 'knife-cookbook-doc' + gem 'chefspec', '>= 3.2.0' +end diff --git a/README.md b/README.md index 5421136..a197599 100644 --- a/README.md +++ b/README.md @@ -63,3 +63,22 @@ quagga_ospf "#{node.quagga.ospf.area}" do networks node.quagga.networks end ``` + +Author and License +=================== + +__Author__ Bao Nguyen + +Copyright 2014, Ooyala Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..91d10e8 --- /dev/null +++ b/Rakefile @@ -0,0 +1,73 @@ +#!/usr/bin/env rake +require 'rake' +require 'rspec/core/rake_task' + +task :default => 'test:quick' + +namespace :test do + + RSpec::Core::RakeTask.new(:spec) do |t| + t.pattern = Dir.glob('test/spec/**/*_spec.rb') + t.rspec_opts = "--color -f d" + end + + begin + require 'kitchen/rake_tasks' + Kitchen::RakeTasks.new + rescue + puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI'] + end + + begin + require 'foodcritic/rake_task' + require 'foodcritic' + task :default => [:foodcritic] + FoodCritic::Rake::LintTask.new do |t| + t.options = { + fail_tags: %w/correctness services libraries deprecated/, + exclude_paths: ['test/**/*', 'spec/**/*', 'features/**/*', 'example/**/*'] + } + end + rescue LoadError + warn "Foodcritic Is missing ZOMG" + end + + begin + require 'rubocop/rake_task' + RuboCop::RakeTask.new do |task| + task.fail_on_error = true + task.options = %w{-D -a} + end + rescue LoadError + warn "Rubocop gem not installed, now the code will look like crap!" + end + + + desc 'Run all of the quick tests.' + task :quick do + Rake::Task['test:rubocop'].invoke + Rake::Task['test:foodcritic'].invoke + Rake::Task['test:spec'].invoke + end + + + desc 'Run _all_ the tests. Go get a coffee.' + task :complete do + Rake::Task['test:quick'].invoke + Rake::Task['test:kitchen:all'].invoke + end + + desc 'Run CI tests' + task :ci do + Rake::Task['test:complete'].invoke + end +end + + +namespace :release do + task :update_metadata do + end + + task :tag_release do + end +end diff --git a/attributes/bgpd.rb b/attributes/bgpd.rb index a83408b..6458d44 100644 --- a/attributes/bgpd.rb +++ b/attributes/bgpd.rb @@ -1,5 +1,5 @@ -include_attribute "quagga" +include_attribute 'quagga' -default[:quagga][:bgp][:local_asn] = "" +default[:quagga][:bgp][:local_asn] = '' default[:quagga][:bgp][:networks] = [] default[:quagga][:bgp][:peers] = {} diff --git a/attributes/default.rb b/attributes/default.rb index 5b4ebd8..ec90a52 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,4 +1,4 @@ -default[:quagga][:dir] = "/etc/quagga" +default[:quagga][:dir] = '/etc/quagga' default[:quagga][:user] = 'quagga' default[:quagga][:group] = 'quagga' @@ -13,9 +13,9 @@ default[:quagga][:daemons][:babeld] = false # insecured default username and password. overwrite when deploy -default[:quagga][:password] = "quagga" -default[:quagga][:enabled_password] = "quagga" +default[:quagga][:password] = 'quagga' +default[:quagga][:enabled_password] = 'quagga' -default[:quagga]["integrated-vtysh-config"] = false +default[:quagga]['integrated-vtysh-config'] = false -default[:quagga][:loopback] = "127.0.0.1" +default[:quagga][:loopback] = '127.0.0.1' diff --git a/attributes/ospfd.rb b/attributes/ospfd.rb index ba5a8d3..4860334 100644 --- a/attributes/ospfd.rb +++ b/attributes/ospfd.rb @@ -1,6 +1,6 @@ -include_attribute "quagga" +include_attribute 'quagga' -default[:quagga][:ospf][:area] = "" +default[:quagga][:ospf][:area] = '' default[:quagga][:ospf][:networks] = [] default[:quagga][:ospf][:protocols] = [] default[:quagga][:ospf][:interfaces] = [] diff --git a/metadata.rb b/metadata.rb index 68e5dbf..4cb19a1 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,11 +1,7 @@ -name "quagga" -maintainer "Bao Nguyen" -maintainer_email "ngqbao@gmail.com" -license "All rights reserved" -description "Generic Quagga cookbook" +name 'quagga' +maintainer 'Bao Nguyen' +maintainer_email 'ngqbao@gmail.com' +license 'All rights reserved' +description 'Generic Quagga cookbook' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version "0.1.1" - -%w{ }.each do |i| - depends i -end +version '0.1.2' diff --git a/providers/bgp.rb b/providers/bgp.rb index d8bbf57..31f7f04 100644 --- a/providers/bgp.rb +++ b/providers/bgp.rb @@ -20,33 +20,33 @@ action :add do bgpd_path = "#{node.quagga.dir}/bgpd.conf" - Chef::Log.info "Adding #{new_resource.name.to_s}: acl to #{bgpd_path}" + Chef::Log.info "Adding #{new_resource.name}: acl to #{bgpd_path}" template "#{bgpd_path}" do - cookbook "quagga" - source "bgpd.conf.erb" + cookbook 'quagga' + source 'bgpd.conf.erb' owner node.quagga.user group node.quagga.group - mode "0644" + mode '0644' variables( - :local_asn => new_resource.name, - :ebgp_peers => new_resource.ebgp_peers, - :networks => new_resource.networks, - :loopback => new_resource.loopback + local_asn: new_resource.name, + ebgp_peers: new_resource.ebgp_peers, + networks: new_resource.networks, + loopback: new_resource.loopback ) - notifies :reload, "service[quagga]", :delayed + notifies :reload, 'service[quagga]', :delayed end # configure loopback ifconfig "#{new_resource.loopback}/32" do - device "lo:1" + device 'lo:1' end end action :remove do bgpd_path = "#{node.quagga.dir}/#{new_resource.name}" - if ::File.exists?(bgpd_path) - Chef::Log.info "Removing #{new_resource.file_type.to_s}: bgp from #{bgpd_path}" + if ::File.exist?(bgpd_path) + Chef::Log.info "Removing #{new_resource.file_type}: bgp from #{bgpd_path}" file bgpd_path do action :delete end diff --git a/providers/ospf.rb b/providers/ospf.rb index f6c4f4b..945730a 100644 --- a/providers/ospf.rb +++ b/providers/ospf.rb @@ -20,35 +20,35 @@ action :add do ospfd_path = "#{node.quagga.dir}/ospfd.conf" - Chef::Log.info "Adding #{new_resource.name.to_s}: ospf to #{ospfd_path}" + Chef::Log.info "Adding #{new_resource.name}: ospf to #{ospfd_path}" template "#{ospfd_path}" do - cookbook "quagga" - source "ospfd.conf.erb" + cookbook 'quagga' + source 'ospfd.conf.erb' owner node.quagga.user group node.quagga.group - mode "0644" + mode '0644' variables( - :area => new_resource.name, - :networks => new_resource.networks, - :loopback => new_resource.loopback, - :protocols => new_resource.protocols, - :interfaces => new_resource.interfaces, - :ospf_options => new_resource.ospf_options + area: new_resource.name, + networks: new_resource.networks, + loopback: new_resource.loopback, + protocols: new_resource.protocols, + interfaces: new_resource.interfaces, + ospf_options: new_resource.ospf_options ) - notifies :reload, "service[quagga]", :delayed + notifies :reload, 'service[quagga]', :delayed end # configure loopback ifconfig "#{new_resource.loopback}/32" do - device "lo:1" + device 'lo:1' end end action :remove do ospfd_path = "#{node.quagga.dir}/ospfd.conf" - if ::File.exists?(ospfd_path) - Chef::Log.info "Removing #{new_resource.file_type.to_s}: ospf from #{ospfd_path}" + if ::File.exist?(ospfd_path) + Chef::Log.info "Removing #{new_resource.file_type}: ospf from #{ospfd_path}" file ospfd_path do action :delete end diff --git a/providers/zebra.rb b/providers/zebra.rb index 626200b..cab3b07 100644 --- a/providers/zebra.rb +++ b/providers/zebra.rb @@ -20,26 +20,26 @@ action :add do zebra_path = "#{node.quagga.dir}/zebra.conf" - Chef::Log.info "Adding #{new_resource.name.to_s}: interface to #{zebra_path}" + Chef::Log.info "Adding #{new_resource.name}: interface to #{zebra_path}" template "#{zebra_path}" do - cookbook "quagga" - source "zebra.conf.erb" + cookbook 'quagga' + source 'zebra.conf.erb' owner node.quagga.user group node.quagga.group - mode "0644" + mode '0644' variables( - :interfaces => new_resource.interfaces, - :static_routes => new_resource.static_routes + interfaces: new_resource.interfaces, + static_routes: new_resource.static_routes ) - notifies :reload, "service[quagga]", :delayed + notifies :reload, 'service[quagga]', :delayed end end action :remove do zebra_path = "#{node.quagga.dir}/zebra.conf" - if ::File.exists?(zebra_path) - Chef::Log.info "Removing #{new_resource.file_type.to_s}: interface from #{zebra_path}" + if ::File.exist?(zebra_path) + Chef::Log.info "Removing #{new_resource.file_type}: interface from #{zebra_path}" file zebra_path do action :delete end diff --git a/recipes/bgpd.rb b/recipes/bgpd.rb index a8e74fc..4cc028c 100644 --- a/recipes/bgpd.rb +++ b/recipes/bgpd.rb @@ -18,6 +18,6 @@ # limitations under the License. # -include_recipe "quagga" +include_recipe 'quagga' node.set[:quagga][:daemons][:bgpd] = true diff --git a/recipes/default.rb b/recipes/default.rb index afeceae..878f252 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -18,48 +18,48 @@ # limitations under the License. # -package "quagga" do +package 'quagga' do action :install end directory node.quagga.dir do owner node.quagga.user group node.quagga.group - mode "0755" + mode '0755' action :create end -service "quagga" do - action :enable +service 'quagga' do + action :enable end -if node.platform == "debian" +if node.platform == 'debian' template "#{node.quagga.dir}/daemons" do - source "daemons.erb" + source 'daemons.erb' owner node.quagga.user group node.quagga.group - mode "0644" - notifies :restart, "service[quagga]", :delayed + mode '0644' + notifies :restart, 'service[quagga]', :delayed end template "#{node.quagga.dir}/debian.conf" do - source "debian.conf.erb" + source 'debian.conf.erb' owner node.quagga.user group node.quagga.group - mode "0644" + mode '0644' end end template "#{node.quagga.dir}/vtysh.conf" do - source "vtysh.conf.erb" + source 'vtysh.conf.erb' owner node.quagga.user group node.quagga.group - mode "0644" + mode '0644' # restart needed? - notifies :restart, "service[quagga]", :delayed + notifies :restart, 'service[quagga]', :delayed end -service "quagga" do - supports :status => true, :restart => true, :reload => true - action [ :enable, :restart ] +service 'quagga' do + supports status: true, restart: true, reload: true + action [:enable, :restart] end diff --git a/recipes/ospfd.rb b/recipes/ospfd.rb index 54bbf62..0058cc5 100644 --- a/recipes/ospfd.rb +++ b/recipes/ospfd.rb @@ -18,6 +18,6 @@ # limitations under the License. # -include_recipe "quagga" +include_recipe 'quagga' -node.set[:quagga][:daemons][:ospfd] = true \ No newline at end of file +node.set[:quagga][:daemons][:ospfd] = true diff --git a/resources/bgp.rb b/resources/bgp.rb index b04ecf6..8f2f31a 100644 --- a/resources/bgp.rb +++ b/resources/bgp.rb @@ -23,7 +23,7 @@ default_action :add # resources for bgp -attribute :name, :kind_of => String, :name_attribute => true -attribute :networks, :kind_of => Array, :default => [] -attribute :ebgp_peers, :kind_of => Hash, :default => {} -attribute :loopback, :kind_of => String, :default => "127.0.0.1" \ No newline at end of file +attribute :name, kind_of: String, name_attribute: true +attribute :networks, kind_of: Array, default: [] +attribute :ebgp_peers, kind_of: Hash, default: {} +attribute :loopback, kind_of: String, default: '127.0.0.1' diff --git a/resources/ospf.rb b/resources/ospf.rb index c3a9980..ef61cad 100644 --- a/resources/ospf.rb +++ b/resources/ospf.rb @@ -22,9 +22,9 @@ default_action :add -attribute :name, :kind_of => String, :name_attribute => true -attribute :networks, :kind_of => Array, :default => [] -attribute :protocols, :kind_of => Array, :default => [] -attribute :loopback, :kind_of => String, :default => "127.0.0.1" -attribute :interfaces, :kind_of => Hash, :default => {} -attribute :ospf_options, :kind_of => Array, :default => [] \ No newline at end of file +attribute :name, kind_of: String, name_attribute: true +attribute :networks, kind_of: Array, default: [] +attribute :protocols, kind_of: Array, default: [] +attribute :loopback, kind_of: String, default: '127.0.0.1' +attribute :interfaces, kind_of: Hash, default: {} +attribute :ospf_options, kind_of: Array, default: [] diff --git a/resources/zebra.rb b/resources/zebra.rb index b1f7430..9244228 100644 --- a/resources/zebra.rb +++ b/resources/zebra.rb @@ -23,6 +23,6 @@ default_action :add # resources for interfaces -attribute :name, :kind_of => String, :name_attribute => true -attribute :interfaces, :kind_of => Hash, :default => {} -attribute :static_routes, :kind_of => Hash, :default => {} \ No newline at end of file +attribute :name, kind_of: String, name_attribute: true +attribute :interfaces, kind_of: Hash, default: {} +attribute :static_routes, kind_of: Hash, default: {} diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/cookbooks/quagga_test/files/default/tests/minitest/default_test.rb b/test/cookbooks/quagga_test/files/default/tests/minitest/default_test.rb new file mode 100644 index 0000000..f5df2f6 --- /dev/null +++ b/test/cookbooks/quagga_test/files/default/tests/minitest/default_test.rb @@ -0,0 +1,5 @@ +require_relative "./support/helpers" + +describe_recipe 'quagga_test::default' do + +end diff --git a/test/cookbooks/quagga_test/metadata.rb b/test/cookbooks/quagga_test/metadata.rb new file mode 100644 index 0000000..411d2fe --- /dev/null +++ b/test/cookbooks/quagga_test/metadata.rb @@ -0,0 +1,7 @@ +name "quagga-test" +maintainer "Bao Nguyen" +maintainer_email "ngqbao@gmail.com" +license "All rights reserved" +description "Generic Quagga cookbook testing" +long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) +version "0.1.0" diff --git a/recipes/bgp_sample.rb b/test/cookbooks/quagga_test/recipes/bgp_test.rb similarity index 100% rename from recipes/bgp_sample.rb rename to test/cookbooks/quagga_test/recipes/bgp_test.rb diff --git a/test/cookbooks/quagga_test/recipes/default.rb b/test/cookbooks/quagga_test/recipes/default.rb new file mode 100644 index 0000000..f04a89c --- /dev/null +++ b/test/cookbooks/quagga_test/recipes/default.rb @@ -0,0 +1,2 @@ +include_recipe "quagga_test::ospf_test.rb" +include_recipe "quagga_test::bgp_test.rb" diff --git a/recipes/ospf_sample.rb b/test/cookbooks/quagga_test/recipes/ospf_test.rb similarity index 100% rename from recipes/ospf_sample.rb rename to test/cookbooks/quagga_test/recipes/ospf_test.rb diff --git a/test/intergration/default/bats/vtysh.bats b/test/intergration/default/bats/vtysh.bats new file mode 100644 index 0000000..d1c5c8e --- /dev/null +++ b/test/intergration/default/bats/vtysh.bats @@ -0,0 +1,3 @@ +@test "vtysh should be running" { + vtysh +}