@@ -75,11 +75,11 @@ func TestExtractRegistryHost(t *testing.T) {
7575
7676func TestExtractCredentials (t * testing.T ) {
7777 tests := []struct {
78- name string
79- authConfig * DockerAuthConfig
80- expectedUser string
81- expectedPass string
82- expectError bool
78+ name string
79+ authConfig * DockerAuthConfig
80+ expectedUser string
81+ expectedPass string
82+ expectError bool
8383 }{
8484 {
8585 name : "explicit username and password" ,
@@ -327,7 +327,10 @@ func TestExtractRegistryCredentials(t *testing.T) {
327327 },
328328 },
329329 }
330- data , _ := json .Marshal (config )
330+ data , err := json .Marshal (config )
331+ if err != nil {
332+ t .Fatalf ("failed to marshal config: %v" , err )
333+ }
331334 return data
332335 }
333336
@@ -339,16 +342,19 @@ func TestExtractRegistryCredentials(t *testing.T) {
339342 Password : password ,
340343 },
341344 }
342- data , _ := json .Marshal (config )
345+ data , err := json .Marshal (config )
346+ if err != nil {
347+ t .Fatalf ("failed to marshal config: %v" , err )
348+ }
343349 return data
344350 }
345351
346352 tests := []struct {
347- name string
348- secret * corev1.Secret
349- imageURL string
350- expectError bool
351- validateResult bool
353+ name string
354+ secret * corev1.Secret
355+ imageURL string
356+ expectError bool
357+ validateResult bool
352358 }{
353359 {
354360 name : "dockerconfigjson secret with exact match" ,
@@ -508,6 +514,10 @@ func TestFindAuthConfig(t *testing.T) {
508514 Username : "user4" ,
509515 Password : "pass4" ,
510516 },
517+ "registry.local:5000" : {
518+ Username : "user5" ,
519+ Password : "pass5" ,
520+ },
511521 }
512522
513523 tests := []struct {
@@ -535,16 +545,33 @@ func TestFindAuthConfig(t *testing.T) {
535545 expectedUser : "user3" ,
536546 },
537547 {
538- name : "Docker Hub special case" ,
548+ name : "Docker Hub special case - docker.io " ,
539549 registryHost : "docker.io" ,
540550 expectFound : true ,
541551 expectedUser : "user4" ,
542552 },
543553 {
544- name : "not found" ,
554+ name : "Docker Hub special case - index.docker.io" ,
555+ registryHost : "index.docker.io" ,
556+ expectFound : true ,
557+ expectedUser : "user4" ,
558+ },
559+ {
560+ name : "registry with port - exact match" ,
561+ registryHost : "registry.local:5000" ,
562+ expectFound : true ,
563+ expectedUser : "user5" ,
564+ },
565+ {
566+ name : "missing registry entry" ,
545567 registryHost : "notfound.registry.com" ,
546568 expectFound : false ,
547569 },
570+ {
571+ name : "missing registry with port" ,
572+ registryHost : "notfound.registry.com:8080" ,
573+ expectFound : false ,
574+ },
548575 }
549576
550577 for _ , tt := range tests {
@@ -554,6 +581,9 @@ func TestFindAuthConfig(t *testing.T) {
554581 if err == nil {
555582 t .Errorf ("expected error but got none" )
556583 }
584+ if ! strings .Contains (err .Error (), "not found in auth config" ) {
585+ t .Errorf ("expected 'not found in auth config' error, got: %v" , err )
586+ }
557587 return
558588 }
559589 if err != nil {
@@ -571,3 +601,129 @@ func TestFindAuthConfig(t *testing.T) {
571601 }
572602}
573603
604+ // TestFindAuthConfigCornerCases tests specific corner cases for registry matching.
605+ func TestFindAuthConfigCornerCases (t * testing.T ) {
606+ tests := []struct {
607+ name string
608+ auths map [string ]DockerAuthConfig
609+ registryHost string
610+ expectFound bool
611+ expectedUser string
612+ }{
613+ {
614+ name : "registry with port in both" ,
615+ auths : map [string ]DockerAuthConfig {
616+ "registry.example.com:5000" : {Username : "portuser" , Password : "portpass" },
617+ },
618+ registryHost : "registry.example.com:5000" ,
619+ expectFound : true ,
620+ expectedUser : "portuser" ,
621+ },
622+ {
623+ name : "registry with http prefix in config" ,
624+ auths : map [string ]DockerAuthConfig {
625+ "http://registry.local" : {Username : "httpuser" , Password : "httppass" },
626+ },
627+ registryHost : "registry.local" ,
628+ expectFound : true ,
629+ expectedUser : "httpuser" ,
630+ },
631+ {
632+ name : "registry with https and /v1/ suffix" ,
633+ auths : map [string ]DockerAuthConfig {
634+ "https://registry.example.com/v1/" : {Username : "v1user" , Password : "v1pass" },
635+ },
636+ registryHost : "registry.example.com" ,
637+ expectFound : true ,
638+ expectedUser : "v1user" ,
639+ },
640+ {
641+ name : "quay.io exact match" ,
642+ auths : map [string ]DockerAuthConfig {
643+ "quay.io" : {Username : "quayuser" , Password : "quaypass" },
644+ },
645+ registryHost : "quay.io" ,
646+ expectFound : true ,
647+ expectedUser : "quayuser" ,
648+ },
649+ {
650+ name : "quay.io with https" ,
651+ auths : map [string ]DockerAuthConfig {
652+ "https://quay.io" : {Username : "quayuser" , Password : "quaypass" },
653+ },
654+ registryHost : "quay.io" ,
655+ expectFound : true ,
656+ expectedUser : "quayuser" ,
657+ },
658+ {
659+ name : "gcr.io exact match" ,
660+ auths : map [string ]DockerAuthConfig {
661+ "gcr.io" : {Username : "gcruser" , Password : "gcrpass" },
662+ },
663+ registryHost : "gcr.io" ,
664+ expectFound : true ,
665+ expectedUser : "gcruser" ,
666+ },
667+ {
668+ name : "Docker Hub - index.docker.io legacy" ,
669+ auths : map [string ]DockerAuthConfig {
670+ "https://index.docker.io/v1/" : {Username : "dockeruser" , Password : "dockerpass" },
671+ },
672+ registryHost : "docker.io" ,
673+ expectFound : true ,
674+ expectedUser : "dockeruser" ,
675+ },
676+ {
677+ name : "Docker Hub - docker.io in config" ,
678+ auths : map [string ]DockerAuthConfig {
679+ "docker.io" : {Username : "dockeruser" , Password : "dockerpass" },
680+ },
681+ registryHost : "index.docker.io" ,
682+ expectFound : true ,
683+ expectedUser : "dockeruser" ,
684+ },
685+ {
686+ name : "custom registry with port" ,
687+ auths : map [string ]DockerAuthConfig {
688+ "https://registry.local:8443" : {Username : "customuser" , Password : "custompass" },
689+ },
690+ registryHost : "registry.local:8443" ,
691+ expectFound : true ,
692+ expectedUser : "customuser" ,
693+ },
694+ {
695+ name : "registry not in config - clear error" ,
696+ auths : map [string ]DockerAuthConfig {
697+ "other.registry.com" : {Username : "otheruser" , Password : "otherpass" },
698+ },
699+ registryHost : "missing.registry.com" ,
700+ expectFound : false ,
701+ },
702+ }
703+
704+ for _ , tt := range tests {
705+ t .Run (tt .name , func (t * testing.T ) {
706+ authConfig , err := findAuthConfig (tt .auths , tt .registryHost )
707+ if ! tt .expectFound {
708+ if err == nil {
709+ t .Errorf ("expected RegistryEntryMissing error but got none" )
710+ }
711+ if ! strings .Contains (err .Error (), "not found in auth config" ) {
712+ t .Errorf ("expected clear 'not found in auth config' error, got: %v" , err )
713+ }
714+ return
715+ }
716+ if err != nil {
717+ t .Errorf ("unexpected error: %v" , err )
718+ return
719+ }
720+ if authConfig == nil {
721+ t .Errorf ("expected auth config but got nil" )
722+ return
723+ }
724+ if authConfig .Username != tt .expectedUser {
725+ t .Errorf ("expected username %q, got %q" , tt .expectedUser , authConfig .Username )
726+ }
727+ })
728+ }
729+ }
0 commit comments