Skip to content

Instantly share code, notes, and snippets.

@kjetilho
Created June 22, 2017 13:01
Show Gist options
  • Save kjetilho/3a1f41e1b9a0c60b1acd47ac66643f5f to your computer and use it in GitHub Desktop.
Save kjetilho/3a1f41e1b9a0c60b1acd47ac66643f5f to your computer and use it in GitHub Desktop.
spec/unit/facter/server_type_spec.rb
Failures:
1) server_type hostname => pro-dev-db01 should return development
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "development"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
2) server_type hostname => pro-dev01 should return development
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "development"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
3) server_type hostname => pro-web01 should return production
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "production"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
4) server_type hostname => puppetmaster should return production
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "production"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
5) server_type hostname => za-production-widgets-admin01 should return production
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "production"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
6) server_type hostname => za-development-widgets-admin01 should return testing
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "testing"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
7) server_type hostname => za-testing-bar-db02 should return testing
Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)
expected: "testing"
got: "testing za-testing-bar-db02"
(compared using ==)
# ./spec/unit/facter/server_type_spec.rb:23:in `block (5 levels) in <top (required)>'
Finished in 0.02126 seconds (files took 0.67896 seconds to load)
7 examples, 7 failures
# -*- coding: utf-8 -*-
require 'facter'
# Returns the production status based on hostname.
#
# Looks for strings "production", "testing" or "dev". Other hostnames
# are assumed to be "production".
Facter.add(:server_type) do
has_weight 10
setcode do
hostname = Facter.value(:hostname)
if m = hostname.match(/\b(production|testing)\b/)
"#{m[0]} #{hostname}"
elsif hostname.match(/\b(dev|dev\d+)\b/)
"development #{hostname}"
elsif hostname.match(/\b(development)\b/)
"testing #{hostname}" # !
else
"production #{hostname}"
end
end
end
require 'spec_helper'
require './design/profile/lib/facter/server_type'
types = {
'development' => [ 'pro-dev-db01', 'pro-dev01' ],
'production' => [ 'pro-web01', 'puppetmaster', 'za-production-widgets-admin01' ],
'testing' => [ 'za-development-widgets-admin01', 'za-testing-bar-db02' ],
}
describe 'server_type', :type => :fact do
types.each do |result, hostnames|
hostnames.each do |name|
before :each do
Facter.fact(:hostname).stubs(:value).returns(name)
end
context "hostname => #{name}" do
it "should return #{result}" do
expect(Facter.fact(:server_type).value).to eq(result)
end
end
end
end
end
@richardc
Copy link

richardc commented Jun 22, 2017

Dry coded; but less bad:

require 'spec_helper'
require './design/profile/lib/facter/server_type'

types = {
  'development' => [ 'pro-dev-db01', 'pro-dev01' ],
  'production' => [ 'pro-web01', 'puppetmaster', 'za-production-widgets-admin01' ],
  'testing' => [ 'za-development-widgets-admin01', 'za-testing-bar-db02' ],
}

describe 'server_type', :type => :fact do
 before :each do
   # make facter forget computed values
   Facter.flush
  end
  types.each do |result, hostnames|
    hostnames.each do |name|
      context "hostname => #{name}" do
        it "should return #{result}" do
          Facter.fact(:hostname).stubs(:value).returns(name)
          expect(Facter.fact(:server_type).value).to eq(result)
        end
      end
    end
  end
end

@kjetilho
Copy link
Author

  1. server_type hostname => pro-dev-db01 should return development
    Failure/Error: expect(Facter.fact(:server_type).value).to eq(result)

    NoMethodError:
    undefined method `value' for nil:NilClass

    ./spec/unit/facter/server_type_spec.rb:25:in `block (5 levels) in <top (required)>'

@kjetilho
Copy link
Author

The solution is to call Facter.flush to clear the cached value of :server_type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment