Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
interfaces/udev: add udev support code #636
Merged
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83efc8c
interfaces/udev: add udev support code
zyga 8d28bd4
Merge branch 'exec-test' into udev
zyga 8f797a0
testutil: add MockCmd.SetDynamicBehavior()
zyga a3a0f0c
interfaces/udev: improve error handling
zyga 334e929
Merge remote-tracking branch 'upstream/master' into udev
zyga 4cc1adf
interfaces/udev: move command mocking to setup/teardown
zyga 236c499
Merge branch 'master' into udev
zyga d88028c
testutil,interfaces/udev: simplify udev test code
zyga d31bf6e
interfaces/udev: use 'cannot' instead of 'Cannot'
zyga
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,41 @@ | ||
| +// -*- Mode: Go; indent-tabs-mode: t -*- | ||
| + | ||
| +/* | ||
| + * Copyright (C) 2016 Canonical Ltd | ||
| + * | ||
| + * This program is free software: you can redistribute it and/or modify | ||
| + * it under the terms of the GNU General Public License version 3 as | ||
| + * published by the Free Software Foundation. | ||
| + * | ||
| + * This program is distributed in the hope that it will be useful, | ||
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| + * GNU General Public License for more details. | ||
| + * | ||
| + * You should have received a copy of the GNU General Public License | ||
| + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| + * | ||
| + */ | ||
| + | ||
| +package udev | ||
| + | ||
| +import ( | ||
| + "fmt" | ||
| + "os/exec" | ||
| +) | ||
| + | ||
| +// ReloadRules runs two commands that reload udev rule database. | ||
| +// | ||
| +// The commands are: udevadm control --reload-rules | ||
| +// udevadm trigger | ||
| +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)) | ||
| + } | ||
| + 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)) | ||
| + } | ||
| + return nil | ||
| +} |
| @@ -0,0 +1,85 @@ | ||
| +// -*- Mode: Go; indent-tabs-mode: t -*- | ||
| + | ||
| +/* | ||
| + * Copyright (C) 2016 Canonical Ltd | ||
| + * | ||
| + * This program is free software: you can redistribute it and/or modify | ||
| + * it under the terms of the GNU General Public License version 3 as | ||
| + * published by the Free Software Foundation. | ||
| + * | ||
| + * This program is distributed in the hope that it will be useful, | ||
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| + * GNU General Public License for more details. | ||
| + * | ||
| + * You should have received a copy of the GNU General Public License | ||
| + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| + * | ||
| + */ | ||
| + | ||
| +package udev_test | ||
| + | ||
| +import ( | ||
| + "testing" | ||
| + | ||
| + . "gopkg.in/check.v1" | ||
| + | ||
| + "github.com/ubuntu-core/snappy/interfaces/udev" | ||
| + "github.com/ubuntu-core/snappy/testutil" | ||
| +) | ||
| + | ||
| +func Test(t *testing.T) { | ||
| + TestingT(t) | ||
| +} | ||
| + | ||
| +type uDevSuite struct{} | ||
| + | ||
| +var _ = Suite(&uDevSuite{}) | ||
| + | ||
| +// Tests for ReloadRules() | ||
| + | ||
| +func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdm(c *C) { | ||
| + cmd := testutil.MockCommand(c, "udevadm", "") | ||
| + defer cmd.Restore() | ||
| + err := udev.ReloadRules() | ||
| + c.Assert(err, IsNil) | ||
| + c.Assert(cmd.Calls(), DeepEquals, []string{ | ||
| + "control --reload-rules", | ||
| + "trigger", | ||
| + }) | ||
| +} | ||
| + | ||
| +func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromReloadRules(c *C) { | ||
| + cmd := testutil.MockCommand(c, "udevadm", ` | ||
| +if [ "$1" = "control" ]; then | ||
| + echo "failure 1" | ||
| + exit 1 | ||
| +fi | ||
| + `) | ||
| + defer cmd.Restore() | ||
| + err := udev.ReloadRules() | ||
| + c.Assert(err.Error(), Equals, ""+ | ||
| + "cannot reload udev rules: exit status 1\n"+ | ||
| + "udev output:\n"+ | ||
| + "failure 1\n") | ||
| + c.Assert(cmd.Calls(), DeepEquals, []string{"control --reload-rules"}) | ||
| +} | ||
| + | ||
| +func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromTrigger(c *C) { | ||
| + cmd := testutil.MockCommand(c, "udevadm", ` | ||
| +if [ "$1" = "trigger" ]; then | ||
| + echo "failure 2" | ||
| + exit 2 | ||
| +fi | ||
| + `) | ||
| + defer cmd.Restore() | ||
| + err := udev.ReloadRules() | ||
| + c.Assert(err.Error(), Equals, ""+ | ||
| + "cannot run udev triggers: exit status 2\n"+ | ||
| + "udev output:\n"+ | ||
| + "failure 2\n") | ||
| + c.Assert(cmd.Calls(), DeepEquals, []string{ | ||
| + "control --reload-rules", | ||
| + "trigger", | ||
| + }) | ||
| +} |