File: cache_spec.rb

package info (click to toggle)
ruby-fog-core 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: ruby: 4,719; makefile: 5
file content (191 lines) | stat: -rw-r--r-- 5,837 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
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
require "spec_helper"
require "securerandom"
require "tmpdir"

module Fog
  class SubFogTestModel < Fog::Model
    identity  :id
  end
end

module Fog
  class SubFogTestService < Fog::Service

    class Mock
      attr_reader :options

      def initialize(opts = {})
        @options = opts
      end
    end
  end
end

describe Fog::Cache do
  before(:each) do
    Fog.mock!
    @service = Fog::SubFogTestService.new
    Fog::Cache.namespace_prefix = "test-dir"
  end

  it "has a namespace_prefix configurable" do
    Fog::Cache.namespace_prefix = "for-service-user-region-foo"

    # Expand path does not downcase. case insensitive platform tests.
    example_cache = File.expand_path(Fog::Cache.namespace(Fog::SubFogTestModel, @service)).downcase
    expected_namespace = File.expand_path("~/.fog-cache/for-service-user-region-foo").downcase

    assert_equal example_cache.include?(expected_namespace), true
  end

  it "has metadata associated to the namespace that you can save to" do
    Fog::Cache.clean!
    Fog::Cache.namespace_prefix = "for-service-user-region-foo"
    # nothing exists, nothing comes back
    assert_equal Fog::Cache.metadata, {}
    # write/read
    Fog::Cache.write_metadata({:last_dumped => "Tuesday, November 8, 2016"})
    assert_equal Fog::Cache.metadata[:last_dumped], "Tuesday, November 8, 2016"

    # diff namespace, diff metadata
    Fog::Cache.namespace_prefix = "different-namespace"
    assert_nil Fog::Cache.metadata[:last_dumped]
    # still accessible per namespace
    Fog::Cache.namespace_prefix = "for-service-user-region-foo"
    assert_equal Fog::Cache.metadata[:last_dumped],  "Tuesday, November 8, 2016"
    # can overwrite
    Fog::Cache.write_metadata({:last_dumped => "Diff date"})
    assert_equal Fog::Cache.metadata[:last_dumped],  "Diff date"

    # can't write a non-hash/data entry.
    assert_raises Fog::Cache::CacheDir do
      Fog::Cache.write_metadata("boo")
    end

    # namespace must be set as well.
    assert_raises Fog::Cache::CacheDir do
      Fog::Cache.namespace_prefix = nil
      Fog::Cache.write_metadata({:a => "b"})
    end

 end

  it "can load cache data from disk" do
    path = File.expand_path("~/.fog-cache-test-#{Time.now.to_i}.yml")
    data = "--- ok\n...\n"
    File.open(path, "w") { |f|
      f.write(data)
    }

    assert_equal "ok", Fog::Cache.load_cache(path)
  end

  it "load bad cache data - empty file, from disk" do
    path = File.expand_path("~/.fog-cache-test-2-#{Time.now.to_i}.yml")
    data = ""
    File.open(path, "w") { |f|
      f.write(data)
    }

    assert !Fog::Cache.load_cache(path)
  end

  it "must have a namespace_prefix configurable" do
    Fog::Cache.namespace_prefix = nil
    assert_raises Fog::Cache::CacheDir do
      Fog::Cache.load(Fog::SubFogTestModel, @service)
    end
  end

  it "can create a namespace" do
    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)
    assert_equal File.exist?(Fog::Cache.namespace(Fog::SubFogTestModel, @service)), false

    Fog::Cache.create_namespace(Fog::SubFogTestModel, @service)
    assert_equal File.exist?(Fog::Cache.namespace(Fog::SubFogTestModel, @service)), true
  end

  it "will raise if no cache data found" do
    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)

    assert_raises Fog::Cache::CacheNotFound do
      Fog::Cache.load(Fog::SubFogTestModel, @service)
    end
  end

  it "Fog cache ignores bad cache data - empty file, from disk" do
    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)
    id = SecureRandom.hex
    a = Fog::SubFogTestModel.new(:id => id, :service => @service)
    a.cache.dump

    # input bad data
    path_dir = File.expand_path(Fog::Cache.namespace(Fog::SubFogTestModel, @service))
    path = File.join(path_dir, "foo.yml")
    data = ""
    File.open(path, "w") { |f|
      f.write(data)
    }

    assert_equal 1, Fog::Cache.load(Fog::SubFogTestModel, @service).size
  end


  it "can be dumped and reloaded back in" do

    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)

    id = SecureRandom.hex
    a = Fog::SubFogTestModel.new(:id => id, :service => @service)

    assert_equal File.exist?(Fog::Cache.namespace(Fog::SubFogTestModel, @service)), false
    a.cache.dump
    assert_equal File.exist?(Fog::Cache.namespace(Fog::SubFogTestModel, @service)), true

    instances = Fog::Cache.load(Fog::SubFogTestModel, @service)

    assert_equal instances.first.id, a.id
    assert_equal instances.first.class, a.class
  end

  it "dumping two models that have a duplicate identity" do
    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)

    id = SecureRandom.hex

    # security groups on aws for eg can have the same identity group name 'default'.
    # there are no restrictions on `identity` fog attributes to be uniq.
    a = Fog::SubFogTestModel.new(:id => id, :service => @service, :bar => 'bar')
    b = Fog::SubFogTestModel.new(:id => id, :service => @service, :foo => 'foo')

    a.cache.dump
    b.cache.dump

    instances = Fog::Cache.load(Fog::SubFogTestModel, @service)

    assert_equal instances.size, 2
  end

  it "dumping two models that have a duplicate identity twice" do
    Fog::Cache.expire_cache!(Fog::SubFogTestModel, @service)

    id = SecureRandom.hex

    # security groups on aws for eg can have the same identity group name 'default'.
    # there are no restrictions on `identity` fog attributes to be uniq.
    a = Fog::SubFogTestModel.new(:id => id, :service => @service, :bar => 'bar')
    b = Fog::SubFogTestModel.new(:id => id, :service => @service, :foo => 'foo')

    a.cache.dump
    b.cache.dump

    # and then again, w/out expiring cache
    a.cache.dump
    b.cache.dump

    instances = Fog::Cache.load(Fog::SubFogTestModel, @service)

    # unique-ify based on the attributes...
    assert_equal instances.size, 2
  end
end