Project

General

Profile

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

foreman_docker / test / functionals / containers_controller_test.rb @ f6a9acbd

1 8b1f4103 Daniel Lobato
require 'test_plugin_helper'
2
3
class ContainersControllerTest < ActionController::TestCase
4 f6a9acbd Sebastian Gräßl
  setup do
5
    stub_image_existance
6
    stub_registry_api
7
  end
8
9 8b1f4103 Daniel Lobato
  test 'redirect if Docker provider is not available' do
10
    get :index, {}, set_session_user
11
    assert_redirected_to new_compute_resource_path
12
  end
13
14
  test 'index if Docker resource is available' do
15
    Fog.mock!
16
    # Avoid rendering errors by not retrieving any container
17
    ComputeResource.any_instance.stubs(:vms).returns([])
18
    FactoryGirl.create(:docker_cr)
19
    get :index, {}, set_session_user
20
    assert_template 'index'
21
  end
22
23 bbb77219 Daniel Lobato
  context 'delete container' do
24
    setup do
25
      Fog.mock!
26
      @container_resource = FactoryGirl.create(:docker_cr)
27
      @container          = @container_resource.vms.first
28
    end
29
30
    teardown { Fog.unmock! }
31
32
    test 'deleting an unmanaged container redirects to containers index' do
33
      ComputeResource.any_instance.expects(:destroy_vm).with(@container.id)
34
      delete :destroy, { :compute_resource_id => @container_resource,
35
                         :id                  => @container.id }, set_session_user
36
      assert_redirected_to containers_path
37
      assert_equal "Container #{@container.id} is being deleted.",
38
        flash[:notice]
39
    end
40
41
    test 'failed deletion of unmanaged container in Docker' do
42
      ComputeResource.any_instance.stubs(:destroy_vm).
43
        raises(::Foreman::Exception.new('Could not destroy Docker container'))
44
      @request.env['HTTP_REFERER'] = "http://test.host/#{containers_path}"
45
      delete :destroy, { :compute_resource_id => @container_resource,
46
                         :id                  => @container.id }, set_session_user
47
      assert @container.present?
48
      assert_redirected_to :back
49
      assert_equal 'Your container could not be deleted in Docker',
50
        flash[:error]
51
    end
52
53
    test 'deleting a managed container deletes container in Docker' do
54
      managed_container = FactoryGirl.create(
55
        :container,
56
        :compute_resource => @container_resource)
57
      ComputeResource.any_instance.expects(:destroy_vm).
58
        with('randomuuid')
59
      Container.any_instance.expects(:uuid).returns('randomuuid').at_least_once
60
      Container.any_instance.expects(:destroy)
61
      delete :destroy, { :id => managed_container.id }, set_session_user
62
      assert_redirected_to containers_path
63
      assert_equal "Container #{managed_container.uuid} is being deleted.",
64
        flash[:notice]
65
    end
66
67 b2f2c848 Shlomi Zadok
    test 'deleting with container params deletes container object' do
68
      managed_container = FactoryGirl.create(
69
        :container,
70
        :compute_resource => @container_resource)
71
      managed_container.update(:uuid => @container.id)
72
      ComputeResource.any_instance.expects(:destroy_vm).
73
        with(@container.id).returns(true)
74
      Container.any_instance.expects(:destroy)
75
      delete :destroy, { :compute_resource_id => @container_resource.id,
76
                         :id                  => @container.id }, set_session_user
77
      assert_redirected_to containers_path
78
      assert_equal "Container #{managed_container.uuid} is being deleted.",
79
        flash[:notice]
80
    end
81
82 bbb77219 Daniel Lobato
    test 'failed deletion of managed container keeps container in Foreman' do
83
      ComputeResource.any_instance.stubs(:destroy_vm).
84
        raises(::Foreman::Exception.new('Could not destroy Docker container'))
85
      managed_container = FactoryGirl.create(
86
        :container,
87
        :compute_resource => @container_resource)
88
      delete :destroy, { :id => managed_container.id }, set_session_user
89
      assert managed_container.present? # Foreman container would not be deleted
90
      assert_redirected_to containers_path
91
      assert_equal 'Your container could not be deleted in Docker',
92
        flash[:error]
93
    end
94 8b1f4103 Daniel Lobato
  end
95 387babdd Daniel Lobato
96
  test 'committing a managed container' do
97
    container = FactoryGirl.create(:container)
98
    request.env['HTTP_REFERER'] = container_path(:id => container.id)
99
    commit_hash = { :author => 'a', :repo => 'b', :tag => 'c', :comment => 'd' }
100
101
    mock_container = mock
102 5397ee73 David Davis
    ::Docker::Container.expects(:get).with(container.uuid, anything, anything)
103
      .returns(mock_container)
104 387babdd Daniel Lobato
    mock_container.expects(:commit).with(commit_hash)
105
106
    post :commit, { :commit => commit_hash,
107
                    :id     => container.id }, set_session_user
108
  end
109 8b1f4103 Daniel Lobato
end