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
15
16
17
# File 'lib/cli/client_collector.rb', line 6

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

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

end

Instance Method Details

#collectObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/cli/client_collector.rb', line 40

def collect
  get_os
  get_ruby_version
  get_test_reports
  get_git_info
  get_uuid
  get_timestamp
  get_hostname
  get_ipaddr
end

#get_git_infoObject



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

def get_git_info
  pwd = Dir.pwd
  Dir.chdir(@suite_path)
  begin
    @info[:git_hash] = `git log --oneline -n 1`
    @info[:email]    = `git config --get user.email`
    @info[:username] = `git config --get user.name`
  rescue
  end
  Dir.chdir(pwd)
end

#get_hostnameObject



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

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

#get_ipaddrObject



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

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

#get_osObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cli/client_collector.rb', line 67

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_ruby_versionObject



92
93
94
# File 'lib/cli/client_collector.rb', line 92

def get_ruby_version
  @info[:ruby_version] = `ruby -v`
end

#get_test_reportsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cli/client_collector.rb', line 96

def get_test_reports
  @suite_path = File.absolute_path(@suites.suites["suites"][@suite])
  suite_config_path = File.join(@suite_path, 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



63
64
65
# File 'lib/cli/client_collector.rb', line 63

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

#get_uuidObject



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

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

#postObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cli/client_collector.rb', line 19

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("#{@url}/upload", payload)
      @log.debug("post results. URL: #{@url}/upload, payload: #{payload}," +
                     " response code: #{response.code}")
      break if response.code == 200
    rescue Exception => e
      @log.error("post results. URL: #{@url}/upload. Error: #{e.to_s}")
    end
  end
  FileUtils.rm(payload[:file].path)
end

#zip_test_reports(reports_path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cli/client_collector.rb', line 112

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