136 Commits

Author SHA1 Message Date
Jorge Sancho Larraz
265b7c44d1 sandbox/apparmor: aare exclusion rule generation (#13488)
* sandbox/apparmor: add GenerateAAREExclusionPatterns

This function is generic (and complex) enough to be able to handle all of the
overlapping and wildcard behavior we need in docker-support, and it could also
serve to replace numerous other places in the codebase where we need this sort
of complex behavior. It is a generalization of the existing
aareExclusionPatterns helper, though it's actually unclear if this exact
implementation will currently be able to serve the use case from that helper
directly or if more options/adjustments are needed to enable that use case as
well.

To keep the diff smaller, this patch does not actually change any of the
profiles/interfaces, just TODO's are left for where to use it.

Note that the generated rules are slightly more condensed in terms of number of
rules but significantly more verbose in terms of alternations, not sharing more
of repeated substrings between alternations inside the patterns. This was done
explicitly to keep the generating code simpler and easier to understand, but it
may prove to have performance effects, either detrimental or benevolent but
that should be measured before deciding to make the generation code even more
complex than it already is.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* interfaces/docker-support: generate AARE exclusion patterns with helper func

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: unexport helper functions

These were not meant to be exported, only the fully generic one is meant to be
exported.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: fix bug mis-sorting capitalized letters in AARE exclude patt

Thanks to Alberto for spotting this :-)

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* sandbox/apparmor: fix format issues introduced during rebase

* sandbox/apparmor: simplify generateAAREExclusionPatternsGenericImpl

* sandbox/apparmor: add checks for unsupported cases and improve documentation

* sandbox/apparmor: update tests to compare the apparmor binary instead of source

* interfaces/builtin/docker_support: check if userns is supported before adding it to the profile

* interfaces/builtin/docker_support: fix dependencies

* sandbox/apparmor: use placeholders

* i/b/docker_support_test: update TestGenerateAAREExclusionPatterns to use SnapAppSet

* testutil/apparmor: use go crypto/sha1 module instead of system sha1sum command

* {sandbox,testutil}/apparmor: minor format fixes

* move helper to find common prefix to strutil

* add copyright info

* use string builder

* i/b/docker_support_test.go: update accordingly to 277fbc266e (many: add components to interfaces.SnapAppSet (#13837))

* strutil/commonprefix.go: remove extra empty line

* sandbox/apparmor/apparmor.go: sort prefixes to ensure profile is always the same

* sandbox/apparmor/apparmor.go: remove extra empty line

* i/b/docker_support_test: skip TestGenerateAAREExclusionPatterns is apparmor_parser is not usable

---------

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
Co-authored-by: Ian Johnson <ian.johnson@canonical.com>
2024-07-04 12:23:08 +02:00
Miguel Pires
4be3095b1e testutil: increase settle timeout when running -race
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2024-04-29 20:01:04 +01:00
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
5f96b84a18 testutil: add checker for nil typed and valued vars
Add an IsInterfaceNil checker that ensures both a variable's type and
value are nil. This is useful to prevent returned errors with non-nil
types from being compared to a nil value with a nil type (since the
compiler can't tell that the other variable is not nil typed), resulting
in the check failing.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-03-28 10:26:53 +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
Miguel Pires
1af63c2447 testutil: compare unsorted containers nested in maps
Only top-level containers were matched in an unsorted way and maps
were compared with reflect.DeepEqual. Now map values are also "unsorted
matched" so values like map[string][]string can be compared regardless
of order even with nesting. Note that containers nested in slices or
arrays are still compared in the normal way.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2022-11-10 09:02:05 +00:00
Michael Vogt
4eff3617d9 testutil: remove unneeded fmt.Sprintf
As suggested by staticcheck.io
2022-08-16 20:47:29 +02:00
Miguel Pires
6acd7f9061 testutil: add ErrorIs test checker
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2022-05-19 15:22:20 +01:00
Arseniy Aharonov
b1ae485f5e testutil: refactoring code following the comments from the code review
Signed-off-by: Arseniy Aharonov <arseniy.aharonov@canonical.com>
2022-02-24 10:16:21 +00:00
Arseniy Aharonov
6d225cb825 testutil: add generic mocking helpers
Two generic mocking helpers have been added:
- testutil.MockAny() - allows mocking of any mutable object with a compliant substitute,
  it also return a restore function that shall be called to restore the original value

- testutil.(*BaseTest).MockAny() - allows mocking of any mutable object with a compliant substitute,
  unlike the API above, this one takes care of restoring the original value on its own

Signed-off-by: Arseniy Aharonov <arseniy.aharonov@canonical.com>
2022-02-18 11:19:07 +00:00
Sreeja Manghat
9bf5d2485a many: unit test fix when SNAPD_DEBUG=1 is set
Minor changes ensuring unset SNAPD_DEBG to avoid test failures when run with SNAPD_DEBUG=1.
Added support in test.yaml for unit tests with SNAPD_DEBUG=1 to be re-run to avoid regressions.
2022-01-25 20:26:56 +05:30
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