Class: Onceover::Test

Inherits:
Object
  • Object
show all
Defined in:
lib/onceover/test.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_this, test_this, test_config) ⇒ Test

This can accept a bunch of stuff. It can accept nodes, classes or groups anywhere it will then detect them and expand them out into their respective objects so that we just end up with a list of nodes and classes



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
# File 'lib/onceover/test.rb', line 14

def initialize(on_this, test_this, test_config)

  @default_test_config = {
    'check_idempotency'       => true,
    'runs_before_idempotency' => 1
  }

  # Add defaults if they do not exist
  test_config = @default_test_config.merge(test_config)

  @nodes   = []
  @classes = []
  @test_config = test_config
  @test_config.delete('classes') # remove classes from the config

  # Make sure that tags are an array
  @test_config['tags'] ||= []
  @test_config['tags'] = [@test_config['tags']].flatten
  @tags = @test_config['tags']

  # Get the nodes we are working on
  if Onceover::Group.find(on_this)
    @nodes << Onceover::Group.find(on_this).members
  elsif Onceover::Node.find(on_this)
    @nodes << Onceover::Node.find(on_this)
  else
    raise "#{on_this} was not found in the list of nodes or groups!"
  end

  @nodes.flatten!

  # Check that our nodes list contains only nodes
  raise "#{@nodes} contained a non-node object." unless @nodes.all? { |item| item.is_a?(Onceover::Node) }

  if test_this.is_a?(String)
    # If we have just given a string then grab all the classes it corresponds to
    if Onceover::Group.find(test_this)
      @classes << Onceover::Group.find(test_this).members
    elsif Onceover::Class.find(test_this)
      @classes << Onceover::Class.find(test_this)
    else
      raise "#{test_this} was not found in the list of classes or groups!"
    end
    @classes.flatten!
  elsif test_this.is_a?(Hash)
    @classes = Onceover::TestConfig.subtractive_to_list(test_this)
  elsif test_this.is_a?(Onceover::Class)
    @classes << test_this
  end
end

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



6
7
8
# File 'lib/onceover/test.rb', line 6

def classes
  @classes
end

#default_test_configObject (readonly)

Returns the value of attribute default_test_config.



8
9
10
# File 'lib/onceover/test.rb', line 8

def default_test_config
  @default_test_config
end

#nodesObject

Returns the value of attribute nodes.



5
6
7
# File 'lib/onceover/test.rb', line 5

def nodes
  @nodes
end

#tagsObject (readonly)

Returns the value of attribute tags.



9
10
11
# File 'lib/onceover/test.rb', line 9

def tags
  @tags
end

#test_configObject

Returns the value of attribute test_config.



7
8
9
# File 'lib/onceover/test.rb', line 7

def test_config
  @test_config
end

Class Method Details

.allObject



132
133
134
# File 'lib/onceover/test.rb', line 132

def self.all
  @@all
end

.deduplicate(tests) ⇒ Object



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
# File 'lib/onceover/test.rb', line 87

def self.deduplicate(tests)
  require 'deep_merge'
  # This should take an array of tests and remove any duplicates from them

  # this will be an array of arrays, or maybe hashes
  combinations = []
  new_tests    = []

  tests.each do |test|
    test.nodes.each do |node|
      test.classes.each do |cls|
        combo = {node => cls}
        if combinations.member?(combo)

          # Find the right test object:
          relevant_test = new_tests[new_tests.index do |a|
            a.nodes[0] == node and a.classes[0] == cls
          end]

          # Delete all default values in the current options hash
          test.test_config.delete_if do |key, value|
            test.default_test_config[key] == value
          end

          # Merge the non-default options right on in there
          relevant_test.test_config.deep_merge!(test.test_config)
        else
          combinations << combo
          new_tests << Onceover::Test.new(node, cls, test.test_config)
        end
      end
    end
  end

  # The array that this returns should be ephemeral, it does not
  # represent anything defined in a controlrepo and should just
  # be passed into the thing doing the testing and then killed,
  # we don't want too many copies of the same shit going around
  #
  # Actually based on the way things are written I don't think this
  # will deduplicate node or class objects, just test objects,
  # everything else is passed by reference
  new_tests
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/onceover/test.rb', line 65

def eql?(other)
  @nodes.sort.eql?(other.nodes.sort) and @classes.sort.eql?(other.classes.sort)
end

#to_sObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/onceover/test.rb', line 69

def to_s
  class_msg = ""
  node_msg  = ""
  if classes.count > 1
    class_msg = "#{classes.count}_classes"
  else
    class_msg = classes[0].name.gsub(/::/,'__')
  end

  if nodes.count > 1
    node_msg = "#{nodes.count}_nodes"
  else
    node_msg = nodes[0].name
  end

  "#{class_msg}_on_#{node_msg}"
end