File: label_set_validator_spec.rb

package info (click to toggle)
ruby-prometheus-client-mmap 1.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 700 kB
  • sloc: ruby: 3,149; sh: 54; makefile: 21
file content (79 lines) | stat: -rw-r--r-- 2,582 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
# encoding: UTF-8

require 'prometheus/client/label_set_validator'

describe Prometheus::Client::LabelSetValidator do
  let(:validator) { Prometheus::Client::LabelSetValidator.new reserved_labels }
  let(:reserved_labels) { [] }

  describe '.new' do
    it 'returns an instance of a LabelSetValidator' do
      expect(validator).to be_a(Prometheus::Client::LabelSetValidator)
    end
  end

  describe '#valid?' do
    it 'returns true for a valid label check' do
      expect(validator.valid?(version: 'alpha')).to eql(true)
    end

    it 'raises InvalidLabelError if a label value is an array' do
      expect do
        validator.valid?(version: [1, 2, 3])
      end.to raise_exception(described_class::InvalidLabelError)
    end

    it 'raises Invaliddescribed_classError if a label set is not a hash' do
      expect do
        validator.valid?('invalid')
      end.to raise_exception(described_class::InvalidLabelSetError)
    end

    it 'raises InvalidLabelError if a label key is not a symbol' do
      expect do
        validator.valid?('key' => 'value')
      end.to raise_exception(described_class::InvalidLabelError)
    end

    it 'raises InvalidLabelError if a label key starts with __' do
      expect do
        validator.valid?(__reserved__: 'key')
      end.to raise_exception(described_class::ReservedLabelError)
    end

    context "when reserved labels were set" do
      let(:reserved_labels) { [:reserved] }

      it 'raises ReservedLabelError if a label key is reserved' do
        reserved_labels.each do |label|
          expect do
            validator.valid?(label => 'value')
          end.to raise_exception(described_class::ReservedLabelError)
        end
      end
    end
  end

  describe '#validate' do
    it 'returns a given valid label set' do
      hash = { version: 'alpha' }

      expect(validator.validate(hash)).to eql(hash)
    end

    it 'raises an exception if a given label set is not valid' do
      input = 'broken'
      expect(validator).to receive(:valid?).with(input).and_raise(described_class::InvalidLabelSetError)

      expect { validator.validate(input) }.to raise_exception(described_class::InvalidLabelSetError)
    end

    it 'raises InvalidLabelSetError for varying label sets' do
      validator.validate(method: 'get', code: '200')

      expect do
        validator.validate(method: 'get', exception: 'NoMethodError')
      end.to raise_exception(described_class::InvalidLabelSetError, "labels must have the same signature: (expected keys: [:code, :method], got: [:exception, :method])")
    end
  end
end