Skip to content

Commit

Permalink
Merge pull request #51 from v-zhuravlev/encaps_tests
Browse files Browse the repository at this point in the history
Better tests and add deploy
  • Loading branch information
v-zhuravlev authored Sep 24, 2018
2 parents 12af3dc + 517304d commit b3a91aa
Show file tree
Hide file tree
Showing 14 changed files with 485 additions and 484 deletions.
92 changes: 71 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,84 @@ sudo: required
services:
- docker

#ZBX_VERSION must be one of the tags for ubuntu here: https://hub.docker.com/r/zabbix/zabbix-agent/tags/
matrix:
include:
- name: Format
before_install: true
after_failure: true
env: TRAVIS_JOB_TYPE=FORMAT
- name: Zabbix 3.4
env: ZBX_VERSION=3.4.12 ZBX_HEADERS_VERSION=3.2 TRAVIS_JOB_TYPE=TEST
- name: Zabbix 3.2
env: ZBX_VERSION=3.2.11 ZBX_HEADERS_VERSION=3.2 TRAVIS_JOB_TYPE=TEST
- name: Zabbix 3.0
env: ZBX_VERSION=3.0.19 ZBX_HEADERS_VERSION=3 TRAVIS_JOB_TYPE=TEST
#- name: Zabbix 2.2
# env: ZBX_VERSION=2.2.19 ZBX_HEADERS_VERSION=2


before_install:
- ./autogen.sh
- python -m pip install -q --user pytest
- ./configure --enable-zabbix-${ZBX_HEADERS_VERSION} -q
- make
- pushd tests/docker && docker-compose up -d
- popd
- docker logs --since 1h docker_zabbix-agent-modbus_1
- docker exec docker_zabbix-agent-modbus_1 sh -c "zabbix_get -s localhost -k agent.ping"
- docker exec docker_zabbix-agent-modbus_1 sh -c "zabbix_get -s localhost -k modbus_read[172.16.238.2:5020,1,2,3,uint16,1,1]"
- docker exec docker_zabbix-agent-modbus_1 sh -c "zabbix_get -s localhost -k agent.ping" || (docker logs --since 1h docker_zabbix-agent-modbus_1 ; false)
- docker exec docker_zabbix-agent-modbus_1 sh -c "zabbix_get -s localhost -k modbus_read[172.16.238.2:5020,1,2,3,uint16,1,1]" || (docker logs --since 1h docker_zabbix-agent-modbus_1 ; false)
script:
- ./run-tests.sh
after_failure:
- docker logs --since 1h docker_zabbix-agent-modbus_1
- docker logs --since 1h docker_zabbix-agent-modbus_1

