@@ -22,31 +22,106 @@ import (
22
22
"github.com/stretchr/testify/assert"
23
23
)
24
24
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
+
26
63
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 ) {
27
72
// Get current path
28
73
_ , filename , _ , ok := runtime .Caller (0 )
29
74
assert .True (t , ok )
30
75
curPath := path .Dir (filename )
31
76
32
77
// Set the template base dir for all connections
33
78
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 )))
35
80
}
36
81
37
82
// Set config flags
38
83
flags := pflag .NewFlagSet ("test" , pflag .ContinueOnError )
39
84
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
+ }
41
88
viper .Reset ()
42
- root .CfgFile = filepath .Join (curPath , " test/agent-config.yaml" )
89
+ root .CfgFile = filepath .Join (curPath , test . agentConfigPath )
43
90
root .InitConfig ()
91
+ setEnvVars (t , test .packageType )
44
92
45
93
// Run the test
46
94
ctx := logger .WithCtx (context .Background (), logger .GetNop ())
47
95
var output bytes.Buffer
48
96
PrintShortOtelConfig (ctx , & output )
49
- expected , err := os .ReadFile (filepath .Join (curPath , " test/output.yaml" ))
97
+ expected , err := os .ReadFile (filepath .Join (curPath , test . outputPath ))
50
98
assert .NoError (t , err )
51
99
assert .Equal (t , strings .TrimSpace (string (expected )), strings .TrimSpace (output .String ()))
52
100
}
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