forked from argoproj/argo-cd
/
localconfig_test.go
93 lines (78 loc) · 2.45 KB
/
localconfig_test.go
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
//go:build !windows
package localconfig
import (
"fmt"
"github.com/stretchr/testify/require"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetUsername(t *testing.T) {
assert.Equal(t, "admin", GetUsername("admin:login"))
assert.Equal(t, "admin", GetUsername("admin"))
assert.Equal(t, "", GetUsername(""))
}
func TestFilePermission(t *testing.T) {
dirPath := "testfolder/"
err := os.MkdirAll(path.Dir(dirPath), 0700)
require.NoError(t, err, "Could not create argocd folder with 0700 permission: %v", err)
t.Cleanup(func() {
err := os.RemoveAll(dirPath)
require.NoError(t, err, "Could not remove directory")
})
for _, c := range []struct {
name string
testfile string
perm os.FileMode
expectedError error
}{
{
name: "Test config file with permission 0700",
testfile: ".config_0700",
perm: 0700,
expectedError: fmt.Errorf("config file has incorrect permission flags:-rwx------.change the file permission either to 0400 or 0600."),
},
{
name: "Test config file with permission 0777",
testfile: ".config_0777",
perm: 0777,
expectedError: fmt.Errorf("config file has incorrect permission flags:-rwxrwxrwx.change the file permission either to 0400 or 0600."),
},
{
name: "Test config file with permission 0600",
testfile: ".config_0600",
perm: 0600,
expectedError: nil,
},
{
name: "Test config file with permission 0400",
testfile: ".config_0400",
perm: 0400,
expectedError: nil,
},
{
name: "Test config file with permission 0300",
testfile: ".config_0300",
perm: 0300,
expectedError: fmt.Errorf("config file has incorrect permission flags:--wx------.change the file permission either to 0400 or 0600."),
},
} {
t.Run(c.name, func(t *testing.T) {
filePath := filepath.Join(dirPath, c.testfile)
f, err := os.Create(filePath)
require.NoError(t, err, "Could not write create config file: %v", err)
defer f.Close()
err = f.Chmod(c.perm)
require.NoError(t, err, "Could not change the file permission to %s: %v", c.perm, err)
fi, err := os.Stat(filePath)
require.NoError(t, err, "Could not access the fileinfo: %v", err)
if err := getFilePermission(fi); err != nil {
assert.EqualError(t, err, c.expectedError.Error())
} else {
require.Nil(t, c.expectedError)
}
})
}
}