Class: Tes::Request::RSpec::Distribute

Inherits:
Object
  • Object
show all
Includes:
Function
Defined in:
lib/tes/request/rspec/distribute.rb

Constant Summary collapse

DEFAULT_CI_YAML_FILE =
'.ci.yaml'
EXCLUDE_CLUSTER_RES_PATTERN_PROFILE =
'.ci_exclude_res_pattern'
@@ci_yaml_file =
DEFAULT_CI_YAML_FILE
@@ci_exclude_cluster_pattern_file =
EXCLUDE_CLUSTER_RES_PATTERN_PROFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Function

#get_spec_path_info, #parse_spec_profile_lines

Constructor Details

#initialize(project_dir, ci_yaml_file = @@ci_yaml_file) ⇒ Distribute

Returns a new instance of Distribute.

Parameters:

  • project_dir (String)

    测试项目的根目录路径

  • ci_yaml_file (String) (defaults to: @@ci_yaml_file)

    测试项目内的描述spec测试的配置文件路径(相对‘project_dir`)



19
20
21
22
# File 'lib/tes/request/rspec/distribute.rb', line 19

def initialize(project_dir, ci_yaml_file=@@ci_yaml_file)
  @project_dir = project_dir
  @ci_cfg = YAML.load_file(File.join(@project_dir, ci_yaml_file))
end

Instance Attribute Details

#ci_cfgObject (readonly)

Returns the value of attribute ci_cfg.



24
25
26
# File 'lib/tes/request/rspec/distribute.rb', line 24

def ci_cfg
  @ci_cfg
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



24
25
26
# File 'lib/tes/request/rspec/distribute.rb', line 24

def project_dir
  @project_dir
end

Instance Method Details

#distribute_jobs(type, count, res_addition_attr_map = {}, adapt_pool = {}) ⇒ Array<Hash>

生成分发任务的配置结构

Parameters:

  • type (String)

    task type

  • count (Integer)

    分批任务数

  • res_addition_attr_map (Hash) (defaults to: {})

    资源属性需要调整的映射表

  • adapt_pool (Hash, nil) (defaults to: {})

Returns:



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
# File 'lib/tes/request/rspec/distribute.rb', line 32

def distribute_jobs(type, count, res_addition_attr_map={}, adapt_pool = {})
  task_cfg = get_rspec_task(type)
  spec_paths = spec_files(type)
  rspec_parser = Tes::Request::RSpec::ProfileParser.new(spec_paths)
  rspec_parser.parse_profiles!
  rspec_profiles = rspec_parser.profiles

  if res_addition_attr_map and res_addition_attr_map.size > 0
    rspec_profiles.each do |spec|
      res_addition_attr_map.each do |res_filter_pattern, attr_add_map|
        request_asks = spec[:profile].data.select {|ask| ask.to_s.include? res_filter_pattern}
        request_asks.each do |ask|
          # only add the resource attribution when no request the attribution for the resource
          attr_add_map.each do |attr_name, attr_limit|
            unless ask.data.include?(attr_name)
              ask.data[attr_name] = Tes::Request::Expression.new("#{attr_name}#{attr_limit}")
            end
          end
        end
      end
    end
  end


  if adapt_pool and adapt_pool.size > 0
    rspec_profiles.delete_if do |spec|
      pool_not_satisfied = !(spec[:profile].request(adapt_pool).all?)
      warn "POOL is not satisfied for: #{spec[:file]}" if pool_not_satisfied
      pool_not_satisfied
    end
  end

  gen_pieces(rspec_profiles, count).map do |piece|
    profile = piece[:profile]
    specs = piece[:specs].inject([]) do |t, spec|
      file_path = spec[:file].sub(/^#{@project_dir}/, '').sub(/^\//, '')
      if (spec[:locations] and spec[:locations].size > 0) or (spec[:ids] and spec[:ids].size > 0)
        if spec[:locations] and spec[:locations].size > 0
          t.push(file_path + ':' + spec[:locations].join(':'))
        end
        if spec[:ids] and spec[:ids].size > 0
          t.push(file_path + '[' + spec[:ids].join(',') + ']')
        end
      else
        t.push file_path
      end
      t
    end

    {profile: profile, specs: specs}
  end.map {|p| p.merge(tag: task_cfg['tag'])}
end