Skip to content

test: move snapshot integration tests to unit test #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 0 additions & 93 deletions integration/scripts/test_snapshot_conf.py

This file was deleted.

25 changes: 0 additions & 25 deletions integration/tests/integration.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,6 @@ run "test_version" {
}
}

run "test_snapshot_conf" {
module {
source = "observeinc/collection/aws//modules/testing/exec"
version = "2.9.0"
}

variables {
command = "python3 ./scripts/test_snapshot_conf.py"
env_vars = {
HOST = run.setup_ec2.public_ip
USER = run.setup_ec2.user_name
KEY_FILENAME = run.setup_ec2.private_key_path
PASSWORD = run.setup_ec2.password
MACHINE_NAME = run.setup_ec2.machine_name
MACHINE_CONFIG = run.setup_ec2.machine_config
}
}

assert {
condition = output.error == ""
error_message = "Error in Snapshot Test"
}
}


run "test_configure" {
module {
source = "observeinc/collection/aws//modules/testing/exec"
Expand Down
85 changes: 80 additions & 5 deletions internal/commands/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,106 @@ import (
"github.com/stretchr/testify/assert"
)

// TODO rework this test to handle our snapshot tests as go unit tests.
type PackageType string

const MacOS = PackageType("macos")
const Linux = PackageType("linux")
const Windows = PackageType("windows")
const Docker = PackageType("docker")

type snapshotTest struct {
agentConfigPath string
otelConfigPath string
outputPath string
packageType PackageType
}

var allSnapshotTests = []snapshotTest{
{
agentConfigPath: "test/snap1-full-agent-config.yaml",
outputPath: "test/snap1-docker-output.yaml",
packageType: Docker,
},
{
agentConfigPath: "test/snap1-full-agent-config.yaml",
outputPath: "test/snap1-linux-output.yaml",
packageType: Linux,
},
{
agentConfigPath: "test/snap1-full-agent-config.yaml",
outputPath: "test/snap1-windows-output.yaml",
packageType: Windows,
},
{
agentConfigPath: "test/snap2-empty-agent-config.yaml",
otelConfigPath: "test/snap2-otel-config.yaml",
outputPath: "test/snap2-with-otel-output.yaml",
packageType: MacOS,
},
}

func Test_RenderOtelConfig(t *testing.T) {
for _, test := range allSnapshotTests {
t.Run(test.outputPath, func(t *testing.T) {
runSnapshotTest(t, test)
})
}
}

func runSnapshotTest(t *testing.T, test snapshotTest) {
// Get current path
_, filename, _, ok := runtime.Caller(0)
assert.True(t, ok)
curPath := path.Dir(filename)

// Set the template base dir for all connections
for _, conn := range connections.AllConnectionTypes {
conn.ApplyOptions(connections.WithConfigFolderPath(filepath.Join(curPath, "../../../packaging/macos/connections")))
conn.ApplyOptions(connections.WithConfigFolderPath(getPackagingPath(t, test.packageType, curPath)))
}

// Set config flags
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
observecol.AddConfigFlags(flags)
flags.Parse([]string{"--config", filepath.Join(curPath, "test/otel-config.yaml")})
if test.otelConfigPath != "" {
flags.Parse([]string{"--config", filepath.Join(curPath, test.otelConfigPath)})
}
viper.Reset()
root.CfgFile = filepath.Join(curPath, "test/agent-config.yaml")
root.CfgFile = filepath.Join(curPath, test.agentConfigPath)
root.InitConfig()
setEnvVars(t, test.packageType)

// Run the test
ctx := logger.WithCtx(context.Background(), logger.GetNop())
var output bytes.Buffer
PrintShortOtelConfig(ctx, &output)
expected, err := os.ReadFile(filepath.Join(curPath, "test/output.yaml"))
expected, err := os.ReadFile(filepath.Join(curPath, test.outputPath))
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(string(expected)), strings.TrimSpace(output.String()))
}

func getPackagingPath(t *testing.T, packageType PackageType, curPath string) string {
const packagingPath = "../../../packaging"
switch packageType {
case MacOS, Linux, Windows:
return filepath.Join(curPath, packagingPath, string(packageType), "connections")
case Docker:
return filepath.Join(curPath, packagingPath, "docker/observe-agent/connections")
default:
t.Errorf("Unknown package type: %s", packageType)
return ""
}
}

func setEnvVars(t *testing.T, packageType PackageType) {
switch packageType {
case MacOS:
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "/var/lib/observe-agent/filestorage"))
case Windows:
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "C:\\ProgramData\\Observe\\observe-agent\\filestorage"))
case Linux, Docker:
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "/var/lib/observe-agent/filestorage"))
default:
t.Errorf("Unknown package type: %s", packageType)
}

}
52 changes: 0 additions & 52 deletions internal/connections/confighandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"

"github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/commands/util/logger"
"github.com/observeinc/observe-agent/internal/config"
"github.com/spf13/viper"
Expand All @@ -20,11 +18,6 @@ const (
)

