File: swift_keystone_test.erb

package info (click to toggle)
puppet-module-swift 9.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,248 kB
  • ctags: 95
  • sloc: ruby: 5,804; python: 35; makefile: 17; sh: 15
file content (115 lines) | stat: -rw-r--r-- 3,929 bytes parent folder | download
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
114
115
#!/usr/bin/env ruby
#
# This is a script that uses
# instructions here: http://swift.openstack.org/howto_installmultinode.html
# Even though I expect this script will work with a wide range
# of swift versions, it is currently only tested with: 1.4.6
require 'open3'
require 'fileutils'

# connection variables
proxy_local_net_ip="<%= @auth_server %>"
user='<%= @tenant %>:<%= @user %>'
password='<%= @password %>'

## headers for curl requests
#user_header="-H 'X-Storage-User: #{user}'"
#password_header="-H 'X-Storage-Pass: #{password}'"
#get_cred_command="curl -k -v #{user_header} #{password_header} http://#{proxy_local_net_ip}:5000/v2.0/"
#
## verify that we can retrive credentials from our user
#result_hash = {}
#puts "getting credentials: #{get_cred_command}"
#Open3.popen3(get_cred_command) do |stdin, stdout, stderr|
#  result_hash[:stderr] = stderr.read
#  result_hash[:stderr].split("\n").each do |line|
#    if line =~ /^< HTTP\/\d\.\d (\d\d\d)/
#      result_hash[:status_code]=$1
#    end
#    if line =~ /< X-Storage-Url: (http\S+)/
#      result_hash[:auth_url]=$1
#    end
#    if line =~ /< X-Storage-Token: (AUTH_\S+)/
#      result_hash[:auth_token]=$1
#    end
#  end
#end
#raise(Exception, "Call to get auth tokens failed:\n#{result_hash[:stderr]}") unless result_hash[:status_code] == '200'
#
## verify that the credentials are valid
#auth_token_header="-H 'X-Auth-Token: #{result_hash[:auth_token]}'"
#puts auth_token_header
#get_account_head="curl -k -v #{auth_token_header} #{result_hash[:auth_url]}"
## what is the expected code?
#puts "verifying connection auth: #{get_account_head}"
#Open3.popen3(get_account_head) do |stdin, stdout, stderr|
#  #puts stdout.read
#  #puts stderr.read
#end


swift_command_prefix="swift -A http://#{proxy_local_net_ip}:5000/v2.0/ -V 2 -U #{user} -K #{password}"

swift_test_command="#{swift_command_prefix} stat"

puts "Testing swift: #{swift_test_command}"
status_hash={}
Open3.popen3(swift_test_command) do |stdin, stdout, stderr|
  status_hash[:stdout] = stdout.read
  status_hash[:stderr] = stderr.read
  status_hash[:stdout].split("\n").each do |line|
    if line =~ /\s*Containers:\s+(\d+)/
      status_hash[:containers] = $1
    end
    if line =~ /\s*Objects:\s+(\d+)/
      status_hash[:objects] = $1
    end
  end
end

unless(status_hash[:containers] =~ /\d+/ and status_hash[:objects] =~ /\d+/)
  raise(Exception, "Expected to find the number of containers/objects:\n#{status_hash[:stdout]}\n#{status_hash[:stderr]}")
else
  puts "found containers/objects: #{status_hash[:containers]}/#{status_hash[:objects]}"
end

# test that we can upload something
File.open('/tmp/foo1', 'w') do |fh|
  fh.write('test1')
end

container = 'my_container'

swift_upload_command="#{swift_command_prefix} upload #{container} /tmp/foo1"
puts "Uploading file to swift with command: #{swift_upload_command}"

Open3.popen3(swift_upload_command) do |stdin, stdout, stderr|
  puts stdout.read
  puts stderr.read
end

# test that we can download the thing that we uploaded
download_test_dir = '/tmp/test/downloadtest/'
FileUtils.rm_rf download_test_dir
FileUtils.mkdir_p download_test_dir

swift_download_command="#{swift_command_prefix} download #{container}"
puts "Downloading file with command: #{swift_download_command}"
Dir.chdir(download_test_dir) do
  Open3.popen3(swift_download_command) do |stdin, stdout, stderr|
    puts stdout.read
    puts stderr.read
  end
end

expected_file = File.join(download_test_dir, 'tmp', 'foo1')

if File.exists?(expected_file)
  if File.read(expected_file) == 'test1'
    puts "Dude!!!! It actually seems to work, we can upload and download files!!!!"
  else
    raise(Exception, "So close, but the contents of the downloaded file are not what I expected: Got: #{File.read(expected_file)}, expected: test1")
  end
else
  raise(Exception, "file #{expected_file} did not exist somehow, probably b/c swift is not installed correctly")
end