Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/packaging/agent_linux_packages_test.go
4094 views
1
//go:build !nonetwork && !nodocker && !race && packaging
2
// +build !nonetwork,!nodocker,!race,packaging
3
4
package packaging_test
5
6
import (
7
"fmt"
8
"os"
9
"os/exec"
10
"path/filepath"
11
"runtime"
12
"testing"
13
14
"github.com/ory/dockertest/v3"
15
"github.com/stretchr/testify/require"
16
)
17
18
// TestAgentLinuxPackages runs the entire test suite for the Linux packages.
19
func TestAgentLinuxPackages(t *testing.T) {
20
packageName := "grafana-agent"
21
22
fmt.Println("Building packages (this may take a while...)")
23
buildAgentPackages(t)
24
25
dockerPool, err := dockertest.NewPool("")
26
require.NoError(t, err)
27
28
tt := []struct {
29
name string
30
f func(*AgentEnvironment, *testing.T)
31
}{
32
{"install package", (*AgentEnvironment).TestInstall},
33
{"ensure existing config doesn't get overridden", (*AgentEnvironment).TestConfigPersistence},
34
{"test data folder permissions", (*AgentEnvironment).TestDataFolderPermissions},
35
36
// TODO: a test to verify that the systemd service works would be nice, but not
37
// required.
38
//
39
// An implementation of the test would have to consider what host platforms it
40
// works on; bind mounting /sys/fs/cgroup and using the host systemd wouldn't
41
// work on macOS or Windows.
42
}
43
44
for _, tc := range tt {
45
t.Run(tc.name+"/rpm", func(t *testing.T) {
46
env := &AgentEnvironment{RPMEnvironment(t, packageName, dockerPool)}
47
tc.f(env, t)
48
})
49
t.Run(tc.name+"/deb", func(t *testing.T) {
50
env := &AgentEnvironment{DEBEnvironment(t, packageName, dockerPool)}
51
tc.f(env, t)
52
})
53
}
54
}
55
56
func buildAgentPackages(t *testing.T) {
57
t.Helper()
58
59
wd, err := os.Getwd()
60
require.NoError(t, err)
61
root, err := filepath.Abs(filepath.Join(wd, ".."))
62
require.NoError(t, err)
63
64
cmd := exec.Command("make", fmt.Sprintf("dist-agent-packages-%s", runtime.GOARCH))
65
cmd.Env = append(
66
os.Environ(),
67
"VERSION=v0.0.0",
68
"DOCKER_OPTS=",
69
)
70
cmd.Dir = root
71
cmd.Stdout = os.Stdout
72
cmd.Stderr = os.Stderr
73
require.NoError(t, cmd.Run())
74
}
75
76
type AgentEnvironment struct{ Environment }
77
78
func (env *AgentEnvironment) TestInstall(t *testing.T) {
79
res := env.Install()
80
require.Equal(t, 0, res.ExitCode, "installing failed")
81
82
res = env.ExecScript(`[ -f /usr/bin/grafana-agent ]`)
83
require.Equal(t, 0, res.ExitCode, "expected grafana-agent to be installed")
84
res = env.ExecScript(`[ -f /usr/bin/grafana-agentctl ]`)
85
require.Equal(t, 0, res.ExitCode, "expected grafana-agentctl to be installed")
86
res = env.ExecScript(`[ -f /etc/grafana-agent.yaml ]`)
87
require.Equal(t, 0, res.ExitCode, "expected grafana agent configuration file to exist")
88
89
res = env.Uninstall()
90
require.Equal(t, 0, res.ExitCode, "uninstalling failed")
91
92
res = env.ExecScript(`[ -f /usr/bin/grafana-agent ]`)
93
require.Equal(t, 1, res.ExitCode, "expected grafana-agent to be uninstalled")
94
res = env.ExecScript(`[ -f /usr/bin/grafana-agentctl ]`)
95
require.Equal(t, 1, res.ExitCode, "expected grafana-agentctl to be uninstalled")
96
// NOTE(rfratto): we don't check for what happens to the config file here,
97
// since the behavior is inconsistent: rpm uninstalls it, but deb doesn't.
98
}
99
100
func (env *AgentEnvironment) TestConfigPersistence(t *testing.T) {
101
res := env.ExecScript(`echo -n "keepalive" > /etc/grafana-agent.yaml`)
102
require.Equal(t, 0, res.ExitCode, "failed to write config file")
103
104
res = env.Install()
105
require.Equal(t, 0, res.ExitCode, "installation failed")
106
107
res = env.ExecScript(`cat /etc/grafana-agent.yaml`)
108
require.Equal(t, "keepalive", res.Stdout, "Expected existing file to not be overridden")
109
}
110
111
func (env *AgentEnvironment) TestDataFolderPermissions(t *testing.T) {
112
// Installing should create /var/lib/grafana-agent, assign it to the
113
// grafana-agent user and group, and set its permissions to 0770.
114
res := env.Install()
115
require.Equal(t, 0, res.ExitCode, "installation failed")
116
117
res = env.ExecScript(`[ -d /var/lib/grafana-agent ]`)
118
require.Equal(t, 0, res.ExitCode, "Expected /var/lib/grafana-agent to have been created during install")
119
120
res = env.ExecScript(`stat -c '%a:%U:%G' /var/lib/grafana-agent`)
121
require.Equal(t, "770:grafana-agent:grafana-agent\n", res.Stdout, "wrong permissions for data folder")
122
require.Equal(t, 0, res.ExitCode, "stat'ing data folder failed")
123
}
124
125