@@ -91,7 +91,8 @@ def run_parser(scenario, configmap)
9191 "SIDECAR_SCRAPING_ENABLED" => nil ,
9292 } . merge ( spec [ :env ] )
9393
94- stdout , stderr , = Open3 . capture3 ( env , RbConfig . ruby , parser , chdir : sandbox )
94+ stdout , stderr , status = Open3 . capture3 ( env , RbConfig . ruby , parser , chdir : sandbox )
95+ assert status . success? , "config parser failed: #{ stdout } \n #{ stderr } "
9596
9697 telemetry_path = File . join ( sandbox , "telemetry_prom_config_env_var" )
9798 result = {
@@ -170,7 +171,7 @@ def test_invalid_telegraf_durations_are_rejected
170171 "1m;id" , # shell metacharacters
171172 "$(id)" ,
172173 "`id`" ,
173- "1d" , # rejected: the telegraf shipped on windows does not support days
174+ "1d" , # rejected by the existing config-map duration contract
174175 "1" , # missing unit
175176 "m" , # missing number
176177 "" ,
@@ -307,6 +308,98 @@ def test_namespace_plugins_are_generated_for_valid_namespaces
307308 assert_no_injected_plugins ( :replicaset , result [ :conf ] , "a benign namespace configuration" )
308309 end
309310
311+ [ :replicaset , :sidecar , :windows ] . each do |scenario |
312+ { unfiltered : nil , empty_filter : [ ] , namespace_filtered : [ "default" , "kube-system" ] } . each do |name , namespaces |
313+ define_method ( "test_prometheus_timeout_and_mapping_#{ scenario } _#{ name } " ) do
314+ body = "monitor_kubernetes_pods = true\n " \
315+ "interval = \" 45s\" \n " \
316+ "fieldpass = [\" requests_total\" ]\n " \
317+ "fielddrop = [\" debug_total\" ]\n " \
318+ "kubernetes_label_selector = \" app=metrics\" \n " \
319+ "kubernetes_field_selector = \" spec.nodeName=test-node\" "
320+ body += "\n monitor_kubernetes_pods_namespaces = #{ namespaces . inspect } " unless namespaces . nil?
321+
322+ result = run_parser ( scenario , configmap_for ( scenario , body ) )
323+ plugins = parse_generated_toml ( result [ :conf ] ) [ "inputs" ] [ "prometheus" ]
324+ monitored = plugins . select { |plugin | plugin [ "monitor_kubernetes_pods" ] }
325+ expected_namespaces = namespaces . nil? || namespaces . empty? ? [ nil ] : namespaces
326+ assert_equal expected_namespaces , monitored . map { |plugin | plugin [ "monitor_kubernetes_pods_namespace" ] }
327+
328+ monitored . each do |plugin |
329+ assert_equal "15s" , plugin [ "timeout" ] , "the overall scrape timeout must remain 15s"
330+ refute plugin . key? ( "response_timeout" ) , "response_timeout only bounds response headers in current Telegraf"
331+ assert_equal "45s" , plugin [ "interval" ]
332+ assert_equal 2 , plugin [ "metric_version" ]
333+ assert_equal "scrapeUrl" , plugin [ "url_tag" ]
334+ assert_equal "pod_namespace" , plugin [ "pod_namespace_label_name" ]
335+ assert_equal ( scenario == :replicaset ? "cluster" : "node" ) , plugin [ "pod_scrape_scope" ]
336+ assert_equal [ "requests_total" ] , plugin [ "fieldpass" ]
337+ assert_equal [ "debug_total" ] , plugin [ "fielddrop" ]
338+ assert_equal "app=metrics" , plugin [ "kubernetes_label_selector" ]
339+ assert_equal "spec.nodeName=test-node" , plugin [ "kubernetes_field_selector" ]
340+ end
341+
342+ if scenario == :windows
343+ plugins . each do |plugin |
344+ assert_equal "15s" , plugin [ "timeout" ] , "the base Windows plugin must also retain the overall timeout"
345+ refute plugin . key? ( "response_timeout" )
346+ end
347+ end
348+ end
349+ end
350+ end
351+
352+ def test_windows_rendered_configs_load_in_packaged_telegraf
353+ binary = ENV [ "TELEGRAF_WINDOWS_BINARY" ]
354+ skip "Set TELEGRAF_WINDOWS_BINARY to run the Windows binary config smoke test" if binary . nil? || binary . empty?
355+ assert File . file? ( binary ) , "TELEGRAF_WINDOWS_BINARY must point to telegraf.exe"
356+
357+ [ nil , [ ] , [ "default" , "kube-system" ] ] . each do |namespaces |
358+ body = "monitor_kubernetes_pods = true\n fieldpass = [\" requests_total\" ]\n fielddrop = [\" debug_total\" ]"
359+ body += "\n monitor_kubernetes_pods_namespaces = #{ namespaces . inspect } " unless namespaces . nil?
360+ conf = run_parser ( :windows , configmap_for ( :windows , body ) ) [ :conf ]
361+
362+ # This smoke test loads the actual generated options without contacting Kubernetes
363+ # or requiring a mounted service-account CA. --test never runs output plugins.
364+ conf = conf . gsub ( /^(\s *monitor_kubernetes_pods\s *=\s *)true[ \t ]*$/ ) { "#{ Regexp . last_match ( 1 ) } false" }
365+ conf = conf . gsub ( /^(\s *tls_ca\s *=\s *)"[^"]*"[ \t ]*$/ ) { "#{ Regexp . last_match ( 1 ) } \" \" " }
366+
367+ Dir . mktmpdir ( "windows-telegraf-config" ) do |dir |
368+ path = File . join ( dir , "telegraf.conf" )
369+ File . write ( path , conf )
370+ Open3 . popen3 ( { "NODE_IP" => "127.0.0.1" } , binary , "--console" , "--test" , "--config" , path ) do |stdin , stdout , stderr , process | # DevSkim: ignore DS162092 -- Loopback-only config smoke test.
371+ stdin . close
372+ readers = [ Thread . new { stdout . read } , Thread . new { stderr . read } ]
373+ completed = !process . join ( 20 ) . nil?
374+ Process . kill ( "KILL" , process . pid ) unless completed
375+ output = readers . map ( &:value ) . join ( "\n " )
376+ assert completed , "Telegraf config smoke test exceeded 20 seconds"
377+ assert process . value . success? , "Telegraf rejected the rendered config for #{ namespaces . inspect } : #{ output } "
378+ end
379+ end
380+ end
381+ end
382+
383+ def test_windows_process_metrics_config_preserves_pid_tags_and_fields
384+ path = File . join ( REPO_ROOT , "build/windows/installer/conf/telegraf-ama-logs-process-metrics.conf" )
385+ config = Tomlrb . load_file ( path )
386+ assert_equal ( { "telegraf_role" => "ama-logs-process-metrics" } , config [ "global_tags" ] )
387+ assert_equal 11 , config [ "inputs" ] [ "procstat" ] . length
388+ config [ "inputs" ] [ "procstat" ] . each do |plugin |
389+ assert_equal [ "pid" ] , plugin [ "tag_with" ]
390+ refute plugin . key? ( "pid_tag" )
391+ assert_equal [ "cpu_usage" , "memory_rss" ] , plugin [ "fieldpass" ]
392+ assert_equal "native" , plugin [ "pid_finder" ]
393+ assert_equal "agent_telemetry" , plugin [ "name_override" ]
394+ assert_equal "t.azm.ms/" , plugin [ "name_prefix" ]
395+ assert_equal "DaemonSet-Windows" , plugin [ "tags" ] [ "ControllerType" ]
396+ end
397+ assert_equal (
398+ { "ai.cloud.role" => "ControllerType" , "ai.cloud.roleInstance" => "PodName" } ,
399+ config [ "outputs" ] [ "application_insights" ] . first [ "context_tag_sources" ]
400+ )
401+ end
402+
310403 def test_selectors_are_escaped_in_generated_namespace_plugins
311404 body = "monitor_kubernetes_pods = true\n " \
312405 "monitor_kubernetes_pods_namespaces = [\" default\" ]\n " \
0 commit comments