jobs:
include:
- name: Format
before_install: true
after_failure: true
env: TRAVIS_JOB_TYPE=FORMAT
#ZBX_VERSION must be one of the tags for ubuntu here: https://hub.docker.com/r/zabbix/zabbix-agent/tags/
- name: Zabbix 3.4 Modbus TCP
env: <
ZBX_VERSION=3.4.12
ZBX_HEADERS_VERSION=3.2
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=172.16.238.2:5020
- name: Zabbix 3.2 Modbus TCP
env: <
ZBX_VERSION=3.2.11
ZBX_HEADERS_VERSION=3.2
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=172.16.238.2:5020
- name: Zabbix 3.0 Modbus TCP
env: <
ZBX_VERSION=3.0.19
ZBX_HEADERS_VERSION=3
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=172.16.238.2:5020
- name: Zabbix 3.4 Modbus RTU over TCP
env: <
ZBX_VERSION=3.4.12
ZBX_HEADERS_VERSION=3.2
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=enc://172.16.238.2:5021
- name: Zabbix 3.2 Modbus RTU over TCP
env: <
ZBX_VERSION=3.2.11
ZBX_HEADERS_VERSION=3.2
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=enc://172.16.238.2:5021
- name: Zabbix 3.0 Modbus RTU over TCP
env: <
ZBX_VERSION=3.0.19
ZBX_HEADERS_VERSION=3
TRAVIS_JOB_TYPE=TEST
MODBUS_HOST=enc://172.16.238.2:5021
#-name: Zabbix 2.2
# env: ZBX_VERSION=2.2.19 ZBX_HEADERS_VERSION=2
- stage: deploy
name: Deploy to GitHub
before_install: true
script: true
after_failure: true
before_deploy:
- ./autogen.sh
- make dist
deploy:
provider: releases
api_key:
secure: AUlyQK/z79Ty/84fsjxNb6/g2V3DqbMOhcCSs46YvyMNAVSJBeL2MHlO9rOkviLo/YIPKowy7xTAxCnRmDda8yZBYqdBqqCgdphFtPVABTuWEVomQ/RlM5j9BLZWpqqY7NcikP5xbF5L7N35DnRaATgHMLXyDmJKA0Zg460GX6clZR29JlkE7tBmAsrNvdc8uY+fRfO+s3Oa1/J48ndJIMqkOw4+Brr9m8YJAK3EmF0B6dhqJ6pMIb7iFBDRhnS0vyerjKDjJlDb5P65jTx2TUsghUlDv8W1zv5wFmWS7+S3yJg9MTmZ+SSjjIufNMqT1R3jE/jpfxHJAHd5pFrtft7GdmZ22j41nJHUb2PW6KHGhj5IgSMbnylqEd5bTXMMOsxlErRxtC7/ykVGO7dKK47BdbIbwhWTa0QQ21Z/Y1BJgFpp3PQoWx01x4YjCspQaSmKRmdqpBj0BG2+SO6/fWf9ZuUCh+0yKr9meIN6LlckPMiRoPCdoJ5Z0Rf5ppyWRXDKBet69XUnK2LT0TmtIEAMuE308NBjrYodC/yqC4A9C4PfBCLvIW3Lm5kCOqTC9hw+7jwMWGxRv8J3DgxJ/TZeAcOZlVKeXDtZdKyYB/KfHNPSQcYhlY4KutpZe2C1AnRJmviE2oWlLGTujwXPwvLxG7kCS7PrYS0LWPEdGD8=
file_glob: true
file: libzbxmodbus-*.tar.gz
skip_cleanup: true
on:
repo: v-zhuravlev/libzbxmodbus
branch: deploy
tags: true
stages:
- test
- deploy
9 changes: 7 additions & 2 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ if [[ ${TRAVIS_JOB_TYPE} == "FORMAT" ]]; then
git diff --exit-code
else
# run tests
echo "Going to test with docker and modbus-slave emulation..."
pytest -v
if [[ ${MODBUS_HOST} ]]; then
echo "Going to test with docker and modbus-slave emulation, host=${MODBUS_HOST}"
pytest -v --host=$MODBUS_HOST
else
echo "Going to test with docker and modbus-slave emulation..."
pytest -v
fi
fi
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

host_default = "172.16.238.2:5020"


def pytest_addoption(parser):
parser.addoption(
"--host", action="store", default=host_default, help="Setup connection string"
)


@pytest.fixture
def host(request):
return request.config.getoption("--host")
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import unittest

import pytest
from zabbix_get import zabbix_get

class ModbusConnectionStringTestCase(unittest.TestCase):

class TestModbusConnectionString(object):

host = "172.16.238.2:5020"
host_rtutcp = "172.16.238.2:5021"


# test enc:// (rtu over tcp)

def test_modbus_test_rtutcp(self):
key = "modbus_read[enc://"+self.host_rtutcp+",1,0,3,uint16]"
self.assertEqual(zabbix_get(key),'49807')
assert zabbix_get(key) == '49807'
# test tcp:// (plain tcp)

def test_modbus_tcp(self):
key = "modbus_read[tcp://"+self.host+",1,1,3,uint16]"
self.assertEqual(zabbix_get(key),'49677')
assert zabbix_get(key) == '49677'

# test serial /dev/ttyS0
@unittest.skip("implement this first")
def test_modbus_serial(self):
@pytest.mark.skip("implement this first")
def test_modbus_serial(self, host):
key = "modbus_read[/dev/ttyS0,1,1,3,uint16]"
self.assertEqual(zabbix_get(key),'49677')



if __name__ == '__main__':
unittest.main(verbosity=2)
assert zabbix_get(key) == '49677'
69 changes: 69 additions & 0 deletions tests/test_01modbus_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pytest
from zabbix_get import zabbix_get

class TestModbusErrors(object):


# test errors
def test_empty(self, host):
"""Test Empty"""
key = "modbus_read"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Invalid number of parameters.'
def test_empty_squares(self, host):
"""Test empty squares"""
key = "modbus_read[]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Invalid number of parameters.'

def test_no_IP(self, host):
"""Test no IP"""
key = "modbus_read_registers[,3,14,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: No connection address provided.'
def test_bad_IP(self, host):
"""Test bad IP"""
key = "modbus_read_registers[badIP,3,14,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Network is unreachable'

def test_no_slaveID(self, host):
key = "modbus_read_registers[{HOST.CONN},,14,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: No slave id provided.'
def test_no_function(self, host):
key = "modbus_read_registers["+host+",3,14,,uint32,BE,0]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: No Modbus function provided! Please provide either 1,2,3 == 4.'

