Skip to content

Commit 7cab33d

Browse files
committed
add tests
1 parent a5eed6a commit 7cab33d

File tree

31 files changed

+501
-22
lines changed

31 files changed

+501
-22
lines changed

.github/workflows/docker-image.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: Create and publish a Docker image
33
on:
44
push:
55
branches:
6-
- 'main'
6+
- main
7+
- feature/*
78
tags:
89
- 'v*'
910

@@ -28,8 +29,8 @@ jobs:
2829

2930
- run: |
3031
bundle install
32+
bundle exec rake spec
3133
bundle exec rake build
32-
3334
- name: Log in to the Container registry
3435
uses: docker/login-action@v1
3536
with:

.rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Gemfile.lock

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PATH
1010
GEM
1111
remote: https://rubygems.org/
1212
specs:
13+
diff-lcs (1.4.4)
14+
docile (1.4.0)
1315
domain_name (0.5.20190701)
1416
unf (>= 0.0.5, < 1.0.0)
1517
http-accept (1.7.0)
@@ -27,18 +29,41 @@ GEM
2729
http-cookie (>= 1.0.2, < 2.0)
2830
mime-types (>= 1.16, < 4.0)
2931
netrc (~> 0.8)
32+
rspec (3.10.0)
33+
rspec-core (~> 3.10.0)
34+
rspec-expectations (~> 3.10.0)
35+
rspec-mocks (~> 3.10.0)
36+
rspec-core (3.10.1)
37+
rspec-support (~> 3.10.0)
38+
rspec-expectations (3.10.1)
39+
diff-lcs (>= 1.2.0, < 2.0)
40+
rspec-support (~> 3.10.0)
41+
rspec-mocks (3.10.2)
42+
diff-lcs (>= 1.2.0, < 2.0)
43+
rspec-support (~> 3.10.0)
44+
rspec-support (3.10.2)
45+
simplecov (0.21.2)
46+
docile (~> 1.1)
47+
simplecov-html (~> 0.11)
48+
simplecov_json_formatter (~> 0.1)
49+
simplecov-html (0.12.3)
50+
simplecov_json_formatter (0.1.3)
3051
unf (0.1.4)
3152
unf_ext
3253
unf_ext (0.0.8)
3354
version (1.1.1)
3455

3556
PLATFORMS
57+
ruby
58+
x86_64-darwin-20
3659
x86_64-linux
3760

3861
DEPENDENCIES
3962
bundler (~> 2.0)
4063
deploy-to-ionos!
4164
rake (~> 13.0)
65+
rspec (= 3.10.0)
66+
simplecov (= 0.21.2)
4267

4368
BUNDLED WITH
4469
2.2.16

Rakefile

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require 'bundler/gem_tasks'
44
require 'rake/version_task'
5+
require 'rspec/core/rake_task'
6+
7+
RSpec::Core::RakeTask.new(:spec)
58

69
task default: :build
710

deploy-to-ionos.gemspec

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
2121

2222
spec.add_development_dependency 'bundler', '~> 2.0'
2323
spec.add_development_dependency 'rake', '~> 13.0'
24+
spec.add_development_dependency 'rspec', '3.10.0'
25+
spec.add_development_dependency 'simplecov', '0.21.2'
2426

2527
spec.add_runtime_dependency 'net-ssh', '6.1.0'
2628
spec.add_runtime_dependency 'passgen', '1.2.0'

exe/deploy-to-ionos

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require 'size_checker'
88
require 'remote_host'
99

1010
config = ConfigurationParser.parse(dist_folder: ENV['DIST_FOLDER'], bootstrap: ENV['BOOTSTRAP'] == 'true')
11-
common_excludes = %w[.deploy-now .git .github]
11+
common_excludes = %w[logs .deploy-now .git .github]
1212
deploy_now = DeployNowApi.new(endpoint: ENV['SERVICE_HOST'],
1313
api_key: ENV['API_KEY'],
1414
project_id: ENV['PROJECT_ID'],

lib/configuration_parser.rb

+26-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ class << self
77
def parse(options)
88
return Configuration.new(options[:dist_folder]) unless File.exist? '.deploy-now/config.yaml'
99

10-
config = YAML.safe_load(File.read('.deploy-now/config.yaml'))
10+
begin
11+
config = YAML.safe_load(File.read('.deploy-now/config.yaml'))
12+
rescue Exception => e
13+
abort "unable to pare the .deploy-now/config.yaml config file\n#{e}"
14+
end
1115

12-
abort 'version must be specified' unless config.include? 'version'
16+
abort 'version must be specified in .deploy-now/config.yaml config file' unless config.include? 'version'
1317

1418
version = config['version'].to_s
1519
abort "unknown version: #{version}" unless version == '1.0'
@@ -21,13 +25,18 @@ def parse(options)
2125

2226
def parse_1_0(config, dist_folder, bootstrap)
2327
if config.include? 'deploy'
28+
abort "only 'bootstrap' or 'recurring are allowed for deploy.force in .deploy-now/config.yaml" unless config['deploy']['force'].eql?('bootstrap') || config['deploy']['force'].eql?('recurring') || config['deploy']['force'].nil?
2429
deploy_config = config['deploy']['force'] || (bootstrap ? 'bootstrap' : 'recurring')
2530

26-
abort "deploy configuration '#{deploy_config}' is missing" unless config['deploy'].include? deploy_config
27-
28-
Configuration.new(dist_folder,
29-
config['deploy'][deploy_config]['excludes'] || [],
30-
config['deploy'][deploy_config]['remote-commands'])
31+
if config['deploy'].include? deploy_config
32+
Configuration.new(dist_folder,
33+
config['deploy'][deploy_config]['excludes'] || [],
34+
config['deploy'][deploy_config]['remote-commands'])
35+
else
36+
Configuration.new(dist_folder,
37+
[],
38+
nil)
39+
end
3140
else
3241
Configuration.new(dist_folder)
3342
end
@@ -43,4 +52,14 @@ def initialize(dist_folder, excludes = [], remote_commands = nil)
4352
@excludes = excludes
4453
@remote_commands = remote_commands
4554
end
55+
56+
def eql(other)
57+
self.dist_folder == other.dist_folder &&
58+
self.excludes == other.excludes &&
59+
self.remote_commands == other.remote_commands
60+
end
61+
62+
def to_s
63+
"{dist: \"#{self.dist_folder}\", excludes: #{self .excludes.to_s}, remote_commands:#{self.remote_commands.to_s}}"
64+
end
4665
end

lib/remote_host.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(options)
1010
end
1111

1212
def deploy(options)
13-
exclude_options = (options[:excludes] + ['logs']).map { |exclude| "--exclude=#{exclude}" }.join(' ')
13+
exclude_options = (options[:excludes]).map { |exclude| "--exclude=#{exclude}" }.join(' ')
1414
cmd = "rsync -av --delete --rsh=\"/usr/bin/sshpass -e ssh -o StrictHostKeyChecking=no\" #{exclude_options} #{options[:dist_folder]} #{@user[:username]}@#{@host}:"
1515
puts cmd
1616
IO.popen(ENV.merge!({ 'SSHPASS' => @user[:password] }), cmd) do |io|

lib/size_checker.rb

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def check(options)
99
remote_excludes_size = calculate_remote_excludes_size(options[:user],
1010
options[:host],
1111
options[:excludes])
12+
1213
size = deployment_size + remote_excludes_size
1314

1415
if size > options[:allowed_size]
@@ -20,37 +21,37 @@ def check(options)
2021

2122
def calculate_local_size(dist_folder, excludes)
2223
get_entries(dist_folder).reject { |entry| excludes.include? entry }
23-
.map { |entry| File.directory?(entry) ? calculate_dir_size(entry) : File.size(entry) }
24+
.map { |entry| File.directory?(dist_folder + File::SEPARATOR + entry) ?
25+
calculate_dir_size(dist_folder + File::SEPARATOR + entry) :
26+
File.size(dist_folder + File::SEPARATOR + entry) }
2427
.sum
2528
end
2629

2730
def calculate_dir_size(dir)
28-
size = File.size(dir)
31+
size = 0
2932
get_entries(dir).each do |entry|
30-
path = "#{dir}/#{entry}"
33+
path = "#{dir}#{File::SEPARATOR}#{entry}"
34+
3135
size += if File.directory? path
3236
calculate_dir_size(path)
3337
else
3438
File.size(path)
3539
end
3640
end
37-
3841
size
3942
end
4043

4144
def get_entries(dir)
4245
Dir.entries(dir).select { |value| value != '.' && value != '..' }
4346
end
4447

45-
def calculate_remote_excludes_size(user, host, dirs)
46-
return 0 if dirs.empty?
48+
def calculate_remote_excludes_size(user, host, excludes)
49+
return 0 if excludes.empty?
50+
51+
exclude_options = (excludes).map { |exclude| "--exclude=#{exclude}" }.join(' ')
4752

4853
Net::SSH.start(host, user[:username], password: user[:password], verify_host_key: :never) do |ssh|
49-
ssh.exec!('shopt -s dotglob; du -sb *')
50-
.scan(/(\d+)\s+(\S+)/)
51-
.select { |_, path| dirs.include? path }
52-
.map { |size, _| size.to_i }
53-
.sum
54+
ssh.exec!("expr $(du -sb . | cut -f1 ) - $(du -sb . #{exclude_options} | cut -f1)").to_i
5455
end
5556
end
5657
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
bootstrap:
4+
remote-commands:
5+
- ls -al
6+
- echo "test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
bootstrap:
4+
excludes:
5+
- var
6+
- storage/database.sqlite
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1.0
2+
deploy:
3+
force: bootstrap
4+
bootstrap:
5+
excludes:
6+
- var/bootstrap
7+
remote-commands:
8+
- echo "bootstrap"
9+
recurring:
10+
excludes:
11+
- var/recurring
12+
remote-commands:
13+
- echo "recurring"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
recurring:
4+
excludes:
5+
- var
6+
- storage/database.sqlite
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: 'Some other Text File
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version: 1.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
deploy:
2+
force: true

spec/configTest/noConfig/.empty

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
recurring:
4+
remote-commands:
5+
- ls -al
6+
- echo "test"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
recurring:
4+
excludes:
5+
- var
6+
- storage/database.sqlite
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1.0
2+
deploy:
3+
force: recurring
4+
bootstrap:
5+
excludes:
6+
- var/bootstrap
7+
remote-commands:
8+
- echo "bootstrap"
9+
recurring:
10+
excludes:
11+
- var/recurring
12+
remote-commands:
13+
- echo "recurring"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.0
2+
deploy:
3+
bootstrap:
4+
excludes:
5+
- var
6+
- storage/database.sqlite
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version: 1.0
2+
deploy:
3+
force: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version: 0.1

0 commit comments

Comments
 (0)