test(config,git,logger): add some test functions
Some checks failed
ci/woodpecker/push/lint Pipeline failed
ci/woodpecker/push/test unknown status
ci/woodpecker/push/build unknown status
ci/woodpecker/push/deploy unknown status

This commit is contained in:
Tom Neuber 2024-12-06 23:56:18 +01:00
parent 2e41d3d004
commit b22d060cc6
Signed by: tom
GPG key ID: F17EFE4272D89FF6
16 changed files with 419 additions and 25 deletions

View file

@ -7,6 +7,15 @@ import (
"github.com/rs/zerolog"
)
var (
ErrInvalidGrafanaURL = errors.New("invalid grafana URL, must not be blank")
ErrInvalidAuthToken = errors.New("invalid auth token for grafana, must not be blank")
ErrInvalidRepoURL = errors.New("invalid repo url for git, must not be blank")
ErrInvalidGitUser = errors.New("invalid username for git, must not be blank")
ErrInvalidGitPass = errors.New("invalid password for git, must not be blank")
ErrInvalidBranchName = errors.New("invalid branch name for git, must not be blank")
)
type Config struct {
Debug bool
ForceCommits bool
@ -29,27 +38,27 @@ func (c *Config) Validate() []error {
var errs []error
if c.GrafanaURL == "" {
errs = append(errs, errors.New("invalid grafana URL, must not be blank"))
errs = append(errs, ErrInvalidGrafanaURL)
}
if c.GrafanaToken == "" {
errs = append(errs, errors.New("invalid auth token for grafana, must not be blank"))
errs = append(errs, ErrInvalidAuthToken)
}
if c.GitRepo == "" {
errs = append(errs, errors.New("invalid repo url for git, must not be blank"))
errs = append(errs, ErrInvalidRepoURL)
}
if c.GitUser == "" {
errs = append(errs, errors.New("invalid username for git, must not be blank"))
errs = append(errs, ErrInvalidGitUser)
}
if c.GitPass == "" {
errs = append(errs, errors.New("invalid password for git, must not be blank"))
errs = append(errs, ErrInvalidGitPass)
}
if c.GitBranch == "" {
errs = append(errs, errors.New("invalid branch name for git, must not be blank"))
errs = append(errs, ErrInvalidBranchName)
}
return errs

View file

@ -0,0 +1,52 @@
package config_test
import (
"testing"
"git.ar21.de/yolokube/grafana-backuper/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_Validate_MissingFields(t *testing.T) {
cfg := config.Config{}
errs := cfg.Validate()
assert.Len(t, errs, 6) // Expecting 6 errors since all required fields are missing
require.ErrorIs(t, errs[0], config.ErrInvalidGrafanaURL)
require.ErrorIs(t, errs[1], config.ErrInvalidAuthToken)
require.ErrorIs(t, errs[2], config.ErrInvalidRepoURL)
require.ErrorIs(t, errs[3], config.ErrInvalidGitUser)
require.ErrorIs(t, errs[4], config.ErrInvalidGitPass)
require.ErrorIs(t, errs[5], config.ErrInvalidBranchName)
}
func TestConfig_Validate_AllFieldsPresent(t *testing.T) {
cfg := config.Config{
GrafanaURL: "http://grafana.example.com",
GrafanaToken: "sometoken",
GitRepo: "https://github.com/user/repo",
GitUser: "username",
GitPass: "password",
GitBranch: "main",
}
errs := cfg.Validate()
assert.Empty(t, errs) // No errors should be returned when all fields are valid
}
func TestConfig_Validate_PartiallyPopulated(t *testing.T) {
cfg := config.Config{
GrafanaURL: "http://grafana.example.com",
GitRepo: "https://github.com/user/repo",
GitUser: "username",
}
errs := cfg.Validate()
assert.Len(t, errs, 3) // Expecting 3 errors for missing GrafanaToken, GitPass, and GitBranch
require.ErrorIs(t, errs[0], config.ErrInvalidGrafanaURL)
require.ErrorIs(t, errs[1], config.ErrInvalidGitPass)
require.ErrorIs(t, errs[2], config.ErrInvalidBranchName)
}