Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syberalexis committed Apr 12, 2020
1 parent 590d161 commit b7476db
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file.

## Release 1.0.0

First Major Release

**Features**

- Add unit tests

## Release 0.1.4

**Bug fixes**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ storj::storage: '[THE DEDICATED ANOUT OF STORAGE]'
storj::storage_path: '[THE DEDICATED STORAGE LOCATION]'
```
### Examples
## Examples
#### Default installation
Expand Down
6 changes: 3 additions & 3 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Group under which storj is running.

Default value: 'storj'

##### `usershell`
##### `user_shell`

Data type: `Stdlib::Absolutepath`

Expand Down Expand Up @@ -406,13 +406,13 @@ Group under which storj is running.

Default value: $storj::group

##### `usershell`
##### `user_shell`

Data type: `Stdlib::Absolutepath`

if requested, we create a user for storj. The default shell is false. It can be overwritten to any valid path.

Default value: $storj::usershell
Default value: $storj::user_shell

##### `extra_groups`

Expand Down
4 changes: 2 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# User running storj.
# @param group
# Group under which storj is running.
# @param usershell
# @param user_shell
# if requested, we create a user for storj. The default shell is false. It can be overwritten to any valid path.
# @param extra_groups
# Add other groups to the managed user.
Expand Down Expand Up @@ -82,7 +82,7 @@
Boolean $manage_group = true,
String $user = 'storj',
String $group = 'storj',
Stdlib::Absolutepath $usershell = '/bin/false',
Stdlib::Absolutepath $user_shell = '/bin/false',
Array[String] $extra_groups = [],

# Service
Expand Down
11 changes: 7 additions & 4 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# User running storj.
# @param group
# Group under which storj is running.
# @param usershell
# @param user_shell
# if requested, we create a user for storj. The default shell is false. It can be overwritten to any valid path.
# @param extra_groups
# Add other groups to the managed user.
Expand All @@ -48,7 +48,7 @@
Boolean $manage_group = $storj::manage_group,
String $user = $storj::user,
String $group = $storj::group,
Stdlib::Absolutepath $usershell = $storj::usershell,
Stdlib::Absolutepath $user_shell = $storj::user_shell,
Array[String] $extra_groups = $storj::extra_groups,
) {
ensure_packages('unzip', { ensure => 'present' })
Expand All @@ -65,7 +65,10 @@
}
file {
"${base_dir}/storj-${version}.${os}-${arch}":
ensure => 'directory';
ensure => 'directory',
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0755';
"${base_dir}/storj-${version}.${os}-${arch}/identity":
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
Expand All @@ -85,7 +88,7 @@
ensure => 'present',
system => true,
groups => concat([$group, 'docker'], $extra_groups),
shell => $usershell,
shell => $user_shell,
})

User[$user] -> File[$config_dir]
Expand Down
2 changes: 1 addition & 1 deletion manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
String $mail = $storj::mail,
Stdlib::Host $host = $storj::host,
String $storage = $storj::storage,
Stdlib::Absolutepath $config_dir = $storj::config_dir,
Stdlib::Absolutepath $config_dir = $storj::config_dir,
Stdlib::Absolutepath $storage_path = $storj::storage_path,
String $docker_tag = $storj::docker_tag,
) {
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maeq-storj",
"version": "0.1.4",
"version": "1.0.0",
"author": "maeq",
"summary": "This module install and configure Storj storagenode",
"license": "Apache-2.0",
Expand Down
179 changes: 179 additions & 0 deletions spec/classes/storj_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,189 @@
parameters
end

s_os = os_facts[:kernel].downcase

s_arch = case os_facts[:architecture]
when 'aarch64'
'arm64'
when 'armv7l'
'arm'
else
'amd64'
end

authorization_token = parameters[:authorization_token]
wallet = parameters[:wallet]
mail = parameters[:mail]
host = parameters[:host]
storage = parameters[:storage]
storage_path = parameters[:storage_path]
version = parameters[:version]
base_dir = parameters[:base_dir] || '/opt'
bin_dir = parameters[:bin_dir] || '/usr/local/bin'
base_url = parameters[:base_url] || 'https://github.com/storj/storj/releases/download'
download_extension = parameters[:download_extension] || 'zip'
download_url = parameters[:download_url] || "#{base_url}/v#{version}/identity_#{s_os}_#{s_arch}.#{download_extension}"
extract_command = parameters[:extract_command] || nil
config_dir = parameters[:config_dir] || '/etc/storj'
manage_user = parameters[:manage_user].nil? ? true : parameters[:manage_user]
manage_group = parameters[:manage_group].nil? ? true : parameters[:manage_group]
user = parameters[:user] || 'storj'
group = parameters[:group] || 'storj'
user_shell = parameters[:user_shell] || '/bin/false'
extra_groups = parameters[:extra_groups] || []
service_ensure = parameters[:service_ensure] || 'running'
docker_tag = parameters[:docker_tag] || 'beta'
port = parameters[:port] || 28_967
dashboard_port = parameters[:dashboard_port] || 14_002
manage_docker = parameters[:manage_docker].nil? ? true : parameters[:manage_docker]

case service_ensure
when 'running'
file_ensure = 'file'
service_ensure = 'running'
when 'stopped'
file_ensure = 'file'
service_ensure = 'stopped'
else
file_ensure = 'absent'
service_ensure = 'stopped'
end

# Compilation
it {
is_expected.to compile
}

# Implementation
it {
is_expected.to contain_class('storj::install')
is_expected.to contain_class('storj::config')
is_expected.to contain_class('storj::service')

if manage_docker
is_expected.to contain_class('docker')
else
is_expected.not_to contain_class('docker')
end
}

# Install
it {
is_expected.to contain_file("#{base_dir}/storj-#{version}.#{s_os}-#{s_arch}").with(
'ensure' => 'directory',
'owner' => 'root',
'group' => '0',
'mode' => '0755',
)
is_expected.to contain_archive("/tmp/identity_#{version}.#{download_extension}").with(
'ensure' => 'present',
'extract' => true,
'extract_path' => "#{base_dir}/storj-#{version}.#{s_os}-#{s_arch}",
'source' => download_url,
'checksum_verify' => false,
'creates' => "#{base_dir}/storj-#{version}.#{s_os}-#{s_arch}/identity",
'cleanup' => true,
'extract_command' => extract_command,
)
is_expected.to contain_file("#{base_dir}/storj-#{version}.#{s_os}-#{s_arch}/identity").with(
'owner' => 'root',
'group' => '0',
'mode' => '0555',
)
is_expected.to contain_file("#{bin_dir}/identity").with(
'ensure' => 'link',
'target' => "#{base_dir}/storj-#{version}.#{s_os}-#{s_arch}/identity",
)

# User
if manage_user
is_expected.to contain_user(user).with(
'ensure' => 'present',
'system' => true,
'groups' => [group, 'docker'] + extra_groups,
'shell' => user_shell,
)
else
is_expected.not_to contain_user(user)
end
# Group
if manage_group
is_expected.to contain_group(group).with(
'ensure' => 'present',
'system' => true,
)
else
is_expected.not_to contain_group(group)
end

is_expected.to contain_file(config_dir).with(
'ensure' => 'directory',
'owner' => user,
'group' => group,
)
}

# Config
it {
is_expected.to contain_exec('Request authorization command').with(
'command' => "#{bin_dir}/identity authorize storagenode #{authorization_token} --identity-dir #{config_dir} --config-dir #{config_dir}",
'onlyif' => "/usr/bin/test `grep -c BEGIN #{config_dir}/storagenode/identity.cert` != 3",
'user' => group,
'group' => user,
'logoutput' => true,
).that_notifies('Service[storagenode]')

