reagan 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcd3469e7bc317073069aca8545749fc91e2deeb
4
- data.tar.gz: 839ce97f7831bb90a2dfe4471e2378bac9abd954
3
+ metadata.gz: 38f1709f20a9992d8685abf028ab706efc1fd584
4
+ data.tar.gz: a20e5db170d66837c2711e3c9aae10f35616d885
5
5
  SHA512:
6
- metadata.gz: e8b5229fabf0a74cb6ecdb940eab38cabc80bdb05a84c902a2d6a24b4e2df8eb8d9aa0768b9150aee6364e2dd0b338d75fdaab11389c316458fac7ed24536b48
7
- data.tar.gz: 3f9918f84823b43ca7219d4defab2ef6a910adea416bc02e7e8919cfc0eab51437e6e2ac1817a0f7c4c938d98474001dc4db592c2edccdba8e84977ca96d8eb9
6
+ metadata.gz: f112be2741f0cd51aaa0db555f011024edaa6d5403d9e59ed58b687c7f65acdb21c386011da6508da874d1e386d5a03c0a87e58bc134b2912557abbf5b0a9db7
7
+ data.tar.gz: 399fc3e48f8c2d483eed5fb991f38cacbc54d22d3831ccace9871e68c529257f6eeb5b7ceef6c9af39cb227dd0e5dcae81b9e5074b0c008073c9344a72f2a4e2
data/bin/reagan CHANGED
@@ -27,7 +27,7 @@ flags = { :pull => nil, :cookbooks => nil, :config => '/etc/reagan.yml' }
27
27
  parser = OptionParser.new do |opts|
28
28
  opts.banner = 'Usage: reagan [options]'
29
29
  opts.on('-o', '--override cb1,cb2', 'Comma separated list of cookbooks to test') do |cookbooks|
30
- flags[:cookbooks] = cookbooks
30
+ flags[:override_cookbooks] = cookbooks
31
31
  end
32
32
 
33
33
  opts.on('-p', '--pull_num 123', 'Github pull number to test') do |pull|
data/lib/change.rb CHANGED
@@ -28,49 +28,72 @@ end
28
28
  class Change < Reagan
29
29
  attr_accessor :files
30
30
  def initialize
31
- @files = list_files_changed
31
+ @files = files_to_test
32
32
  end
33
33
 
34
- def unique_cookbooks(files)
35
- cookbooks = []
36
- files.each do |file|
37
- path_array = file.split('/')
38
- cookbooks << path_array[1] if path_array[0].match('^cookbooks')
34
+ # return hash of json files / cookbooks that have been changed
35
+ def files_to_test
36
+ if @@config['flags']['override_cookbooks']
37
+ files_from_override
38
+ else
39
+ pull = pull_num
40
+ puts "Grabbing contents of pull request #{pull}\n"
41
+ hash_builder(query_gh(pull))
39
42
  end
40
- cookbooks.to_set
41
43
  end
42
44
 
43
- def files_from_pull(pull_changes)
44
- files = []
45
- pull_changes.each do |file|
46
- files << file[:filename]
47
- end
45
+ # build a files hash based on the override cookbooks passed by the user
46
+ def files_from_override
47
+ files = {}
48
+ files['json'] = []
49
+ files['cookbooks'] = @@config['flags']['override_cookbooks'].split(',')
48
50
  files
49
51
  end
50
52
 
53
+ # fetch pull num from either ENV or CLI param
51
54
  def pull_num
52
55
  if @@config['flags']['pull']
53
56
  pull = @@config['flags']['pull']
54
57
  elsif ENV['ghprbPullId']
55
58
  pull = ENV['ghprbPullId']
56
59
  else
57
- fail 'Jenkins ghprbPullId environmental variable not set or --pull option not used. Cannot continue'
60
+ puts 'Jenkins ghprbPullId environmental variable not set or --pull option not used. Cannot continue'
61
+ exit 1
58
62
  end
59
63
  pull
60
64
  end
61
65
 
