124 Commits

Author SHA1 Message Date
Miguel Pires
aa623a49ea many: remove unused parameters and returns 2021-09-02 17:44:34 +01:00
Miguel Pires
68bc383c72 testutil: add DeepUnsortedMatches Checker
Adds a Checker to test that two containers have the same elements (duplicates must occur in the same number) but possibly in different orders.
2021-08-23 09:30:27 +01:00
Maciej Borzecki
f116572efe testutil: fix typo in json checker unit tests
Thanks to @anonymouse64 for spotting this

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-07-07 10:48:12 +02:00
Maciej Borzecki
7fa77b81bf testutil: introduce a checker which compares the type after having passed them through a JSON marshaller
This is useful when comparing more complex structure from external packages,
when the structures may have unexported fields that would otherwise be picked up
by check.DeepEquals.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-06-24 16:33:20 +02:00
Samuele Pedroni
bcbf8edafd osutil,strutil,testutil: fix imports order (according to gci) 2021-06-15 18:04:44 +02:00
Maciej Borzecki
409c929498 testutil: tweak naming and comments
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-04-22 15:41:10 +02:00
Maciej Borzecki
af509a3d10 testutil: support reference files in FileEquals checker
The FileEquals checker can verify that the file content is equal to strings,
byte slices or something that implements fmt.Stringer. Extend the functionality
to allow checking that file contents are the same as of a reference file.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-04-22 13:05:14 +02:00
James Henstridge
ba547a653f dbusutil: extract helper for creating private session bus connection 2020-12-21 10:43:20 +08:00
Samuele Pedroni
2f89acd032 testutil: use the original environment when calling shellcheck
our own test fixtures might have messed up with it, notably
some tests change DBUS_SESSION_BUS_ADDRESS for example
2020-12-11 14:41:14 +01:00
James Henstridge
23e42cf898 testutil: add a documentation comment to sessionBusConfigTemplate 2020-12-10 15:51:35 +08:00
James Henstridge
0ba5d1a04b testutil: make DBusTest use a custom bus configuration file
The standard session bus configuration searches for service activation
files found in /usr/share/dbus-1/services, and may load other custom
local configuration.  This could cause the test suite to spawn arbitrary
services installed on the system.

By using a custom daemon configuration file, without this servicedir
configuration, we ensure that tests using the fixture only see names
provided by the test suite.
2020-12-10 12:25:17 +08:00
Ian Johnson
18fdbfba50 testutil/containschecker_test.go: use correct Compiler value
When not using ggcgo, the value of runtime.Compiler is "gc" not "go".

Thanks to @niemeyer for spotting this.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2020-10-29 10:59:46 -05:00
Zygmunt Krynicki
1cb14008f0 testutil: add checkers for symbolic link target
Add three checkers relevant for examining symbolic links:

- SymlinkTargetEquals checks for a fixed target
- SymlinkTargetContains checks for a substring
- SymlinkTargetMatches checks for a regular expression

They are modelled after FileContentsChecker but implement fewer
variants as symbolic link targets are usually short strings.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
2020-08-31 11:59:04 +02:00
Samuele Pedroni
512c4b909b many: check that users of BaseTest don't forget to consume cleanups
make their order of execution also LIFO which is more expected

fix places that were wrong, also cleanup some of devicestate
suites to use AddCleanup more and not need specific TearDownTest
2020-08-28 16:52:43 +02:00
Michael Vogt
41009e2cc5 testutil: tweak HostScaledTimeout and tests
This commit avoids extra imports in timeouts.go and tweaks
the TimeoutTestSuite to follow the testutil_test pattern and
adds a MockRuntimeARCH() helper.
2020-06-10 09:44:03 +02:00
Dimitri John Ledkov
745ae77f2a testutil: use snapd arch and add test. 2020-06-09 18:12:44 +01:00
Michael Vogt
1bd395934b testutils: add HostScaledTimeout 2020-06-09 17:58:22 +01:00
Zygmunt Krynicki
9483501739 dbusutil: move all D-Bus helpers and D-Bus test helpers
This commit is squashed history taken out of the refresh-app-awareness-v2
branch. It aggregated most of the new D-Bus helper logic in a pair of
packages. The dbusutil package contains non-silly logic to connect
to the session and system bus, as well as public mocking helpers.
The dbustest sub-package contains two helpers that provide dbus.Conn
for testing interaction and for testing bus access.

