pdksync 0.5.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/managed_modules.yml CHANGED
@@ -1,56 +1,39 @@
1
1
  ---
2
- ## windows team
3
- #- puppetlabs-acl
4
- #- puppetlabs-chocolatey
5
- #- puppetlabs-dsc
6
- #- puppetlabs-dsc_lite
7
- #- puppetlabs-iis
8
- #- puppetlabs-powershell
9
- #- puppetlabs-registry
10
- #- puppetlabs-reboot
11
- #- puppetlabs-scheduled_task
12
- #- puppetlabs-sqlserver
13
- #- puppetlabs-windows
14
- #- puppetlabs-wsus_client
15
- ## modules team
2
+ ## Content and Tooling Team
3
+ #- device_manager
16
4
  #- puppetlabs-accounts
5
+ #- puppetlabs-acl
17
6
  #- puppetlabs-apache
18
7
  #- puppetlabs-apt
19
- #- puppetlabs-bootstrap
8
+ #- puppetlabs-chocolatey
20
9
  #- puppetlabs-concat
10
+ #- puppetlabs-docker
21
11
  #- puppetlabs-exec
22
12
  #- puppetlabs-facter_task
23
13
  #- puppetlabs-firewall
24
14
  #- puppetlabs-haproxy
25
- #- puppetlabs-ibm_installation_manager
15
+ #- puppetlabs-iis
26
16
  #- puppetlabs-inifile
27
17
  #- puppetlabs-java
28
18
  #- puppetlabs-java_ks
19
+ #- puppetlabs-kubernetes
20
+ #- puppetlabs-lvm
29
21
  #- puppetlabs-motd
22
+ #- puppetlabs-mount_iso
30
23
  #- puppetlabs-mysql
24
+ #- puppetlabs-node_encrypt
31
25
  #- puppetlabs-ntp
32
26
  #- puppetlabs-package
33
27
  #- puppetlabs-postgresql
28
+ #- puppetlabs-powershell
34
29
  #- puppetlabs-puppet_conf
35
- #- puppetlabs-resource
30
+ #- puppetlabs-reboot
31
+ #- puppetlabs-registry
36
32
  #- puppetlabs-satellite_pe_tools
33
+ #- puppetlabs-scheduled_task
37
34
  #- puppetlabs-service
35
+ #- puppetlabs-sqlserver
38
36
  #- puppetlabs-stdlib
39
- #- puppetlabs-tagmail
40
37
  #- puppetlabs-tomcat
41
- #- puppetlabs-translate
42
38
  #- puppetlabs-vcsrepo