62
- # return list of cookbooks changed either from override value or from polling github pull request
63
- def list_files_changed
64
- if @@config['flags']['cookbooks']
65
- @@config['flags']['cookbooks'].split(',')
66
- else
67
- pull = pull_num
68
- puts "Grabbing contents of pull request #{pull}\n"
66
+ # queries github for the files that have changed
67
+ def query_gh(pull_num)
68
+ gh = Octokit::Client.new(:access_token => @@config['github']['auth_token'])
69
+ files_from_pull(gh.pull_request_files(@@config ['github']['repo'], pull_num))
70
+ end
71
+
72
+ # convert pull request response to array of changed files
73
+ def files_from_pull(pull_changes)
74
+ files = []
75
+ pull_changes.each do |file|
76
+ files << file[:filename]
77
+ end
78
+ files
79
+ end
69
80
 
70
- gh = Octokit::Client.new(:access_token => @@config['github']['auth_token'])
81
+ # builds a hash of files / cookbooks that changed based on the pull data from GH
82
+ def hash_builder(pull_files)
83
+ files = {}
84
+ files['json'] = []
85
+ files['cookbooks'] = []
86
+ cookbooks = []
87
+
88
+ pull_files.each do |file|
89
+ # populate json array if file is json
90
+ files['json'] << file && next if file.match('.json$')
71
91
 
72
- pull_files = files_from_pull(gh.pull_request_files(@@config ['github']['repo'], pull))
73
- unique_cookbooks(pull_files)
92
+ # populate cookbooks array if filename starts with cookbooks
93
+ cookbooks << file.split('/')[1] if file.match('^cookbooks')
74
94
  end
95
+ # set cookbooks array to set to dedupe list of cookbooks
96
+ files['cookbooks'] = cookbooks.to_set
97
+ files
75
98
  end
76
99
  end
data/lib/config.rb CHANGED
@@ -33,28 +33,25 @@ class ReaganConfig
33
33
 
34
34
  # loads the reagan.yml config file from /etc/reagan.yml or the passed location
35
35
  def load_config_file
36
- begin
37
- config = YAML.load_file(@flags[:config])
38
- if config == false
39
- puts "ERROR: Reagan config at #{@flags[:config]} does not contain any configuration data"
40
- exit 1
41
- end
42
- config
43
- rescue Errno::ENOENT
44
- puts "ERROR: Cannot load Reagan config file at #{@flags[:config]}"
45
- exit 1
46
- rescue Psych::SyntaxError
47
- puts "ERROR: Syntax error in Reagan config file at #{@flags[:config]}"
36
+ config = YAML.load_file(@flags[:config])
37
+ if config == false
38
+ puts "ERROR: Reagan config at #{@flags[:config]} does not contain any configuration data"
48
39
  exit 1
49
40
  end
41
+ config
42
+ rescue Errno::ENOENT
43
+ puts "ERROR: Cannot load Reagan config file at #{@flags[:config]}"
44
+ exit 1
45
+ rescue Psych::SyntaxError
46
+ puts "ERROR: Syntax error in Reagan config file at #{@flags[:config]}"
47
+ exit 1
50
48
  end
51
49
 
52
50
  # join the config file with the passed flags into a single object
53
51
  def build_config
54
52
  config = @config_file
55
53
  config['flags'] = {}
56
- @flags.each{|k,v| config['flags'][k.to_s]=v}
54
+ @flags.each { |k, v| config['flags'][k.to_s] = v }
57
55
  config
58
56
  end
59
-
60
57
  end
data/lib/reagan.rb CHANGED
@@ -23,11 +23,12 @@ class Reagan
23
23
  require 'test_knife'
24
24
  require 'test_version'
25
25
  require 'test_reagan'
26
+ require 'test_json'
26
27
 
27
28
  attr_accessor :config
28
29
  def initialize(flags)
29
30
  @@config = ReaganConfig.new(flags).settings
30
- @cookbooks = Change.new.files
31
+ @changes = Change.new.files
31
32
  end
32
33
 
33
34
  # nicely prints marques
@@ -39,26 +40,31 @@ class Reagan
39
40
 
40
41
  # run tests on each changed cookbook
41
42
  def run
