reagan 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/reagan +1 -1
- data/lib/change.rb +46 -23
- data/lib/config.rb +11 -14
- data/lib/reagan.rb +13 -7
- data/lib/test_json.rb +45 -0
- data/lib/test_version.rb +12 -7
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38f1709f20a9992d8685abf028ab706efc1fd584
|
4
|
+
data.tar.gz: a20e5db170d66837c2711e3c9aae10f35616d885
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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[:
|
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 =
|
31
|
+
@files = files_to_test
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
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
|
-
#
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
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
|
-
|
73
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
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
|
-
@
|
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
|
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 ?
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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.
|
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
|