diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-11-18 08:32:06 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-11-18 08:32:06 +0300 |
commit | b0a0d337d5870792760c5f4fc8291473425285a0 (patch) | |
tree | 5e6e96c22991414abecc0d28f800aaceb77f9bd5 | |
parent | 1a00719667fdd852727b8fcbad67f722296fa5f7 (diff) | |
parent | b6b26c278a138ec5e6bd59cc305879c3d4332ce4 (diff) |
Merge branch 'test/config-validation' into 'master'
test: add config validation tests
See merge request gitlab-org/gitlab-pages!579
-rw-r--r-- | internal/config/validate.go | 32 | ||||
-rw-r--r-- | internal/config/validate_test.go | 145 |
2 files changed, 164 insertions, 13 deletions
diff --git a/internal/config/validate.go b/internal/config/validate.go index 3831b45b..32238786 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -9,6 +9,17 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/config/tls" ) +var ( + ErrNoListener = errors.New("no listener defined, please specify at least one --listen-* flag") + ErrAuthNoSecret = errors.New("auth-secret must be defined if authentication is supported") + ErrAuthNoClientID = errors.New("auth-client-id must be defined if authentication is supported") + ErrAuthNoClientSecret = errors.New("auth-client-secret must be defined if authentication is supported") + ErrAuthNoGitlabServer = errors.New("gitlab-server must be defined if authentication is supported") + ErrAuthNoRedirect = errors.New("auth-redirect-uri must be defined if authentication is supported") + ErrArtifactsServerUnsupportedScheme = errors.New("artifacts-server scheme must be either http:// or https://") + ErrArtifactsServerInvalidTimeout = errors.New("artifacts-server-timeout must be greater than or equal to 1") +) + // Validate values populated in Config func Validate(config *Config) error { if err := validateListeners(config); err != nil { @@ -31,7 +42,7 @@ func validateListeners(config *Config) error { config.ListenHTTPSStrings.Len() == 0 && config.ListenHTTPSProxyv2Strings.Len() == 0 && config.ListenProxyStrings.Len() == 0 { - return errors.New("no listener defined, please specify at least one --listen-* flag") + return ErrNoListener } return nil @@ -45,24 +56,19 @@ func validateAuthConfig(config *Config) error { var result *multierror.Error if config.Authentication.Secret == "" { - err := errors.New("auth-secret must be defined if authentication is supported") - result = multierror.Append(result, err) + result = multierror.Append(result, ErrAuthNoSecret) } if config.Authentication.ClientID == "" { - err := errors.New("auth-client-id must be defined if authentication is supported") - result = multierror.Append(result, err) + result = multierror.Append(result, ErrAuthNoClientID) } if config.Authentication.ClientSecret == "" { - err := errors.New("auth-client-secret must be defined if authentication is supported") - result = multierror.Append(result, err) + result = multierror.Append(result, ErrAuthNoClientSecret) } if config.GitLab.PublicServer == "" { - err := errors.New("gitlab-server must be defined if authentication is supported") - result = multierror.Append(result, err) + result = multierror.Append(result, ErrAuthNoGitlabServer) } if config.Authentication.RedirectURI == "" { - err := errors.New("auth-redirect-uri must be defined if authentication is supported") - result = multierror.Append(result, err) + result = multierror.Append(result, ErrAuthNoRedirect) } return result.ErrorOrNil() } @@ -78,11 +84,11 @@ func validateArtifactsServerConfig(config *Config) error { } // url.Parse ensures that the Scheme attribute is always lower case. if u.Scheme != "http" && u.Scheme != "https" { - return errors.New("artifacts-server scheme must be either http:// or https://") + return ErrArtifactsServerUnsupportedScheme } if config.ArtifactsServer.TimeoutSeconds < 1 { - return errors.New("artifacts-server-timeout must be greater than or equal to 1") + return ErrArtifactsServerInvalidTimeout } return nil diff --git a/internal/config/validate_test.go b/internal/config/validate_test.go new file mode 100644 index 00000000..e9aca9dc --- /dev/null +++ b/internal/config/validate_test.go @@ -0,0 +1,145 @@ +package config + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConfigValidate(t *testing.T) { + tests := []struct { + name string + cfg func(*Config) + expectedErr error + }{ + { + name: "no_listeners", + cfg: noListeners, + expectedErr: ErrNoListener, + }, + { + name: "no_auth", + cfg: noAuth, + }, + { + name: "auth_no_secret", + cfg: authNoSecret, + expectedErr: ErrAuthNoSecret, + }, + { + name: "auth_no_client_id", + cfg: authNoClientID, + expectedErr: ErrAuthNoClientID, + }, + { + name: "auth_no_client_secret", + cfg: authNoClientSecret, + expectedErr: ErrAuthNoClientSecret, + }, + { + name: "auth_no_gitlab_Server", + cfg: authNoPublicServer, + expectedErr: ErrAuthNoGitlabServer, + }, + { + name: "auth_no_redirect", + cfg: authNoRedirect, + expectedErr: ErrAuthNoRedirect, + }, + { + name: "artifact_no_url", + cfg: artifactsNoURL, + }, + { + name: "artifact_malformed_scheme", + cfg: artifactsMalformedScheme, + expectedErr: ErrArtifactsServerUnsupportedScheme, + }, + { + name: "artifact_invalid_timeout", + cfg: artifactsInvalidTimeout, + expectedErr: ErrArtifactsServerInvalidTimeout, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := validConfig() + tt.cfg(&cfg) + + err := Validate(&cfg) + if tt.expectedErr != nil { + require.True(t, errors.Is(err, tt.expectedErr)) + } else { + require.NoError(t, err) + } + }) + } +} + +func noListeners(cfg *Config) { + cfg.ListenHTTPStrings = MultiStringFlag{separator: ","} + cfg.ListenHTTPSStrings = MultiStringFlag{separator: ","} + cfg.ListenProxyStrings = MultiStringFlag{separator: ","} + cfg.ListenHTTPSProxyv2Strings = MultiStringFlag{separator: ","} +} + +func noAuth(cfg *Config) { + cfg.Authentication = Auth{} +} + +func authNoSecret(cfg *Config) { + cfg.Authentication.Secret = "" +} + +func authNoClientID(cfg *Config) { + cfg.Authentication.ClientID = "" +} + +func authNoClientSecret(cfg *Config) { + cfg.Authentication.ClientSecret = "" +} + +func authNoPublicServer(cfg *Config) { + cfg.GitLab.PublicServer = "" +} + +func authNoRedirect(cfg *Config) { + cfg.Authentication.RedirectURI = "" +} + +func artifactsNoURL(cfg *Config) { + cfg.ArtifactsServer.URL = "" +} + +func artifactsMalformedScheme(cfg *Config) { + cfg.ArtifactsServer.URL = "foo://example.com" +} + +func artifactsInvalidTimeout(cfg *Config) { + cfg.ArtifactsServer.TimeoutSeconds = -1 +} + +func validConfig() Config { + cfg := Config{ + ListenHTTPStrings: MultiStringFlag{ + value: []string{"127.0.0.1:80"}, + separator: ",", + }, + ArtifactsServer: ArtifactsServer{ + URL: "https://example.com", + TimeoutSeconds: 1, + }, + Authentication: Auth{ + Secret: "foo", + ClientID: "bar", + ClientSecret: "bar-secret", + RedirectURI: "https://example.com", + }, + GitLab: GitLab{ + PublicServer: "https://gitlab.example.com", + }, + } + + return cfg +} |