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):



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

def initialize(base_dir, build, test, opts = {})
  super()
  @base_dir = base_dir
  @build = build
  @test = test
  @client = OsCtldClient.new
  @keep_failed = opts.has_key?(:keep_failed) ? opts[:keep_failed] : false
  @ip_allocator = opts[:ip_allocator]
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.



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

def client
  @client
end

#ctidObject (readonly, protected)

Returns the value of attribute ctid.



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

def ctid
  @ctid
end

#ip_allocatorObject (readonly, protected)

Returns the value of attribute ip_allocator.



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

def ip_allocator
  @ip_allocator
end

#keep_failedObject (readonly, protected)

Returns the value of attribute keep_failed.



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

def keep_failed
  @keep_failed
end

#statusObject (readonly, protected)

Returns the value of attribute status.



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

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)



93
94
95
96
97
98
99
100
101
# File 'lib/osctl/image/operations/test/run.rb', line 93

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

#create_container(file) ⇒ Object (protected)



65
66
67
68
69
70
# File 'lib/osctl/image/operations/test/run.rb', line 65

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}'")
  client.unset_container_start_menu(ctid)
end

#executeStatus

Returns:



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

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)



103
104
105
# File 'lib/osctl/image/operations/test/run.rb', line 103

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

#run_testObject (protected)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/osctl/image/operations/test/run.rb', line 72

def run_test
  ip = ip_allocator.get

  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
    ],
    { env: ENV.to_h.update({ 'OSCTL_IMAGE_TEST_IPV4_ADDRESS' => ip.to_s }) }
  )
  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
  ip_allocator.put(ip)
  cleanup(ctid)
end