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>
This commit is contained in:
Zygmunt Krynicki
2016-03-10 16:47:44 +01:00
parent 6e03b5b386
commit 83efc8cbbf
2 changed files with 94 additions and 0 deletions

37
interfaces/udev/udev.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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
}

View File

@@ -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 <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 {
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")
}