Project

General

Profile

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

foreman_docker / test / functionals / api / v2 / containers_controller_test.rb @ d2e54db4

1
require 'test_plugin_helper'
2

    
3
module Api
4
  module V2
5
    class ContainersControllerTest < ActionController::TestCase
6
      test 'index returns a list of all containers' do
7
        get :index, {}, set_session_user
8
        assert_response :success
9
        assert_template 'index'
10
      end
11

    
12
      test 'index can be filtered by name' do
13
        %w(thomas clayton wolfe).each do |name|
14
          FactoryGirl.create(:container, :name => name)
15
        end
16
        get :index, { :search => 'name = thomas' }, set_session_user
17
        assert_response :success
18
        assert_equal 1, assigns(:containers).length
19
      end
20

    
21
      context 'container operations' do
22
        setup do
23
          @container = FactoryGirl.create(:container, :name => 'foo')
24
          @registry = FactoryGirl.create(:docker_registry)
25
          @compute_resource = FactoryGirl.create(:docker_cr)
26
        end
27

    
28
        test 'logs returns latest lines of container log' do
29
          fake_container = Struct.new(:logs)
30
          fake_container.expects(:logs).returns('I am a log').twice
31
          Docker::Container.expects(:get).with(@container.uuid).returns(fake_container)
32
          get :logs, :id => @container.id
33
          assert_response :success
34
          assert_equal ActiveSupport::JSON.decode(response.body)['logs'], fake_container.logs
35
        end
36

    
37
        test 'show returns information about container'  do
38
          get :show, :id => @container.id
39
          assert_response :success
40
          assert_equal ActiveSupport::JSON.decode(response.body)['name'], 'foo'
41
        end
42

    
43
        test 'delete removes a container in foreman and in Docker host' do
44
          delete :destroy, :id => @container.id
45
          assert_response :success
46
          assert_equal ActiveSupport::JSON.decode(response.body)['name'], 'foo'
47
        end
48

    
49
        test 'power call turns on/off container in Docker host' do
50
          Fog.mock!
51
          Fog::Compute::Fogdocker::Server.any_instance.expects(:start)
52
          put :power, :id => @container.id, :power_action => 'start'
53
          assert_response :success
54
        end
55

    
56
        test 'power call checks status of container in Docker host' do
57
          Fog.mock!
58
          Fog::Compute::Fogdocker::Server.any_instance.expects(:ready?).returns(false)
59
          put :power, :id => @container.id, :power_action => 'status'
60
          assert_response :success
61
          assert_equal ActiveSupport::JSON.decode(response.body)['running'], false
62
        end
63

    
64
        test 'power call host' do
65
          Fog.mock!
66
          Fog::Compute::Fogdocker::Server.any_instance.expects(:ready?).returns(false)
67
          put :power, :id => @container.id, :power_action => 'status'
68
          assert_response :success
69
          assert_equal ActiveSupport::JSON.decode(response.body)['running'], false
70
        end
71

    
72
        test 'creates a container with correct params' do
73
          repository_name = "centos"
74
          tag = "7"
75
          name = "foo"
76
          registry_uri = URI.parse(@registry.url)
77
          Service::Containers.any_instance.expects(:pull_image).returns(true)
78
          Service::Containers.any_instance
79
            .expects(:start_container).returns(true).with do |container|
80
            container.must_be_kind_of(Container)
81
            container.repository_name.must_equal(repository_name)
82
            container.tag.must_equal(tag)
83
            container.compute_resource_id.must_equal(@compute_resource.id)
84
            container.name.must_equal(name)
85
            container.repository_pull_url.must_include(registry_uri.host)
86
            container.repository_pull_url.must_include("#{repository_name}:#{tag}")
87
          end
88
          post :create, :container => { :compute_resource_id => @compute_resource.id,
89
                                        :name => name,
90
                                        :registry_id => @registry.id,
91
                                        :repository_name => repository_name,
92
                                        :tag => tag }
93
          assert_response :created
94
        end
95

    
96
        test 'creates a katello container with correct params' do
97
          DockerContainerWizardStates::Image.class_eval do
98
            attr_accessor :capsule_id
99
          end
100
          DockerContainerWizardStates::Image.attribute_names.stubs(:include?).returns(true)
101
          repository_name = "katello_centos"
102
          tag = "7"
103
          name = "foo"
104
          capsule_id = "10000"
105
          Service::Containers.any_instance.expects(:start_container!)
106
            .returns(@container).with do |wizard_state|
107
            wizard_state.must_be_kind_of(DockerContainerWizardState)
108
            container_attributes = wizard_state.container_attributes
109
            container_attributes[:repository_name].must_equal(repository_name)
110
            container_attributes[:tag].must_equal(tag)
111
            container_attributes[:compute_resource_id].must_equal(@compute_resource.id)
112
            container_attributes[:name].must_equal(name)
113
            wizard_state.image.capsule_id.must_equal(capsule_id)
114
          end
115
          post :create, :container => { :compute_resource_id => @compute_resource.id,
116
                                        :name => name,
117
                                        :capsule_id => capsule_id,
118
                                        :repository_name => repository_name,
119
                                        :tag => tag }
120
          assert_response :created
121
        end
122
      end
123
    end
124
  end
125
end