Project

General

Profile

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

foreman-docker / test / functionals / api / v2 / containers_controller_test.rb @ 2b80fdd9

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
        context 'deletion' do
44
          setup do
45
            Container.any_instance.stubs(:uuid).returns('randomuuid')
46
          end
47

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

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

    
65
        context 'power calls' do
66
          setup { Fog.mock! }
67
          teardown { Fog.unmock! }
68

    
69
          test 'power call turns on/off container in Docker host' do
70
            Fog::Compute::Fogdocker::Server.any_instance.expects(:start)
71
            put :power, :id => @container.id, :power_action => 'start'
72
            assert_response :success
73
          end
74

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

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

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

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

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

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