Skip to content

Commit

Permalink
collectd::plugin::modbus: reformat data type aliases and add spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vStone committed Jan 23, 2025
1 parent 4964e2c commit 8837c62
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
34 changes: 34 additions & 0 deletions spec/type_aliases/collectd_modbus_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Collectd::Modbus::Data' do
it do
is_expected.to allow_values({
'type' => 'foo',
'register_base' => 123,
'register_type' => 'Int32',
})
end

it do
is_expected.to allow_values({
'type' => 'foo',
'register_base' => 123,
'register_type' => 'Int32',
'register_cmd' => 'ReadInput',
})
end

it do
is_expected.to allow_values({
'instance' => 'foobar',
'type' => 'foo',
'register_base' => 123,
'register_type' => 'Int32',
})
end

it { is_expected.not_to allow_values(nil) }
it { is_expected.not_to allow_values({ 'type' => 'foo', 'register_base' => 123 }) }
end
19 changes: 19 additions & 0 deletions spec/type_aliases/collectd_modbus_host_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Collectd::Modbus::Host' do
let(:slaves) do
{
1 => { 'instance' => 'foo1', 'collect' => 'bar1' },
2 => { 'instance' => 'foo2', 'collect' => %w[bar1 bar2] },
}
end

it { is_expected.to allow_values({ 'address' => '127.0.0.1', 'port' => 1234, 'slaves' => slaves }) }
it { is_expected.to allow_values({ 'address' => '127.0.0.1', 'port' => 1234, 'slaves' => slaves, 'interval' => 120 }) }

it { is_expected.not_to allow_values(nil) }
it { is_expected.not_to allow_values({ 'address' => '127.0.0.1', 'port' => '1234', 'slaves' => slaves, 'interval' => 120 }) }
it { is_expected.not_to allow_values({ 'port' => 1234, 'slaves' => slaves, 'interval' => 120 }) }
end
13 changes: 13 additions & 0 deletions spec/type_aliases/collectd_modbus_slave_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Collectd::Modbus::Slave' do
it { is_expected.to allow_values({ 'instance' => 'foo1', 'collect' => 'bar1' }) }
it { is_expected.to allow_values({ 'instance' => 'foo1', 'collect' => %w[bar1 bar2] }) }

it { is_expected.not_to allow_values(nil) }
it { is_expected.not_to allow_values({ 'collect' => ['bar1'] }) }
it { is_expected.not_to allow_values({ 'instance' => 'foo1' }) }
it { is_expected.not_to allow_values({ 'instance' => 'foo1', 'collect' => [] }) }
end
12 changes: 6 additions & 6 deletions types/modbus/data.pp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#
# @summary represents a modbus data entry
type Collectd::Modbus::Data = Struct[{
Optional['instance'] => String,
NotUndef['type'] => String[1],
NotUndef['register_base'] => Integer[0],
NotUndef['register_type'] => Enum['Int16', 'Int32', 'Uint16', 'Uint32', 'Float'],
Optional['register_cmd'] => Enum['ReadHolding', 'ReadInput'],
Optional['instance'] => String,
NotUndef['type'] => String[1],
NotUndef['register_base'] => Integer[0],
NotUndef['register_type'] => Enum['Int16', 'Int32', 'Uint16', 'Uint32', 'Float'],
Optional['register_cmd'] => Enum['ReadHolding', 'ReadInput'],
}]
9 changes: 7 additions & 2 deletions types/modbus/host.pp
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
#
type Collectd::Modbus::Host = Struct[{NotUndef['address'] => String[1], NotUndef['port'] => String[1], NotUndef['slaves'] => Hash[Integer, Collectd::Modbus::Slave], Optional['interval'] => Integer[0]}]
# @summary represents a modbus host entry
type Collectd::Modbus::Host = Struct[{
NotUndef['address'] => String[1],
NotUndef['port'] => Stdlib::Port,
NotUndef['slaves'] => Hash[Integer, Collectd::Modbus::Slave],
Optional['interval'] => Integer[0]
}]
10 changes: 8 additions & 2 deletions types/modbus/slave.pp
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
#
type Collectd::Modbus::Slave = Struct[{NotUndef['instance'] => String[1], NotUndef['collect'] => Variant[String[1], Array[String[1], 1]]}]
# @summary Represents a modbus host's slave entry
type Collectd::Modbus::Slave = Struct[{
NotUndef['instance'] => String[1],
NotUndef['collect'] => Variant[
String[1],
Array[String[1], 1]
]
}]

0 comments on commit 8837c62

Please sign in to comment.