def test_bad_PDU_flag_integer(self, host):
key = "modbus_read_registers["+host+",3,14,3,uint32,BE,3]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check addressing scheme(PDU == PROTOCOL) used'
def test_bad_PDU_flag_string(self, host):
key = "modbus_read_registers["+host+",3,14,3,uint32,BE,bad]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check addressing scheme(PDU == PROTOCOL) used'


def test_bad_LE_flag_integer(self, host):
key = "modbus_read_registers["+host+",3,14,3,uint32,5,0]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check endiannes used: BE,LE,MLE == MBE.'

def test_bad_LE_flag_string(self, host):
key = "modbus_read_registers["+host+",3,14,3,uint32,bad,0]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check endiannes used: BE,LE,MLE == MBE.'


def test_bad_function_integer(self, host):
key = "modbus_read_registers["+host+",3,14,5,uint32,BE,0]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check function (1,2,3 == 4) used'
def test_bad_function_string(self, host):
key = "modbus_read_registers["+host+",3,14,bad,uint32,BE,0]"
assert zabbix_get(key),'ZBX_NOTSUPPORTED: Check function (1,2,3 == 4) used'

def test_bad_register_out_of_bounds_integer(self, host):
key = "modbus_read_registers["+host+",3,1000,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Illegal data address'
def test_bad_register_string(self, host):
key = "modbus_read_registers["+host+",3,bad,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Check register to read'

def test_bad_slaveid_integer(self, host):
key = "modbus_read_registers["+host+",5000,99,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Illegal data address'
def test_bad_slaveid_string(self, host):
key = "modbus_read_registers["+host+",bad,1,3,uint32,BE,0]"
assert zabbix_get(key) == 'ZBX_NOTSUPPORTED: Check slaveid parameter'
69 changes: 69 additions & 0 deletions tests/test_02modbus_short_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pytest
from zabbix_get import zabbix_get


class TestModbusShort(object):

# test coils(bits)

def test_modbus_datatype_short_bit_0(self, host):
key = "modbus_read["+host+",1,1,1,b]"
assert zabbix_get(key) == '1'

# 16bit, all tests are PDU (start from 0 address)
# INT16,Big Endian
def test_modbus_datatype_short_signed_1(self, host):
key = "modbus_read["+host+",1,1,3,s,1]"
assert zabbix_get(key) == '-15859.000000'

# UINT16,Big Endian, i
def test_modbus_datatype_short_unsigned_2(self, host):
key = "modbus_read["+host+",1,1,3,i,1]"
assert zabbix_get(key) == '49677'

# 32bit.
# Float. In 32bit float we are looking for "-71.879005"
# Big Endian (ABCD)
def test_modbus_datatype_short_float_32bit_be_abcd_0(self, host):
key = "modbus_read["+host+",1,0,3,f,1]"
assert zabbix_get(key) == '-71.879005'
# Little Endian with Wordswap or Mid-Little Endian (CDAB)

def test_modbus_datatype_short_float_32bit_mle_cdab_0(self, host):
key = "modbus_read["+host+",1,4,3,f,0]"
assert zabbix_get(key) == '-71.879005'
# Big Endian with WordSwap or Mid-Big Endian (BADC)

def test_modbus_datatype_short_float_32bit_mbe_badc_0(self, host):
key = "modbus_read["+host+",1,2,3,f,2]"
assert zabbix_get(key) == '-71.879005'
# Little Endian (DCBA)

def test_modbus_datatype_short_float_32bit_le_dcba_0(self, host):
key = "modbus_read["+host+",1,6,3,f,3]"
assert zabbix_get(key) == '-71.879005'

# UINT32,unsigned,long:
# Big Endian (ABCD)
def test_modbus_datatype_short_long_32bit_be_abcd_0(self, host):
key = "modbus_read["+host+",1,0,3,l,1]"
assert zabbix_get(key) == '3264201229'

# INT32(Signed)
# Big Endian (ABCD)
def test_modbus_datatype_short_int32_be_0(self, host):
key = "modbus_read["+host+",1,12,3,S,1]"
assert zabbix_get(key) == '-562.000000'

# UNSIGNED INT64
# Big Endian

def test_modbus_datatype_short_uint64_64bit_be_0(self, host):
key = "modbus_read["+host+",1,14,3,I,1]"
assert zabbix_get(key) == '13816931967501922940'

# FLOAT64(double)
# Big Endian
def test_modbus_datatype_short_double_64bit_be_0(self, host):
key = "modbus_read["+host+",1,14,3,d,1]"
assert zabbix_get(key) == '-0.123450'
Loading

0 comments on commit b3a91aa

Please sign in to comment.