-
Notifications
You must be signed in to change notification settings - Fork 23
/
missing_proc_scrape.go
89 lines (70 loc) · 2.74 KB
/
missing_proc_scrape.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package suites
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stretchr/testify/assert"
)
type MissingProcScrapeTestSuite struct {
IntegrationTestSuiteBase
}
const (
fakePid = 8989
fakeProcName = "short"
fakeProcDir = "/tmp/fake-proc"
)
func (s *MissingProcScrapeTestSuite) createFakeProcDir() {
fakeProc := fmt.Sprintf("%s/%d", fakeProcDir, fakePid)
err := os.MkdirAll(fakeProc, os.ModePerm)
s.Require().NoError(err, "Failed to create fake proc directory")
err = os.Symlink("/bin/sh", filepath.Join(fakeProc, "exe"))
s.Require().NoError(err, "Failed to symlink exe")
status := fmt.Sprintf("Name: %s\n", fakeProcName)
err = ioutil.WriteFile(filepath.Join(fakeProc, "status"), []byte(status), 0644)
s.Require().NoError(err, "Failed to create status file")
cmdline := fmt.Sprintf("/bin/%s\n", fakeProcName)
err = ioutil.WriteFile(filepath.Join(fakeProc, "cmdline"), []byte(cmdline), 0644)
s.Require().NoError(err, "Failed to create cmdline file")
f, err := os.OpenFile(filepath.Join(fakeProc, "environ"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
s.Require().NoError(err, "Failed to create environ file")
defer f.Close()
for _, v := range os.Environ() {
_, err := f.WriteString(v + "\n")
s.Require().NoError(err, "Failed to write to environ")
}
err = ioutil.WriteFile(filepath.Join(fakeProcDir, "stat"), []byte("btime 1695972922\n"), 0644)
s.Require().NoError(err, "Failed to write to stat")
err = os.MkdirAll(filepath.Join(fakeProcDir, "1"), os.ModePerm)
s.Require().NoError(err, "Failed to create /proc/1")
err = ioutil.WriteFile(filepath.Join(fakeProcDir, "1", "mounts"), []byte("cgroup2 /sys/fs/cgroup cgroup2 rw,seclabel,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot 0 0\n"), 0644)
s.Require().NoError(err, "Failed to write to /proc/1/mounts")
}
func (s *MissingProcScrapeTestSuite) SetupSuite() {
s.RegisterCleanup()
_, err := os.Stat(fakeProcDir)
if os.IsNotExist(err) {
s.createFakeProcDir()
}
collectorOptions := collector.StartupOptions{
Mounts: map[string]string{
"/host/proc:ro": fakeProcDir,
},
Env: map[string]string{
// if /etc/hostname is empty collector will read /proc/sys/kernel/hostname
// which will also be empty because of the fake proc, so collector will exit.
// to avoid this, set NODE_HOSTNAME
"NODE_HOSTNAME": "collector-missing-proc-host",
},
}
s.StartCollector(false, &collectorOptions)
}
func (s *MissingProcScrapeTestSuite) TestCollectorRunning() {
collectorRunning, err := s.executor.IsContainerRunning("collector")
s.Require().NoError(err)
assert.True(s.T(), collectorRunning, "Collector isn't running")
}
func (s *MissingProcScrapeTestSuite) TearDownSuite() {
s.StopCollector()
}