Replace ioutil.WriteFile with os.WriteFile since the former has been
deprecated since go1.16 and simply calls the latter.
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
Go 1.19 includes some changes to gofmt which intend to make lists and
heading clearer when rendered (https://go.dev/doc/go1.19). This commit
is the result of running the new gofmt and manually fixing some of it.
This was necessary because the new gofmt assumed lines beginning w/ tabs
to start lists or examples. While this is often true in our codebase,
we occasionally also use tabs to indent the lines after a TODO or FIXME
prefix or in yaml (e.g., excerpts of a snap.yaml). This meant that a lot of the
reformatted comments were broken and had to be fixed manually.
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
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>
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>
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>
when available shellchecking MockCommand scripts was slowing down some
suites quite a lot (20s vs 1s+ for ifacetest for example), this
remembers if we checked the same script essentially already
When shellcheck is running from a snap it will get a private /tmp. Scripts are
generated in a workdir that is places under /tmp, but inside the host mount
namespace, thus the file will not be accessible by shellcheck.
Work around that by feeding the script to be checked through stdin.
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Shell is deceivingly easy to write. It's best that we run the test snippets
through shellcheck.
This adds ~2s to whole suite execution time on my box.
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
* snap: add new `snap run --perf` call
This new argument will allow to trace what is exec()ed when a snap
runs. This allows to e.g. run `snap run vlc --play-and-exit nothing`
to see how much overhead the various wrappers generate.
Sample output:
```
$ snap run --perf test-snapd-tools.echo hello
Slowest 2 exec calls during snap run:
0.011: /usr/lib/snapd/snap-confine
0.016: /usr/lib/snapd/snap-exec
```
* snap: fix go vet warning
* snap: add TestStraceExtractExecRuntime test
* snap: handle execveat() and fix review issues
* snap: show total runtime
* snap: rename exec tracing to `snap run --trace-exec`
* snap: fix silly comment out line
* snap: refactor straceExtractExecRuntime and sort descending
* add test for TestDisplaySortedExecRuntimes
* snap: keep only nSlowestSamples in SnapTrace
* snap: show slowest calls in order of them getting called
* snap: use named pipe for strace log to avoid spaming the disk
* snap: address some review feedback (thanks to Samuele)
* snap: add test TestRunCmdWithTraceExecUnhappy
* snap,osutil: add new osutil/strace package
This extract the strace usage in cmd/snap/cmd_run.go into a new
osutil/strace package for better separation and testing.
* snap,osutil: move all timing related helpers to the new strace pkg
* strace: simplify code
* strace: improve comments for TraceExecveTimings
* osutil/strace: add new TraceExecCommand() helper
* strace: improve comments in runCmdWithTraceExec()
* strace: address review feedback
* strace: fix imports ordering
This comes as a result of investigating a failure of
SnapSuite.TestSnapRunAppWithStraceIntegration on Arch. In this particular case,
/bin/sh is symlinked to bash, thus `echo` is actually a [/usr]/bin/echo coming
from coreutils.
strace command in the test is called like this (sans the newlines and \):
sudo -E <strace-path> -u maciek -f \
-e !select,pselect6,_newselect,clock_gettime \
<snap-confine-path> \
snap.snapname.app \
/usr/lib/snapd/snap-exec \
snapname.app --arg1 arg2
Then the piece of shell script is expected to save each argument in a separate
line, with the script:
for arg in "$@"; do
echo "$arg" >> %[1]q
done
the arguments -E and -e will be called like this:
echo "-E" <-- actually /usr/bin/echo "-E"
echo "-e"
According to echo(1) both -e and -E are valid arguments and will be 'swallowed'
by the command, thus never appearing in the output.
Fix this by using a printf which does not have this problem.
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
For some operations we will need to support to prompt the user
via userd. To avoid heavy dependencies the UI will use zenity
(and kdialog and mate-dialogs in the future).