add config tests

This commit is contained in:
Massaki Archambault 2024-06-08 01:02:35 -04:00
parent 96f250fc47
commit 65d3a00fa3
4 changed files with 193 additions and 7 deletions

View File

@ -14,6 +14,12 @@ gitlab:
# Default to anonymous (only public projects will be visible). # Default to anonymous (only public projects will be visible).
#token: #token:
# Must be set to either "http" or "ssh".
# The protocol to configure the git remote on.
# "http" may not work on private repos unless a credential manager is configured
# If possible, prefer "ssh" over "http"
pull_method: http
# A list of the group ids to expose their projects in the filesystem. # A list of the group ids to expose their projects in the filesystem.
group_ids: group_ids:
- 9970 # gitlab-org - 9970 # gitlab-org
@ -32,12 +38,6 @@ git:
# The name of the remote in the local clone. # The name of the remote in the local clone.
remote: origin remote: origin
# Must be set to either "http" or "ssh".
# The protocol to configure the git remote on.
# "http" may not work on private repos unless a credential manager is configured
# If possible, prefer "ssh" over "http"
pull_method: http
# Must be set to either "init", or "clone". # Must be set to either "init", or "clone".
# If set to "init", the local copy will be initialized with `git init` and the remote is configured manually. The git server is nerver queried. (fast) # If set to "init", the local copy will be initialized with `git init` and the remote is configured manually. The git server is nerver queried. (fast)
# If set to "clone", the local copy will be initialized with `git clone`. (slow) # If set to "clone", the local copy will be initialized with `git clone`. (slow)

22
config/config.test.yaml Normal file
View File

@ -0,0 +1,22 @@
fs:
mountpoint: /tmp/gitlabfs/test/mnt
mountoptions: nodev
gitlab:
url: https://example.com
token: "12345"
group_ids:
- 123
user_ids:
- 456
include_current_user: true
pull_method: ssh
git:
clone_location: /tmp/gitlabfs/test/clone
remote: origin
on_clone: clone
auto_pull: false
depth: 0
queue_size: 100
worker_count: 1

View File

@ -38,10 +38,10 @@ func LoadConfig(configPath string) (*Config, error) {
Gitlab: gitlab.GitlabClientConfig{ Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com", URL: "https://gitlab.com",
Token: "", Token: "",
PullMethod: "http",
GroupIDs: []int{9970}, GroupIDs: []int{9970},
UserIDs: []int{}, UserIDs: []int{},
IncludeCurrentUser: true, IncludeCurrentUser: true,
PullMethod: "http",
}, },
Git: git.GitClientParam{ Git: git.GitClientParam{
CloneLocation: defaultCloneLocation, CloneLocation: defaultCloneLocation,

164
config/loader_test.go Normal file
View File

@ -0,0 +1,164 @@
package config_test
import (
"reflect"
"testing"
"github.com/badjware/gitlabfs/config"
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/platforms/gitlab"
)
func TestLoadConfig(t *testing.T) {
tests := map[string]struct {
input string
expected *config.Config
}{
"LoadConfig": {
input: "config.test.yaml",
expected: &config.Config{
FS: config.FSConfig{
Mountpoint: "/tmp/gitlabfs/test/mnt",
MountOptions: "nodev",
},
Gitlab: gitlab.GitlabClientConfig{
URL: "https://example.com",
Token: "12345",
PullMethod: "ssh",
GroupIDs: []int{123},
UserIDs: []int{456},
IncludeCurrentUser: true,
},
Git: git.GitClientParam{
CloneLocation: "/tmp/gitlabfs/test/clone",
Remote: "origin",
OnClone: "clone",
AutoPull: false,
Depth: 0,
QueueSize: 100,
QueueWorkerCount: 1,
}},
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.LoadConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("LoadConfig(%v) returned %v; expected %v; error: %v", test.input, got, expected, err)
}
})
}
}
func TestMakeGitConfig(t *testing.T) {
tests := map[string]struct {
input *config.Config
expected *git.GitClientParam
}{
"ValidConfig": {
input: &config.Config{
Git: git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
expected: &git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "init",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
"InvalidOnClone": {
input: &config.Config{
Git: git.GitClientParam{
CloneLocation: "/tmp",
Remote: "origin",
OnClone: "invalid",
AutoPull: false,
Depth: 0,
QueueSize: 200,
QueueWorkerCount: 5,
},
},
expected: nil,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.MakeGitConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("MakeGitConfig(%v) returned %v; expected %v; error %v", test.input, got, expected, err)
}
})
}
}
func TestMakeGitlabConfig(t *testing.T) {
tests := map[string]struct {
input *config.Config
expected *gitlab.GitlabClientConfig
}{
"ValidConfig": {
input: &config.Config{
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "http",
},
},
expected: &gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "http",
},
},
"InvalidPullMethod": {
input: &config.Config{
Gitlab: gitlab.GitlabClientConfig{
URL: "https://gitlab.com",
Token: "",
GroupIDs: []int{9970},
UserIDs: []int{},
IncludeCurrentUser: true,
PullMethod: "invalid",
},
},
expected: nil,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := config.MakeGitlabConfig(test.input)
expected := test.expected
if !reflect.DeepEqual(got, expected) {
t.Fatalf("MakeGitlabConfig(%v) returned %v; expected %v; error: %v", test.input, got, expected, err)
}
})
}
}