diff --git a/interfaces/udev/udev.go b/interfaces/udev/udev.go new file mode 100644 index 0000000000..06414af0b0 --- /dev/null +++ b/interfaces/udev/udev.go @@ -0,0 +1,37 @@ +// -*- 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 . + * + */ + +package udev + +import ( + "os/exec" +) + +// ReloadRules runs two commands that reload udev rule database. +// +// The commands are: udevadm control --reload-rules +// udevadm trigger +func ReloadRules() error { + err := exec.Command("udevadm", "control", "--reload-rules").Run() + if err != nil { + return err + } + err = exec.Command("udevadm", "trigger").Run() + return err +} diff --git a/interfaces/udev/udev_test.go b/interfaces/udev/udev_test.go new file mode 100644 index 0000000000..5ed1e6400b --- /dev/null +++ b/interfaces/udev/udev_test.go @@ -0,0 +1,57 @@ +// -*- 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 . + * + */ + +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 { + testutil.ExecTest +} + +var _ = Suite(&uDevSuite{}) + +// Tests for ReloadRules() + +func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdm(c *C) { + s.ExecTest.MockExecutable(c, "udevadm") + err := udev.ReloadRules() + c.Assert(err, IsNil) + c.Assert(s.CallsToExecutable(c, "udevadm"), DeepEquals, []string{ + "control --reload-rules", + "trigger", + }) +} + +func (s *uDevSuite) TestReloadUDevRulesReportsErrors(c *C) { + s.ExecTest.MockFailingExecutable(c, "udevadm", 42) + err := udev.ReloadRules() + c.Assert(err, ErrorMatches, "exit status 42") +}