File: file_tests.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (263 lines) | stat: -rw-r--r-- 7,777 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
require "test_helper"
require "helpers/model_helper"

unless Fog.mocking?
  def object_attributes(file = @instance)
    @instance.service.head_object(@directory.key, file.key).headers
  end

  def object_meta_attributes
    @instance.service.head_object(
      @directory.key,
      @instance.key
    ).headers.reject { |k, _| !(k =~ /X-Object-Meta-/) }
  end

  def clear_metadata
    @instance.metadata.tap do |metadata|
      metadata.each_pair { |k, _| metadata[k] = nil }
    end
  end

  @file_attributes = {
    :key  => 'fog_file_tests',
    :body => lorem_file
  }

  directory_attributes = {
    # Add a random suffix to prevent collision
    :key => "fogfilestests-#{rand(65536)}"
  }

  @directory = Fog::OpenStack::Storage.new.directories.create(directory_attributes)

  describe "Fog::OpenStack::Storage | file" do
    after do
      @directory.destroy
    end

    model_tests(@directory.files, @file_attributes.merge(:etag => 'foo'), Fog.mocking?) do
      it "#save should not blow up with etag" do
        @instance.save
      end
    end

    model_tests(@directory.files, @file_attributes, Fog.mocking?) do
      it "#metadata should load empty metadata" do
        @instance.metadata.must_equal {}
      end

      describe "#save" do
        it "#metadata" do
          before do
            @instance.metadata[:foo] = 'bar'
            @instance.save
          end

          after do
            clear_metadata
            @instance.save
          end

          it "should update metadata" do
            object_meta_attributes['X-Object-Meta-Foo'].must_equal 'bar'
          end

          it "should cache metadata" do
            @instance.metadata[:foo].must_equal 'bar'
          end

          it "should remove empty metadata" do
            @instance.metadata[:foo] = nil
            @instance.save
            object_meta_attributes.must_equal {}
          end
        end

        describe "#cache_control" do
          before do
            @instance = @directory.files.create(
              :key           => 'meta-test',
              :body          => lorem_file,
              :cache_control => 'public, max-age=31536000'
            )
          end

          after do
            clear_metadata
            @instance.save
          end

          it "sets Cache-Control on create" do
            object_attributes(@instance)["Cache-Control"].must_equal "public, max-age=31536000"
          end
        end

        describe "#content_disposition" do
          before do
            @instance = @directory.files.create(
              :key                 => 'meta-test',
              :body                => lorem_file,
              :content_disposition => 'ho-ho-ho'
            )
          end

          after do
            clear_metadata
            @instance.save
          end

          it "sets Content-Disposition on create" do
            object_attributes(@instance)["Content-Disposition"].must_equal "ho-ho-ho"
          end
        end

        describe "#metadata keys" do
          after do
            clear_metadata
            @instance.save
          end

          it "should support compound key names" do
            @instance.metadata[:foo_bar] = 'baz'
            @instance.save
            object_meta_attributes['X-Object-Meta-Foo-Bar'].must_equal 'baz'
          end

          it "should support string keys" do
            @instance.metadata['foo'] = 'bar'
            @instance.save
            object_meta_attributes['X-Object-Meta-Foo'].must_equal 'bar'
          end

          it "should support compound string key names" do
            @instance.metadata['foo_bar'] = 'baz'
            @instance.save
            object_meta_attributes['X-Object-Meta-Foo-Bar'].must_equal 'baz'
          end

          it "should support hyphenated keys" do
            @instance.metadata['foo-bar'] = 'baz'
            @instance.save
            object_meta_attributes['X-Object-Meta-Foo-Bar'].must_equal 'baz'
          end

          it "should only support one value per metadata key" do
            @instance.metadata['foo-bar'] = 'baz'
            @instance.metadata[:foo_bar] = 'bref'
            @instance.save
            object_meta_attributes['X-Object-Meta-Foo-Bar'].must_equal 'bref'
          end
        end
      end

      describe "#access_control_allow_origin" do
        it "#access_control_allow_origin should default to nil" do
          @instance.access_control_allow_origin.must_equal nil
        end

        @instance.access_control_allow_origin = 'http://example.com'
        @instance.save
        it "#access_control_allow_origin should return access control attribute" do
          @instance.access_control_allow_origin.must_equal 'http://example.com'
        end

        @instance.access_control_allow_origin = 'foo'
        @instance.save
        it "#access_control_allow_origin= should update access_control_allow_origin" do
          @instance.access_control_allow_origin = 'bar'
          @instance.save
          @instance.access_control_allow_origin.must_equal 'bar'
        end

        it "#access_control_allow_origin= should not blow up on nil" do
          @instance.access_control_allow_origin = nil
          @instance.save
        end
      end

      describe "#delete_at" do
        @delete_at_time = (Time.now + 300).to_i

        it "#delete_at should default to nil" do
          @instance.delete_at.must_equal nil
        end

        @instance.delete_at = @delete_at_time
        @instance.save
        it "#delete_at should return delete_at attribute" do
          @instance.delete_at.must_equal @delete_at_time
        end

        @instance.delete_at = @delete_at_time
        @instance.save
        it "#delete_at= should update delete_at" do
          @instance.delete_at = @delete_at_time + 100
          @instance.save
          @instance.delete_at.must_equal(@delete_at_time + 100)
        end

        it "#delete_at= should not blow up on nil" do
          @instance.delete_at = nil
          @instance.save
        end
      end
    end

    model_tests(@directory.files, @file_attributes, Fog.mocking?) do
      describe "#origin" do
        it "#origin should default to nil" do
          @instance.save
          @instance.origin.must_equal nil
        end

        @instance.origin = 'http://example.com'
        @instance.save
        it "#origin should return access control attributes" do
          @instance.origin.must_equal('http://example.com')
        end
        @instance.attributes.delete('Origin')

        @instance.origin = 'foo'
        @instance.save
        it "#origin= should update origin" do
          @instance.origin = 'bar'
          @instance.save
          @instance.origin.must_equal 'bar'
        end

        it "#origin= should not blow up on nil" do
          @instance.origin = nil
          @instance.save
        end
      end

      describe "#content_encoding" do
        it "#content_encoding should default to nil" do
          @instance.save
          @instance.content_encoding.must_equal nil
        end

        @instance.content_encoding = 'gzip'
        @instance.save
        it "#content_encoding should return the content encoding" do
          @instance.content_encoding.must_equal 'gzip'
        end
        @instance.attributes.delete('content_encoding')

        @instance.content_encoding = 'foo'
        @instance.save
        it "#content_encoding= should update content_encoding" do
          @instance.content_encoding = 'bar'
          @instance.save
          @instance.content_encoding.must_equal 'bar'
        end

        it "#content_encoding= should not blow up on nil" do
          @instance.content_encoding = nil
          @instance.save
        end
      end
    end
  end
end