interfaces/udev: add udev support code #636

Merged
merged 9 commits into from Mar 14, 2016

Conversation

Projects
None yet
5 participants
Contributor

zyga commented Mar 10, 2016

This is stacked on #634

This branch adds a function for reloading the udev database.

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

interfaces/udev: add udev support code
This patch adds a function for reloading the udev database.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/udev/udev.go
+ return err
+ }
+ err = exec.Command("udevadm", "trigger").Run()
+ return err
@niemeyer

niemeyer Mar 10, 2016

Contributor

Rather than Run, I suggest using CombinedOutput and doing a nice fmt.Errorf including it (probably needs to be multiline). Otherwise the err by itself will likely be very boring and hard to decipher, and the valueable output will be on the terminal, whatever that happens to be.

Same applies to the apparmor one, if you're doing that. Overlooked that there.

@zyga

zyga Mar 10, 2016

Contributor

Good catch, I didn't consider this. I'll play with those tools and see what output they have on failures and adjust this accordingly.

@jdstrand

jdstrand Mar 10, 2016

Contributor

I'm not a udev expert. In general this looks ok but I did notice in the udevadm man page the following:
-R, --reload
Signal systemd-udevd to reload the rules files and other databases
like the kernel module index. Reloading rules and databases does not
apply any changes to already existing devices; the new configuration
will only be applied to new events.

I mention this because of the 'existing devices' part. I'm not sure of the implications of that-- I suggest you ask pitti to comment.

zyga added some commits Mar 10, 2016

testutil: add MockCmd.SetDynamicBehavior()
This patch adds a way to customize MockCmd() with simple dynamic
behavior, depending on the number of times the executable is being
invoked. Each invocation can return a custom error message and status
code.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/udev: improve error handling
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/udev/udev_test.go
+// Tests for ReloadRules()
+
+func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdm(c *C) {
+ cmd := testutil.MockCommand(c, "udevadm", 0)
@mvo5

mvo5 Mar 11, 2016

Collaborator

Given that you always mock udevadm, I wonder if it would make sense to put the mock/restore into SetUpTest/TearDownTest ?

interfaces/udev: move command mocking to setup/teardown
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/udev/udev_test.go
+func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromReloadRules(c *C) {
+ cmd := testutil.MockCommand(c, "udevadm", 0)
+ defer cmd.Restore()
+ cmd.SetDynamicBehavior(1, func(n int) (string, int) {
@mvo5

mvo5 Mar 11, 2016

Collaborator

I wonder if cmd.SetCustom(if "$1" == "control"; then exit 1; fi)` would be easier to read? And have a SetCustom func instead of SetDynamicBehavior? Because of YAGNI and for the tests at hand it seems like "$1" is the important bit. But again, might be personal preference.

interfaces/udev/udev_test.go
+func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromTrigger(c *C) {
+ cmd := testutil.MockCommand(c, "udevadm", 0)
+ defer cmd.Restore()
+ cmd.SetDynamicBehavior(2, func(n int) (string, int) {
@mvo5

mvo5 Mar 11, 2016

Collaborator

See above, something like cmd.SetCustom(if "$1" == "trigger"; then echo "failure 2"; exit 2; fi) ? Or maybe something else, to me SetDynamicBehavior feels both relatively complex to use and also relatively limited (must know amount of calls instead of being able to check for other things like arguments passed). But again, maybe just a personal preference thing.

Member

chipaca commented Mar 14, 2016

@mvo5 @niemeyer you guys need to re-review this one... :-)

interfaces/udev/udev.go
+func ReloadRules() error {
+ output, err := exec.Command("udevadm", "control", "--reload-rules").CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("Cannot reload udev rules: %s\nudev output:\n%s", err, string(output))
@niemeyer

niemeyer Mar 14, 2016

Contributor

s/Cannot/cannot/

interfaces/udev/udev.go
+ }
+ output, err = exec.Command("udevadm", "trigger").CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("Cannot run udev triggers: %s\nudev output:\n%s", err, string(output))
@niemeyer

niemeyer Mar 14, 2016

Contributor

s/Cannot/cannot/

interfaces/udev/udev_test.go
+}
+
+func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromReloadRules(c *C) {
+ s.cmd.SetDynamicBehavior(1, func(n int) (string, int) {
@niemeyer

niemeyer Mar 14, 2016

Contributor

Sorry for bikesheding even further on this, but I don't understand what this test is doing. The helper, which is supposed to make things simple, is actually obscuring what's going on past the point of being understandable. This was supposed to be a trivial command, right? exec, and this is what happens.. what is "dynamic behavior", 1, with n == 0 or 1 for a mocked command!?

Contributor

niemeyer commented Mar 14, 2016

Okay, we've agreed to drop the SetDynamicBehavior method, and replace the status parameter in MockCommand by the actual script. If empty, it defaults to the usual behavior of empty scripts: return 0. If we want a non-zero status, it may be simply exit 1. If we want anything more complex, just cook it in.

zyga added some commits Mar 14, 2016

testutil,interfaces/udev: simplify udev test code
This patch changes MockCmd and the only test code that was using it,
udev so that testing non-constant mocked behavior is easier. Instead of
an elaborate auto-generated shell script the caller can simply splice a
bit of shell at the end of the mocked command. This has the same power
but greater simplicity.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/udev: use 'cannot' instead of 'Cannot'
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

niemeyer added a commit that referenced this pull request Mar 14, 2016

Merge pull request #636 from zyga/udev
interfaces/udev: add udev support code

@niemeyer niemeyer merged commit 388d65d into snapcore:master Mar 14, 2016

2 checks passed

Integration tests Success 68 tests run, 0 skipped, 0 failed.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@zyga zyga deleted the zyga:udev branch Mar 14, 2016

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