forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_start_time_test.go
72 lines (59 loc) · 2.13 KB
/
pid_start_time_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build linux
// +build linux
/*
* Copyright (C) 2017 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 polkit
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"gopkg.in/check.v1"
)
func Test(t *testing.T) { check.TestingT(t) }
type polkitSuite struct{}
var _ = check.Suite(&polkitSuite{})
func (s *polkitSuite) TestGetStartTime(c *check.C) {
pid := os.Getpid()
startTime, err := getStartTimeForPid(int32(pid))
c.Assert(err, check.IsNil)
c.Check(startTime, check.Not(check.Equals), uint64(0))
}
func (s *polkitSuite) TestGetStartTimeBadPid(c *check.C) {
// Find an unused process ID by checking for errors from Kill.
pid := 2
for {
if err := syscall.Kill(pid, 0); err == syscall.ESRCH {
break
}
pid += 1
}
startTime, err := getStartTimeForPid(int32(pid))
c.Assert(err, check.ErrorMatches, "open .*: no such file or directory")
c.Check(startTime, check.Equals, uint64(0))
}
func (s *polkitSuite) TestProcStatParsing(c *check.C) {
filename := filepath.Join(c.MkDir(), "stat")
contents := []byte("18433 (cat) R 9732 18433 9732 34818 18433 4194304 96 0 1 0 0 0 0 0 20 0 1 0 123104764 7602176 182 18446744073709551615 94902526107648 94902526138492 140734457666896 0 0 0 0 0 0 0 0 0 17 5 0 0 0 0 0 94902528236168 94902528237760 94902542680064 140734457672267 140734457672287 140734457672287 140734457675759 0")
err := ioutil.WriteFile(filename, contents, 0644)
c.Assert(err, check.IsNil)
startTime, err := getStartTimeForProcStatFile(filename)
c.Assert(err, check.IsNil)
c.Check(startTime, check.Equals, uint64(123104764))
}