Class: Houcho::Spec::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/houcho/spec/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



15
16
17
18
19
20
21
22
# File 'lib/houcho/spec/runner.rb', line 15

def initialize
  @role = Houcho::Role.new
  @host = Houcho::Host.new
  @spec = Houcho::Spec.new
  @outerrole = Houcho::OuterRole.new
  @specdir = Houcho::Config::SPECDIR
  @logger = Logger.new(Houcho::Config::SPECLOG, 10)
end

Instance Method Details

#check(spec, host_count, dry_run = false, console_output = false) ⇒ Object

dry_run for test



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/houcho/spec/runner.rb', line 25

def check(spec, host_count, dry_run = false, console_output = false) #dry_run for test
  spec = spec.is_a?(Array) ? spec : [spec]
  role = spec.map { |s| @spec.details(s)[s]["role"] }.flatten
  host = []

  @role.details(role).each do |rolename, value|
    host.concat(value["host"]).uniq if value["host"]

    if value["outer role"]
      value["outer role"].each do |outerrolename, v|
        host.concat(v["host"]).uniq if v["host"]
      end
    end
  end

  execute_manually(host.sample(host_count), spec, dry_run, console_output)
end

#execute_manually(host, spec, dryrun = false, console_output = false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/houcho/spec/runner.rb', line 44

def execute_manually(host, spec, dryrun = false, console_output = false)
  host = host.is_a?(Array) ? host : [host]
  spec = spec.is_a?(Array) ? spec : [spec]

  run(
    { rand(36**50).to_s(36) => { "host" => host, "spec" => spec } },
    dryrun,
    false,
    console_output
  )
end

#execute_role(role, ex_host = [], dryrun = false, console_output = false) ⇒ Object



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/houcho/spec/runner.rb', line 57

def execute_role(role, ex_host = [], dryrun = false, console_output = false)
  role = role.is_a?(Array) ? role : [role]
  ex_host = ex_host.is_a?(Array) ? ex_host : [ex_host]

  role_valiables = {}

  @role.details(role).each do |rolename, value|
    next unless value["spec"]

    rv = {}
    rv["host"] = []
    rv["spec"] = []

    rv["spec"].concat(value["spec"]).uniq
    rv["host"].concat(value["host"]).uniq if value["host"]

    if value["outer role"]
      value["outer role"].each do |outerrolename, v|
        rv["host"].concat(v["host"]).uniq if v["host"]
      end
    end

    rv["host"] = rv["host"] - ex_host
    role_valiables[rolename] = rv
  end

  run(role_valiables, dryrun, true, console_output)
end

#run(target, dryrun, ci = false, console_output = false) ⇒ Object



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/houcho/spec/runner.rb', line 87

def run(target, dryrun, ci = false, console_output = false)
  messages = []
  failure = false

  target.each do |role, v|
    @spec.check_existence(v["spec"])
    spec = v["spec"].map { |spec| "#{@specdir}/#{spec}_spec.rb" }
    command = "rspec --format documentation #{spec.sort.uniq.join(" ")}"

    if dryrun
      v["host"].each do |host|
        drymsg = "TARGET_HOST=#{host} #{command}"
        puts drymsg if console_output
        messages << drymsg
      end
      next
    end

    Parallel.each(v["host"], :in_threads => Parallel.processor_count)  do |host|
      attr_role = @role.get_attr(role)
      attr_host = @host.get_attr(host)
      attr_outerrole = {}
      outerrole = []
      
      @host.details(host).each do |h, v|
        outerrole = outerrole.concat((v["outer role"] || [])).uniq
      end

      outerrole.each do |o|
        attr_outerrole.merge!(@outerrole.get_attr(o))
      end

      attr = attr_role.merge(attr_outerrole)
      attr = attr.merge(attr_host)

      logmesg = ""
      if attr != {} && attr_host == {} && outerrole.size > 1
        logmsg = "might not be given the appropriate attribute value, because #{host} have no attributes and belongs to more than one outer role - #{outerrole.join(", ")}"
        @logger.warn(host) { logmsg }
        logmsg += "\n"
      end

      result = systemu(
        command,
        :env => {
          "TARGET_HOST" => host,
          "TARGET_HOST_ATTR" => JSON.generate(attr)
        },
        :cwd => File.join(@specdir, "..")
      )

      @logger.info(host) { "#{result[1]}\n#{result[2]}" }
      failure = true if result[0] != 0

      msg = result[1].scan(/\d* examples?, \d* failures?\n/).first
      msg = msg ? msg.chomp : "error"
      msg += "\t#{host} => #{v["spec"].join(", ")}\n"
      puts msg if console_output

      if ci
        begin
          post_result(result, role, host, v["spec"], command, logmsg)
        rescue
          ci = false
        end
      end
    end
  end

  failure ? false : messages
end