Class: CTT::Cli::ClientCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/client_collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(command, suite, runner) ⇒ ClientCollector

Returns a new instance of ClientCollector.



6
7
8
9
10
11
12
13
14
# File 'lib/cli/client_collector.rb', line 6

def initialize(command, suite, runner)
  @info = {}
  @suite  = suite
  @suites = runner.suites
  @uuid   = runner.uuid

  @info[:suite]   = @suite
  @info[:command] = command
end

Instance Method Details

#collectObject



34
35
36
37
38
39
40
41
42
# File 'lib/cli/client_collector.rb', line 34

def collect
  get_os
  get_test_reports
  get_uuid
  get_timestamp
  get_hostname
  get_username
  get_ipaddr
end

#get_hostnameObject



44
45
46
# File 'lib/cli/client_collector.rb', line 44

def get_hostname
  @info[:hostname] = `hostname`.strip
end

#get_ipaddrObject



52
53
54
# File 'lib/cli/client_collector.rb', line 52

def get_ipaddr
  @info[:ip] = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
end

#get_osObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cli/client_collector.rb', line 64

def get_os
  @info[:os] = RUBY_PLATFORM

  case 1.size
    when 4
      @info[:platform] = "32bit"
    when 8
      @info[:platform] = "64bit"
    else
      @info[:platform] = nil
  end
end

#get_test_reportsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cli/client_collector.rb', line 78

def get_test_reports
  suite_config_path = File.absolute_path(File.join(@suites.suites["suites"][@suite], TEST_SUITE_CONFIG_FILE))
  suite_config = YAML.load_file(suite_config_path)
  unless suite_config["results"]
    say("no results field in #{suite_config_path}. abort!", :red)
    exit(1)
  end
  report_path = File.absolute_path(File.join(@suites.suites["suites"][@suite], suite_config["results"]))
  unless File.exists?(report_path)
    say("report path did not exists. abort!", :red)
    exit(1)
  end
  @tar_file_path = zip_test_reports(report_path)
end

#get_timestampObject



60
61
62
# File 'lib/cli/client_collector.rb', line 60

def get_timestamp
  @info[:time] = Time.now.getutc.to_i
end

#get_usernameObject



48
49
50
# File 'lib/cli/client_collector.rb', line 48

def get_username
  @info[:username] = Etc.getlogin
end

#get_uuidObject



56
57
58
# File 'lib/cli/client_collector.rb', line 56

def get_uuid
  @info[:uuid] = @uuid.to_s
end

#postObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cli/client_collector.rb', line 16

def post
  collect

  payload = @info.dup
  payload[:file]        = File.open(@tar_file_path, "r")
  payload[:multipart]   = true

  #retry 3 times
  3.times do
    begin
      response = RestClient.post("#{RESULTS_SERVER_URL}/tac/upload", payload)
      break if response.code == 200
    rescue
    end
  end
  FileUtils.rm(payload[:file].path)
end

#zip_test_reports(reports_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cli/client_collector.rb', line 93

def zip_test_reports(reports_path)

  temp_file_path = File.join(Dir.tmpdir, "reports-#{@uuid}.tgz")
  pwd = Dir.pwd
  Dir.chdir(File.dirname(reports_path))
  `tar czf #{temp_file_path} #{File.basename(reports_path)} 2>&1`
  unless $?.exitstatus == 0
    say("fail to tarball test reports. abort!", :red)
    FileUtils.rm(temp_file_path)
    exit(1)
  end
  Dir.chdir(pwd)

  temp_file_path
end