Skip to content

Commit

Permalink
Testing (#1)
Browse files Browse the repository at this point in the history
* Added pry, byebug & etc

* Added specs for StepDownFromRoot

* Fix StepDownFromRoot

* Switch to alpine linux
  • Loading branch information
vovimayhem authored Feb 17, 2020
1 parent cbacd5a commit 473c887
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM ruby:2.6.5-buster AS development
FROM ruby:2.6.5-alpine AS development

WORKDIR /usr/src

ENV HOME=/usr/src PATH=/usr/src/bin:$PATH

RUN apk add --no-cache su-exec alpine-sdk

COPY Gemfile Gemfile.lock on_container.gemspec /usr/src/
COPY lib/on_container/version.rb /usr/src/lib/on_container/

Expand All @@ -15,7 +17,7 @@ ARG DEVELOPER_USERNAME=you

ENV DEVELOPER_UID=${DEVELOPER_UID}

RUN useradd -r -M -u ${DEVELOPER_UID} -d /usr/src -c "Developer User,,," ${DEVELOPER_USERNAME}
RUN adduser -D -H -u ${DEVELOPER_UID} -h /usr/src -g "Developer User,,," ${DEVELOPER_USERNAME}

FROM development AS testing

Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ gemspec

gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"

gem 'byebug'
gem 'pry-byebug'
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ PATH
GEM
remote: https://rubygems.org/
specs:
byebug (11.1.1)
coderay (1.1.2)
diff-lcs (1.3)
method_source (0.9.2)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.8.0)
byebug (~> 11.0)
pry (~> 0.10)
rake (12.3.3)
rspec (3.9.0)
rspec-core (~> 3.9.0)
Expand All @@ -27,7 +36,9 @@ PLATFORMS

DEPENDENCIES
bundler (~> 1.17)
byebug
on_container!
pry-byebug
rake (~> 12.0)
rspec (~> 3.0)

Expand Down
6 changes: 3 additions & 3 deletions lib/on_container/step_down_from_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def root_user?
end

def developer_uid?
developer_uid_as_integer > 0
developer_uid > 0
end

def developer_uid
@developer_uid ||= ENV['DEVELOPER_UID'].to_i
@developer_uid ||= ENV.fetch('DEVELOPER_UID', '').to_i
end

protected

def switch_to_developer_user
target_user_name = target_user.name
puts "Switching from 'root' user to '#{target_user_name}'..."
exec 'su-exec', target_user_name, $0, *$*
Kernel.exec 'su-exec', target_user_name, $0, *$*
end

def warn_no_developer_uid
Expand Down
63 changes: 63 additions & 0 deletions spec/step_down_from_root_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'on_container/step_down_from_root'

RSpec.describe OnContainer::StepDownFromRoot do
let(:root_user) do
instance_double "struct Etc::Passwd",
name: 'root',
passwd: 'x',
uid: 0,
gid: 0,
gecos: 'root',
dir: '/root',
shell: '/bin/bash'
end

let(:developer_user) do
instance_double "struct Etc::Passwd",
name: 'developer',
passwd: 'x',
uid: 1000,
gid: 1000,
gecos: 'Developer User,,,',
dir: '/usr/src',
shell: '/bin/bash'
end

let(:example_current_user) { root_user }
let(:example_target_user) { developer_user }
let(:example_developer_uid) { '1000' }

before do
allow(ENV).to receive(:fetch).with('DEVELOPER_UID', '') { example_developer_uid }
allow(Etc).to receive(:getpwuid) { example_current_user }
allow(Etc).to receive(:getpwuid).with(example_target_user.uid) { example_target_user }
allow(Kernel).to receive(:exec).with('su-exec', example_target_user.name, any_args)
end

describe '#perform' do
it 'changes to the target user' do
subject.perform
expect(Kernel).to have_received(:exec).with 'su-exec', example_target_user.name, any_args
end

context 'without a developer uid' do
let(:example_developer_uid) { '' }

example 'does not change the current user' do
subject.perform
expect(Kernel).not_to have_received(:exec)
end
end

context 'when not as root' do
let(:example_current_user) { developer_user }

example 'does not change the current user' do
subject.perform
expect(Kernel).not_to have_received(:exec)
end
end
end
end

0 comments on commit 473c887

Please sign in to comment.