Project

General

Profile

Revision f6a9acbd

Added by Sebastian Gräßl over 5 years ago

Fixes #18518 - Add proper validation for container image

For now the image has only been validate via the result
of the search, but not enforced on the server side.

The model validation brings now proper feedback when
the form has been submitted to the server.

View differences:

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

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

  
22
    test 'image show doesnot load katello' do
23
      compute_resource = FactoryGirl.create(:docker_cr)
24
      state = DockerContainerWizardState.create!
25
      create_options = { :wizard_state => state,
26
                         :compute_resource_id => compute_resource.id
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
27 61

  
28
                       }
29
      state.preliminary = DockerContainerWizardStates::Preliminary.create!(create_options)
30
      DockerContainerWizardState.expects(:find).at_least_once.returns(state)
31
      get :show, { :wizard_state_id => state.id, :id => :image }, set_session_user
32
      refute state.image.katello?
33
      refute response.body.include?("katello") # this is code generated by katello partial
34
      docker_image = @controller.instance_eval do
35
        @docker_container_wizard_states_image
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
36 68
      end
37
      assert_equal state.image, docker_image
38 69
    end
39 70

  
40 71
    test 'new container respects exposed_ports configuration' do
41
      state = DockerContainerWizardState.create!
42 72
      environment_options = {
43
        :docker_container_wizard_state_id => state.id
73
        :docker_container_wizard_state_id => @state.id
44 74
      }
45
      state.environment = DockerContainerWizardStates::Environment.create!(environment_options)
46
      state.environment.exposed_ports.create!(:key => '1654', :value => 'tcp')
47
      state.environment.exposed_ports.create!(:key => '1655', :value => 'udp')
48
      get :show, { :wizard_state_id => state.id, :id => :environment }, set_session_user
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
49 79
      assert response.body.include?("1654")
50 80
      assert response.body.include?("1655")
51 81

  
52 82
      # Load ExposedPort variables into container
53
      state.environment.exposed_ports.each do |e|
83
      @state.environment.exposed_ports.each do |e|
54 84
        @container.exposed_ports.build :key => e.key,
55 85
                                       :value => e.value
56 86
      end
......
61 91
    end
62 92

  
63 93
    test 'new container respects dns configuration' do
64
      state = DockerContainerWizardState.create!
65 94
      environment_options = {
66
        :docker_container_wizard_state_id => state.id
95
        :docker_container_wizard_state_id => @state.id
67 96
      }
68
      state.environment = DockerContainerWizardStates::Environment.create!(environment_options)
69
      state.environment.dns.create!(:key => '18.18.18.18')
70
      state.environment.dns.create!(:key => '19.19.19.19')
71
      get :show, { :wizard_state_id => state.id, :id => :environment }, set_session_user
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
72 101
      assert response.body.include?("18.18.18.18")
73 102
      assert response.body.include?("19.19.19.19")
74 103

  
75 104
      # Load Dns variables into container
76
      state.environment.dns.each do |e|
105
      @state.environment.dns.each do |e|
77 106
        @container.dns.build :key => e.key
78 107
      end
79 108
      # Check if parametrized value of container matches Docker API's expectations
......
83 112
    end
84 113

  
85 114
    test "does not create a container with 2 exposed ports with the same key" do
86
      state = DockerContainerWizardState.new
87 115
      environment_options = {
88
          :docker_container_wizard_state_id => state.id
116
          :docker_container_wizard_state_id => @state.id
89 117
      }
90
      state.environment = DockerContainerWizardStates::Environment.new(environment_options)
91
      state.environment.exposed_ports.new(:key => '1654', :value => 'tcp')
92
      state.environment.exposed_ports.new(:key => '1654', :value => 'udp')
93
      refute_valid state
94
      assert_equal "Please ensure the following parameters are unique", state.errors[:'environment.exposed_ports'].first
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
95 123
    end
96 124
  end
97 125
end

Also available in: Unified diff