Project

General

Profile

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

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

1
require 'test_plugin_helper'
2

    
3
module Api
4
  module V2
5
    class ContainersControllerTest < ActionController::TestCase
6
      setup do
7
        stub_image_existance
8
        stub_registry_api
9
      end
10

    
11
      test 'index returns a list of all containers' do
12
        get :index, {}, set_session_user
13
        assert_response :success
14
        assert_template 'index'
15
      end
16

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

    
26
      context 'container operations' do
27
        setup do
28
          @container = FactoryGirl.create(:container, :name => 'foo')
29
          @registry = FactoryGirl.create(:docker_registry)
30
          @compute_resource = FactoryGirl.create(:docker_cr)
31
        end
32

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

    
42
        test 'show returns information about container' do
43
          get :show, :id => @container.id
44
          assert_response :success
45
          assert_equal ActiveSupport::JSON.decode(response.body)['name'], 'foo'
46
        end
47

    
48
        context 'deletion' do
49
          setup do
50
            Container.any_instance.stubs(:uuid).returns('randomuuid')
51
          end
52

    
53
          test 'delete removes a container in foreman and in Docker host' do
54
            Fog.mock!
55
            delete :destroy, :id => @container.id
56
            Fog.unmock!
57
            assert_response :success
58
            assert_equal ActiveSupport::JSON.decode(response.body)['name'], 'foo'
59
          end
60

    
61
          test 'if deletion on Docker host fails, Foreman deletion fails' do
62
            ComputeResource.any_instance.expects(:destroy_vm).
63
              with('randomuuid').
64
              raises(::Foreman::Exception.new('Problem removing container'))
65
            delete :destroy, :id => @container.id
66
            assert_response :precondition_failed
67
          end
68
        end
69

    
70
        context 'power calls' do
71
          setup { Fog.mock! }
72
          teardown { Fog.unmock! }
73

    
74
          test 'power call turns on/off container in Docker host' do
75
            Fog::Compute::Fogdocker::Server.any_instance.expects(:start)
76
            put :power, :id => @container.id, :power_action => 'start'
77
            assert_response :success
78
          end
79

    
80
          test 'power call checks status of container in Docker host' do
81
            Fog::Compute::Fogdocker::Server.any_instance.expects(:ready?).returns(false)
82
            put :power, :id => @container.id, :power_action => 'status'
83
            assert_response :success
84
            assert_equal ActiveSupport::JSON.decode(response.body)['running'], false
85
          end
86

    
87
          test 'power call host' do
88
            Fog::Compute::Fogdocker::Server.any_instance.expects(:ready?).returns(false)
89
            put :power, :id => @container.id, :power_action => 'status'
90
            assert_response :success
91
            assert_equal ActiveSupport::JSON.decode(response.body)['running'], false
92
          end
93

    
94
          test 'creates a container with correct params' do
95
            repository_name = "centos"
96
            tag = "7"
97
            name = "foo"
98
            registry_uri = URI.parse(@registry.url)
99
            Service::Containers.any_instance.expects(:pull_image).returns(true)
100
            Service::Containers.any_instance
101
              .expects(:start_container).returns(true).with do |container|
102
              container.must_be_kind_of(Container)
103
              container.repository_name.must_equal(repository_name)
104
              container.tag.must_equal(tag)
105
              container.compute_resource_id.must_equal(@compute_resource.id)
106
              container.name.must_equal(name)
107
              container.repository_pull_url.must_include(registry_uri.host)
108
              container.repository_pull_url.must_include("#{repository_name}:#{tag}")
109
            end
110
            post :create, :container => { :compute_resource_id => @compute_resource.id,
111
                                          :name => name,
112
                                          :registry_id => @registry.id,
113
                                          :repository_name => repository_name,
114
                                          :tag => tag }
115
            assert_response :created
116
          end
117

    
118
          test 'creates a container with correct params' do
119
            repository_name = "centos"
120
            tag = "8"
121
            name = "foo2"
122
            Service::Containers.any_instance.expects(:pull_image).returns(true)
123
            Service::Containers.any_instance.expects(:start_container).returns(true)
124
            post :create, :container => { :compute_resource_id => @compute_resource.id,
125
                                          :name => name,
126
                                          :registry_id => @registry.id,
127
                                          :repository_name => repository_name,
128
                                          :tag => tag,
129
                                          :environment_variables => [{:key => 'ping_host', :value => 'example.com'}]}
130
            assert_response :created
131
          end
132
        end
133

    
134
        test 'creates a katello container with correct params' do
135
          DockerContainerWizardStates::Image.class_eval do
136
            attr_accessor :capsule_id
137
          end
138
          DockerContainerWizardStates::Image.attribute_names.stubs(:include?).returns(true)
139
          repository_name = "katello_centos"
140
          tag = "7"
141
          name = "foo"
142
          capsule_id = "10000"
143
          Service::Containers.any_instance.expects(:start_container!)
144
            .returns(@container).with do |wizard_state|
145
            wizard_state.must_be_kind_of(DockerContainerWizardState)
146
            container_attributes = wizard_state.container_attributes
147
            container_attributes[:repository_name].must_equal(repository_name)
148
            container_attributes[:tag].must_equal(tag)
149
            container_attributes[:compute_resource_id].must_equal(@compute_resource.id)
150
            container_attributes[:name].must_equal(name)
151
            wizard_state.image.capsule_id.must_equal(capsule_id)
152
          end
153
          post :create, :container => { :compute_resource_id => @compute_resource.id,
154
                                        :name => name,
155
                                        :capsule_id => capsule_id,
156
                                        :repository_name => repository_name,
157
                                        :tag => tag }
158
          assert_response :created
159
        end
160

    
161
        test 'creation fails with invalid container name' do
162
          post :create, :container => { :compute_resource_id => @container.compute_resource_id,
163
                                        :name => @container.name,
164
                                        :registry_id => @registry.id,
165
                                        :repository_name => 'centos',
166
                                        :tag => 'latest' }
167
          assert_response :unprocessable_entity
168
          assert_match /Name has already been taken/, @response.body
169
        end
170
      end
171
    end
172
  end
173
end