Skip to content

Commit bac1b65

Browse files
author
Dan Bode
committed
Add test for keystone auth
This commit adds an integration test for swift configured with keystone.
1 parent 478c4b9 commit bac1b65

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

ext/swift_keystone_test.rb

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# This is a script that uses
4+
# instructions here: http://swift.openstack.org/howto_installmultinode.html
5+
# Even though I expect this script will work with a wide range
6+
# of swift versions, it is currently only tested with: 1.4.6
7+
require 'open3'
8+
require 'fileutils'
9+
10+
# connection variables
11+
proxy_local_net_ip='127.0.0.1'
12+
user='openstack:admin'
13+
password='admin_password'
14+
15+
# headers for curl requests
16+
user_header="-H 'X-Storage-User: #{user}'"
17+
password_header="-H 'X-Storage-Pass: #{password}'"
18+
get_cred_command="curl -k -v #{user_header} #{password_header} http://#{proxy_local_net_ip}:5000/v2.0/"
19+
20+
# verify that we can retrive credentials from our user
21+
result_hash = {}
22+
puts "getting credentials: #{get_cred_command}"
23+
Open3.popen3(get_cred_command) do |stdin, stdout, stderr|
24+
result_hash[:stderr] = stderr.read
25+
result_hash[:stderr].split("\n").each do |line|
26+
if line =~ /^< HTTP\/\d\.\d (\d\d\d)/
27+
result_hash[:status_code]=$1
28+
end
29+
if line =~ /< X-Storage-Url: (http\S+)/
30+
result_hash[:auth_url]=$1
31+
end
32+
if line =~ /< X-Storage-Token: (AUTH_\S+)/
33+
result_hash[:auth_token]=$1
34+
end
35+
end
36+
end
37+
raise(Exception, "Call to get auth tokens failed:\n#{result_hash[:stderr]}") unless result_hash[:status_code] == '200'
38+
39+
# verify that the credentials are valid
40+
auth_token_header="-H 'X-Auth-Token: #{result_hash[:auth_token]}'"
41+
puts auth_token_header
42+
get_account_head="curl -k -v #{auth_token_header} #{result_hash[:auth_url]}"
43+
# what is the expected code?
44+
puts "verifying connection auth: #{get_account_head}"
45+
Open3.popen3(get_account_head) do |stdin, stdout, stderr|
46+
#puts stdout.read
47+
#puts stderr.read
48+
end
49+
50+
51+
swift_command_prefix="swift -A http://#{proxy_local_net_ip}:5000/v2.0/ -V 2 -U #{user} -K #{password}"
52+
53+
swift_test_command="#{swift_command_prefix} stat"
54+
55+
puts "Testing swift: #{swift_test_command}"
56+
status_hash={}
57+
Open3.popen3(swift_test_command) do |stdin, stdout, stderr|
58+
status_hash[:stdout] = stdout.read
59+
status_hash[:stderr] = stderr.read
60+
status_hash[:stdout].split("\n").each do |line|
61+
if line =~ /\s*Containers:\s+(\d+)/
62+
status_hash[:containers] = $1
63+
end
64+
if line =~ /\s*Objects:\s+(\d+)/
65+
status_hash[:objects] = $1
66+
end
67+
end
68+
end
69+
70+
unless(status_hash[:containers] =~ /\d+/ and status_hash[:objects] =~ /\d+/)
71+
raise(Exception, "Expected to find the number of containers/objects:\n#{status_hash[:stdout]}\n#{status_hash[:stderr]}")
72+
else
73+
puts "found containers/objects: #{status_hash[:containers]}/#{status_hash[:objects]}"
74+
end
75+
76+
# test that we can upload something
77+
File.open('/tmp/foo1', 'w') do |fh|
78+
fh.write('test1')
79+
end
80+
81+
container = 'my_container'
82+
83+
swift_upload_command="#{swift_command_prefix} upload #{container} /tmp/foo1"
84+
puts "Uploading file to swift with command: #{swift_upload_command}"
85+
86+
Open3.popen3(swift_upload_command) do |stdin, stdout, stderr|
87+
puts stdout.read
88+
puts stderr.read
89+
end
90+
91+
# test that we can download the thing that we uploaded
92+
download_test_dir = '/tmp/test/downloadtest/'
93+
FileUtils.rm_rf download_test_dir
94+
FileUtils.mkdir_p download_test_dir
95+
96+
swift_download_command="#{swift_command_prefix} download #{container}"
97+
puts "Downloading file with command: #{swift_download_command}"
98+
Dir.chdir(download_test_dir) do
99+
Open3.popen3(swift_download_command) do |stdin, stdout, stderr|
100+
puts stdout.read
101+
puts stderr.read
102+
end
103+
end
104+
105+
expected_file = File.join(download_test_dir, 'tmp', 'foo1')
106+
107+
if File.exists?(expected_file)
108+
if File.read(expected_file) == 'test1'
109+
puts "Dude!!!! It actually seems to work, we can upload and download files!!!!"
110+
else
111+
raise(Exception, "So close, but the contents of the downloaded file are not what I expected: Got: #{File.read(expected_file)}, expected: test1")
112+
end
113+
else
114+
raise(Exception, "file #{expected_file} did not exist somehow, probably b/c swift is not installed correctly")
115+
end

0 commit comments

Comments
 (0)