Class: OsCtl::Image::Operations::Test::Run
- Includes:
- Lib::Utils::Log
- Defined in:
- lib/osctl/image/operations/test/run.rb
Defined Under Namespace
Classes: Status
Instance Attribute Summary collapse
- #base_dir ⇒ String readonly
- #build ⇒ Operations::Image::Build readonly
-
#client ⇒ Object
readonly
protected
Returns the value of attribute client.
-
#ctid ⇒ Object
readonly
protected
Returns the value of attribute ctid.
-
#ip_allocator ⇒ Object
readonly
protected
Returns the value of attribute ip_allocator.
-
#keep_failed ⇒ Object
readonly
protected
Returns the value of attribute keep_failed.
-
#status ⇒ Object
readonly
protected
Returns the value of attribute status.
- #test ⇒ Test readonly
Instance Method Summary collapse
- #cleanup(ctid) ⇒ Object protected
- #create_container(file) ⇒ Object protected
- #execute ⇒ Status
- #gen_ctid ⇒ Object protected
-
#initialize(base_dir, build, test, opts = {}) ⇒ Run
constructor
A new instance of Run.
- #run_test ⇒ Object protected
Methods inherited from Base
Constructor Details
#initialize(base_dir, build, test, opts = {}) ⇒ Run
Returns a new instance of Run.
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_dir ⇒ String (readonly)
15 16 17 |
# File 'lib/osctl/image/operations/test/run.rb', line 15 def base_dir @base_dir end |
#build ⇒ Operations::Image::Build (readonly)
21 22 23 |
# File 'lib/osctl/image/operations/test/run.rb', line 21 def build @build end |
#client ⇒ Object (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 |
#ctid ⇒ Object (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_allocator ⇒ Object (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_failed ⇒ Object (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 |
#status ⇒ Object (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 |
#test ⇒ Test (readonly)
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.(ctid) end |
#execute ⇒ Status
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_ctid ⇒ Object (protected)
103 104 105 |
# File 'lib/osctl/image/operations/test/run.rb', line 103 def gen_ctid "test-#{SecureRandom.hex(4)}" end |
#run_test ⇒ Object (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 |