Skip to content

Commit 738c6e0

Browse files
Copilotillume
andcommitted
Fix lint errors: Split test and extract helper function
- Split TestValidateAPIServerEndpoint into two functions: - TestValidateAPIServerEndpoint_ValidCases (37 lines) - TestValidateAPIServerEndpoint_InvalidCases (40 lines) - Extract setupInClusterContext helper to reduce nesting complexity - Reduces if config.UseInCluster block from complexity 6 to 1 - All tests still pass (14 validation tests + integration test) - Backend linter passes with no errors Addresses lint errors: funlen and nestif. Co-authored-by: illume <9541+illume@users.noreply.github.com>
1 parent 2007e3d commit 738c6e0

File tree

2 files changed

+63
-65
lines changed

2 files changed

+63
-65
lines changed

backend/cmd/headlamp.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,29 +428,7 @@ func createHeadlampHandler(config *HeadlampConfig) http.Handler {
428428

429429
// In-cluster
430430
if config.UseInCluster {
431-
context, err := kubeconfig.GetInClusterContext(
432-
config.InClusterContextName,
433-
config.OidcIdpIssuerURL,
434-
config.OidcClientID, config.OidcClientSecret,
435-
strings.Join(config.OidcScopes, ","),
436-
config.OidcSkipTLSVerify,
437-
config.OidcCACert,
438-
config.APIServerEndpoint)
439-
if err != nil {
440-
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
441-
} else {
442-
context.Source = kubeconfig.InCluster
443-
444-
err = context.SetupProxy()
445-
if err != nil {
446-
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
447-
}
448-
449-
err = config.KubeConfigStore.AddContext(context)
450-
if err != nil {
451-
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
452-
}
453-
}
431+
setupInClusterContext(config)
454432
}
455433

456434
if config.StaticDir != "" {
@@ -1095,6 +1073,34 @@ func (c *HeadlampConfig) OIDCTokenRefreshMiddleware(next http.Handler) http.Hand
10951073
})
10961074
}
10971075

