Top Level Namespace

Defined Under Namespace

Modules: Fog, Shindo

Constant Summary collapse

LOREM =
<<HERE
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
HERE

Instance Method Summary collapse

Instance Method Details

#array_differences(array_a, array_b) ⇒ Object



26
27
28
# File 'lib/fog/test_helpers/helper.rb', line 26

def array_differences(array_a, array_b)
  (array_a - array_b) | (array_b - array_a)
end

#collection_tests(collection, params = {}, mocks_implemented = true) ⇒ Object



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
# File 'lib/fog/test_helpers/collection_helper.rb', line 1

def collection_tests(collection, params = {}, mocks_implemented = true)
  tests("success") do
    tests("#new(#{params.inspect})").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      collection.new(params)
    end

    tests("#create(#{params.inspect})").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      @instance = collection.create(params)
    end

    # FIXME: work around for timing issue on AWS describe_instances mocks
    if Fog.mocking? && @instance.respond_to?(:ready?)
      @instance.wait_for { ready? }
    end

    tests("#all").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      collection.all
    end

    @identity = @instance.identity if !Fog.mocking? || mocks_implemented

    tests("#get(#{@identity})").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      collection.get(@identity)
    end

    tests("Enumerable") do
      pending if Fog.mocking? && !mocks_implemented

      methods = %w(all? any? find detect collect map find_index flat_map
                   collect_concat group_by none? one?)

      methods.each do |enum_method|
        next unless collection.respond_to?(enum_method)
        tests("##{enum_method}").succeeds do
          block_called = false
          collection.send(enum_method) { block_called = true }
          block_called
        end
      end

      %w(max_by min_by).each do |enum_method|
        next unless collection.respond_to?(enum_method)
        tests("##{enum_method}").succeeds do
          block_called = false
          collection.send(enum_method) do
            block_called = true
            0
          end
          block_called
        end
      end
    end

    yield if block_given?

    @instance.destroy if !Fog.mocking? || mocks_implemented
  end

  tests("failure") do
    if !Fog.mocking? || mocks_implemented
      @identity = @identity.to_s
      @identity = @identity.gsub(/[a-zA-Z]/) { Fog::Mock.random_letters(1) }
      @identity = @identity.gsub(/\d/)       { Fog::Mock.random_numbers(1) }
      @identity
    end

    tests("#get('#{@identity}')").returns(nil) do
      pending if Fog.mocking? && !mocks_implemented
      collection.get(@identity)
    end
  end
end

#flavors_tests(connection, _params = {}, mocks_implemented = true) ⇒ Object



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
# File 'lib/fog/test_helpers/compute/flavors_helper.rb', line 1

def flavors_tests(connection, _params = {}, mocks_implemented = true)
  tests("success") do
    tests("#all").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      connection.flavors.all
    end

    if !Fog.mocking? || mocks_implemented
      @identity = connection.flavors.first.identity
    end

    tests("#get('#{@identity}')").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      connection.flavors.get(@identity)
    end
  end

  tests("failure") do
    if !Fog.mocking? || mocks_implemented
      invalid_flavor_identity = connection.flavors.first.identity.to_s.gsub(/\w/, "0")
    end

    tests("#get('#{invalid_flavor_identity}')").returns(nil) do
      pending if Fog.mocking? && !mocks_implemented
      connection.flavors.get(invalid_flavor_identity)
    end
  end
end

#lorem_fileObject



19
20
21
22
23
24
# File 'lib/fog/test_helpers/helper.rb', line 19

def lorem_file
  Tempfile.new("lorem").tap do |f|
    f.write(LOREM)
    f.rewind
  end
end

#model_tests(collection, params = {}, mocks_implemented = true) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fog/test_helpers/model_helper.rb', line 1

def model_tests(collection, params = {}, mocks_implemented = true)
  tests("success") do
    @instance = collection.new(params)

    tests("#save").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      @instance.save
    end

    yield if block_given?

    tests("#destroy").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      @instance.destroy
    end
  end
end

#server_tests(connection, params = {}, mocks_implemented = true) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/test_helpers/compute/server_helper.rb', line 1

def server_tests(connection, params = {}, mocks_implemented = true)
  model_tests(connection.servers, params, mocks_implemented) do
    tests("#reload").returns(true) do
      pending if Fog.mocking? && !mocks_implemented
      @instance.wait_for { ready? }
      identity = @instance.identity
      !identity.nil? && identity == @instance.reload.identity
    end

    responds_to([:ready?, :state])
    yield if block_given?

    tests("#reboot").succeeds do
      pending if Fog.mocking? && !mocks_implemented
      @instance.wait_for { ready? }
      @instance.reboot
    end

    @instance.wait_for { ready? } if !Fog.mocking? || mocks_implemented
  end
end

#servers_tests(connection, params = {}, mocks_implemented = true) ⇒ Object



1
2
3
4
5
6
7
8
# File 'lib/fog/test_helpers/compute/servers_helper.rb', line 1

def servers_tests(connection, params = {}, mocks_implemented = true)
  collection_tests(connection.servers, params, mocks_implemented) do
    if !Fog.mocking? || mocks_implemented
      @instance.wait_for { ready? }
      yield if block_given?
    end
  end
end

#uniq_id(base_name = "fog-test") ⇒ Object

Generates a unique identifier with a random differentiator. Useful when rapidly re-running tests, so we don“t have to wait serveral minutes for deleted objects to disappear from the API E.g. ”fog-test-1234“



23
24
25
26
27
# File 'lib/fog/test_helpers/model_helper.rb', line 23

def uniq_id(base_name = "fog-test")
  # random_differentiator
  suffix = rand(65_536).to_s(16).rjust(4, "0")
  [base_name, suffix].join("-")
end