@@ -40,14 +40,6 @@ func TestRegisterConfigFile(t *testing.T) {
4040}
4141
4242func TestResolveConfig (t * testing.T ) {
43- allowedDir := []string {
44- "/etc/nginx" , "/usr/local/etc/nginx" , "/var/run/nginx" ,
45- "/usr/share/nginx/modules" , "/var/log/nginx" ,
46- }
47-
48- excludeFiles := []string {
49- "\\ .*log$" ,
50- }
5143
5244 viperInstance = viper .NewWithOptions (viper .KeyDelimiter (KeyDelimiter ))
5345 err := loadPropertiesFromFile ("./testdata/nginx-agent.conf" )
@@ -63,51 +55,7 @@ func TestResolveConfig(t *testing.T) {
6355
6456 actual , err := ResolveConfig ()
6557 require .NoError (t , err )
66-
67- assert .Equal (t , "debug" , actual .Log .Level )
68- assert .Equal (t , "./" , actual .Log .Path )
69-
70- assert .Equal (t , 30 * time .Second , actual .DataPlaneConfig .Nginx .ReloadMonitoringPeriod )
71- assert .False (t , actual .DataPlaneConfig .Nginx .TreatWarningsAsErrors )
72- assert .Equal (t , []string {"/var/log/nginx/error.log" , "^/var/log/nginx/.*.log$" },
73- actual .DataPlaneConfig .Nginx .ExcludeLogs )
74- assert .Equal (t , excludeFiles , actual .Watchers .FileWatcher .ExcludeFiles )
75-
76- require .NotNil (t , actual .Collector )
77- assert .Equal (t , "/etc/nginx-agent/nginx-agent-otelcol.yaml" , actual .Collector .ConfigPath )
78- assert .NotEmpty (t , actual .Collector .Receivers )
79- assert .Equal (t , Processors {Batch : & Batch {}}, actual .Collector .Processors )
80- assert .NotEmpty (t , actual .Collector .Exporters )
81- assert .NotEmpty (t , actual .Collector .Extensions )
82-
83- // Client GRPC Settings
84- assert .Equal (t , 15 * time .Second , actual .Client .Grpc .KeepAlive .Timeout )
85- assert .Equal (t , 10 * time .Second , actual .Client .Grpc .KeepAlive .Time )
86- assert .False (t , actual .Client .Grpc .KeepAlive .PermitWithoutStream )
87- assert .Equal (t , 1048575 , actual .Client .Grpc .MaxMessageSize )
88- assert .Equal (t , 1048575 , actual .Client .Grpc .MaxMessageReceiveSize )
89- assert .Equal (t , 1048575 , actual .Client .Grpc .MaxMessageSendSize )
90-
91- // Client HTTP Settings
92- assert .Equal (t , 15 * time .Second , actual .Client .HTTP .Timeout )
93-
94- // Client Backoff Settings
95- assert .Equal (t , 200 * time .Millisecond , actual .Client .Backoff .InitialInterval )
96- assert .Equal (t , 10 * time .Second , actual .Client .Backoff .MaxInterval )
97- assert .Equal (t , 25 * time .Second , actual .Client .Backoff .MaxElapsedTime )
98- assert .InDelta (t , 1.5 , actual .Client .Backoff .RandomizationFactor , 0.01 )
99- assert .InDelta (t , 2.5 , actual .Client .Backoff .Multiplier , 0.01 )
100-
101- assert .Equal (t ,
102- allowedDir ,
103- actual .AllowedDirectories ,
104- )
105-
106- assert .Equal (t , allowedDir , actual .AllowedDirectories )
107-
108- assert .Equal (t , 5 * time .Second , actual .Watchers .InstanceWatcher .MonitoringFrequency )
109- assert .Equal (t , 5 * time .Second , actual .Watchers .InstanceHealthWatcher .MonitoringFrequency )
110- assert .Equal (t , 5 * time .Second , actual .Watchers .FileWatcher .MonitoringFrequency )
58+ assert .Equal (t , createConfig (), actual )
11159}
11260
11361func TestSetVersion (t * testing.T ) {
@@ -185,7 +133,7 @@ func TestLoadPropertiesFromFile(t *testing.T) {
185133 require .NoError (t , err )
186134
187135 assert .Equal (t , "debug" , viperInstance .GetString (LogLevelKey ))
188- assert .Equal (t , "./" , viperInstance .GetString (LogPathKey ))
136+ assert .Equal (t , "./test-path " , viperInstance .GetString (LogPathKey ))
189137
190138 assert .Equal (t , 15 * time .Second , viperInstance .GetDuration (ClientKeepAliveTimeoutKey ))
191139
@@ -778,3 +726,208 @@ func getAgentConfig() *Config {
778726 Labels : make (map [string ]any ),
779727 }
780728}
729+
730+ func createConfig () * Config {
731+ return & Config {
732+ Log : & Log {
733+ Level : "debug" ,
734+ Path : "./test-path" ,
735+ },
736+ Client : & Client {
737+ HTTP : & HTTP {
738+ Timeout : 15 * time .Second ,
739+ },
740+ Grpc : & GRPC {
741+ KeepAlive : & KeepAlive {
742+ Timeout : 15 * time .Second ,
743+ Time : 10 * time .Second ,
744+ PermitWithoutStream : false ,
745+ },
746+ MaxMessageSize : 1048575 ,
747+ MaxMessageReceiveSize : 1048575 ,
748+ MaxMessageSendSize : 1048575 ,
749+ },
750+ Backoff : & BackOff {
751+ InitialInterval : 200 * time .Millisecond ,
752+ MaxInterval : 10 * time .Second ,
753+ MaxElapsedTime : 25 * time .Second ,
754+ RandomizationFactor : 1.5 ,
755+ Multiplier : 2.5 ,
756+ },
757+ },
758+ AllowedDirectories : []string {
759+ "/etc/nginx" , "/usr/local/etc/nginx" , "/var/run/nginx" , "/usr/share/nginx/modules" , "/var/log/nginx" ,
760+ },
761+ DataPlaneConfig : & DataPlaneConfig {
762+ Nginx : & NginxDataPlaneConfig {
763+ ExcludeLogs : []string {"/var/log/nginx/error.log" , "^/var/log/nginx/.*.log$" },
764+ ReloadMonitoringPeriod : 30 * time .Second ,
765+ TreatWarningsAsErrors : true ,
766+ },
767+ },
768+ Collector : & Collector {
769+ ConfigPath : "/etc/nginx-agent/nginx-agent-otelcol.yaml" ,
770+ Exporters : Exporters {
771+ OtlpExporters : []OtlpExporter {
772+ {
773+ Server : & ServerConfig {
774+ Host : "127.0.0.1" ,
775+ Port : 5643 ,
776+ },
777+ Authenticator : "test-saas-token" ,
778+ TLS : & TLSConfig {
779+ Cert : "/path/to/server-cert.pem" ,
780+ Key : "/path/to/server-key.pem" ,
781+ Ca : "/path/to/server-cert.pem" ,
782+ SkipVerify : false ,
783+ ServerName : "test-saas-server" ,
784+ },
785+ },
786+ },
787+ PrometheusExporter : & PrometheusExporter {
788+ Server : & ServerConfig {
789+ Host : "127.0.0.1" ,
790+ Port : 1235 ,
791+ },
792+ TLS : & TLSConfig {
793+ Cert : "/path/to/server-cert.pem" ,
794+ Key : "/path/to/server-key.pem" ,
795+ Ca : "/path/to/server-cert.pem" ,
796+ SkipVerify : false ,
797+ ServerName : "test-server" ,
798+ },
799+ },
800+ Debug : & DebugExporter {},
801+ },
802+ Processors : Processors {
803+ Batch : & Batch {
804+ SendBatchMaxSize : 1 ,
805+ SendBatchSize : 8199 ,
806+ Timeout : 30 * time .Second ,
807+ },
808+ Attribute : & Attribute {
809+ Actions : []Action {
810+ {
811+ Key : "test" ,
812+ Action : "insert" ,
813+ Value : "value" ,
814+ },
815+ },
816+ },
817+ },
818+ Receivers : Receivers {
819+ OtlpReceivers : []OtlpReceiver {
820+ {
821+ Server : & ServerConfig {
822+ Host : "127.0.0.1" ,
823+ Port : 4317 ,
824+ },
825+ Auth : & AuthConfig {
826+ Token : "secret-receiver-token" ,
827+ },
828+ OtlpTLSConfig : & OtlpTLSConfig {
829+ GenerateSelfSignedCert : false ,
830+ Cert : "/tmp/cert.pem" ,
831+ Key : "/tmp/key.pem" ,
832+ Ca : "/tmp/ca.pem" ,
833+ SkipVerify : true ,
834+ ServerName : "test-local-server" ,
835+ },
836+ },
837+ },
838+ NginxReceivers : []NginxReceiver {
839+ {
840+ InstanceID : "cd7b8911-c2c5-4daf-b311-dbead151d938" ,
841+ AccessLogs : []AccessLog {
842+ {
843+ LogFormat : "$remote_addr - $remote_user [$time_local] \" $request\" " +
844+ " $status $body_bytes_sent \" $http_referer\" \" $http_user_agent\" " +
845+ "\" $http_x_forwarded_for\" " ,
846+ FilePath : "/var/log/nginx/access-custom.conf" ,
847+ },
848+ },
849+ },
850+ },
851+ NginxPlusReceivers : []NginxPlusReceiver {
852+ {
853+ InstanceID : "cd7b8911-c2c5-4daf-b311-dbead151d939" ,
854+ },
855+ },
856+ HostMetrics : & HostMetrics {
857+ CollectionInterval : 10 * time .Second ,
858+ InitialDelay : 2 * time .Second ,
859+ Scrapers : & HostMetricsScrapers {
860+ CPU : & CPUScraper {},
861+ Disk : nil ,
862+ Filesystem : nil ,
863+ Memory : nil ,
864+ Network : nil ,
865+ },
866+ },
867+ },
868+ Extensions : Extensions {
869+ Health : & Health {
870+ Server : & ServerConfig {
871+ Host : "127.0.0.1" ,
872+ Port : 1337 ,
873+ },
874+ TLS : & TLSConfig {
875+ Cert : "/path/to/server-cert.pem" ,
876+ Key : "/path/to/server-key.pem" ,
877+ Ca : "/path/to/server-ca.pem" ,
878+ SkipVerify : false ,
879+ ServerName : "server-name" ,
880+ },
881+ Path : "/test" ,
882+ },
883+ HeadersSetter : & HeadersSetter {
884+ Headers : []Header {
885+ {
886+ Action : "action" ,
887+ Key : "key" ,
888+ Value : "value" ,
889+ },
890+ },
891+ },
892+ },
893+ Log : & Log {
894+ Level : "INFO" ,
895+ Path : "/var/log/nginx-agent/opentelemetry-collector-agent.log" ,
896+ },
897+ },
898+ Command : & Command {
899+ Server : & ServerConfig {
900+ Host : "127.0.0.1" ,
901+ Port : 8888 ,
902+ Type : Grpc ,
903+ },
904+ Auth : & AuthConfig {
905+ Token : "1234" ,
906+ },
907+ TLS : & TLSConfig {
908+ Cert : "some.cert" ,
909+ Key : "some.key" ,
910+ Ca : "some.ca" ,
911+ SkipVerify : false ,
912+ ServerName : "server-name" ,
913+ },
914+ },
915+ Watchers : & Watchers {
916+ InstanceWatcher : InstanceWatcher {
917+ MonitoringFrequency : 10 * time .Second ,
918+ },
919+ InstanceHealthWatcher : InstanceHealthWatcher {
920+ MonitoringFrequency : 10 * time .Second ,
921+ },
922+ FileWatcher : FileWatcher {
923+ MonitoringFrequency : 10 * time .Second ,
924+ ExcludeFiles : []string {"\\ .*log$" },
925+ },
926+ },
927+ Labels : map [string ]any {
928+ "label1" : "label 1" ,
929+ "label2" : "new-value" ,
930+ "label3" : 123 ,
931+ },
932+ }
933+ }
0 commit comments