1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package admin
import (
"io"
"log"
"os"
"path/filepath"
"strings"
"testing"
)
var (
updatedServerConfig = []byte(`
---
name: "go-git-server"
version: "v1alpha1"
basepath: ./repos
repos:
- name: mgmt
permissions:
- role: admin
mode: 1
- name: testmerepo
git_web_config:
owner: grumps
description: >-
A wrapper to git http-backend providing authentcation and authorization
inspired by gitolite.
permissions:
- role: maintainers
mode: 1
- name: thisismynewrepo
git_web_config:
owner: grumps
description: >-
A wrapper to git http-backend providing authentcation and authorization
inspired by gitolite.
permissions:
- role: maintainers
mode: 1
`)
)
func copyFile(t *testing.T, srcFilePath, destPath string) {
srcFile, err := os.Open(srcFilePath)
if err != nil {
t.Fatalf("Error opening base config %s", err)
}
defer srcFile.Close()
// dest
destFile, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
t.Fatalf("failed to open destination in git repo %s", err)
}
defer destFile.Close()
// copy
if _, err := io.Copy(destFile, srcFile); err != nil {
t.Fatalf("Error copying file %s", err)
}
}
func TestInitServer(t *testing.T) {
tempDir := t.TempDir()
tempRepoDir := t.TempDir()
// auth model
destModelFile := filepath.Join(tempDir, "auth_model.ini")
srcModelFile := "../../auth_model.ini"
copyFile(t, srcModelFile, destModelFile)
// end auth model
// policy
destPolicyFile := filepath.Join(tempDir, "testpolicy.csv")
srcPolicyFile := "../../testpolicy.csv"
copyFile(t, srcPolicyFile, destPolicyFile)
// end policy
// config
destConfigFile := filepath.Join(tempRepoDir, "gitserver.yaml")
srcConfigFile := "../../gitserver.yaml"
copyFile(t, srcConfigFile, destConfigFile)
// end config
t.Run("test reload config success", func(t *testing.T) {
svc := NewService(destModelFile,
destPolicyFile,
filepath.Join(tempRepoDir, "gitserver.yaml"),
tempRepoDir,
false)
//nolint:gosec
err := os.WriteFile(destConfigFile, updatedServerConfig, 0500)
if err != nil {
t.Fatal(err)
}
// stuff
svc.Reload()
// check policy file to make sure it was saved
data, err := os.ReadFile(destPolicyFile)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "thisismynewrepo") {
t.Fatal("expected to find test new repo but didn't")
}
})
t.Run("test reload config err", func(t *testing.T) {
svc := NewService(destModelFile,
destPolicyFile,
// TODO set abs path
filepath.Join(tempRepoDir, "gitserver.yaml"),
tempRepoDir,
false)
notAGoodConfig := []byte("this is not valid yaml")
//nolint:gosec
err := os.WriteFile(destConfigFile, notAGoodConfig, 0500)
if err != nil {
t.Fatal(err)
}
// stuff
svc.Reload()
// check policy file to make sure it wasn't saved
data, err := os.ReadFile(destPolicyFile)
if err != nil {
log.Fatal(err)
}
if !strings.Contains(string(data), "mgmt") {
log.Fatal("expected to mgmt repo but didn't in policy")
}
})
}
|