Project

General

Profile

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

foreman_docker / test / functionals / containers_steps_controller_test.rb @ f6a9acbd

1
require 'test_plugin_helper'
2

    
3
module Containers
4
  class StepsControllerTest < ActionController::TestCase
5
    setup do
6
      stub_image_existance
7
      stub_registry_api
8
      @container = FactoryGirl.create(:container)
9
      @state = DockerContainerWizardState.create!
10
    end
11

    
12
    test 'wizard finishes with a redirect to the managed container' do
13
      Service::Containers.any_instance.expects(:start_container!).with(equals(@state))
14
        .returns(@container)
15
      put :update, { :wizard_state_id => @state.id,
16
                     :id => :environment,
17
                     :start_on_create => true,
18
                     :docker_container_wizard_states_environment => { :tty => false } },
19
          set_session_user
20

    
21
      assert_redirected_to container_path(:id => @container.id)
22
    end
23

    
24
    describe 'on image step' do
25
      setup do
26
        @compute_resource = FactoryGirl.create(:docker_cr)
27
        @create_options = { :wizard_state => @state,
28
                           :compute_resource_id => @compute_resource.id }
29
        @state.preliminary = DockerContainerWizardStates::Preliminary.create!(@create_options)
30
        DockerContainerWizardState.expects(:find).at_least_once.returns(@state)
31
      end
32

    
33
      test 'image show doesnot load katello' do
34
        get :show, { :wizard_state_id => @state.id, :id => :image }, set_session_user
35
        refute @state.image.katello?
36
        refute response.body.include?("katello") # this is code generated by katello partial
37
        docker_image = @controller.instance_eval do
38
          @docker_container_wizard_states_image
39
        end
40
        assert_equal @state.image, docker_image
41
      end
42

    
43
      describe 'submitting' do
44
        setup do
45
          @image_params = {
46
            docker_container_wizard_states_image: {
47
              repository_name: 'test',
48
              tag: 'test'
49
          }}
50
          @params = @image_params.merge({
51
            wizard_state_id: @state.id,
52
            id: :image,
53
          })
54
        end
55

    
56
        test 'has no errors if the image exists' do
57
          put :update, @params, set_session_user
58
          assert_valid @state.image
59
          assert css_select('#hub_image_search.has-error').size == 0
60
        end
61

    
62
        test 'shows an error when the image does not exist' do
63
          stub_image_existance(false)
64
          put :update, @params, set_session_user
65
          refute_valid @state.image
66
          assert_select '#hub_image_search.has-error'
67
        end
68
      end
69
    end
70

    
71
    test 'new container respects exposed_ports configuration' do
72
      environment_options = {
73
        :docker_container_wizard_state_id => @state.id
74
      }
75
      @state.environment = DockerContainerWizardStates::Environment.create!(environment_options)
76
      @state.environment.exposed_ports.create!(:key => '1654', :value => 'tcp')
77
      @state.environment.exposed_ports.create!(:key => '1655', :value => 'udp')
78
      get :show, { :wizard_state_id => @state.id, :id => :environment }, set_session_user
79
      assert response.body.include?("1654")
80
      assert response.body.include?("1655")
81

    
82
      # Load ExposedPort variables into container
83
      @state.environment.exposed_ports.each do |e|
84
        @container.exposed_ports.build :key => e.key,
85
                                       :value => e.value
86
      end
87
      # Check if parametrized value of container matches Docker API's expectations
88
      assert @container.parametrize.key? "ExposedPorts"
89
      assert @container.parametrize["ExposedPorts"].key? "1654/tcp"
90
      assert @container.parametrize["ExposedPorts"].key? "1655/udp"
91
    end
92

    
93
    test 'new container respects dns configuration' do
94
      environment_options = {
95
        :docker_container_wizard_state_id => @state.id
96
      }
97
      @state.environment = DockerContainerWizardStates::Environment.create!(environment_options)
98
      @state.environment.dns.create!(:key => '18.18.18.18')
99
      @state.environment.dns.create!(:key => '19.19.19.19')
100
      get :show, { :wizard_state_id => @state.id, :id => :environment }, set_session_user
101
      assert response.body.include?("18.18.18.18")
102
      assert response.body.include?("19.19.19.19")
103

    
104
      # Load Dns variables into container
105
      @state.environment.dns.each do |e|
106
        @container.dns.build :key => e.key
107
      end
108
      # Check if parametrized value of container matches Docker API's expectations
109
      assert @container.parametrize.key? "HostConfig"
110
      assert @container.parametrize["HostConfig"].key? "Dns"
111
      assert @container.parametrize["HostConfig"].value? ["18.18.18.18", "19.19.19.19"]
112
    end
113

    
114
    test "does not create a container with 2 exposed ports with the same key" do
115
      environment_options = {
116
          :docker_container_wizard_state_id => @state.id
117
      }
118
      @state.environment = DockerContainerWizardStates::Environment.new(environment_options)
119
      @state.environment.exposed_ports.new(:key => '1654', :value => 'tcp')
120
      @state.environment.exposed_ports.new(:key => '1654', :value => 'udp')
121
      refute_valid @state
122
      assert_equal "Please ensure the following parameters are unique", @state.errors[:'environment.exposed_ports'].first
123
    end
124
  end
125
end