Project

General

Profile

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

foreman_docker / test / units / docker_container_wizard_states / image_test.rb @ 27a7d2c4

1
require 'test_plugin_helper'
2

    
3
module DockerContainerWizardStates
4
  class ImageTest < ActiveSupport::TestCase
5
    let(:image) { 'centos' }
6
    let(:tags) { ['latest', '5', '4.3'] }
7
    let(:docker_hub) { Service::RegistryApi.new(url: 'https://nothub.com') }
8
    let(:registry) { FactoryGirl.create(:docker_registry) }
9
    let(:compute_resource) { FactoryGirl.create(:docker_cr) }
10
    let(:image_search_service) { ForemanDocker::ImageSearch.new }
11
    let(:wizard_state) do
12
      DockerContainerWizardState.create
13
    end
14
    let(:preliminary) do
15
      DockerContainerWizardStates::Preliminary.create(
16
        compute_resource: compute_resource,
17
        wizard_state: wizard_state
18
      )
19
    end
20

    
21
    subject do
22
      Image.new(
23
        repository_name: image,
24
        tag: tags.first,
25
        wizard_state: wizard_state
26
      )
27
    end
28

    
29
    setup do
30
      ForemanDocker::ImageSearch.any_instance.unstub(:available?)
31
      wizard_state.preliminary = preliminary
32
      Service::RegistryApi.stubs(:docker_hub).returns(docker_hub)
33
    end
34

    
35
    describe 'it validates that the image is available' do
36
      test 'validates via the image_search_service' do
37
        DockerContainerWizardStates::Image.any_instance
38
                                          .stubs(:image_search_service)
39
                                          .returns(image_search_service)
40
        available = [true, false].sample
41
        image_search_service.expects(:available?)
42
                            .with("#{image}:#{tags.first}").at_least_once
43
                            .returns(available)
44
        assert_equal available, subject.valid?
45
      end
46

    
47
      context 'when no registy is set' do
48
        test 'it queries the compute_resource and docker_hub' do
49
          compute_resource.expects(:image).with(image).at_least_once
50
                          .returns(image)
51
          compute_resource.expects(:tags_for_local_image).at_least_once
52
                          .with(image, tags.first)
53
                          .returns([])
54
          docker_hub.expects(:tags).at_least_once
55
                    .returns([])
56

    
57
          subject.validate
58
        end
59
      end
60

    
61
      context 'when a registy is set' do
62
        setup do
63
          subject.stubs(:registry_id).returns(registry.id)
64
          DockerRegistry.expects(:find).with(registry.id)
65
                        .returns(registry)
66
        end
67

    
68
        test 'it queries the compute_resource and registry' do
69
          compute_resource.expects(:image).with(image).at_least_once
70
                          .returns(image)
71
          compute_resource.expects(:tags_for_local_image).at_least_once
72
                          .with(image, tags.first)
73
                          .returns([])
74
          docker_hub.expects(:tags).never
75
          registry.api.expects(:tags).with(image, tags.first).at_least_once
76
                  .returns([])
77

    
78
          subject.validate
79
        end
80
      end
81
    end
82
  end
83
end