From 65d3a00fa3a37cad70eee00c2ba634b84b8a60ee Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Sat, 8 Jun 2024 01:02:35 -0400 Subject: [PATCH] add config tests --- config.example.yaml | 12 +-- config/config.test.yaml | 22 ++++++ config/loader.go | 2 +- config/loader_test.go | 164 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 config/config.test.yaml create mode 100644 config/loader_test.go diff --git a/config.example.yaml b/config.example.yaml index 9bd2843..2634bd2 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -14,6 +14,12 @@ gitlab: # Default to anonymous (only public projects will be visible). #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. group_ids: - 9970 # gitlab-org @@ -32,12 +38,6 @@ git: # The name of the remote in the local clone. 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". # 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) diff --git a/config/config.test.yaml b/config/config.test.yaml new file mode 100644 index 0000000..e8a0f3d --- /dev/null +++ b/config/config.test.yaml @@ -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 \ No newline at end of file diff --git a/config/loader.go b/config/loader.go index a0d244e..d9002e9 100644 --- a/config/loader.go +++ b/config/loader.go @@ -38,10 +38,10 @@ func LoadConfig(configPath string) (*Config, error) { Gitlab: gitlab.GitlabClientConfig{ URL: "https://gitlab.com", Token: "", + PullMethod: "http", GroupIDs: []int{9970}, UserIDs: []int{}, IncludeCurrentUser: true, - PullMethod: "http", }, Git: git.GitClientParam{ CloneLocation: defaultCloneLocation, diff --git a/config/loader_test.go b/config/loader_test.go new file mode 100644 index 0000000..49e259e --- /dev/null +++ b/config/loader_test.go @@ -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) + } + }) + } +}