Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/packaging/flow_linux_packages_test.go
4093 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
// TestFlowLinuxPackages runs the entire test suite for the Linux packages.
19
func TestFlowLinuxPackages(t *testing.T) {
20
packageName := "grafana-agent-flow"
21
22
fmt.Println("Building packages (this may take a while...)")
23
buildFlowPackages(t)
24
25
dockerPool, err := dockertest.NewPool("")
26
require.NoError(t, err)
27
28
tt := []struct {
29
name string
30
f func(*FlowEnvironment, *testing.T)
31
}{
32
{"install package", (*FlowEnvironment).TestInstall},
33
{"ensure existing config doesn't get overridden", (*FlowEnvironment).TestConfigPersistence},
34
{"test data folder permissions", (*FlowEnvironment).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 := &FlowEnvironment{RPMEnvironment(t, packageName, dockerPool)}
47
tc.f(env, t)
48
})
49
t.Run(tc.name+"/deb", func(t *testing.T) {
50
env := &FlowEnvironment{DEBEnvironment(t, packageName, dockerPool)}
51
tc.f(env, t)
52
})
53
}
54
}
55
56
func buildFlowPackages(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-flow-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 FlowEnvironment struct{ Environment }
77
78
func (env *FlowEnvironment) 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-flow ]`)
83
require.Equal(t, 0, res.ExitCode, "expected grafana-agent-flow to be installed")
84
res = env.ExecScript(`[ -f /etc/grafana-agent-flow.river ]`)
85
require.Equal(t, 0, res.ExitCode, "expected grafana agent configuration file to exist")
86
87
res = env.Uninstall()
88
require.Equal(t, 0, res.ExitCode, "uninstalling failed")
89
90
res = env.ExecScript(`[ -f /usr/bin/grafana-agent-flow ]`)
91
require.Equal(t, 1, res.ExitCode, "expected grafana-agent-flow to be uninstalled")
92
// NOTE(rfratto): we don't check for what happens to the config file here,
93
// since the behavior is inconsistent: rpm uninstalls it, but deb doesn't.
94
}
95
96
func (env *FlowEnvironment) TestConfigPersistence(t *testing.T) {
97
res := env.ExecScript(`echo -n "keepalive" > /etc/grafana-agent.river`)
98
require.Equal(t, 0, res.ExitCode, "failed to write config file")
99
100
res = env.Install()
101
require.Equal(t, 0, res.ExitCode, "installation failed")
102
103
res = env.ExecScript(`cat /etc/grafana-agent.river`)
104
require.Equal(t, "keepalive", res.Stdout, "Expected existing file to not be overridden")
105
}
106
107
func (env *FlowEnvironment) TestDataFolderPermissions(t *testing.T) {
108
// Installing should create /var/lib/grafana-agent, assign it to the
109
// grafana-agent user and group, and set its permissions to 0770.
110
res := env.Install()
111
require.Equal(t, 0, res.ExitCode, "installation failed")
112
113
res = env.ExecScript(`[ -d /var/lib/grafana-agent-flow ]`)
114
require.Equal(t, 0, res.ExitCode, "Expected /var/lib/grafana-agent-flow to have been created during install")
115
116
res = env.ExecScript(`stat -c '%a:%U:%G' /var/lib/grafana-agent-flow`)
117
require.Equal(t, "770:grafana-agent-flow:grafana-agent\n", res.Stdout, "wrong permissions for data folder")
118
require.Equal(t, 0, res.ExitCode, "stat'ing data folder failed")
119
}
120
121