func SetupAndGetConfigFiles(ctx context.Context) ([]string, func(), error) {
// Set Env Vars from config
err := SetEnvVars()
if err != nil {
return nil, nil, err
}
// Set up our temp dir annd temp config files
tmpDir, err := os.MkdirTemp("", TempFilesFolder)
if err != nil {
Expand Down Expand Up @@ -84,38 +77,6 @@ func GetAllOtelConfigFilePaths(ctx context.Context, tmpDir string) ([]string, er
return configFilePaths, nil
}

func SetEnvVars() error {
collector_url, token, debug := viper.GetString("observe_url"), viper.GetString("token"), viper.GetBool("debug")
// Ensure the collector url does not end with a slash for consistency. This will allow endpoints to be configured like:
// "${env:OBSERVE_COLLECTOR_URL}/v1/kubernetes/v1/entity"
// without worrying about a double slash.
collector_url = strings.TrimRight(collector_url, "/")
otelEndpoint := util.JoinUrl(collector_url, "/v2/otel")
promEndpoint := util.JoinUrl(collector_url, "/v1/prometheus")
// Setting values from the Observe agent config as env vars to fill in the OTEL collector config
os.Setenv("OBSERVE_COLLECTOR_URL", collector_url)
os.Setenv("OBSERVE_OTEL_ENDPOINT", otelEndpoint)
os.Setenv("OBSERVE_PROMETHEUS_ENDPOINT", promEndpoint)
os.Setenv("OBSERVE_AUTHORIZATION_HEADER", "Bearer "+token)
os.Setenv("FILESTORAGE_PATH", GetDefaultFilestoragePath())

// Default TRACE_TOKEN to be the value of the configured token if it's not set. This allows for users to upgrade to
// direct write tracing with ingest tokens in kubernetes without breaking backwards compatibility in our helm chart.
// TODO: remove this once our helm chart no longer supports TRACE_TOKEN
if os.Getenv("TRACE_TOKEN") == "" {
os.Setenv("TRACE_TOKEN", token)
}

if os.Getenv("OTEL_LOG_LEVEL") == "" {
if debug {
os.Setenv("OTEL_LOG_LEVEL", "DEBUG")
} else {
os.Setenv("OTEL_LOG_LEVEL", "INFO")
}
}
return nil
}

func GetOverrideConfigFile(tmpDir string, data map[string]any) (string, error) {
f, err := os.CreateTemp(tmpDir, "otel-config-overrides-*.yaml")
if err != nil {
Expand Down Expand Up @@ -148,16 +109,3 @@ func GetDefaultAgentPath() string {
return "/etc/observe-agent"
}
}

func GetDefaultFilestoragePath() string {
switch currOS := runtime.GOOS; currOS {
case "darwin":
return "/var/lib/observe-agent/filestorage"
case "windows":
return os.ExpandEnv("$ProgramData\\Observe\\observe-agent\\filestorage")
case "linux":
return "/var/lib/observe-agent/filestorage"
default:
return "/var/lib/observe-agent/filestorage"
}
}
53 changes: 53 additions & 0 deletions internal/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"context"
"fmt"
"os"
"runtime"
"strings"

"github.com/observeinc/observe-agent/build"
"github.com/observeinc/observe-agent/internal/commands/util"
"github.com/observeinc/observe-agent/internal/commands/util/logger"
"github.com/observeinc/observe-agent/internal/config"
"github.com/observeinc/observe-agent/internal/connections"
Expand Down Expand Up @@ -79,4 +82,54 @@ func InitConfig() {

// Apply feature gates
observecol.ApplyFeatureGates(ctx)

// Set up env vars
if err := setEnvVars(); err != nil {
fmt.Fprintln(os.Stderr, "error setting env vars:", err)
}
}

func setEnvVars() error {
collector_url, token, debug := viper.GetString("observe_url"), viper.GetString("token"), viper.GetBool("debug")
// Ensure the collector url does not end with a slash for consistency. This will allow endpoints to be configured like:
// "${env:OBSERVE_COLLECTOR_URL}/v1/kubernetes/v1/entity"
// without worrying about a double slash.
collector_url = strings.TrimRight(collector_url, "/")
otelEndpoint := util.JoinUrl(collector_url, "/v2/otel")
promEndpoint := util.JoinUrl(collector_url, "/v1/prometheus")
// Setting values from the Observe agent config as env vars to fill in the OTEL collector config
os.Setenv("OBSERVE_COLLECTOR_URL", collector_url)
os.Setenv("OBSERVE_OTEL_ENDPOINT", otelEndpoint)
os.Setenv("OBSERVE_PROMETHEUS_ENDPOINT", promEndpoint)
os.Setenv("OBSERVE_AUTHORIZATION_HEADER", "Bearer "+token)
os.Setenv("FILESTORAGE_PATH", getDefaultFilestoragePath())

// Default TRACE_TOKEN to be the value of the configured token if it's not set. This allows for users to upgrade to
// direct write tracing with ingest tokens in kubernetes without breaking backwards compatibility in our helm chart.
// TODO: remove this once our helm chart no longer supports TRACE_TOKEN
if os.Getenv("TRACE_TOKEN") == "" {
os.Setenv("TRACE_TOKEN", token)
}

if os.Getenv("OTEL_LOG_LEVEL") == "" {
if debug {
os.Setenv("OTEL_LOG_LEVEL", "DEBUG")
} else {
os.Setenv("OTEL_LOG_LEVEL", "INFO")
}
}
return nil
}

func getDefaultFilestoragePath() string {
switch currOS := runtime.GOOS; currOS {
case "darwin":
return "/var/lib/observe-agent/filestorage"
case "windows":
return os.ExpandEnv("$ProgramData\\Observe\\observe-agent\\filestorage")
case "linux":
return "/var/lib/observe-agent/filestorage"
default:
return "/var/lib/observe-agent/filestorage"
}
}
Loading