31 Commits

Author SHA1 Message Date
Miguel Pires
79c5ac14b2 many: remove usages of deprecated io/ioutil package (#13768)
* many: remove usages of deprecated io/ioutil package

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* .golangci.yml: remove errcheck ignore rule for io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* run-checks: prevent new usages of io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

---------

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2024-04-03 23:23:24 +02:00
Miguel Pires
29c9752d66 many: s/ioutil.WriteFile/os.WriteFile (#13217)
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>
2023-09-26 11:38:46 +01:00
Miguel Pires
d097436c1c many: fix formatting w/ gofmt 1.19
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>
2023-01-16 14:23:11 +01: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
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
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
Maciej Borzecki
d51c6d701a testutil: create the prefix of absolute paths when mocking commands
When mocking a command at an absolute path, create the leading prefix.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2019-11-22 11:25:22 +01:00
Samuele Pedroni
73ffb9e7a1 drive-by: skip shellchecking the same MockCommand scripts again
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
2019-05-08 14:36:38 +02:00
Michael Vogt
3b0dce45e6 testutil: fix MockCmd for shellcheck 0.5
With shellcheck 0.5 there is an error that
```
printf "\0"
```
should be use single quotes (SC1117). This PR fixes this.
2019-04-03 21:24:04 +02:00
Maciej Borzecki
bf24be3119 testutil: make mocked command work with shellcheck from snaps
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>
2019-04-03 12:53:02 +02:00
Maciej Borzecki
a1f3919502 testutil: run mocked commands though shellcheck
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>
2019-03-28 08:35:32 +01:00
Michael Vogt
c7f6a60a75 snap: add new snap run --trace-exec call (#6185)
* 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
2018-12-03 15:26:08 +01:00
Michael Vogt
5e0fd5e805 userd: add kdialog and make userd use userd/ui package
This allows to use zenity and kdialog from the `snap userd`
instance.
2018-01-30 09:27:54 +01:00
Michael Vogt
60c7b47ff3 Merge remote-tracking branch 'upstream/master' into userd-ui 2018-01-29 10:28:39 +01:00
Maciej Borzecki
15f918ab36 testutil: do not use echo for printing potentially conflicting arguments
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>
2018-01-26 13:23:10 +01:00
Michael Vogt
498c2211d7 userd: add support for a simple UI that can be used from userd
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).
2017-12-01 17:09:28 +01:00
Michael Vogt
c0f98cada9 testutils: address review feedback from PR#2997 (#3008)
This improves the documentation of testutils.MockCommand() and also
log an error to the logger if /proc/self/exe cannot be read.
Thanks to zyga!
2017-03-10 09:19:53 -03:00
Michael Vogt
ddf20fcb2d hookstate: run the right "snap" command in the hookmanager (#2992) 2017-03-07 19:50:22 -03:00
Zygmunt Krynicki
b07db3ef43 many: simplify mocking of snap-discard-ns
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2016-09-22 13:27:56 +02:00
John R. Lenton
18e7b01b29 address review feedback 2016-09-06 17:03:07 +01:00
Michael Vogt
43072fbf4f use echo instead of printf to avoid % escaping issues (less portable but portable enough) 2016-06-03 19:28:19 +02:00
Michael Vogt
1eb6dcfa03 improve MockCommand.Calls() 2016-06-03 16:18:52 +02:00
Zygmunt Krynicki
579f817d58 testutil: don't panic in MockCmd.Calls() if no calls were made
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
2016-03-24 00:33:16 +01:00