Project

General

Profile

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

foreman-docker / test / functionals / containers_controller_test.rb @ ef3feba4

1
require 'test_plugin_helper'
2

    
3
class ContainersControllerTest < ActionController::TestCase
4
  test 'redirect if Docker provider is not available' do
5
    get :index, {}, set_session_user
6
    assert_redirected_to new_compute_resource_path
7
  end
8

    
9
  test 'index if Docker resource is available' do
10
    Fog.mock!
11
    # Avoid rendering errors by not retrieving any container
12
    ComputeResource.any_instance.stubs(:vms).returns([])
13
    FactoryGirl.create(:docker_cr)
14
    get :index, {}, set_session_user
15
    assert_template 'index'
16
  end
17

    
18
  context 'delete container' do
19
    setup do
20
      Fog.mock!
21
      @container_resource = FactoryGirl.create(:docker_cr)
22
      @container          = @container_resource.vms.first
23
    end
24

    
25
    teardown { Fog.unmock! }
26

    
27
    test 'deleting an unmanaged container redirects to containers index' do
28
      ComputeResource.any_instance.expects(:destroy_vm).with(@container.id)
29
      delete :destroy, { :compute_resource_id => @container_resource,
30
                         :id                  => @container.id }, set_session_user
31
      assert_redirected_to containers_path
32
      assert_equal "Container #{@container.id} is being deleted.",
33
        flash[:notice]
34
    end
35

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

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

    
62
    test 'failed deletion of managed container keeps container in Foreman' do
63
      ComputeResource.any_instance.stubs(:destroy_vm).
64
        raises(::Foreman::Exception.new('Could not destroy Docker container'))
65
      managed_container = FactoryGirl.create(
66
        :container,
67
        :compute_resource => @container_resource)
68
      delete :destroy, { :id => managed_container.id }, set_session_user
69
      assert managed_container.present? # Foreman container would not be deleted
70
      assert_redirected_to containers_path
71
      assert_equal 'Your container could not be deleted in Docker',
72
        flash[:error]
73
    end
74
  end
75

    
76
  test 'committing a managed container' do
77
    container = FactoryGirl.create(:container)
78
    request.env['HTTP_REFERER'] = container_path(:id => container.id)
79
    commit_hash = { :author => 'a', :repo => 'b', :tag => 'c', :comment => 'd' }
80

    
81
    mock_container = mock
82
    ::Docker::Container.expects(:get).with(container.uuid, anything, anything)
83
      .returns(mock_container)
84
    mock_container.expects(:commit).with(commit_hash)
85

    
86
    post :commit, { :commit => commit_hash,
87
                    :id     => container.id }, set_session_user
88
  end
89
end