mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-29 17:58:15 +08:00
Covered 'main' with tests
This commit is contained in:
@@ -6,12 +6,6 @@ export GO15VENDOREXPERIMENT=1
|
||||
|
||||
.PHONY: install test test-with-etcd remove-temp files test-package update test-grep-package cover-package cover-package-with-etcd run profile sloccount set-etcd install-assets docs-serve
|
||||
|
||||
conf:
|
||||
go test -v github.com/gravitational/teleport/tool/teleport \
|
||||
github.com/gravitational/teleport/lib/config \
|
||||
github.com/gravitational/teleport/lib/utils \
|
||||
github.com/gravitational/teleport/lib/defaults
|
||||
|
||||
#
|
||||
# Default target: builds all 3 executables and plaaces them in a current directory
|
||||
#
|
||||
|
||||
@@ -48,13 +48,6 @@ const (
|
||||
// Default DB to use for persisting state. Another options is "etcd"
|
||||
BackendType = "bolt"
|
||||
|
||||
// Default path to teleport config file
|
||||
ConfigFilePath = "/etc/teleport.yaml"
|
||||
|
||||
// This is where all mutable data is stored (user keys, recorded sessions,
|
||||
// registered SSH servers, etc):
|
||||
DataDir = "/var/lib/teleport"
|
||||
|
||||
// Name of events bolt database file stored in DataDir
|
||||
EventsBoltFile = "events.db"
|
||||
|
||||
@@ -89,6 +82,13 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// Default path to teleport config file
|
||||
ConfigFilePath = "/etc/teleport.yaml"
|
||||
|
||||
// This is where all mutable data is stored (user keys, recorded sessions,
|
||||
// registered SSH servers, etc):
|
||||
DataDir = "/var/lib/teleport"
|
||||
|
||||
// Default roles teleport assumes when started via 'start' command
|
||||
StartRoles = []string{RoleProxy, RoleNode, RoleAuthService}
|
||||
)
|
||||
|
||||
@@ -57,7 +57,7 @@ func readConfigFile(cliConfigPath string) (*config.FileConfig, error) {
|
||||
configFilePath := defaults.ConfigFilePath
|
||||
// --config tells us to use a specific conf. file:
|
||||
if cliConfigPath != "" {
|
||||
configFilePath := cliConfigPath
|
||||
configFilePath = cliConfigPath
|
||||
if !fileExists(configFilePath) {
|
||||
return nil, trace.Errorf("file not found: %s", configFilePath)
|
||||
}
|
||||
@@ -67,6 +67,7 @@ func readConfigFile(cliConfigPath string) (*config.FileConfig, error) {
|
||||
log.Info("not using a config file")
|
||||
return nil, nil
|
||||
}
|
||||
log.Debug("reading config file: ", configFilePath)
|
||||
return config.ReadFromFile(configFilePath)
|
||||
}
|
||||
|
||||
@@ -209,14 +210,20 @@ func applyFileConfig(fc *config.FileConfig, cfg *service.Config) error {
|
||||
}
|
||||
cfg.SSH.Addr = *addr
|
||||
}
|
||||
for k, v := range fc.SSH.Labels {
|
||||
cfg.SSH.Labels[k] = v
|
||||
if fc.SSH.Labels != nil {
|
||||
cfg.SSH.Labels = make(map[string]string)
|
||||
for k, v := range fc.SSH.Labels {
|
||||
cfg.SSH.Labels[k] = v
|
||||
}
|
||||
}
|
||||
for _, cmdLabel := range fc.SSH.Commands {
|
||||
cfg.SSH.CmdLabels[cmdLabel.Name] = services.CommandLabel{
|
||||
Period: cmdLabel.Period,
|
||||
Command: cmdLabel.Command,
|
||||
Result: "",
|
||||
if fc.SSH.Commands != nil {
|
||||
cfg.SSH.CmdLabels = make(services.CommandLabels)
|
||||
for _, cmdLabel := range fc.SSH.Commands {
|
||||
cfg.SSH.CmdLabels[cmdLabel.Name] = services.CommandLabel{
|
||||
Period: cmdLabel.Period,
|
||||
Command: cmdLabel.Command,
|
||||
Result: "",
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -303,8 +310,6 @@ func configure(clf *CommandLineFlags) (cfg *service.Config, err error) {
|
||||
applyListenIP(clf.ListenIP, cfg)
|
||||
}
|
||||
|
||||
log.Info(cfg.DebugDumpToYAML())
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
|
||||
+26
-17
@@ -29,6 +29,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
const testRun = false
|
||||
run(os.Args[1:], testRun)
|
||||
}
|
||||
|
||||
// same as main() but has a testing switch
|
||||
func run(cmdlineArgs []string, testRun bool) (executedCommand string, appliedConfig *service.Config) {
|
||||
var err error
|
||||
|
||||
// configure logger for a typical CLI scenario until configuration file is
|
||||
@@ -69,14 +75,13 @@ func main() {
|
||||
StringVar(&ccf.NodeName)
|
||||
start.Flag("config",
|
||||
fmt.Sprintf("Path to a configuration file [%v]", defaults.ConfigFilePath)).
|
||||
Short('c').
|
||||
StringVar(&ccf.ConfigFile)
|
||||
Short('c').ExistingFileVar(&ccf.ConfigFile)
|
||||
|
||||
// define start's usage info (we use kingpin's "alias" field for this)
|
||||
start.Alias(usageNotes + usageExamples)
|
||||
|
||||
// parse CLI commands+flags:
|
||||
command, err := app.Parse(os.Args[1:])
|
||||
command, err := app.Parse(cmdlineArgs)
|
||||
if err != nil {
|
||||
utils.FatalError(err)
|
||||
}
|
||||
@@ -87,22 +92,26 @@ func main() {
|
||||
utils.FatalError(err)
|
||||
}
|
||||
|
||||
// execute the selected command:
|
||||
switch command {
|
||||
case start.FullCommand():
|
||||
err = onStart(config)
|
||||
case status.FullCommand():
|
||||
err = onStatus(config)
|
||||
case dump.FullCommand():
|
||||
err = onConfigDump()
|
||||
case ver.FullCommand():
|
||||
onVersion()
|
||||
}
|
||||
// execute the selected command unless we're running tests
|
||||
if !testRun {
|
||||
log.Debug(config.DebugDumpToYAML())
|
||||
|
||||
if err != nil {
|
||||
utils.FatalError(err)
|
||||
switch command {
|
||||
case start.FullCommand():
|
||||
err = onStart(config)
|
||||
case status.FullCommand():
|
||||
err = onStatus(config)
|
||||
case dump.FullCommand():
|
||||
err = onConfigDump()
|
||||
case ver.FullCommand():
|
||||
onVersion()
|
||||
}
|
||||
if err != nil {
|
||||
utils.FatalError(err)
|
||||
}
|
||||
log.Info("teleport: clean exit")
|
||||
}
|
||||
log.Info("teleport: clean exit")
|
||||
return command, config
|
||||
}
|
||||
|
||||
// onStart is the handler for "start" CLI command
|
||||
|
||||
@@ -16,8 +16,14 @@ limitations under the License.
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/gravitational/teleport/lib/defaults"
|
||||
|
||||
"gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
@@ -26,9 +32,110 @@ func TestSrv(t *testing.T) { check.TestingT(t) }
|
||||
|
||||
// register test suite
|
||||
type MainTestSuite struct {
|
||||
hostname string
|
||||
configFile string
|
||||
}
|
||||
|
||||
var _ = check.Suite(&MainTestSuite{})
|
||||
|
||||
func (s *MainTestSuite) SetUpSuite(c *check.C) {
|
||||
var err error
|
||||
// get the hostname
|
||||
s.hostname, err = os.Hostname()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// generate the fixture config file
|
||||
dirname, err := ioutil.TempDir("", "teleport")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s.configFile = filepath.Join(dirname, "teleport.yaml")
|
||||
ioutil.WriteFile(s.configFile, []byte(YAMLConfig), 770)
|
||||
|
||||
// set imprtant defaults to test-mode (non-existing files&locations)
|
||||
defaults.ConfigFilePath = "/tmp/teleport/etc/teleport.yaml"
|
||||
defaults.DataDir = "/tmp/teleport/var/lib/teleport"
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestDefault(c *check.C) {
|
||||
cmd, conf := run([]string{"start"}, true)
|
||||
c.Assert(cmd, check.Equals, "start")
|
||||
c.Assert(conf.Hostname, check.Equals, s.hostname)
|
||||
c.Assert(conf.DataDir, check.Equals, "/tmp/teleport/var/lib/teleport")
|
||||
c.Assert(conf.Auth.Enabled, check.Equals, true)
|
||||
c.Assert(conf.SSH.Enabled, check.Equals, true)
|
||||
c.Assert(conf.Proxy.Enabled, check.Equals, true)
|
||||
c.Assert(conf.Console, check.Equals, os.Stdout)
|
||||
c.Assert(log.GetLevel(), check.Equals, log.InfoLevel)
|
||||
|
||||
cmd, conf = run([]string{"start", "-d"}, true)
|
||||
c.Assert(log.GetLevel(), check.Equals, log.DebugLevel)
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestRolesFlag(c *check.C) {
|
||||
cmd, conf := run([]string{"start", "--roles=node"}, true)
|
||||
c.Assert(conf.SSH.Enabled, check.Equals, true)
|
||||
c.Assert(conf.Auth.Enabled, check.Equals, false)
|
||||
c.Assert(conf.Proxy.Enabled, check.Equals, false)
|
||||
|
||||
cmd, conf = run([]string{"start", "--roles=proxy"}, true)
|
||||
c.Assert(conf.SSH.Enabled, check.Equals, false)
|
||||
c.Assert(conf.Auth.Enabled, check.Equals, false)
|
||||
c.Assert(conf.Proxy.Enabled, check.Equals, true)
|
||||
|
||||
cmd, conf = run([]string{"start", "--roles=auth"}, true)
|
||||
c.Assert(conf.SSH.Enabled, check.Equals, false)
|
||||
c.Assert(conf.Auth.Enabled, check.Equals, true)
|
||||
c.Assert(conf.Proxy.Enabled, check.Equals, false)
|
||||
c.Assert(cmd, check.Equals, "start")
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestConfigFile(c *check.C) {
|
||||
cmd, conf := run([]string{"start", "--roles=node", "-d", "--config=" + s.configFile}, true)
|
||||
c.Assert(cmd, check.Equals, "start")
|
||||
c.Assert(conf.SSH.Enabled, check.Equals, true)
|
||||
c.Assert(conf.Auth.Enabled, check.Equals, false)
|
||||
c.Assert(conf.Proxy.Enabled, check.Equals, false)
|
||||
c.Assert(log.GetLevel(), check.Equals, log.DebugLevel)
|
||||
c.Assert(conf.Hostname, check.Equals, "hvostongo.example.org")
|
||||
c.Assert(conf.SSH.Token, check.Equals, "xxxyyy")
|
||||
}
|
||||
|
||||
const YAMLConfig = `
|
||||
teleport:
|
||||
nodename: hvostongo.example.org
|
||||
auth_servers: tcp://auth.server.example.org:3024
|
||||
auth_token: xxxyyy
|
||||
log:
|
||||
output: stderr
|
||||
severity: DEBUG
|
||||
connection_limits:
|
||||
max_connections: 90
|
||||
max_users: 91
|
||||
rates:
|
||||
- period: 1m1s
|
||||
average: 70
|
||||
burst: 71
|
||||
- period: 10m10s
|
||||
average: 170
|
||||
burst: 171
|
||||
|
||||
auth_service:
|
||||
enabled: yes
|
||||
listen_addr: tcp://auth
|
||||
|
||||
ssh_service:
|
||||
enabled: no
|
||||
listen_addr: tcp://ssh
|
||||
labels:
|
||||
name: mondoserver
|
||||
role: slave
|
||||
commands:
|
||||
- name: hostname
|
||||
command: [/bin/hostname]
|
||||
period: 10ms
|
||||
- name: date
|
||||
command: [/bin/date]
|
||||
period: 20ms
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user