11package network
22
33import (
4+ "os"
5+
46 . "github.com/onsi/ginkgo/v2"
57 . "github.com/onsi/gomega"
68
@@ -323,12 +325,13 @@ var _ = Describe("Network Ports", func() {
323325 },
324326 },
325327 }
326- ports := GetOutputPortsWithProtocols (outputs )
327- Expect (ports ).To (ConsistOf (
328- factory.PortProtocol {Port : 9200 , Protocol : corev1 .ProtocolTCP },
329- factory.PortProtocol {Port : 8088 , Protocol : corev1 .ProtocolTCP },
330- factory.PortProtocol {Port : 3100 , Protocol : corev1 .ProtocolTCP },
331- ))
328+ portMap := map [factory.PortProtocol ]bool {}
329+ GetOutputPortsWithProtocols (outputs , portMap )
330+ Expect (portMap ).To (Equal (map [factory.PortProtocol ]bool {
331+ {Port : 9200 , Protocol : corev1 .ProtocolTCP }: true ,
332+ {Port : 8088 , Protocol : corev1 .ProtocolTCP }: true ,
333+ {Port : 3100 , Protocol : corev1 .ProtocolTCP }: true ,
334+ }))
332335 })
333336
334337 It ("should deduplicate ports from multiple outputs" , func () {
@@ -352,11 +355,12 @@ var _ = Describe("Network Ports", func() {
352355 },
353356 },
354357 }
355- ports := GetOutputPortsWithProtocols (outputs )
356- Expect (ports ).To (ConsistOf (
357- factory.PortProtocol {Port : 9200 , Protocol : corev1 .ProtocolTCP },
358- factory.PortProtocol {Port : 8088 , Protocol : corev1 .ProtocolTCP },
359- ))
358+ portMap := map [factory.PortProtocol ]bool {}
359+ GetOutputPortsWithProtocols (outputs , portMap )
360+ Expect (portMap ).To (Equal (map [factory.PortProtocol ]bool {
361+ {Port : 9200 , Protocol : corev1 .ProtocolTCP }: true ,
362+ {Port : 8088 , Protocol : corev1 .ProtocolTCP }: true ,
363+ }))
360364 })
361365
362366 It ("should handle outputs with default ports" , func () {
@@ -374,11 +378,12 @@ var _ = Describe("Network Ports", func() {
374378 },
375379 },
376380 }
377- ports := GetOutputPortsWithProtocols (outputs )
378- Expect (ports ).To (ConsistOf (
379- factory.PortProtocol {Port : 9200 , Protocol : corev1 .ProtocolTCP },
380- factory.PortProtocol {Port : 80 , Protocol : corev1 .ProtocolTCP },
381- ))
381+ portMap := map [factory.PortProtocol ]bool {}
382+ GetOutputPortsWithProtocols (outputs , portMap )
383+ Expect (portMap ).To (Equal (map [factory.PortProtocol ]bool {
384+ {Port : 9200 , Protocol : corev1 .ProtocolTCP }: true ,
385+ {Port : 80 , Protocol : corev1 .ProtocolTCP }: true ,
386+ }))
382387 })
383388
384389 It ("should handle complex Kafka output with multiple brokers" , func () {
@@ -401,12 +406,13 @@ var _ = Describe("Network Ports", func() {
401406 },
402407 },
403408 }
404- ports := GetOutputPortsWithProtocols (outputs )
405- Expect (ports ).To (ConsistOf (
406- factory.PortProtocol {Port : 9092 , Protocol : corev1 .ProtocolTCP },
407- factory.PortProtocol {Port : 9093 , Protocol : corev1 .ProtocolTCP },
408- factory.PortProtocol {Port : 9200 , Protocol : corev1 .ProtocolTCP },
409- ))
409+ portMap := map [factory.PortProtocol ]bool {}
410+ GetOutputPortsWithProtocols (outputs , portMap )
411+ Expect (portMap ).To (Equal (map [factory.PortProtocol ]bool {
412+ {Port : 9092 , Protocol : corev1 .ProtocolTCP }: true ,
413+ {Port : 9093 , Protocol : corev1 .ProtocolTCP }: true ,
414+ {Port : 9200 , Protocol : corev1 .ProtocolTCP }: true ,
415+ }))
410416 })
411417 })
412418 })
@@ -557,4 +563,163 @@ var _ = Describe("Network Ports", func() {
557563 })
558564 })
559565 })
566+
567+ Describe ("GetProxyPorts" , func () {
568+ var originalEnvVars map [string ]string
569+
570+ BeforeEach (func () {
571+ // Save original environment variables
572+ originalEnvVars = make (map [string ]string )
573+ for _ , envVar := range []string {"HTTP_PROXY" , "HTTPS_PROXY" , "http_proxy" , "https_proxy" , "NO_PROXY" , "no_proxy" } {
574+ originalEnvVars [envVar ] = os .Getenv (envVar )
575+ os .Unsetenv (envVar ) // Clear all proxy env vars for clean test state
576+ }
577+ })
578+
579+ AfterEach (func () {
580+ // Restore original environment variables
581+ for envVar , value := range originalEnvVars {
582+ if value != "" {
583+ os .Setenv (envVar , value )
584+ } else {
585+ os .Unsetenv (envVar )
586+ }
587+ }
588+ })
589+
590+ DescribeTable ("when proxy environment variables are set" ,
591+ func (envVars map [string ]string , expectedPorts []factory.PortProtocol ) {
592+ for key , value := range envVars {
593+ os .Setenv (key , value )
594+ }
595+
596+ portMap := map [factory.PortProtocol ]bool {}
597+ GetProxyPorts (portMap )
598+ ports := make ([]factory.PortProtocol , 0 , len (portMap ))
599+ for pp := range portMap {
600+ ports = append (ports , pp )
601+ }
602+ if len (expectedPorts ) == 0 {
603+ Expect (ports ).To (BeEmpty ())
604+ } else {
605+ Expect (ports ).To (ConsistOf (expectedPorts ))
606+ }
607+ },
608+ Entry ("should extract ports from HTTP proxy URLs with explicit ports" ,
609+ map [string ]string {
610+ "http_proxy" : "http://proxy.example.com:8080" ,
611+ "https_proxy" : "https://proxy.example.com:8443" ,
612+ },
613+ []factory.PortProtocol {
614+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
615+ {Port : 8443 , Protocol : corev1 .ProtocolTCP },
616+ },
617+ ),
618+ Entry ("should use default ports when proxy URLs don't specify ports" ,
619+ map [string ]string {
620+ "http_proxy" : "http://proxy.example.com" ,
621+ "https_proxy" : "https://proxy.example.com" ,
622+ },
623+ []factory.PortProtocol {
624+ {Port : 80 , Protocol : corev1 .ProtocolTCP },
625+ {Port : 443 , Protocol : corev1 .ProtocolTCP },
626+ },
627+ ),
628+ Entry ("should return empty when proxy URLs have unknown schemes without ports" ,
629+ map [string ]string {
630+ "http_proxy" : "proxy://proxy.example.com" ,
631+ },
632+ []factory.PortProtocol {},
633+ ),
634+ Entry ("should deduplicate identical proxy ports" ,
635+ map [string ]string {
636+ "http_proxy" : "http://proxy.example.com:8080" ,
637+ "https_proxy" : "https://proxy.example.com:8080" ,
638+ },
639+ []factory.PortProtocol {
640+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
641+ },
642+ ),
643+ Entry ("should handle malformed proxy URLs gracefully" ,
644+ map [string ]string {
645+ "http_proxy" : "not-a-valid-url" ,
646+ "https_proxy" : "http://proxy.example.com:8080" ,
647+ },
648+ []factory.PortProtocol {
649+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
650+ },
651+ ),
652+ Entry ("should handle uppercase proxy environment variables" ,
653+ map [string ]string {
654+ "HTTP_PROXY" : "http://proxy.example.com:3128" ,
655+ "HTTPS_PROXY" : "https://proxy.example.com:3129" ,
656+ },
657+ []factory.PortProtocol {
658+ {Port : 3128 , Protocol : corev1 .ProtocolTCP },
659+ {Port : 3129 , Protocol : corev1 .ProtocolTCP },
660+ },
661+ ),
662+ Entry ("should extract explicitly specified standard ports from proxy URLs" ,
663+ map [string ]string {
664+ "http_proxy" : "http://proxy.example.com:80" ,
665+ "https_proxy" : "https://proxy.example.com:443" ,
666+ },
667+ []factory.PortProtocol {
668+ {Port : 80 , Protocol : corev1 .ProtocolTCP },
669+ {Port : 443 , Protocol : corev1 .ProtocolTCP },
670+ },
671+ ),
672+ Entry ("should extract explicit ports when both proxy and no-proxy environment variables are set" ,
673+ map [string ]string {
674+ "http_proxy" : "http://proxy.example.com:8080" ,
675+ "https_proxy" : "https://proxy.example.com:8080" ,
676+ "no_proxy" : "localhost,127.0.0.1" ,
677+ },
678+ []factory.PortProtocol {
679+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
680+ },
681+ ),
682+ Entry ("should handle proxy URLs with authentication" ,
683+ map [string ]string {
684+ "http_proxy" :
"http://user:[email protected] :8080" ,
685+ },
686+ []factory.PortProtocol {
687+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
688+ },
689+ ),
690+ Entry ("should handle IPv6 proxy URLs" ,
691+ map [string ]string {
692+ "http_proxy" : "http://[::1]:8080" ,
693+ },
694+ []factory.PortProtocol {
695+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
696+ },
697+ ),
698+ Entry ("should handle empty string proxy URLs gracefully" ,
699+ map [string ]string {
700+ "http_proxy" : "" ,
701+ "https_proxy" : "https://proxy.example.com:8443" ,
702+ },
703+ []factory.PortProtocol {
704+ {Port : 8443 , Protocol : corev1 .ProtocolTCP },
705+ },
706+ ),
707+ Entry ("should handle mix of uppercase and lowercase proxy environment variables" ,
708+ map [string ]string {
709+ "HTTP_PROXY" : "http://proxy.example.com:8080" ,
710+ "https_proxy" : "https://proxy.example.com:8443" ,
711+ },
712+ []factory.PortProtocol {
713+ {Port : 8080 , Protocol : corev1 .ProtocolTCP },
714+ {Port : 8443 , Protocol : corev1 .ProtocolTCP },
715+ },
716+ ),
717+ )
718+
719+ It ("should be empty portMap when no proxy environment variables are set" , func () {
720+ portMap := map [factory.PortProtocol ]bool {}
721+ GetProxyPorts (portMap )
722+ Expect (portMap ).To (BeEmpty ())
723+ })
724+ })
560725})
0 commit comments