42
- # exit with a friendly message if no cookbooks have been changed
43
- if @cookbooks.empty?
43
+ # exit with a friendly message if nothing we test has been changed
44
+ if @changes['json'].empty? && @changes['cookbooks'].empty?
44
45
  pretty_print('Nothing to test in this change. Reagan approves')
45
46
  exit 0
46
47
  end
47
48
 
48
- pretty_print('The following cookbooks will be tested')
49
- @cookbooks.each { |cb| puts cb }
49
+ pretty_print('The following cookbooks & json files will be tested')
50
+ @changes['cookbooks'].each { |cb| puts cb }
51
+ @changes['json'].each { |json| puts json }
50
52
 
51
53
  results = []
52
- @cookbooks.each do |cookbook|
54
+ @changes['cookbooks'].each do |cookbook|
53
55
  pretty_print("Testing cookbook #{cookbook}")
54
56
  results << TestKnife.new(cookbook).test
55
57
  results << TestVersion.new(cookbook).test
56
58
  results << TestReagan.new(cookbook).test
57
59
  end
60
+ @changes['json'].each do |file|
61
+ pretty_print("Testing JSON file #{file}")
62
+ results << TestJSON.new(file).test
63
+ end
58
64
 
59
65
  # print success or failure
60
66
  failure = results.include?(false)
61
- text = failure ? "Reagan testing has failed" : "All Reagan tests have suceeded"
67
+ text = failure ? 'Reagan testing has failed' : 'All Reagan tests have suceeded'
62
68
  pretty_print(text)
63
69
 
64
70
  # if any test failed then exit 1 so jenkins can pick up the failure
data/lib/test_json.rb ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ begin
20
+ require 'json'
21
+ rescue LoadError => e
22
+ raise "Missing gem or lib #{e}"
23
+ end
24
+
25
+ # tests to make sure the version has been updated on the cookbook
26
+ class TestJSON < Reagan
27
+ def initialize(file)
28
+ @file = file
29
+ end
30
+
31
+ # performs JSON format test
32
+ # returns true if json can be parsed and false if it cannot
33
+ def test
34
+ puts "Running JSON parsing test:"
35
+ begin
36
+ json_file = File.read(File.join(@@config['jenkins']['workspace_dir'], @file))
37
+ JSON.parse(json_file)
38
+ success = true
39
+ rescue JSON::JSONError
40
+ success = false
41
+ end
42
+ puts success ? " PASS: JSON parses" : " FAIL: JSON does NOT parse"
43
+ success
44
+ end
45
+ end
data/lib/test_version.rb CHANGED
@@ -40,13 +40,18 @@ class TestVersion < Reagan
40
40
  # grab the most recent version of the cookbook on the chef server
41
41
  def check_server_version
42
42
  Ridley::Logging.logger.level = Logger.const_get 'ERROR'
43
- server_con = Ridley.new(
44
- server_url: @@config['chef']['server_url'],
45
- client_name: @@config['chef']['client_name'],
46
- client_key: @@config['chef']['pem_path'],
47
- ssl: { verify: false }
48
- )
49
- server_con.cookbook.all[@cookbook][0]
43
+ begin
44
+ server_con = Ridley.new(
45
+ server_url: @@config['chef']['server_url'],
46
+ client_name: @@config['chef']['client_name'],
47
+ client_key: @@config['chef']['pem_path'],
48
+ ssl: { verify: false }
49
+ )
50
+ server_con.cookbook.all[@cookbook][0]
51
+ rescue Ridley::Errors::ConnectionFailed
52
+ puts ' ERROR: Failed to connect to the Chef server. Cannot continue'
53
+ exit 1
54
+ end
50
55
  end
51
56
 
52
57
  # performs version update test
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reagan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Smith
@@ -83,6 +83,7 @@ files:
83
83
  - lib/change.rb
84
84
  - lib/config.rb
85
85
  - lib/reagan.rb
86
+ - lib/test_json.rb
86
87
  - lib/test_knife.rb
87
88
  - lib/test_reagan.rb
88
89
  - lib/test_version.rb