Path: blob/main/packaging/flow_linux_packages_test.go
4093 views
//go:build !nonetwork && !nodocker && !race && packaging1// +build !nonetwork,!nodocker,!race,packaging23package packaging_test45import (6"fmt"7"os"8"os/exec"9"path/filepath"10"runtime"11"testing"1213"github.com/ory/dockertest/v3"14"github.com/stretchr/testify/require"15)1617// TestFlowLinuxPackages runs the entire test suite for the Linux packages.18func TestFlowLinuxPackages(t *testing.T) {19packageName := "grafana-agent-flow"2021fmt.Println("Building packages (this may take a while...)")22buildFlowPackages(t)2324dockerPool, err := dockertest.NewPool("")25require.NoError(t, err)2627tt := []struct {28name string29f func(*FlowEnvironment, *testing.T)30}{31{"install package", (*FlowEnvironment).TestInstall},32{"ensure existing config doesn't get overridden", (*FlowEnvironment).TestConfigPersistence},33{"test data folder permissions", (*FlowEnvironment).TestDataFolderPermissions},3435// TODO: a test to verify that the systemd service works would be nice, but not36// required.37//38// An implementation of the test would have to consider what host platforms it39// works on; bind mounting /sys/fs/cgroup and using the host systemd wouldn't40// work on macOS or Windows.41}4243for _, tc := range tt {44t.Run(tc.name+"/rpm", func(t *testing.T) {45env := &FlowEnvironment{RPMEnvironment(t, packageName, dockerPool)}46tc.f(env, t)47})48t.Run(tc.name+"/deb", func(t *testing.T) {49env := &FlowEnvironment{DEBEnvironment(t, packageName, dockerPool)}50tc.f(env, t)51})52}53}5455func buildFlowPackages(t *testing.T) {56t.Helper()5758wd, err := os.Getwd()59require.NoError(t, err)60root, err := filepath.Abs(filepath.Join(wd, ".."))61require.NoError(t, err)6263cmd := exec.Command("make", fmt.Sprintf("dist-agent-flow-packages-%s", runtime.GOARCH))64cmd.Env = append(65os.Environ(),66"VERSION=v0.0.0",67"DOCKER_OPTS=",68)69cmd.Dir = root70cmd.Stdout = os.Stdout71cmd.Stderr = os.Stderr72require.NoError(t, cmd.Run())73}7475type FlowEnvironment struct{ Environment }7677func (env *FlowEnvironment) TestInstall(t *testing.T) {78res := env.Install()79require.Equal(t, 0, res.ExitCode, "installing failed")8081res = env.ExecScript(`[ -f /usr/bin/grafana-agent-flow ]`)82require.Equal(t, 0, res.ExitCode, "expected grafana-agent-flow to be installed")83res = env.ExecScript(`[ -f /etc/grafana-agent-flow.river ]`)84require.Equal(t, 0, res.ExitCode, "expected grafana agent configuration file to exist")8586res = env.Uninstall()87require.Equal(t, 0, res.ExitCode, "uninstalling failed")8889res = env.ExecScript(`[ -f /usr/bin/grafana-agent-flow ]`)90require.Equal(t, 1, res.ExitCode, "expected grafana-agent-flow to be uninstalled")91// NOTE(rfratto): we don't check for what happens to the config file here,92// since the behavior is inconsistent: rpm uninstalls it, but deb doesn't.93}9495func (env *FlowEnvironment) TestConfigPersistence(t *testing.T) {96res := env.ExecScript(`echo -n "keepalive" > /etc/grafana-agent.river`)97require.Equal(t, 0, res.ExitCode, "failed to write config file")9899res = env.Install()100require.Equal(t, 0, res.ExitCode, "installation failed")101102res = env.ExecScript(`cat /etc/grafana-agent.river`)103require.Equal(t, "keepalive", res.Stdout, "Expected existing file to not be overridden")104}105106func (env *FlowEnvironment) TestDataFolderPermissions(t *testing.T) {107// Installing should create /var/lib/grafana-agent, assign it to the108// grafana-agent user and group, and set its permissions to 0770.109res := env.Install()110require.Equal(t, 0, res.ExitCode, "installation failed")111112res = env.ExecScript(`[ -d /var/lib/grafana-agent-flow ]`)113require.Equal(t, 0, res.ExitCode, "Expected /var/lib/grafana-agent-flow to have been created during install")114115res = env.ExecScript(`stat -c '%a:%U:%G' /var/lib/grafana-agent-flow`)116require.Equal(t, "770:grafana-agent-flow:grafana-agent\n", res.Stdout, "wrong permissions for data folder")117require.Equal(t, 0, res.ExitCode, "stat'ing data folder failed")118}119120121