43
- #- puppetlabs-websphere_application_server
44
- ## networking team
45
- #- cisco_ios
46
- #- device_manager
47
- #- puppetlabs-panos
48
- #- puppetlabs-resource_api
49
- ## testing only
50
- #- puppetlabs-testing
51
- #- puppetlabs-testing1
52
- #- puppetlabs-testing2
53
- ## cloud and containers team
54
- #- puppetlabs-kubernetes
55
- #- puppetlabs-helm
56
- #- puppetlabs-rook
39
+ #- puppetlabs-wsus_client
data/pdksync.gemspec CHANGED
@@ -1,31 +1,34 @@
1
1
  lib = File.expand_path('../lib', __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'pdksync/version'
3
4
 
4
5
  Gem::Specification.new do |spec|
5
- spec.name = 'pdksync'
6
- spec.version = '0.5.0'
7
- spec.authors = ['Puppet']
8
- spec.email = ['']
9
- spec.summary = 'Puppet Module PDK Synchronizer'
10
- spec.description = 'Utility to synchronize common files across puppet modules using PDK Update.'
11
- spec.homepage = 'http://github.com/puppetlabs/pdksync'
12
- spec.license = 'Apache-2.0'
13
- spec.required_ruby_version = '>= 2.0.0'
6
+ spec.name = 'pdksync'
7
+ spec.version = PdkSync::VERSION
8
+ spec.authors = ['Puppet']
9
+ spec.email = ['']
10
+ spec.summary = 'Puppet Module PDK Synchronizer'
11
+ spec.description = 'Utility to synchronize common files across puppet modules using PDK Update.'
12
+ spec.homepage = 'http://github.com/puppetlabs/pdksync'
13
+ spec.license = 'Apache-2.0'
14
+ spec.required_ruby_version = '>= 2.7'
14
15
 
15
- spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
19
  spec.require_paths = ['lib']
19
20
 
20
- spec.add_development_dependency 'bundler', '~> 1.15'
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'codecov'
23
+ spec.add_development_dependency 'pry'
21
24
  spec.add_development_dependency 'rspec'
22
25
  spec.add_development_dependency 'rubocop', '~> 0.50.0'
23
- spec.add_development_dependency 'pry'
24
-
26
+ spec.add_development_dependency 'simplecov'
27
+ spec.add_development_dependency 'simplecov-console'
28
+ spec.add_runtime_dependency 'pdk', '>= 1.14.1'
25
29
  spec.add_runtime_dependency 'git', '~>1.3'
26
- spec.add_runtime_dependency 'pdk', '>= 1.4.1'
27
30
  spec.add_runtime_dependency 'rake'
28
31
  spec.add_runtime_dependency 'gitlab'
29
32
  spec.add_runtime_dependency 'octokit'
30
- spec.add_runtime_dependency 'colorize'
33
+ spec.add_runtime_dependency 'jenkins_api_client'
31
34
  end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'pdksync/configuration'
3
+
4
+ RSpec.describe 'configuration' do
5
+ before(:each) do
6
+ allow(ENV).to receive(:[]).with('HOME').and_return('./')
7
+ allow(ENV).to receive(:[]).with('GITHUB_TOKEN').and_return('blah')
8
+ allow(ENV).to receive(:[]).with('PDKSYNC_CONFIG_PATH').and_return(nil)
9
+ end
10
+
11
+ let(:instance) do
12
+ PdkSync::Configuration.new
13
+ end
14
+
15
+ it '#new' do
16
+ expect(instance).to be_a PdkSync::Configuration
17
+ end
18
+
19
+ it 'passes when token is provided' do
20
+ expect(instance).to be_a PdkSync::Configuration
21
+ end
22
+
23
+ it 'raises error without token' do
24
+ allow(ENV).to receive(:[]).with('GITHUB_TOKEN').and_return(nil)
25
+ expect { instance }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ it '#custom_config' do
29
+ expect(instance.custom_config).to be_a Hash
30
+ end
31
+
32
+ it '#custom_config does not exist' do
33
+ expect(instance.custom_config('/tmp/blah')).to be_a Hash
34
+ end
35
+
36
+ it '#custom_config exists' do
37
+ config = File.join(fixtures_dir, 'pdksync.yml')
38
+ data = instance.custom_config(config)
39
+ expect(data).to be_a Hash
40
+ expect(data[:namespace]).to eq('voxpupuli')
41
+ end
42
+
43
+ it '#locate_config_path' do
44
+ expect(instance.local_config_path).to be_nil
45
+ end
46
+
47
+ it '#locate_config_path with value' do
48
+ config = File.join(fixtures_dir, 'pdksync.yml')
49
+ expect(instance.locate_config_path(config)).to eq(config)
50
+ end
51
+
52
+ it 'gets a different config file path when variable is used' do
53
+ allow(ENV).to receive(:[]).with('PDKSYNC_CONFIG_PATH').and_return(File.join(fixtures_dir, 'pdksync.yml'))
54
+ expect(instance.locate_config_path(ENV['PDKSYNC_CONFIG_PATH'])).to eq(File.join(fixtures_dir, 'pdksync.yml'))
55
+ end
56
+ end
@@ -0,0 +1,2 @@
1
+ ---
2
+ - puppetlabs/puppetlabs-motd
@@ -0,0 +1,2 @@
1
+ ---
2
+ namespace: voxpupuli
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'pdksync/logger'
3
+
4
+ RSpec.describe 'logger' do
5
+ before(:each) do
6
+ allow(ENV).to receive(:[]).with('PDKSYNC_LOG_FILENAME').and_return('dev/')
7
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return(nil)
8
+ # allow(PdkSync::Logger).to receive(:logger).and_return(PdkSync::Logger.logger($stderr))
9
+ end
10
+
11
+ let(:logger) do
12
+ PdkSync::Logger.instance_variable_set('@logger', nil)
13
+ PdkSync::Logger.logger
14
+ end
15
+
16
+ it '#self.logger' do
17
+ expect(PdkSync::Logger.logger).to be_a Logger
18
+ end
19
+
20
+ it '#self.debug' do
21
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return('debug')
22
+ expect(logger.debug('this is a debug')).to be_truthy
23
+ # wasn't able to capture stdout with rspec, no idea why
24
+ # expect { logger.debug("this is a debug") }.to output(/DEBUG - PdkSync: this is a debug/).to_stdout
25
+ end
26
+
27
+ it '#self.warn' do
28
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return('warn')
29
+ # wasn't able to capture stdout with rspec, no idea why
30
+ expect(logger.warn('this is a warning')).to be_truthy # output(/WARN - PdkSync: this is a warning/).to_stdout
31
+ end
32
+
33
+ it '#self.info' do
34
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return('info')
35
+ # wasn't able to capture stdout with rspec, no idea why
36
+ expect(logger.info('this is a info')).to be_truthy # output(/INFO - PdkSync: this is a info/).to_stderr
37
+ end
38
+
39
+ it '#self.fatal' do
40
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return('error')
41
+ # wasn't able to capture stdout with rspec, no idea why
42
+ expect(logger.fatal('this is a fatal')).to be_truthy # output(/FATAL - PdkSync: this is a fatal/).to_stderr
43
+ end
44
+ end
@@ -0,0 +1,185 @@
1
+ require 'pdksync'
2
+ require 'spec_helper'
3
+ require 'git'
4
+ require 'fileutils'
5
+ require 'octokit'
6
+
7
+ describe PdkSync do
8
+ before(:all) do
9
+ @pdksync_dir = './modules_pdksync'
10
+ @pdksync_gem_dir = './gems_pdksync'
11
+ module_name = 'puppetlabs-motd'
12
+ gem_name = 'puppet-strings'
13
+ @module_names = ['puppetlabs-motd']
14
+ @output_path_module = "#{@pdksync_dir}/#{module_name}"
15
+ @output_path_gem = "#{@pdksync_gem_dir}/#{gem_name}"
16
+ @folder = Dir.pwd
17
+ # Make changes to modules_managed.yaml file
18
+ text = File.read('managed_modules.yml')
19
+ new_contents = text.gsub(%r{#- puppetlabs-motd$}, '- puppetlabs-motd')
20
+ File.open('managed_modules.yml', 'w') { |file| file.puts new_contents }
21
+ end
22
+
23
+ let(:platform) { Object.new }
24
+
25
+ before(:each) do
26
+ allow(ENV).to receive(:[]).with('HOME').and_return('./')
27
+ allow(ENV).to receive(:[]).with('GIT_DIR').and_return(nil)
28
+ allow(ENV).to receive(:[]).with('GIT_WORK_TREE').and_return(nil)
29
+ allow(ENV).to receive(:[]).with('GIT_INDEX_FILE').and_return(nil)
30
+ allow(ENV).to receive(:[]).with('PDKSYNC_LOG_FILENAME').and_return(nil)
31
+ allow(ENV).to receive(:[]).with('LOG_LEVEL').and_return(nil)
32
+ allow(ENV).to receive(:[]).with('GIT_SSH').and_return(nil)
33
+ allow(ENV).to receive(:[]).with('GIT_PATH').and_return(nil)
34
+ allow(ENV).to receive(:[]).with('GITHUB_TOKEN').and_return('blah')
35
+ allow(ENV).to receive(:[]).with('GEMFURY_TOKEN').and_return('blah')
36
+ allow(ENV).to receive(:[]).with('PDKSYNC_VERSION_CHECK').and_return(nil)
37
+ allow(ENV).to receive(:[]).with('http_proxy').and_return(nil)
38
+ allow(ENV).to receive(:[]).with('HTTP_PROXY').and_return(nil)
39
+ allow(ENV).to receive(:[]).with('PDKSYNC_CONFIG_PATH').and_return(nil)
40
+ allow(ENV).to receive(:[]).with('TERM').and_return(nil)
41
+ allow(ENV).to receive(:[]).with('NO_COLOR').and_return(nil)
42
+ allow(PdkSync::Utils).to receive(:return_modules).and_return(@module_names)
43
+ allow(PdkSync::Utils).to receive(:validate_modules_exist).and_return(@module_names)
44
+ allow(PdkSync::Utils).to receive(:setup_client).and_return(git_client)
45
+ Dir.chdir(@folder)
46
+ allow(PdkSync::GitPlatformClient).to receive(:new).and_return(platform)
47
+ allow(Octokit).to receive(:tags).with('puppetlabs/pdk').and_return([{ name: '1' }])
48
+ allow(PdkSync::Utils.configuration).to receive(:git_base_uri).and_return('https://github.com')
49
+ end
50
+
51
+ let(:git_client) do
52
+ double(PdkSync::GitPlatformClient)
53
+ end
54
+
55
+ context 'main method' do
56
+ it 'runs clone sucessfully' do
57
+ allow(PdkSync::Utils).to receive(:setup_client).and_return(git_client)
58
+ FileUtils.rm_rf(@pdksync_dir)
59
+ PdkSync::Utils.create_filespace
60
+ PdkSync.main(steps: [:clone])
61
+ expect(Dir.exist?(@pdksync_dir)).to be(true)
62
+ expect(Dir.exist?(@output_path_module)).to be(true)
63
+ end
64
+
65
+ it 'runs pdk convert, and files have changed' do
66
+ PdkSync.main(steps: [:pdk_convert])
67
+ File.exist?("#{@output_path_module}/convert_report.txt")
68
+ end
69
+
70
+ it 'raise when running a command with no argument' do
71
+ expect { PdkSync.main(steps: [:run_a_command]) }.to raise_error(RuntimeError, %r{"run_a_command" requires an argument to run.})
72
+ end
73
+
74
+ it 'runs a command "touch cat.meow"' do
75
+ PdkSync.main(steps: [:run_a_command], args: { command: 'touch cat.meow' })
76
+ expect File.exist?("#{@output_path_module}/cat.meow")
77
+ end
78
+
79
+ it 'raise when create_commit with no arguments' do
80
+ expect { PdkSync.main(steps: [:create_commit]) }.to raise_error(RuntimeError, %r{Needs a branch_name and commit_message})
81
+ end
82
+
83
+ it 'raise when create_pr with no arguments' do
84
+ expect { PdkSync.main(steps: [:create_pr]) }.to raise_error(RuntimeError, %r{Needs a pr_title})
85
+ end
86
+
87
+ it 'create_pr with 1 argument' do
88
+ expect { PdkSync.main(steps: [:create_pr], args: { pr_title: 'some title' }) }.to_not raise_error
89
+ end
90
+
91
+ it 'raise when clean_branches with no arguments' do
92
+ expect { PdkSync.main(steps: [:clean_branches]) }.to raise_error(RuntimeError, %r{Needs a branch_name, and the branch name contains the string pdksync})
93
+ end
94
+
95
+ it 'raise when gem_file_update with no arguments' do
96
+ expect { PdkSync.main(steps: [:gem_file_update]) }.to raise_error(NoMethodError)
97
+ end
98
+ it 'gem_file_update runs with invalid gem_line given' do
99
+ expect { PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus', gem_line: "gem 'puppet_litmus'\, git: 'https://github.com/test/puppet_litmus.git'" }) }. to raise_error(Errno::ENOENT) # rubocop:disable Metrics/LineLength
100
+ end
101
+ it 'gem_file_update runs with invalid gem_sha_replacer' do
102
+ expect { PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus', gem_sha_finder: 'jsjsjsjsjsjsjs', gem_sha_replacer: 'abcdefgjhkk' }) }.to raise_error(RuntimeError) # , ("Couldn't find sha: abcdefgjhkk in your repository: puppet_litmus")) # rubocop:disable Metrics/LineLength
103
+ end
104
+ it 'gem_file_update runs with invalid gem_version_replacer' do
105
+ expect { PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus', gem_version_finder: '<= 0.4.9', gem_version_replacer: '<= 1.4.11' }) }.to raise_error(RuntimeError) # , ("Couldn't find version: 1.4.11 in your repository: puppet_litmus")) # rubocop:disable Metrics/LineLength
106
+ end
107
+ it 'gem_file_update runs with invalid gem_branch_replacer' do
108
+ expect { PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus', gem_branch_finder: 'jsjsjsjsjsjsjs', gem_branch_replacer: 'abcdefgjhkk' }) }.to raise_error(RuntimeError) # , "Couldn't find branch: abcdefgjhkk in your repository: puppet_litmus") # rubocop:disable Metrics/LineLength
109
+ end
110
+ it 'raise when run_tests with no arguments' do
111
+ expect { PdkSync.main(steps: [:run_tests_locally]) }.to raise_error(NoMethodError) # , %r{run_tests" requires arguments (module_type) to run.})
112
+ end
113
+ it 'raise when run_tests_jenkins with no arguments' do
114
+ allow(ENV).to receive(:[]).with('JENKINS_USERNAME').and_return('JENKINS_USERNAME')
115
+ allow(ENV).to receive(:[]).with('JENKINS_PASSWORD').and_return('JENKINS_PASSWORD')
116
+ expect { PdkSync.main(steps: [:run_tests_jenkins], args: {}) }.to raise_error(RuntimeError) # , "run_tests_jenkins requires arguments (github_repo, github_branch) to run"
117
+ end
118
+ it 'raise errors without jenkins credentials' do
119
+ allow(ENV).to receive(:[]).with('JENKINS_USERNAME').and_return(nil)
120
+ allow(ENV).to receive(:[]).with('JENKINS_PASSWORD').and_return(nil)
121
+ expect { PdkSync.main(steps: [:run_tests_jenkins], args: { jenkins_server_url: 'https//jenkins.com', github_repo: 'test', github_branch: 'test' }) }.to raise_error(RuntimeError, %r{Jenkins access token for Jenkins not set})
122
+ end
123
+
124
+ describe 'gem_file_update with valid values' do
125
+ before(:all) do
126
+ PdkSync.main(steps: [:gem_file_update], args: {
127
+ gem_to_test: 'puppet_litmus',
128
+ gem_line: "gem 'puppet_litmus'\, git: 'https://github.com/puppetlabs/puppet_litmus.git'\, branch: 'main'\, ref: '04da90638f5b5fd7f007123c8c0cc551c8cb3e54'\, '=0.1.0'"
129
+ })
130
+ end
131
+ it 'gem_file_update with valid gem_branch_replacer' do
132
+ PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus',
133
+ gem_branch_finder: 'master', gem_branch_replacer: 'main' })
134
+ expect(File.read('Gemfile')).to match(%r{main})
135
+ end
136
+ it 'gem_file_update runs, and contains the gem_sha given' do
137
+ PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus',
138
+ gem_sha_finder: '04da90638f5b5fd7f007123c8c0cc551c8cb3e54', gem_sha_replacer: '95ed1c62ffcf89003eb0fe9d66989caa45884538' })
139
+ expect(File.read('Gemfile')).to match(%r{95ed1c62ffcf89003eb0fe9d66989caa45884538})
140
+ end
141
+ it 'gem_file_update runs, and contains the gem_version given' do
142
+ PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus',
143
+ gem_version_finder: '=0.1.0', gem_version_replacer: '<=0.3.0' })
144
+ expect(File.read('Gemfile')).to match(%r{0.3.0})
145
+ end
146
+ it 'gem_file_update with valid gem_line' do
147
+ PdkSync.main(steps: [:gem_file_update], args: { gem_to_test: 'puppet_litmus',
148
+ gem_line: "gem 'puppet_litmus'\, git: 'https://github.com/puppetlabs/puppet_litmus.git'" })
149
+ expect(File.read('Gemfile')).to match(%r{gem 'puppet_litmus', git: 'https://github.com/puppetlabs/puppet_litmus.git'})
150
+ end
151
+ it 'multigem updated in the module gemfile' do
152
+ PdkSync.main(steps: [:multi_gemfile_update], args: { gem_name: 'puppet-module', gemfury_username: 'tester' })
153
+ expect File.exist?("#{@output_path_module}/Gemfile")
154
+ end
155
+ end
156
+ end
157
+ context 'main method' do
158
+ let(:platform) { Object.new }
159
+
160
+ it 'runs clone gem sucessfully' do
161
+ allow(PdkSync::Utils).to receive(:setup_client).and_return(git_client)
162
+ FileUtils.rm_rf(@pdksync_gem_dir)
163
+ PdkSync::Utils.create_filespace_gem
164
+ PdkSync.main(steps: [:clone_gem], args: { gem_name: 'puppet-strings' })
165
+ expect(Dir.exist?(@pdksync_dir)).to be(true)
166
+ expect(Dir.exist?(@pdksync_gem_dir)).to be(true)
167
+ expect(Dir.exist?(@output_path_gem)).to be(true)
168
+ end
169
+ it 'raise when running a command with no argument' do
170
+ expect { PdkSync.main(steps: [:multi_gem_testing]) }.to raise_error(RuntimeError, %r{"multi_gem_testing" requires arguments to run version_file and build_gem})
171
+ end
172
+ it 'runs a command for updating the version and building the gem' do
173
+ allow(Octokit).to receive(:tags).with('puppetlabs/puppet-strings').and_return([{ name: '1' }])
174
+ PdkSync.main(steps: [:multi_gem_testing], args: { gem_name: 'puppet-strings', version_file: 'lib/puppet-strings/version.rb', build_gem: 'rake build', gem_path: 'pkg', gem_username: 'tester' })
175
+ expect File.exist?("#{@output_path_gem}/pkg")
176
+ end
177
+ end
178
+ after(:all) do
179
+ # Make changes to modules_managed.yaml file
180
+ Dir.chdir(@folder)
181
+ text = File.read('managed_modules.yml')
182
+ new_contents = text.gsub(%r{- puppetlabs-motd$}, '#- puppetlabs-motd')
183
+ File.open('managed_modules.yml', 'w') { |file| file.puts new_contents }
184
+ end
185
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV['SIMPLECOV'] == 'yes'
4
+ begin
5
+ require 'simplecov'
6
+ require 'simplecov-console'
7
+ require 'codecov'
8
+
9
+ SimpleCov.formatters = [
10
+ SimpleCov::Formatter::HTMLFormatter,
11
+ SimpleCov::Formatter::Console,
12
+ SimpleCov::Formatter::Codecov
13
+ ]
14
+ SimpleCov.start do
15
+ track_files 'lib/**/*.rb'
16
+ add_filter '/spec'
17
+
18
+ # do not track vendored files
19
+ add_filter '/vendor'
20
+ add_filter '/.vendor'
21
+
22
+ # do not track gitignored files
23
+ # this adds about 4 seconds to the coverage check
24
+ # this could definitely be optimized
25
+ add_filter do |f|
26
+ # system returns true if exit status is 0, which with git-check-ignore means file is ignored
27
+ system("git check-ignore --quiet #{f.filename}")
28
+ end
29
+ end
30
+ rescue LoadError
31
+ raise 'Add the simplecov, simplecov-console, codecov gems to Gemfile to enable this task'
32
+ end
33
+ end
34
+
35
+ require 'rspec'
36
+ require 'git'
37
+ require 'fileutils'
38
+
39
+ def pupmods_dir
40
+ @pupmods_dir ||= begin
41
+ p = File.join(fixtures_dir, 'puppetlabs')
42
+ FileUtils.mkdir_p(p) unless File.exist?(p)
43
+ p
44
+ end
45
+ end
46
+
47
+ def remote_testing_repo
48
+ File.join(pupmods_dir, 'puppetlabs-motd.git')
49
+ end
50
+
51
+ # localizes the remote repo for faster testing, download once and use the local machine as the remote repo
52
+ def setup_fake_module
53
+ Git.clone('https://github.com/puppetlabs/puppetlabs-motd.git', remote_testing_repo) unless Dir.exist?(File.join(remote_testing_repo, '.git'))
54
+ end
55
+
56
+ def destroy_fake_modules
57
+ FileUtils.rm_rf(pupmods_dir)
58
+ end
59
+
60
+ def fixtures_dir
61
+ @fixtures_dir ||= File.join(__dir__, 'fixtures')
62
+ end
63
+
64
+ RSpec.configure do |config|
65
+ config.before(:suite) do
66
+ setup_fake_module
67
+ # provide a fake github token for the tests
68
+ ENV['GITHUB_TOKEN'] = 'github-token'
69
+ end
70
+ config.before(:each) do
71
+ allow(PdkSync::Utils.configuration).to receive(:git_base_uri).and_return("file://#{fixtures_dir}")
72
+ end
73
+ config.after(:suite) { FileUtils.rm_rf(pupmods_dir) }
74
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require 'pdksync/utils'
3
+ require 'tempfile'
4
+
5
+ describe 'PdkSync::Utils' do
6
+ before(:all) do
7
+ @tmp_dir = Dir.mktmpdir('testing')
8
+ end
9
+
10
+ let(:cloned_module) do
11
+ begin
12
+ Git.open(@tmp_dir)
13
+ rescue ArgumentError
14
+ PdkSync::Utils.clone_directory('puppetlabs',
15
+ 'puppetlabs-motd', @tmp_dir)
16
+ end
17
+ end
18
+
19
+ let(:metadata_file) do
20
+ File.join(@tmp_dir, 'metadata.json')
21
+ end
22
+
23
+ before(:each) do
24
+ cloned_module
25
+ cloned_module.config('user.name', 'puppetlabs')
26
+ cloned_module.config('user.email', 'email@email.com')
27
+ end
28
+
29
+ after(:all) do
30
+ FileUtils.remove_entry @tmp_dir
31
+ end
32
+
33
+ it '#self.clone_directory' do
34
+ Dir.mktmpdir do |dir|
35
+ PdkSync::Utils.clone_directory('puppetlabs',
36
+ 'puppetlabs-motd', dir)
37
+ expect(cloned_module).to be_a Git::Base
38
+ end
39
+ end
40
+
41
+ it '#self.create_commit' do
42
+ File.write(File.join(@tmp_dir, 'README.md'), rand(32_332))
43
+ expect(PdkSync::Utils.create_commit(cloned_module, 'main', 'boom')).to match(%r{1 file changed})
44
+ end
45
+
46
+ it '#self.run_command' do
47
+ expect(PdkSync::Utils.run_command('./', 'pwd', nil)).to eq(0)
48
+ end
49
+
50
+ it '#self.pdk_update' do
51
+ expect(PdkSync::Utils.pdk_update(@tmp_dir)).to eq(0)
52
+ end
53
+
54
+ it '#self.return_template_ref' do
55
+ expect(PdkSync::Utils.return_template_ref(metadata_file)).to match(%r{heads/main-0-|tags/})
56
+ end
57
+
58
+ it '#self.module_templates_url' do
59
+ allow(Octokit).to receive(:tags).with('puppetlabs/pdk').and_return([{ name: 'v1.14.1' }])
60
+ url, version = PdkSync::Utils.module_templates_url(metadata_file).split('#')
61
+ expect(url).to eq('https://github.com/puppetlabs/pdk-templates.git')
62
+ expect(version).to match(%r{main})
63
+ end
64
+
65
+ it '#self.change_module_template_url' do
66
+ url = 'https://github.com/nwops/pdk-templates'
67
+ ref = 'special'
68
+ expect(PdkSync::Utils.change_module_template_url(url, ref, metadata_file)).to eq('https://github.com/nwops/pdk-templates#special')
69
+ end
70
+
71
+ it '#self.checkout_branch' do
72
+ PdkSync::Utils.checkout_branch(cloned_module, 'sync1234')
73
+ branch = cloned_module.branches.find { |b| b.name.eql?('pdksync_sync1234') }
74
+ expect(branch).to be_a Git::Branch
75
+ end
76
+
77
+ it '#self.check_pdk_version is false' do
78
+ process = double
79
+ allow(process).to receive(:exitstatus).and_return(true)
80
+ allow(Octokit).to receive(:tags).with('puppetlabs/pdk').and_return([{ name: 'v1.14.1' }])
81
+ allow(PdkSync::Utils).to receive(:return_pdk_path).and_return('/opt/puppetlabs/pdk/bin/pdk')
82
+ allow(Open3).to receive(:capture3).with('/opt/puppetlabs/pdk/bin/pdk --version').and_return(['1.14.0', nil, process])
83
+ expect(PdkSync::Utils.check_pdk_version).to be false
84
+ end
85
+
86
+ it '#self.check_pdk_version is true' do
87
+ process = double
88
+ allow(process).to receive(:exitstatus).and_return(true)
89
+ allow(PdkSync::Utils).to receive(:return_pdk_path).and_return('/opt/puppetlabs/pdk/bin/pdk')
90
+ allow(Open3).to receive(:capture3).with('/opt/puppetlabs/pdk/bin/pdk --version').and_return(['1.14.0', nil, process])
91
+ allow(Octokit).to receive(:tags).with('puppetlabs/pdk').and_return([{ name: 'v1.14.0' }])
92
+ expect(PdkSync::Utils.check_pdk_version).to be true
93
+ end
94
+
95
+ it '#self.check_gem_latest_version' do
96
+ process = double
97
+ allow(process).to receive(:exitstatus).and_return(true)
98
+ allow(Octokit).to receive(:tags).with('puppetlabs/puppet_module_gems').and_return([{ name: '0.4.0' }])
99
+ expect(PdkSync::Utils.check_gem_latest_version('puppet_module_gems')).to eq '0.4.0'
100
+ end
101
+
102
+ it '#self.update_gem_latest_version_by_one' do
103
+ expect(PdkSync::Utils.update_gem_latest_version_by_one('0.4.0')).to eq Gem::Version.new('0.5')
104
+ end
105
+
106
+ it '#self.create_filespace' do
107
+ expect(PdkSync::Utils.create_filespace).to eq('modules_pdksync')
108
+ end
109
+
110
+ it '#self.setup_client' do
111
+ g = double(PdkSync::GitPlatformClient)
112
+ expect(PdkSync::GitPlatformClient).to receive(:new).with(:github,
113
+ {access_token: 'github-token',
114
+ api_endpoint: nil,
115
+ gitlab_api_endpoint: 'https://gitlab.com/api/v4'}).and_return(g)
116
+ expect(PdkSync::Utils.setup_client).to eq(g)
117
+ end
118
+
119
+ it '#self.return_modules' do
120
+ allow_any_instance_of(PdkSync::Configuration).to receive(:managed_modules).and_return(File.join(fixtures_dir, 'fake_managed_modules.yaml'))
121
+ expect(PdkSync::Utils.return_modules).to eq(['puppetlabs/puppetlabs-motd'])
122
+ end
123
+
124
+ it '#self.validate_modules_exist' do
125
+ client = double
126
+ allow_any_instance_of(PdkSync::Configuration).to receive(:managed_modules).and_return(File.join(fixtures_dir, 'fake_managed_modules.yaml'))
127
+ allow(client).to receive(:repository?).with('puppetlabs/puppetlabs-motd').and_return(true)
128
+ expect(PdkSync::Utils.validate_modules_exist(client, ['puppetlabs-motd'])).to be true
129
+ end
130
+
131
+ it '#self.create_filespace_gem' do
132
+ expect(PdkSync::Utils.create_filespace_gem).to eq('gems_pdksync')
133
+ end
134
+ end