1076+
// setupInClusterContext sets up the in-cluster Kubernetes context.
1077+
func setupInClusterContext(config *HeadlampConfig) {
1078+
context, err := kubeconfig.GetInClusterContext(
1079+
config.InClusterContextName,
1080+
config.OidcIdpIssuerURL,
1081+
config.OidcClientID, config.OidcClientSecret,
1082+
strings.Join(config.OidcScopes, ","),
1083+
config.OidcSkipTLSVerify,
1084+
config.OidcCACert,
1085+
config.APIServerEndpoint)
1086+
if err != nil {
1087+
logger.Log(logger.LevelError, nil, err, "Failed to get in-cluster context")
1088+
return
1089+
}
1090+
1091+
context.Source = kubeconfig.InCluster
1092+
1093+
err = context.SetupProxy()
1094+
if err != nil {
1095+
logger.Log(logger.LevelError, nil, err, "Failed to setup proxy for in-cluster context")
1096+
}
1097+
1098+
err = config.KubeConfigStore.AddContext(context)
1099+
if err != nil {
1100+
logger.Log(logger.LevelError, nil, err, "Failed to add in-cluster context")
1101+
}
1102+
}
1103+
10981104
func StartHeadlampServer(config *HeadlampConfig) {
10991105
tel, err := telemetry.NewTelemetry(config.TelemetryConfig)
11001106
if err != nil {

backend/pkg/kubeconfig/kubeconfig_test.go

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -827,114 +827,106 @@ func TestHandleConfigLoadError(t *testing.T) {
827827
}
828828
}
829829

830-
func TestValidateAPIServerEndpoint(t *testing.T) {
830+
func TestValidateAPIServerEndpoint_ValidCases(t *testing.T) {
831831
tests := []struct {
832-
name string
833-
endpoint string
834-
wantResult string
835-
wantErr bool
836-
errContains string
832+
name string
833+
endpoint string
834+
wantResult string
837835
}{
838836
{
839837
name: "empty string returns empty",
840838
endpoint: "",
841839
wantResult: "",
842-
wantErr: false,
843840
},
844841
{
845842
name: "whitespace only returns empty",
846843
endpoint: " \t\n ",
847844
wantResult: "",
848-
wantErr: false,
849845
},
850846
{
851847
name: "valid https URL",
852848
endpoint: "https://kube-oidc-proxy.example.com:443",
853849
wantResult: "https://kube-oidc-proxy.example.com:443",
854-
wantErr: false,
855850
},
856851
{
857852
name: "valid https URL with whitespace is trimmed",
858853
endpoint: " https://kube-oidc-proxy.example.com:443 ",
859854
wantResult: "https://kube-oidc-proxy.example.com:443",
860-
wantErr: false,
861855
},
856+
{
857+
name: "URL with root path is allowed",
858+
endpoint: "https://proxy.example.com:443/",
859+
wantResult: "https://proxy.example.com:443/",
860+
},
861+
{
862+
name: "URL without port",
863+
endpoint: "https://proxy.example.com",
864+
wantResult: "https://proxy.example.com",
865+
},
866+
}
867+
868+
for _, tt := range tests {
869+
t.Run(tt.name, func(t *testing.T) {
870+
result, err := kubeconfig.ValidateAPIServerEndpoint(tt.endpoint)
871+
require.NoError(t, err)
872+
assert.Equal(t, tt.wantResult, result)
873+
})
874+
}
875+
}
876+
877+
func TestValidateAPIServerEndpoint_InvalidCases(t *testing.T) {
878+
tests := []struct {
879+
name string
880+
endpoint string
881+
errContains string
882+
}{
862883
{
863884
name: "http URL is rejected",
864885
endpoint: "http://insecure-proxy.example.com:443",
865-
wantErr: true,
866886
errContains: "must be a full https:// URL",
867887
},
868888
{
869889
name: "missing scheme is rejected",
870890
endpoint: "kube-oidc-proxy.example.com:443",
871-
wantErr: true,
872891
errContains: "must be an absolute URL with scheme and host",
873892
},
874893
{
875894
name: "relative URL is rejected",
876895
endpoint: "/path/to/proxy",
877-
wantErr: true,
878896
errContains: "must be an absolute URL with scheme and host",
879897
},
880898
{
881899
name: "URL with embedded credentials is rejected",
882900
endpoint: "https://user:password@proxy.example.com:443",
883-
wantErr: true,
884901
errContains: "must not include user info (credentials)",
885902
},
886903
{
887904
name: "URL with empty hostname is rejected",
888905
endpoint: "https://:443",
889-
wantErr: true,
890906
errContains: "must be an absolute URL with scheme and host",
891907
},
892908
{
893909
name: "URL with query string is rejected",
894910
endpoint: "https://proxy.example.com:443?token=secret",
895-
wantErr: true,
896911
errContains: "must not include a query string",
897912
},
898913
{
899914
name: "URL with fragment is rejected",
900915
endpoint: "https://proxy.example.com:443#section",
901-
wantErr: true,
902916
errContains: "must not include a fragment",
903917
},
904918
{
905919
name: "URL with path is rejected",
906920
endpoint: "https://proxy.example.com:443/api/v1",
907-
wantErr: true,
908921
errContains: "path must be empty or '/'",
909922
},
910-
{
911-
name: "URL with root path is allowed",
912-
endpoint: "https://proxy.example.com:443/",
913-
wantResult: "https://proxy.example.com:443/",
914-
wantErr: false,
915-
},
916-
{
917-
name: "URL without port",
918-
endpoint: "https://proxy.example.com",
919-
wantResult: "https://proxy.example.com",
920-
wantErr: false,
921-
},
922923
}
923924

924925
for _, tt := range tests {
925926
t.Run(tt.name, func(t *testing.T) {
926-
result, err := kubeconfig.ValidateAPIServerEndpoint(tt.endpoint)
927-
928-
if tt.wantErr {
929-
require.Error(t, err)
930-
931-
if tt.errContains != "" {
932-
assert.Contains(t, err.Error(), tt.errContains)
933-
}
934-
} else {
935-
require.NoError(t, err)
936-
assert.Equal(t, tt.wantResult, result)
937-
}
927+
_, err := kubeconfig.ValidateAPIServerEndpoint(tt.endpoint)
928+
require.Error(t, err)
929+
assert.Contains(t, err.Error(), tt.errContains)
938930
})
939931
}
940932
}

0 commit comments

Comments
 (0)