File: test_wait_blob_copy_operation_to_finish.rb

package info (click to toggle)
ruby-gitlab-fog-azure-rm 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 756 kB
  • sloc: ruby: 5,926; sh: 9; makefile: 4
file content (157 lines) | stat: -rw-r--r-- 5,070 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require File.expand_path '../../test_helper', __dir__

# Storage Blob Class
class TestWaitBlobCopyOperationToFinish < Minitest::Test
  # This class posesses the test cases for the requests of waiting storage blob copy operation to finish.
  def setup
    Fog.mock!
    @mock_service = Fog::AzureRM::Storage.new(storage_account_credentials)
    Fog.unmock!
    @mocked_response = mocked_storage_http_error
    @blob = storage_blob

    @service = Fog::AzureRM::Storage.new(storage_account_credentials)
    @blob_client = @service.instance_variable_get(:@blob_client)
  end

  def test_wait_blob_copy_operation_to_finish_with_success
    copy_id = @blob.properties[:copy_id]
    copy_status = 'pending'

    i = 0
    multiple_values = lambda do |*|
      i += 1
      if i == 1
        @blob.properties[:copy_status] = copy_status
        @blob.properties[:copy_progress] = '0/1024'
        @blob.properties[:copy_status_description] = 'in progress'
        return @blob
      end

      if i == 2
        @blob.properties[:copy_status] = copy_status
        @blob.properties[:copy_progress] = '2/1024'
        @blob.properties[:copy_status_description] = 'in progress'
        return @blob
      end

      if i == 3
        @blob.properties[:copy_status] = copy_status
        @blob.properties[:copy_progress] = '1023/1024'
        @blob.properties[:copy_status_description] = 'in progress'
        return @blob
      end

      @blob.properties[:copy_status] = 'success'
      @blob.properties[:copy_progress] = '1024/1024'
      @blob.properties[:copy_status_description] = 'finish'
      @blob
    end

    @service.stub :get_blob_properties, multiple_values do
      @service.stub :sleep, ->(delay) {} do
        assert @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
      end
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_copy_finished_success
    copy_id = nil
    copy_status = 'success'

    @service.stub :get_blob_properties, @blob do
      assert @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_copy_id_is_nil
    copy_id = nil
    copy_status = 'pending'

    @service.stub :get_blob_properties, @blob do
      assert @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_copy_id_not_match_exception
    copy_id = 'copy_id'
    copy_status = 'pending'

    @service.stub :get_blob_properties, @blob do
      @service.stub :delete_blob, true do
        assert_raises(RuntimeError) do
          @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
        end
      end
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_timeout_exception
    copy_id = @blob.properties[:copy_id]
    copy_status = 'pending'

    i = 0
    multiple_values = lambda do |*|
      i += 1
      if i == 1
        @blob.properties[:copy_status] = copy_status
        @blob.properties[:copy_progress] = '0/1024'
        @blob.properties[:copy_status_description] = 'in progress'
        return @blob
      end

      @blob.properties[:copy_status] = copy_status
      @blob.properties[:copy_progress] = '0/1024'
      @blob.properties[:copy_status_description] = 'in progress'
      @blob
    end

    stubbed_times = [
      Time.now,
      Time.now + 5
    ]

    @service.stub :get_blob_properties, multiple_values do
      Time.stub :new, -> { stubbed_times.shift } do
        @service.stub :delete_blob, true do
          assert_raises(Timeout::Error) do
            @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status, 2)
          end
        end
      end
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_fail_exception
    copy_id = @blob.properties[:copy_id]
    copy_status = 'pending'

    @blob.properties[:copy_status] = 'failed'
    @service.stub :get_blob_properties, @blob do
      @service.stub :delete_blob, true do
        assert_raises(RuntimeError) do
          @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
        end
      end
    end
  end

  def test_wait_blob_copy_operation_to_finish_with_fail_when_delete_blob_http_exception
    copy_id = @blob.properties[:copy_id]
    copy_status = 'pending'

    @blob.properties[:copy_status] = 'failed'
    http_exception = ->(*) { raise Azure::Core::Http::HTTPError.new(@mocked_response) }
    @service.stub :get_blob_properties, @blob do
      @service.stub :delete_blob, http_exception do
        assert_raises(RuntimeError) do
          @service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', copy_id, copy_status)
        end
      end
    end
  end

  def test_wait_blob_copy_operation_to_finish_mock
    assert @mock_service.wait_blob_copy_operation_to_finish('test_container', 'test_blob', 'copy_id', 'pending')
  end
end