How to test my beat output

(Brandon Klise) #1

I notice that the example beats use python to test the output of the beat. I copied the packetbeat example for one test and tried to get mine to run, and I keep getting errors.

I aslo do not understand in the files the significance of . env/bin/activate so I must be missing something.

When I try to run my test, this is the error I receive:

$ nosetests
ERROR: Failure: ImportError (No module named test_stats)
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/", line 411, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/", line 79, in importFromDir
    fh, filename, desc = find_module(part, path)
ImportError: No module named test_stats

Ran 1 test in 0.001s

FAILED (errors=1)

Can someone please describe step-by-step how to create a working integration test for my beat and what is required?


(Steffen Siering) #2

Error seems to be related to python environment missing some dependencies. All beats have a $ make python-env make target to build a virtualenv containing all dependencies. Once $ make python-env succeeds one can active the virtual environment via $ source build/python-env/bin/active. To deactive the activated python virtualenv run $ deactivate (for example when wanting to switch system testing of another beat).

Most python based tests use file output. See packetbeat config template used by test/system. This method is used to configure and start packetbeat. And read_output call is used to read all events being published by packetbeat tests.

Packetbeat tests/system includes some shared functionality from libbeat tests. Instead of checking output file, you can also use log_contains to check beat log-output.

(Steffen Siering) #3

In order to run packetbeat tests manually without running all tests via make testsuite I normally do these steps:

  1. prepare and build python environment
    $ make python-env

  2. active python test environment
    $ source build/python-env/bin/activate

  3. build test-beat (adds support for collecting coverage information). Creates a packetbeat.test binary consisting of packetbeat + golang test support.
    make buildbeat.test

  4. go to tests/system (alternatively use -w tests/system with nosetest)

  5. run nosetests (-x = stop on first failure, -v = verbose)
    $ nosetests --with-timer -v -x.

nosetests without python file will scan directory for tests to run and executes them all.

Alternatively to executing all these steps manually one can run $ make testsuite. This executes all unit tests, integration tests and system tests. Some beats requiring additional services will require docker to run all tests in order to start up additional services in docker containers.

(Brandon Klise) #4

Awesome, thanks for the help! I really appreciate it!

(Brandon Klise) #5

@steffens your step-by-step explanation was very helpful for getting to run tests, but I'm still having issues getting my test to function properly. Here is my error:

$ nosetests --with-timer -v -x
Checks that table stats are found in the ... ERROR

ERROR: Checks that table stats are found in the
Traceback (most recent call last):
  File "/home/bklise/work/src/", line 18, in test_table_stats
    self.wait_until(lambda: self.output_has(lines=1))
  File "/home/bklise/work/src/", line 144, in wait_until
    "Waited {} seconds.".format(max_timeout))
Exception: Timeout waiting for 'cond' to be true. Waited 10 seconds.

test_stats.Test.test_table_stats: 10.0567s
Ran 1 test in 10.058s

FAILED (errors=1)

I'm not getting any errors in my log file cassandrabeat/build/system-tests/last_run/cassandrabeat.log and my .yml file cassandrabeat/build/system-tests/last_run/cassandrabeat.yml is being generated correctly. However, there is no output file created and written to even though I have the path and filename fields set in cassandrabeat/tests/system/config/cassandrabeat.yml.j2:


  ### File as output
    # Enabling file output
    enabled: true

    # Path to the directory where to save the generated files. The option is mandatory.
    path: "{{ output_file_path|default(cassandrabeat.working_dir + "/output") }}"

    # Name of the generated files. The default is `cassandrabeat` and it generates
    # files: `cassandrabeat`, `cassandrabeat.1`, `cassandrabeat.2`, etc.
    filename: "{{ output_file_filename|default("cassandrabeat") }}"

Since none of the community beats seem to have any integration tests (at least, none that I have seen), I don't have anything to base mine off of. Here is a link to my beat. If anyone has any ideas for how to create a functional integration test, it would be greatly appreciated.

(Brandon Klise) #6

Also, I am running into an issue with building the python environment. When I use the make python-env command from $GOPATH/, I get the following error:

make: *** No rule to make target ../libbeat/tests/system/requirements.txt', needed bypython-env'. Stop.

I assume the solution to this is very simple, but I've been able to get around it by copying my beat to $GOPATH/ So, what should I do to be able to build the python environment from the goomzee directory?


(Brandon Klise) #7

I changed my cassandrabeat.go file to be able to run the beat from any directory without needing my shell script in the same directory, and that seemed to have solved my issue of the test not creating and writing to the output/cassandra file! Now I'm getting an AssertionError that I need to look into, but this issue seems to be resolved.

(ruflin) #8

For the python-env: The path depends on ${ES_BEATS} variable. What is the value of ${ES_BEATS} in your Makefile?

About the integration tests: There could be potential path issues with the included template. I never really tested it if it works with community beats (but we should). Most of the fixes are quite simple by adding ${ES_BEATS} to the path in some files. It would be nice if you could keep reporting errors so we can fix it and all community beats can profit from it.

(Brandon Klise) #9

I didn't have the ${ES_BEATS} variable declared in my Makefile. After setting its value to ../../elastic/beats I am now able to build the python-env from the desired directory.


(system) #10

This topic was automatically closed after 21 days. New replies are no longer allowed.