Project

General

Profile

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

foreman_docker / test / units / docker_container_wizard_states / image_test.rb @ f6a9acbd

1 f6a9acbd Sebastian Gräßl
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
36
    describe 'it validates that the image is available' do
37
      test 'validates via the image_search_service' do
38
        DockerContainerWizardStates::Image.any_instance
39
                                          .stubs(:image_search_service)
40
                                          .returns(image_search_service)
41
        available = [true, false].sample
42
        image_search_service.expects(:available?)
43
                            .with("#{image}:#{tags.first}").at_least_once
44
                            .returns(available)
45
        assert_equal available, subject.valid?
46
      end
47
48
      context 'when no registy is set' do
49
        test 'it queries the compute_resource and docker_hub' do
50
          compute_resource.expects(:image).with(image).at_least_once
51
                          .returns(image)
52
          compute_resource.expects(:tags_for_local_image).at_least_once
53
                          .with(image, tags.first)
54
                          .returns([])
55
          docker_hub.expects(:tags).at_least_once
56
                    .returns([])
57
58
          subject.validate
59
        end
60
      end
61
62
      context 'when a registy is set' do
63
        setup do
64
          subject.stubs(:registry_id).returns(registry.id)
65
          DockerRegistry.expects(:find).with(registry.id)
66
                        .returns(registry)
67
        end
68
69
        test 'it queries the compute_resource and registry' do
70
          compute_resource.expects(:image).with(image).at_least_once
71
                          .returns(image)
72
          compute_resource.expects(:tags_for_local_image).at_least_once
73
                          .with(image, tags.first)
74
                          .returns([])
75
          docker_hub.expects(:tags).never
76
          registry.api.expects(:tags).with(image, tags.first).at_least_once
77
                  .returns([])
78
79
          subject.validate
80
        end
81
      end
82
    end
83
  end
84
end