Project

General

Profile

Download (2.17 KB) Statistics
| Branch: | Tag: | Revision:

foreman_docker / test / units / containers_service_test.rb @ 27a7d2c4

1
require 'test_plugin_helper'
2
require 'ostruct'
3

    
4
class ContainersServiceTest < ActiveSupport::TestCase
5
  setup do
6
    stub_image_existance
7
    stub_registry_api
8

    
9
    @state = DockerContainerWizardState.create! do |s|
10
      s.build_preliminary(:compute_resource_id => FactoryGirl.create(:docker_cr).id,
11
                          :locations           => [taxonomies(:location1)],
12
                          :organizations       => [taxonomies(:organization1)])
13
      s.build_image(:repository_name => 'test', :tag => 'test', :wizard_state => s)
14
      s.build_configuration(:name => 'test', :command => '/bin/bash')
15
      s.build_environment(:tty => false)
16
    end
17
  end
18

    
19
  test 'removes current state after successful container creation' do
20
    ret = OpenStruct.new(:id => 1)
21
    ForemanDocker::Docker.any_instance.expects(:create_image).returns(ret).with do |subject|
22
      subject.must_equal(:fromImage => "test:test")
23
    end
24
    ForemanDocker::Docker.any_instance.expects(:create_container)
25
      .returns(OpenStruct.new(:uuid => 1))
26
    Fog.mock!
27
    Service::Containers.new.start_container!(@state)
28
    Fog.unmock!
29
    assert_equal DockerContainerWizardState.where(:id => @state.id).count, 0
30
  end
31

    
32
  context 'errors' do
33
    setup do
34
      @containers_service = Service::Containers.new
35
    end
36

    
37
    test 'from compute resource' do
38
      # Since the compute resource will be unreachable, this test will always
39
      # fail at the 'pull_image' step
40
      assert_raises(ActiveRecord::Rollback) do
41
        @containers_service.create_container_object(@state)
42
      end
43
      assert @containers_service.errors.present?
44
      assert_match(/No such file or directory.*ENOENT/,
45
                   @containers_service.full_messages.join(' '))
46
    end
47

    
48
    test 'from multiple sources' do
49
      Container.any_instance.expects(:valid?).returns(false)
50
      Container.any_instance.stubs(:errors).returns(
51
        OpenStruct.new(:full_messages => ['foo']))
52
      assert_raises(ActiveRecord::Rollback) do
53
        @containers_service.create_container_object(@state)
54
      end
55
      assert @containers_service.errors.present?
56
      assert_match(/foo/,
57
                   @containers_service.full_messages.join(' '))
58
    end
59
  end
60
end