Class: OsCtl::Image::Operations::Test::Run

Inherits:
Base
  • Object
show all
Includes:
Lib::Utils::Log
Defined in:
lib/osctl/image/operations/test/run.rb

Defined Under Namespace

Classes: Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

run

Constructor Details

#initialize(base_dir, build, test, opts = {}) ⇒ Run

Returns a new instance of Run.

Parameters:

Options Hash (opts):

  • :keep_failed (Boolean)


28
29
30
31
32
33
34
# File 'lib/osctl/image/operations/test/run.rb', line 28

def initialize(base_dir, build, test, opts = {})
  @base_dir = base_dir
  @build = build
  @test = test
  @client = OsCtldClient.new
  @keep_failed = opts.has_key?(:keep_failed) ? opts[:keep_failed] : false
end

Instance Attribute Details

#base_dirString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/osctl/image/operations/test/run.rb', line 15

def base_dir
  @base_dir
end

#buildOperations::Image::Build (readonly)



21
22
23
# File 'lib/osctl/image/operations/test/run.rb', line 21

def build
  @build
end

#clientObject (readonly, protected)

Returns the value of attribute client.



59
60
61
# File 'lib/osctl/image/operations/test/run.rb', line 59

def client
  @client
end

#ctidObject (readonly, protected)

Returns the value of attribute ctid.



59
60
61
# File 'lib/osctl/image/operations/test/run.rb', line 59

def ctid
  @ctid
end

#keep_failedObject (readonly, protected)

Returns the value of attribute keep_failed.



59
60
61
# File 'lib/osctl/image/operations/test/run.rb', line 59

def keep_failed
  @keep_failed
end

#statusObject (readonly, protected)

Returns the value of attribute status.



59
60
61
# File 'lib/osctl/image/operations/test/run.rb', line 59

def status
  @status
end

#testTest (readonly)

Returns:



18
19
20
# File 'lib/osctl/image/operations/test/run.rb', line 18

def test
  @test
end

Instance Method Details

#cleanup(ctid) ⇒ Object (protected)



84
85
86
87
88
89
90
91
# File 'lib/osctl/image/operations/test/run.rb', line 84

def cleanup(ctid)
  if status.success? || !keep_failed
    log(:info, "Cleaning up assets of test '#{test}'")
    client.delete_container(ctid)
  else
    log(:info, "Preserving container '#{ctid}' of failed test '#{test}'")
  end
end

#create_container(file) ⇒ Object (protected)



61
62
63
64
65
# File 'lib/osctl/image/operations/test/run.rb', line 61

def create_container(file)
  client.create_container_from_file(ctid, file)
  sleep(3) # FIXME: wait for osctld...
  log(:info, "Created container '#{ctid}' for test '#{test}'")
end

#executeStatus

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osctl/image/operations/test/run.rb', line 37

def execute
  @ctid = gen_ctid

  if File.exist?(build.output_stream)
    create_container(build.output_stream)
  elsif File.exist?(build.output_tar)
    create_container(build.output_tar)
  else
    raise OperationError,
          "no image file for '#{build.image}' found in output directory"
  end

  client.set_container_attr(
    ctid,
    'org.vpsadminos.osctl-image:type',
    'test'
  )

  run_test
end

#gen_ctidObject (protected)



93
94
95
# File 'lib/osctl/image/operations/test/run.rb', line 93

def gen_ctid
  "test-#{SecureRandom.hex(4)}"
end

#run_testObject (protected)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osctl/image/operations/test/run.rb', line 67

def run_test
  Operations::Nix::RunInShell.run(
    File.join(base_dir, 'shell-test.nix'),
    [
      File.join(base_dir, 'bin/test'), 'image', 'run',
      build.image.name, test.name, ctid,
    ]
  )
  log(:warn, "Test '#{test}' successful")
  @status = Status.new(build.image, test, true, 0, nil)
rescue OsCtl::Lib::Exceptions::SystemCommandFailed => e
  log(:warn, "Test '#{test}' failed with status #{e.rc}: #{e.output}")
  @status = Status.new(build.image, test, false, e.rc, e.output)
ensure
  cleanup(ctid)
end