Project

General

Profile

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

foreman_docker / test / functionals / containers_controller_test.rb @ fb471374

1
require 'test_plugin_helper'
2

    
3
class ContainersControllerTest < ActionController::TestCase
4
  setup do
5
    stub_image_existance
6
    stub_registry_api
7
  end
8

    
9
  test 'redirect if Docker provider is not available' do
10
    get :index, session: 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
    FactoryBot.create(:docker_cr)
19
    get :index, session: set_session_user
20
    assert_template 'index'
21
  end
22

    
23
  context 'delete container' do
24
    setup do
25
      Fog.mock!
26
      @container_resource = FactoryBot.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, params: { :compute_resource_id => @container_resource, :id => @container.id }, session: set_session_user
35
      assert_redirected_to containers_path
36
      assert_equal "Container #{@container.id} is being deleted.",
37
        flash[:notice]
38
    end
39

    
40
    test 'failed deletion of unmanaged container in Docker' do
41
      ComputeResource.any_instance.stubs(:destroy_vm).
42
        raises(::Foreman::Exception.new('Could not destroy Docker container'))
43
      @request.env['HTTP_REFERER'] = "http://test.host/#{containers_path}"
44
      delete :destroy, params: { :compute_resource_id => @container_resource, :id => @container.id }, session: set_session_user
45
      assert @container.present?
46
      assert_redirected_to :back
47
      assert_equal 'Your container could not be deleted in Docker',
48
        flash[:error]
49
    end
50

    
51
    test 'deleting a managed container deletes container in Docker' do
52
      managed_container = FactoryBot.create(
53
        :container,
54
        :compute_resource => @container_resource)
55
      ComputeResource.any_instance.expects(:destroy_vm).
56
        with('randomuuid')
57
      Container.any_instance.expects(:uuid).returns('randomuuid').at_least_once
58
      Container.any_instance.expects(:destroy)
59
      delete :destroy, params: { :id => managed_container.id }, session: set_session_user
60
      assert_redirected_to containers_path
61
      assert_equal "Container #{managed_container.uuid} is being deleted.",
62
        flash[:notice]
63
    end
64

    
65
    test 'deleting with container params deletes container object' do
66
      managed_container = FactoryBot.create(
67
        :container,
68
        :compute_resource => @container_resource)
69
      managed_container.update(:uuid => @container.id)
70
      ComputeResource.any_instance.expects(:destroy_vm).
71
        with(@container.id).returns(true)
72
      Container.any_instance.expects(:destroy)
73
      delete :destroy, params: { :compute_resource_id => @container_resource.id, :id => @container.id }, session: set_session_user
74
      assert_redirected_to containers_path
75
      assert_equal "Container #{managed_container.uuid} is being deleted.",
76
        flash[:notice]
77
    end
78

    
79
    test 'failed deletion of managed container keeps container in Foreman' do
80
      ComputeResource.any_instance.stubs(:destroy_vm).
81
        raises(::Foreman::Exception.new('Could not destroy Docker container'))
82
      managed_container = FactoryBot.create(
83
        :container,
84
        :compute_resource => @container_resource)
85
      delete :destroy, params: { :id => managed_container.id }, session: set_session_user
86
      assert managed_container.present? # Foreman container would not be deleted
87
      assert_redirected_to containers_path
88
      assert_equal 'Your container could not be deleted in Docker',
89
        flash[:error]
90
    end
91
  end
92

    
93
  test 'committing a managed container' do
94
    container = FactoryBot.create(:container)
95
    request.env['HTTP_REFERER'] = container_path(:id => container.id)
96
    commit_hash = { :author => 'a', :repo => 'b', :tag => 'c', :comment => 'd' }
97

    
98
    mock_container = mock
99
    ::Docker::Container.expects(:get).with(container.uuid, anything, anything)
100
      .returns(mock_container)
101
    mock_container.expects(:commit).with(commit_hash)
102

    
103
    post :commit, params: { :commit => commit_hash, :id => container.id }, session: set_session_user
104
  end
105
end