forked from trinitronx/docker-python-simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_simplehttpserver_spec.rb
More file actions
113 lines (95 loc) · 3.96 KB
/
python_simplehttpserver_spec.rb
File metadata and controls
113 lines (95 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'spec_helper'
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'helpers')
require 'singleton_docker_helper'
describe 'trinitronx/python-simplehttpserver' do
RSpec.shared_context "trinitronx/python-simplehttpserver image" do
def image
# This image will now be cached by docker
# and used for the rest of the RSpec tests
# We will instantiate a SingletonDockerImage to store & retrieve the id throughout
# Then return the Docker::Image class for that image ID
singleton_image = SingletonDockerImage.instance
if ! singleton_image.id.nil?
Docker::Image.get(singleton_image.id)
else
img = Docker::Image.build_from_dir('.')
singleton_image.id = img.id
img
end
end
before(:all) do
# Use Global Variable so we always reference the SAME image
puts "Built Image: #{image.id}"
# Tag it with the REPO and git COMMIT ID
@repo = ! ENV['REPO'].nil? ? ENV['REPO'] : 'trinitronx/python-simplehttpserver'
require 'git'
g = Git.open(Dir.pwd)
head = g.gcommit('HEAD').sha if g.index.readable?
@commit = ! ENV['COMMIT'].nil? ? ENV['COMMIT'] : head[0..7]
@commit ||= 'rspec-testing' # If we failed to get a commit sha, label it with this as default
puts "Tagging Image: #{image.id} with: #{@repo}:#{@commit}"
image.tag( :repo => @repo, :tag => @commit, force: true)
set :os, family: :linux # The busybox image is actually buildroot according to /etc/os-release, SpecInfra does not support this well
set :backend, :docker
set :docker_image, image.id
# Busybox image uses:
# # cat /etc/os-release
# NAME=Buildroot
# VERSION=2014.02
# ID=buildroot
# VERSION_ID=2014.02
# PRETTY_NAME="Buildroot 2014.02"
# uname -a
# Linux 8aaca671f421 4.1.13-boot2docker #1 SMP Fri Nov 20 19:05:50 UTC 2015 x86_64 GNU/Linux
end
end
describe 'trinitronx/python-simplehttpserver image' do
include_context "trinitronx/python-simplehttpserver image"
context "inside Docker Container" do
before(:all) {
t3 = Time.now
puts "Dockerfile_spec.rb: Running 'inside Docker Container' tests on: #{image.id} at: #{t3}.#{t3.nsec}"
}
describe file('/bin/python') do
it { should exist }
it { should be_file }
end
describe command("ps -o args= | grep python | grep -v grep | head -n1") do
its(:stdout) { should match /-m SimpleHTTPServer 8080/ }
end
describe port(8080) do
# Avoid race condition when python is running but hasn't grabbed the port yet...
it "grabs port 8080" do
wait_for { port(8080) }.to be_listening
end
it { is_expected.to be_listening }
end
context "when serving a test file" do
before(:all) do
Specinfra::Runner.send_file(File.join(File.dirname(__FILE__), '..', 'fixtures', 'helloworld.txt'), '/var/www/')
end
describe file('/var/www/helloworld.txt') do
it { should be_file }
end
describe port(8080) do
# Avoid race condition when python is running but hasn't grabbed the port yet...
it "grabs port 8080" do
wait_for { port(8080) }.to be_listening
end
it { is_expected.to be_listening }
end
describe command('wget -O - http://localhost:8080/helloworld.txt') do
its(:stdout) { should match /^hello simple world$/ }
its(:stderr) { is_expected.not_to match /can't connect/ }
its(:exit_status) { is_expected.to eq 0 }
end
describe command('wget -O - http://localhost:8080/') do
its(:stdout) { should match /Directory listing for \// }
its(:stdout) { should match /<a href="helloworld\.txt">helloworld\.txt<\/a>/ }
its(:stderr) { is_expected.not_to match /can't connect/ }
its(:exit_status) { is_expected.to eq 0 }
end
end
end
end
end