-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
115 lines (102 loc) · 3.05 KB
/
config_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gerrittest
import (
"io/ioutil"
"os"
"github.com/go-ini/ini"
"github.com/opalmer/dockertest"
. "gopkg.in/check.v1"
)
type ConfigTest struct {
value string
set bool
}
var _ = Suite(&ConfigTest{})
func (s *ConfigTest) SetUpTest(c *C) {
value, set := os.LookupEnv(DefaultImageEnvironmentVar)
s.value = value
s.set = set
c.Assert(os.Unsetenv(DefaultImageEnvironmentVar), IsNil)
}
func (s *ConfigTest) TearDownTest(c *C) {
if s.set {
c.Assert(os.Setenv(DefaultImageEnvironmentVar, s.value), IsNil)
return
}
c.Assert(os.Unsetenv(DefaultImageEnvironmentVar), IsNil)
}
func (s *ConfigTest) TestNewConfigOverride(c *C) {
c.Assert(os.Setenv(DefaultImageEnvironmentVar, "override"), IsNil)
cfg := NewConfig()
c.Assert(cfg.Image, Equals, "override")
}
func (s *ConfigTest) testWrittenConfig(c *C, path string) {
cfg, err := ini.LoadSources(ini.LoadOptions{AllowShadows: true}, path)
c.Assert(err, IsNil)
heads := cfg.Section(accessHeads)
c.Assert(
heads.Key("label-Verified").ValueWithShadows(), DeepEquals,
[]string{"-1..+1 group Administrators", "-1..+1 group Project Owners"})
verifiedLabel := cfg.Section(labelVerified)
c.Assert(verifiedLabel.Key("function").Value(), Equals, "MaxWithBlock")
c.Assert(verifiedLabel.Key("defaultValue").Value(), Equals, "0")
c.Assert(
verifiedLabel.Key("value").ValueWithShadows(), DeepEquals,
[]string{"-1 Fails", "0 No Score", "+1 Verified"})
}
func (s *ConfigTest) Test_projectConfig_newProjectConfig_missingFile(c *C) {
file, err := ioutil.TempFile("", "")
defer os.Remove(file.Name()) // nolint: errcheck
c.Assert(err, IsNil)
c.Assert(file.Close(), IsNil)
c.Assert(os.Remove(file.Name()), IsNil)
cfg, err := newProjectConfig(file.Name())
c.Assert(err, IsNil)
c.Assert(cfg.write(file.Name()), IsNil)
s.testWrittenConfig(c, file.Name())
}
func (s *ConfigTest) Test_projectConfig_newProjectConfig_existingConifg(c *C) {
file, err := ioutil.TempFile("", "")
c.Assert(err, IsNil)
_, err = file.WriteString(`
[label "Verified"]
default = 1
`)
c.Assert(err, IsNil)
defer os.Remove(file.Name()) // nolint: errcheck
c.Assert(err, IsNil)
c.Assert(file.Close(), IsNil)
cfg, err := newProjectConfig(file.Name())
c.Assert(err, IsNil)
c.Assert(cfg.write(file.Name()), IsNil)
s.testWrittenConfig(c, file.Name())
}
func (s *ConfigTest) getConfigForGetSSHCommandTest() *Gerrit {
return &Gerrit{
Config: &Config{
Username: "testing",
SSHKeys: []*SSHKey{{
Path: "/tmp/id_rsa",
Default: true,
}},
},
SSHPort: &dockertest.Port{
Address: "1.2.3.4",
Public: 65535,
},
}
}
func (s *ConfigTest) TestGetSSHCommand(c *C) {
cmd, err := GetSSHCommand(s.getConfigForGetSSHCommandTest())
c.Assert(err, IsNil)
c.Assert(
cmd, Equals,
"ssh -i /tmp/id_rsa -o UserKnownHostsFile=/dev/null "+
"-o StrictHostKeyChecking=no -p 65535 testing@1.2.3.4")
}
func (s *ConfigTest) TestGetSSHCommandNoDefaultKeys(c *C) {
g := s.getConfigForGetSSHCommandTest()
g.Config.SSHKeys[0].Default = false
cmd, err := GetSSHCommand(g)
c.Assert(err, ErrorMatches, "no default ssh keys present")
c.Assert(cmd, Equals, "")
}