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
# File 'lib/osctl/image/operations/test/run.rb', line 29

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
  @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.



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

def client
  @client
end

#ctidObject (readonly, protected)

Returns the value of attribute ctid.



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

def ctid
  @ctid
end

#ip_allocatorObject (readonly, protected)

Returns the value of attribute ip_allocator.



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

def ip_allocator
  @ip_allocator
end

#keep_failedObject (readonly, protected)

Returns the value of attribute keep_failed.



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

def keep_failed
  @keep_failed
end

#statusObject (readonly, protected)

Returns the value of attribute status.



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

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)



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

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)



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

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:



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

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)



101
102
103
# File 'lib/osctl/image/operations/test/run.rb', line 101

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

#run_testObject (protected)



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

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