Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autotest: add autotest framework for utility unit tests #348

Merged
merged 21 commits into from
Mar 16, 2017

Conversation

hanw
Copy link
Contributor

@hanw hanw commented Mar 6, 2017

We don't have a framework for testing utility tools, such as compiler driver. Here is an attempt to add support for autotest framework.

@ChrisDodd Can you take a look and let me know if you are okay with the approach?

Makefile.am Outdated
@@ -40,6 +40,7 @@ noinst_LIBRARIES = # Static libraries built
noinst_PROGRAMS = # Binaries built that are not installed
BUILT_SOURCES = # Generated source files
TESTS = # Tests to execute
EXTRA_DIST = # ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this.

AT_SETUP([p4c])

m4_define([P4C_CHECK], [
AT_CHECK([p4c -t tofino min.p4], [1],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no p4c binary in the public repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am working on a compiler driver called p4c which is analogous to clang or gcc. I will push the code for p4c before this PR goes in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should write a document describing what the compiler driver is supposed to do.
This code is really the infrastructure for building a family of compilers, one for each target. Since we don't know the needs of each target, I am not sure whether it's easy to create a driver common to all targets.

* ....
* }
*
* P4CTEST_REGISTER("my-test", my_test_main);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about REGISTER taking just one argument? The string should be generated from the test name; perhaps you can always append _main as well.

@@ -0,0 +1,89 @@
# Copyright 2013-present Barefoot Networks, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know how this autotest stuff works, and I am trying to avoid learning if possible.
It would be nice if the extensions/ folder was used for testing as well - so you can actually "merge" the tests here with whatever tests/ folders may exist in any extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added an example in backend/p4test/tests to show how to extend unit test with custom backend.

}

static void
add_command(struct p4c_cmdl_command* cmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the style guide would require opening brace on the previous line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

#include "lib/error.h"
#include "p4ctest.h"

static std::vector<p4c_cmdl_command*> commands;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not package all of these in a class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

exit(1);
}
if (ferror(stderr)) {
std::cerr << "write to stderr failed" << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected to succeed? Probably it does not matter.

tests/p4ctest.h Outdated
struct p4c_cmdl_command {
const char *name;
const char *usage;
int min_args;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how these are used.

@hanw hanw force-pushed the unittest branch 5 times, most recently from c46815c to 12f3d4b Compare March 7, 2017 21:58
@hanw hanw changed the title WIP autotest: add autotest framework for utility unit tests autotest: add autotest framework for utility unit tests Mar 7, 2017
@hanw
Copy link
Contributor Author

hanw commented Mar 7, 2017

@ChrisDodd @mbudiu-vmw This patch is ready for review.

@hanw hanw force-pushed the unittest branch 3 times, most recently from ceb4b87 to e31cb45 Compare March 7, 2017 23:57
tests/p4ctest.h Outdated
/* Overview
* ========
*
* P4C tests directory contains many small test programs. One down side
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this comment should not describe the history of the program creation, it should just say what the program does. This information should be in the pull request, but not in a comment - it will become obsolete after a while.

tests/ir.at Outdated
@@ -0,0 +1,22 @@
AT_BANNER([intermediate representation unit tests])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that users writing tests won't have to learn this language...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m4 syntax takes some getting used to, particularly the automake dialect of it.

This still defines the same macro mulitple times with different names (just once in each file) -- might it make more sense to call it P4TEST_TEST_RUN or (something less verbose) and put it in the top level file (testsuite.at?) That way each sub .at file is just a list of tests.

configure.ac Outdated
AM_INIT_AUTOMAKE([foreign subdir-objects 1.14])
AM_SILENT_RULES([yes])
AM_SILENT_RULES([no])
AM_MISSING_PROG([AUTOM4TE], [autom4te])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I much prefer silent rules as it makes it easier to see compiler warnings (not buried in masses of libtool invokation cruft). Using V=1 is usually adequate for debugging make problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

tests/p4ctest.h Outdated
* all test programs into a single program, p4c-test. Regardless of
* the number of test programs, linking will be done only once to produce
* p4ctest.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much time does this actually save? Each little program only take about .3 seconds to link on my machine, so it seems unlike to save more that a second or two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed comments.

tests/ir.at Outdated
AT_CHECK([p4ctest $2], [0], [stdout], [])
AT_CLEANUP]
)
TEST_IR_OPEQ([ir - transform ], [test-transform])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a lot of boilerplate -- why define a new TEST_XXX macro for each test that you then invoke once, particularly since they're all the same. Why not just a single macro invoked mulitple times (once for each test).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@hanw hanw force-pushed the unittest branch 11 times, most recently from fcc3135 to d360b25 Compare March 11, 2017 05:02
Han Wang added 2 commits March 10, 2017 21:24
Removed /unittests, divided existing unit tests into: ir, frontend and
util groups.
@hanw hanw force-pushed the unittest branch 2 times, most recently from ff7b3e0 to 801be33 Compare March 11, 2017 07:36
@hanw
Copy link
Contributor Author

hanw commented Mar 13, 2017

@ChrisDodd How about the following usage?

make check D=1 // for enabling -gdb debugging
make check J=8 // for enabling -j8 flag
make check N=8, 10-12 // for running test case 8, 10, 11 and 12.
make check V=1 // for enabling verbose mode.
make check K=bmv2 // for run all test cases for bmv2.

I can try to generate a simplified version of testsuite.log file that only contains information about the failed cases, and save the info in test-suite.log

I can print the command to rerun a test case as part of the log output, so you can click on the string and copy/paste to terminal.

Let me know if there are more options you would like to have.

@ChrisDodd
Copy link
Contributor

ChrisDodd commented Mar 13, 2017

For -j, you probably need a long name so it doesn't collide with other things; most people will just want to set it in their .bashrc file and always have it 'just work'. That's the way MAKEFLAGS=-j8 works, for the most part

V=1 is going to collide with the existing use of V for verbose builds

How do you run a particular test by name? How do you know what "test 47" is -- how do you find the source and intermediate files of the test so you can figure out what went wrong?

@cc10512
Copy link
Contributor

cc10512 commented Mar 13, 2017

@hanw: isn't it just a matter of propagating $MAKEFLAGS to the make commands to the testsuite command? tests/testsuite --help has this gem:

  -j, --jobs[=N]
                 Allow N jobs at once; infinite jobs with no arg (default 1)

@hanw
Copy link
Contributor Author

hanw commented Mar 14, 2017

I am able to make these to work.
make check-local MAKEFLAGS=-j8
make check-local AM_MAKEFLAGS=-j8
AM_MAKEFLAGS=-j8 make check-local

but not these two.
MAKEFLAGS=-j8 make check-local
make check-local -j8

In the second case, MAKEFLAGS has a value of "-j --jobserver-fds=4,5", instead of what I set on command line (-j8). not sure where it comes from.

"$$@" || (test X'$(RECHECK)' = Xyes && "$$@" --recheck)

check-bmv2:
make check-local TESTSUITEFLAGS='-k bmv2 $(filter-out $@,$(MAKECMDGOALS))'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried passing $(MAKEFLAGS) to this command? That is:

check-bmv2:
           make check-local TESTSUITEFLAGS='-k bmv2 $(filter-out $@,$(MAKECMDGOALS))' $(MAKEFLAGS)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This turns out to be a known issue in make 4.1, where the value N in -jN is not passed down to MAKEFLAGS. I installed make 4.2.1 from source, and problem solved.

@hanw
Copy link
Contributor Author

hanw commented Mar 14, 2017

@ChrisDodd, to run a particular test, we can use the name of the p4 program as keyword.
make check-local TESTSUITEFLAGS="-k chain.p4"

In addition, -v will print all commands and stdout/stderr from running the test case:
make check-local TESTSUITEFLAGS="-k chain.p4 -v"

@hanw
Copy link
Contributor Author

hanw commented Mar 14, 2017

We can also narrow down the test case by specifying the name of the test group, e.g.:

make check-local TESTSUITEFLAGS="-k tofino,switch.p4 -v"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants