Project

General

Profile

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

foreman_docker / test / functionals / containers_steps_controller_test.rb @ fb471374

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 = FactoryBot.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, params: { :wizard_state_id => @state.id, :id => :environment, :start_on_create => true, :docker_container_wizard_states_environment => { :tty => false } }, session: set_session_user
16

    
17
      assert_redirected_to container_path(:id => @container.id)
18
    end
19

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

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

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

    
52
        test 'has no errors if the image exists' do
53
          put :update, params: @params, session: set_session_user
54
          assert_valid @state.image
55
          assert css_select('#hub_image_search.has-error').size == 0
56
        end
57

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

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

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

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

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

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