interfaces/builtin: reduce duplication and remove cruft in Sanitize{Plug,Slot} #3619

Merged
merged 16 commits into from Jul 28, 2017

Conversation

Projects
None yet
5 participants
Contributor

zyga commented Jul 25, 2017

This branch unifies how Sanitize{Plug,Slot} are implemented across all of the interfaces.
This removes a lot of copy-pasted code and adds some missing calls in other copy-pasted code.
Lastly, because all of the error messages are in a small set of four functions, there is much more
uniformity in how error messages look like.

Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com

zyga added some commits Jul 11, 2017

interfaces/builtin: helpers for common sanitization tasks
The helpers do the interface<->{plug,slot} interface match chek
and that slot is reserved for OS snap (and is only defined on OS
snaps).

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: use sanitization helpers
This just moves a very common check into one spot so that it can be
consistently worded and there is some less duplication.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Looks good and nice to see this duplication all consolidated into single helpers! Thanks for doing this work. A small question inline, otherwise good.

interfaces/builtin/utils.go
@@ -91,3 +91,39 @@ func udevUsbDeviceSnippet(subsystem string, usbVendor int64, usbProduct int64, k
func udevSnapSecurityName(snapName string, appName string) string {
return fmt.Sprintf(`snap_%s_%s`, snapName, appName)
}
+
+func ensureSlotIfaceMatch(iface interfaces.Interface, slot *interfaces.Slot) {
@mvo5

mvo5 Jul 25, 2017

Collaborator

Do we need (explicit) tests for the new helpers?

@zyga

zyga Jul 25, 2017

Contributor

They are tested indirectly but I'll add a small set of tests. Thanks for spotting that.

@zyga

zyga Jul 25, 2017

Contributor

I just pushed some nice tests

interfaces/builtin: add tests for new functions
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

codecov-io commented Jul 25, 2017

Codecov Report

Merging #3619 into master will increase coverage by 0.04%.
The diff coverage is 86.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3619      +/-   ##
==========================================
+ Coverage   75.14%   75.19%   +0.04%     
==========================================
  Files         386      386              
  Lines       33693    33418     -275     
==========================================
- Hits        25318    25128     -190     
+ Misses       6551     6479      -72     
+ Partials     1824     1811      -13
Impacted Files Coverage Δ
interfaces/builtin/lxd_support.go 61.9% <ø> (-6.1%) ⬇️
interfaces/builtin/bluez.go 75.75% <ø> (+8.19%) ⬆️
interfaces/builtin/maliit.go 73.33% <ø> (+15.43%) ⬆️
interfaces/builtin/location_control.go 48.48% <ø> (+5.24%) ⬆️
interfaces/builtin/uhid.go 65.38% <ø> (+2.52%) ⬆️
interfaces/builtin/unity8_pim_common.go 73.17% <ø> (-2.34%) ⬇️
interfaces/builtin/network_status.go 71.42% <ø> (-0.8%) ⬇️
interfaces/builtin/docker_support.go 69.44% <ø> (+5.15%) ⬆️
interfaces/builtin/pulseaudio.go 37.03% <ø> (-8.13%) ⬇️
interfaces/builtin/ofono.go 61.53% <ø> (+5.72%) ⬆️
... and 57 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c81d104...f6fb74a. Read the comment docs.

Contributor

zyga commented Jul 27, 2017

After discussing this with @pedronis I'm going to move the responsibility of checking interface name from Sanitize{Plug,Slot} to the caller.

zyga added some commits Jul 27, 2017

interfaces/builtin: don't sanitize interface type
This patch discards the code present in each Sanitize{Plug,Slot} that
was cross-checking iface.Name() with {plug,slot}.Interface.

This code was repetitive and served little use. There are four call
sites to Sanitize{Plug,Slot} in the whole codebase and they already
check the interface type up front.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: add {Plug,Slot}.Sanitize
This patch adds Sanitize function to plugs and slots. This has several
advantages. We can now ensure plug.Interface or slot.Interface matches
Interface.Name. We can also keep this code in one spot, instead of
spreading it around the interface code. Lastly we can now make the
sanitization function on specific interfaces optional, thus removing a
lot of boilerplate code.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: use new Sanitize API in repo
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: make Interface.Sanitize{Plug,Slot} optional
With the new helpers we don't need those methods on each interface.
They only need to be defined if required.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: use {Plug,Slot}.Sanitize in all tests
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: remove the same cruft as before
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: use new Sanitize APIs in testiface
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: use new Sanitize API in policy tests
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: drop unused Sanitize{Plug,Slot} methods
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/core.go
+ plug.Ref(), plug.Interface, iface.Name())
+ }
+ type sanitizer interface {
+ SanitizePlug(plug *Plug) error
@chipaca

chipaca Jul 28, 2017

Member

I'd make these interfaces public, to better document the thing (much as for example http.Flusher is public to document that). It adds a path to discovery of this feature.

@zyga

zyga Jul 28, 2017

Contributor

Done.

pedronis approved these changes Jul 28, 2017 edited

lgtm, as I discussed we probably want to say "core snap" for the time being instead of "operating system snap".

Also I agree with Chipaca's comment, though it seems for the backends we really went the route of secret interfaces which I don't fully understand from a documenting POV.

zyga added some commits Jul 28, 2017

interfaces: make PlugSanitizer and SlotSanitizer interfaces public
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: talk about core snap, not operating system snap
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces: reword {Plug,Slot}Sanitizer documentation
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

@zyga zyga merged commit 1bb7d6d into snapcore:master Jul 28, 2017

7 checks passed

artful-amd64 autopkgtest finished (success)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-i386 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details

@zyga zyga deleted the zyga:tweak/interface-common-code branch Jul 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment