Class: Reagan::TestVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/reagan/test_version.rb

Overview

tests to make sure the version has been updated on the cookbook

Instance Method Summary collapse

Constructor Details

#initialize(cookbook) ⇒ TestVersion

Returns a new instance of TestVersion.



29
30
31
# File 'lib/reagan/test_version.rb', line 29

def initialize(cookbook)
  @cookbook = cookbook
end

Instance Method Details

#check_commit_versionObject

grab the version of the cookbook in the local metadata



34
35
36
37
38
# File 'lib/reagan/test_version.rb', line 34

def check_commit_version
   = Chef::Cookbook::Metadata.new
  .from_file(File.join(Config.settings['jenkins']['workspace_dir'], 'cookbooks', @cookbook, 'metadata.rb'))
  .version
end

#check_server_versionObject

grab the most recent version of the cookbook on the chef server



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reagan/test_version.rb', line 41

def check_server_version
  Ridley::Logging.logger.level = Logger.const_get 'ERROR'
  begin
    server_con = Ridley.new(
      server_url: Config.settings['chef']['server_url'],
      client_name: Config.settings['chef']['client_name'],
      client_key: Config.settings['chef']['pem_path'],
      ssl: { verify: false }
    )
    # return the version if the cookbook exists or return 0.0.0 if it's a new coobook not yet on the server
    if server_con.cookbook.all[@cookbook]
      server_con.cookbook.all[@cookbook][0]
    else
      '0.0.0'
    end
  rescue Ridley::Errors::ConnectionFailed
    puts '    ERROR: Failed to connect to the Chef server. Cannot continue'.to_red
    exit 1
  end
end

#testObject

performs version update test returns true if version has been rev’d and false if not



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/reagan/test_version.rb', line 64

def test
  commit_v = check_commit_version
  serv_v = check_server_version
  updated = Gem::Version.new(serv_v) < Gem::Version.new(commit_v) ? true : false

  puts "Running cookbook version rev'd test:"
  puts "Server version: #{serv_v}".indent
  puts "Commit version: #{commit_v}".indent
  puts updated ? "PASS: Metadata version has been rev'd".indent : "FAIL: Metadata version has NOT been rev'd".indent.to_red
  updated
end