is_expected.to contain_exec('Check certs integrity command').with(
'command' => "/usr/bin/test `grep -c BEGIN #{config_dir}/storagenode/ca.cert` == 2 &&
/usr/bin/test `grep -c BEGIN #{config_dir}/storagenode/identity.cert` == 3",
'logoutput' => true,
'returns' => 0,
)
}

# Service
it {
is_expected.to contain_file('/lib/systemd/system/storagenode.service').with(
'ensure' => file_ensure,
).with_content(
"# THIS FILE IS MANAGED BY PUPPET
[Unit]
Description=Storj storagenode service
BindsTo=docker.service
After=docker.service
[Service]
User=#{user}
Group=#{group}
Environment=NAME=storagenode
Restart=on-failure
RestartSec=10
ExecStartPre=-/usr/bin/docker kill ${NAME}
ExecStartPre=-/usr/bin/docker rm ${NAME}
ExecStart=/usr/bin/docker run --name ${NAME} \\
--stop-timeout 300 \\
-p #{port}:28967 \\
-p #{dashboard_port}:14002 \\
-e WALLET=\"#{wallet}\" \\
-e EMAIL=\"#{mail}\" \\
-e ADDRESS=\"#{host}:#{port}\" \\
-e STORAGE=\"#{storage}\" \\
--mount type=bind,source=\"#{config_dir}/storagenode\",destination=/app/identity \\
--mount type=bind,source=\"#{storage_path}\",destination=/app/config \\
storjlabs/storagenode:#{docker_tag}
ExecStop=/usr/bin/docker stop ${NAME}
[Install]
WantedBy=multi-user.target
",
).that_notifies('Service[storagenode]')

is_expected.to contain_service('storagenode').with(
'ensure' => service_ensure,
'enable' => true,
)
}
end
end
end
Expand Down

0 comments on commit b7476db

Please sign in to comment.