Skip to content

Commit 55793c7

Browse files
test: move snapshot integration tests to unit test (#223)
1 parent 0e3a620 commit 55793c7

12 files changed

+133
-175
lines changed

integration/scripts/test_snapshot_conf.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

integration/tests/integration.tftest.hcl

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,6 @@ run "test_version" {
8989
}
9090
}
9191

92-
run "test_snapshot_conf" {
93-
module {
94-
source = "observeinc/collection/aws//modules/testing/exec"
95-
version = "2.9.0"
96-
}
97-
98-
variables {
99-
command = "python3 ./scripts/test_snapshot_conf.py"
100-
env_vars = {
101-
HOST = run.setup_ec2.public_ip
102-
USER = run.setup_ec2.user_name
103-
KEY_FILENAME = run.setup_ec2.private_key_path
104-
PASSWORD = run.setup_ec2.password
105-
MACHINE_NAME = run.setup_ec2.machine_name
106-
MACHINE_CONFIG = run.setup_ec2.machine_config
107-
}
108-
}
109-
110-
assert {
111-
condition = output.error == ""
112-
error_message = "Error in Snapshot Test"
113-
}
114-
}
115-
116-
11792
run "test_configure" {
11893
module {
11994
source = "observeinc/collection/aws//modules/testing/exec"

internal/commands/config/config_test.go

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,106 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
)
2424

25-
// TODO rework this test to handle our snapshot tests as go unit tests.
25+
type PackageType string
26+
27+
const MacOS = PackageType("macos")
28+
const Linux = PackageType("linux")
29+
const Windows = PackageType("windows")
30+
const Docker = PackageType("docker")
31+
32+
type snapshotTest struct {
33+
agentConfigPath string
34+
otelConfigPath string
35+
outputPath string
36+
packageType PackageType
37+
}
38+
39+
var allSnapshotTests = []snapshotTest{
40+
{
41+
agentConfigPath: "test/snap1-full-agent-config.yaml",
42+
outputPath: "test/snap1-docker-output.yaml",
43+
packageType: Docker,
44+
},
45+
{
46+
agentConfigPath: "test/snap1-full-agent-config.yaml",
47+
outputPath: "test/snap1-linux-output.yaml",
48+
packageType: Linux,
49+
},
50+
{
51+
agentConfigPath: "test/snap1-full-agent-config.yaml",
52+
outputPath: "test/snap1-windows-output.yaml",
53+
packageType: Windows,
54+
},
55+
{
56+
agentConfigPath: "test/snap2-empty-agent-config.yaml",
57+
otelConfigPath: "test/snap2-otel-config.yaml",
58+
outputPath: "test/snap2-with-otel-output.yaml",
59+
packageType: MacOS,
60+
},
61+
}
62+
2663
func Test_RenderOtelConfig(t *testing.T) {
64+
for _, test := range allSnapshotTests {
65+
t.Run(test.outputPath, func(t *testing.T) {
66+
runSnapshotTest(t, test)
67+
})
68+
}
69+
}
70+
71+
func runSnapshotTest(t *testing.T, test snapshotTest) {
2772
// Get current path
2873
_, filename, _, ok := runtime.Caller(0)
2974
assert.True(t, ok)
3075
curPath := path.Dir(filename)
3176

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

3782
// Set config flags
3883
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
3984
observecol.AddConfigFlags(flags)
40-
flags.Parse([]string{"--config", filepath.Join(curPath, "test/otel-config.yaml")})
85+
if test.otelConfigPath != "" {
86+
flags.Parse([]string{"--config", filepath.Join(curPath, test.otelConfigPath)})
87+
}
4188
viper.Reset()
42-
root.CfgFile = filepath.Join(curPath, "test/agent-config.yaml")
89+
root.CfgFile = filepath.Join(curPath, test.agentConfigPath)
4390
root.InitConfig()
91+
setEnvVars(t, test.packageType)
4492

4593
// Run the test
4694
ctx := logger.WithCtx(context.Background(), logger.GetNop())
4795
var output bytes.Buffer
4896
PrintShortOtelConfig(ctx, &output)
49-
expected, err := os.ReadFile(filepath.Join(curPath, "test/output.yaml"))
97+
expected, err := os.ReadFile(filepath.Join(curPath, test.outputPath))
5098
assert.NoError(t, err)
5199
assert.Equal(t, strings.TrimSpace(string(expected)), strings.TrimSpace(output.String()))
52100
}
101+
102+
func getPackagingPath(t *testing.T, packageType PackageType, curPath string) string {
103+
const packagingPath = "../../../packaging"
104+
switch packageType {
105+
case MacOS, Linux, Windows:
106+
return filepath.Join(curPath, packagingPath, string(packageType), "connections")
107+
case Docker:
108+
return filepath.Join(curPath, packagingPath, "docker/observe-agent/connections")
109+
default:
110+
t.Errorf("Unknown package type: %s", packageType)
111+
return ""
112+
}
113+
}
114+
115+
func setEnvVars(t *testing.T, packageType PackageType) {
116+
switch packageType {
117+
case MacOS:
118+
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "/var/lib/observe-agent/filestorage"))
119+
case Windows:
120+
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "C:\\ProgramData\\Observe\\observe-agent\\filestorage"))
121+
case Linux, Docker:
122+
assert.NoError(t, os.Setenv("FILESTORAGE_PATH", "/var/lib/observe-agent/filestorage"))
123+
default:
124+
t.Errorf("Unknown package type: %s", packageType)
125+
}
126+
127+
}

0 commit comments

Comments
 (0)