Note that I chose to leave behind one file in testutils, as it contains
existing test code used by user agent code. I can do that in another pass
to isolate it from this move.

This code is used in feature/refresh-app-awareness-v2, for both the
existing code in "snap run" and the new code in sandbox/cgroup package.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2020-05-29 15:17:43 +02:00
Ian Johnson
8937ad26b2 testutil/exec.go: set PATH after running shellcheck
If we set this before calling shellcheck, if shellcheck is a snap, then we could
have issues mocking specifically systemctl, where shellcheck as a snap tries to
use `systemctl is-system-running` if there is a system key mismatch, and this
will call our mocked systemctl which leads to undesirable and very confusing
unit test failures.

Longer term we may want to consider not setting PATH in this manner as there
have been other similar bugs where some system tool is a snap and it affects
unit tests in a confusing way.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2020-05-25 11:59:49 -05:00
Ian Johnson
8d4993b4fc testutil/exec.go: fix doc-comment typo
Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2020-05-25 11:59:46 -05:00
Zygmunt Krynicki
154be26951 testutil: add NewDBusTestConn
This patch adds support code for testing code making DBus calls.
It is similar in spirit to our HTTP testing code. A DBus connection
is provided, marshaling messages sent between application code driven
by the test and the message bus.

This allows testing any DBus interaction in isolation, with any scenario
that is desired.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2020-04-21 12:19:40 +02:00
Maciej Borzecki
fc623b9ea1 testutil: fix valid nakedret issues
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-01-23 10:03:20 +01:00
Maciej Borzecki
b6d18da7a4 testutil: workaround 14.04 weirdness in synchronized mock command
On 14.04, the script file is apparently kept open for writingby flock(1) and
subsequent attempt to execve() it fails with ETXTBSY. Workaround that and place
the lock on the parent directory instead.

$ strace -e flock,execve -f /tmp/check-737788169571723474/0/usr/lib/snapd/snap-seccomp
...
flock(3, LOCK_EX)                       = 0
Process 18827 attached
[pid 18827] execve("/tmp/check-737788169571723474/0/usr/lib/snapd/snap-seccomp", ["/tmp/check-737788169571723474/0/"...], [/* 75 vars */]) = -1 ETXTBSY (Text file busy)
flock: /tmp/check-737788169571723474/0/usr/lib/snapd/snap-seccomp: Text file busy
[pid 18827] +++ exited with 69 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=18827, si_status=69, si_utime=0, si_stime=0} ---
+++ exited with 69 +++

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-12-03 14:12:57 +01:00
Maciej Borzecki
84e151ec5e testutil: add a locked variant of mocked command
The script generated by MockCommand() writes the call list into the log. Thus,
it cannot be called in parallel without risking the call log being corrupted by
interleaving writes.

Add a locked variant of mocked command that wraps the script execution with a
flock call that takes an exclusive lock.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-11-28 14:17:19 +01:00
Zygmunt Krynicki
79b03162e5 osutil/mount: add {Unm,M}outFlagsToOpts helpers
The pair of helpers turn numeric flags for mount(2) and umount(2) to a
list of source constants that are useful for debugging. We had,
partially, similar helpers in lowlevel test-only code. Those are now
simple wrappers around the more general logic.

Due to cyclic imports osutil -> testutil -> osutil I've added the
new helpers to osutil/mount.

The new helpers will be shortly used by snap-update-ns logging
routines.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
2019-11-25 15:05:31 +01:00