Class: CTT::Cli::Command::TestSuite

Inherits:
Base show all
Includes:
CTT::Cli, Interactive
Defined in:
lib/cli/commands/test_suite_management.rb

Constant Summary collapse

SUPPORT_OPTIONS =

USER_INPUT = “USER_INPUT” TEST_SUITE_CONFIG_FILE = “orc.yml”

{"--force" => "bypass git dirty state check"}

Constants included from CTT::Cli

RESULTS_SERVER_URL, STATIC_COMMANDS, TEST_RESULT_FILE, TEST_SUITE_CONFIG_FILE, VERSION

Instance Method Summary collapse

Constructor Details

#initialize(command, args, runner) ⇒ TestSuite

Returns a new instance of TestSuite.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cli/commands/test_suite_management.rb', line 12

def initialize(command, args, runner)
  super(args, runner)

  pieces = command.split(" ")
  pieces.insert(0, "") if pieces.size == 1
  action, suite = pieces

  @action   = action
  @suite    = suite
  @configs  = runner.configs
  @suites   = runner.suites
  @url      = runner.url
  @log      = runner.log
end

Instance Method Details

#check_configurationObject



119
120
121
122
123
124
125
# File 'lib/cli/commands/test_suite_management.rb', line 119

def check_configuration
  check_orc_version
  unless @suites.suites["suites"].has_key?(@suite)
    say("suite configure file: #{@suites.file} did not has key: #{@suite}")
    exit(1)
  end
end

#check_if_dirty_stateObject



127
128
129
130
131
132
133
134
135
# File 'lib/cli/commands/test_suite_management.rb', line 127

def check_if_dirty_state
  if dirty_state?
    say("\n%s\n" % [`git status`])
    say("Your current directory has some local modifications, " +
            "please discard or commit them first.\n" +
            "Or use #{yellow("--force")} to bypass git dirty check.")
    exit(1)
  end
end

#check_orc_versionObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cli/commands/test_suite_management.rb', line 191

def check_orc_version
  response = nil
  #retry 3 times
  3.times do
    begin
      response = RestClient.get("#{@url}/version")
      @log.debug("check version. response body: #{response.to_s}, response code: #{response.code}")
      break if response.code == 200
    rescue Exception => e
      @log.error("check version. url: #{@url}/version, #{e.to_s}")
    end
  end

  if response
    target = JSON.parse(response.to_s)["version"]
    if need_upgrade?(VERSION, target)
      say("your orc-cli gem should be >= #{target}. Abort!", :red)
      exit(1)
    end
  end
end

#configureObject



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
# File 'lib/cli/commands/test_suite_management.rb', line 50

def configure
  puts "configure #{@suite}"
  location = @configs.configs["suites"][@suite]["location"]
  location = "" if location == USER_INPUT
  invalid_input = true
  3.times do
    user_input = ask("suite: #{@suite} source directory:", :default => location).strip
    if user_input =~ /^~/
      user_input = File.expand_path(user_input)
    else
      user_input = File.absolute_path(user_input)
    end
    if Dir.exist?(user_input)
      if File.exist?(File.join(user_input, TEST_SUITE_CONFIG_FILE))
        invalid_input = false
        location = user_input
        break
      else
        say("the configure file: #{yellow(TEST_SUITE_CONFIG_FILE)} " +
                "cannot be found under #{user_input}.")
      end
    else
      say("the directory: #{user_input} is invalid.")
    end
  end

  if invalid_input
    say("invalid inputs for 3 times. abort!", :red)
    exit(1)
  else
    @configs.configs["suites"][@suite]["location"] = location
    @configs.save
    say("configure suite: #{@suite} successfully.", :green)
  end
end

#dirty_state?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
# File 'lib/cli/commands/test_suite_management.rb', line 137

def dirty_state?
  `which git`
  return false unless $? == 0

  Dir.chdir(@suites.suites["suites"][@suite])
  (File.directory?(".git") || File.directory?(File.join("..", ".git"))) \
    && `git status --porcelain | wc -l`.to_i > 0
end

#get_suite_configsObject



180
181
182
183
184
185
186
187
188
189
# File 'lib/cli/commands/test_suite_management.rb', line 180

def get_suite_configs
  suite_configs_path = File.join(@suites.suites["suites"][@suite],
                                 CTT::Cli::TEST_SUITE_CONFIG_FILE)
  @suite_configs ||= YAML.load_file(suite_configs_path)
  unless @suite_configs.is_a?(Hash)
    say("invalid yaml format for file: #{suite_configs_path}", :red)
    exit(1)
  end
  @suite_configs
end

#listObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cli/commands/test_suite_management.rb', line 33

def list
  check_configuration
  get_suite_configs

  say("all subcommands for test suite: #{@suite}", :yellow)
  say("Options:", :yellow)
  SUPPORT_OPTIONS.each do |opt, helper|
    say("\t[#{opt}]   \t#{helper}")
  end
  nl

  @suite_configs["commands"].each do |command, details|
    say("#{@suite} #{command}", :green)
    say("\t#{details["desc"]}\n")
  end
end

#need_upgrade?(current, target) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cli/commands/test_suite_management.rb', line 213

def need_upgrade?(current, target)
  upgrade = false
  curr_vers = current.split(".")
  targ_vers = target.split(".")

  targ_vers.each_with_index do |item, index|
    if item > curr_vers[index]
      upgrade = true
      break
    end
  end

  upgrade
end

#parse_commandObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cli/commands/test_suite_management.rb', line 157

def parse_command
  subcmd = ""
  dependencies = []
  if @args.empty?
    subcmd =  @suite_configs["commands"]["default"]["exec"]
    dependencies = @suite_configs["commands"]["default"]["dependencies"]
  elsif @suite_configs["commands"].has_key?(@args[0])
    subcmd = @suite_configs["commands"][@args[0]]["exec"]
    dependencies = @suite_configs["commands"][@args[0]]["dependencies"]
    @args.delete(@args[0])
  else
    say("#{@args[0]} is not invalid sub-command, run as default command")
    subcmd = @suite_configs["commands"]["default"]["exec"]
    dependencies = @suite_configs["commands"]["default"]["dependencies"]
  end

  unless @args.empty?
    subcmd = subcmd + " " + @args.join(" ")
  end

  [dependencies, subcmd]
end

#parse_optionsObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/cli/commands/test_suite_management.rb', line 146

def parse_options
  @options = {}
  opts = SUPPORT_OPTIONS.keys
  @args.each do |arg|
    if opts.index(arg)
      @options[arg] = true
      @args.delete(arg)
    end
  end
end

#runObject



27
28
29
30
# File 'lib/cli/commands/test_suite_management.rb', line 27

def run
  @action = "test" if @action == ""
  eval(@action)
end

#testObject



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
116
117
# File 'lib/cli/commands/test_suite_management.rb', line 86

def test
  check_configuration
  parse_options
  check_if_dirty_state unless @options["--force"]
  get_suite_configs

  dependencies, command = parse_command

  threads = []
  pwd = Dir.pwd
  Dir.chdir(@suites.suites["suites"][@suite])
  threads << Thread.new do
    # dependency should be successful before run testing command
    say("preparing test suite: #{@suite}...")
    dependencies.each do |d|
      output = `#{d}`
      unless $? == 0
        say(output)
        say("fail to execute dependency command: #{d}. abort!", :red)
        exit(1)
      end
    end

    say("\nrun command: #{yellow(command)}")
    system(command)
  end

  threads.each { |t| t.join }
  Dir.chdir(pwd)
  collector = ClientCollector.new(@runner.command, @suite, @runner)
  collector.post
end