Permalink
Browse files

tests for group meta data

  • Loading branch information...
1 parent 1f6dabd commit a160ac57e3110cfbb5e3102f54d43386c3cae38f @igrigorik committed Jun 13, 2010
Showing with 26 additions and 18 deletions.
  1. +15 −16 lib/gmetric.rb
  2. +11 −2 spec/gmetric_spec.rb
View
@@ -16,29 +16,29 @@ def self.send(host, port, metric)
conn = UDPSocket.new
conn.connect(host, port)
gmetric = self.pack(metric)
-
+
conn.send gmetric[0], 0
conn.send gmetric[1], 0
end
def self.pack(metric)
metric = {
:hostname => '',
- :group => '',
+ :group => '',
:spoof => 0,
:units => '',
:slope => 'both',
:tmax => 60,
:dmax => 0
}.merge(metric)
-
+
# convert bools to ints
metric[:spoof] = 1 if metric[:spoof].is_a? TrueClass
metric[:spoof] = 0 if metric[:spoof].is_a? FalseClass
-
+
raise "Missing key, value, type" if not metric.key? :name or not metric.key? :value or not metric.key? :type
raise "Invalid metric type" if not %w(string int8 uint8 int16 uint16 int32 uint32 float double).include? metric[:type]
-
+
meta = XDRPacket.new
data = XDRPacket.new
@@ -49,26 +49,25 @@ def self.pack(metric)
meta.pack_int(metric[:spoof].to_i) # spoof hostname flag
meta.pack_string(metric[:type].to_s) # one of: string, int8, uint8, int16, uint16, int32, uint32, float, double
- meta.pack_string(metric[:name].to_s) # name of the metric
+ meta.pack_string(metric[:name].to_s) # name of the metric
meta.pack_string(metric[:units].to_s) # units for the value, e.g. 'kb/sec'
meta.pack_int(SLOPE[metric[:slope]]) # sign of the derivative of the value over time, one of zero, positive, negative, both, default both
meta.pack_uint(metric[:tmax].to_i) # maximum time in seconds between gmetric calls, default 60
meta.pack_uint(metric[:dmax].to_i) # lifetime in seconds of this metric, default=0, meaning unlimited
- ## MAGIC NUMBER: equals the elements of extra data, here it's 1 because I added Group.
- meta.pack_int(1)
-
+ ## MAGIC NUMBER: equals the elements of extra data, here it's 1 because I added Group.
+ meta.pack_int(1)
+
## METADATA EXTRA DATA: functionally key/value
meta.pack_string("GROUP")
meta.pack_string(metric[:group].to_s)
-
- # DATA payload
- data.pack_int(128+5) # string message
- data.pack_string(metric[:hostname].to_s) # hostname
- data.pack_string(metric[:name].to_s) # name of the metric
- data.pack_int(metric[:spoof].to_i) # spoof hostname flag
- data.pack_string("%s") #
+ # DATA payload
+ data.pack_int(128+5) # string message
+ data.pack_string(metric[:hostname].to_s) # hostname
+ data.pack_string(metric[:name].to_s) # name of the metric
+ data.pack_int(metric[:spoof].to_i) # spoof hostname flag
+ data.pack_string("%s") #
data.pack_string(metric[:value].to_s) # value of the metric
[meta.data, data.data]
View
@@ -37,7 +37,7 @@ def hex(data)
g = Ganglia::GMetric.pack(data)
g.size.should == 2
- g[0].should == "\000\000\000\200\000\000\000\000\000\000\000\003foo\000\000\000\000\000\000\000\000\006string\000\000\000\000\000\003foo\000\000\000\000\000\000\000\000\003\000\000\000<\000\000\000\000\000\000\000\000"
+ g[0].should == "\000\000\000\200\000\000\000\000\000\000\000\003foo\000\000\000\000\000\000\000\000\006string\000\000\000\000\000\003foo\000\000\000\000\000\000\000\000\003\000\000\000<\000\000\000\000\000\000\000\001\000\000\000\005GROUP\000\000\000\000\000\000\000"
g[1].should == "\000\000\000\205\000\000\000\000\000\000\000\003foo\000\000\000\000\000\000\000\000\002%s\000\000\000\000\000\003bar\000"
end
@@ -69,10 +69,19 @@ def hex(data)
lambda {
data = {:name => 'a', :type => 'uint8', :value => 'c', :spoof => 1, :host => 'host'}
Ganglia::GMetric.pack(data)
-
+
data = {:name => 'a', :type => 'uint8', :value => 'c', :spoof => true, :host => 'host'}
Ganglia::GMetric.pack(data)
}.should_not raise_error
end
+
+ it "should allow group meta data" do
+ lambda {
+ data = {:name => 'a', :type => 'uint8', :value => 'c', :spoof => 1, :host => 'host', :group => 'test'}
+ g = Ganglia::GMetric.pack(data)
+ g[0].should == "\000\000\000\200\000\000\000\000\000\000\000\001a\000\000\000\000\000\000\001\000\000\000\005uint8\000\000\000\000\000\000\001a\000\000\000\000\000\000\000\000\000\000\003\000\000\000<\000\000\000\000\000\000\000\001\000\000\000\005GROUP\000\000\000\000\000\000\004test"
+
+ }.should_not raise_error
+ end
end

0 comments on commit a160ac5

Please sign in to comment.