You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB

package main
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAgentConfig(t *testing.T) {
a := &agent{}
assert.Nil(t, a.parseConfig("testdata/configs/good.yml"))
a = &agent{}
assert.NotNil(t, a.parseConfig("invalidfile"))
}
func TestAgentConfigValidate(t *testing.T) {
listeners := map[string]bool{
"0.0.0.0:1234": true,
"127.0.0.1:1234": true,
":1234": true,
" 0.0.0.0:1234 ": true,
" :1234 ": true,
":abcdef": false,
"xyzzy:1234": false,
"127.0.0.1: 1234": false,
"1:1234": false,
}
for addr, pass := range listeners {
a := agentConfig{Listen: addr}
if pass {
assert.Nil(t, a.validate(), addr)
} else {
assert.NotNil(t, a.validate(), addr)
}
}
}
func TestAgentAssembleMonitors(t *testing.T) {
a := &agent{}
assert.Nil(t, a.parseConfig("testdata/configs/good.yml"))
monitors, err := a.config.assembleMonitors()
assert.Nil(t, err)
assert.Equal(t, len(monitors), 2)
}
func TestAgentBoot(t *testing.T) {
a := &agent{}
ctx, cancel := context.WithCancel(context.Background())
// this muckery just ensures we hit the cancel function *after* the service boots.
mutex := &sync.Mutex{}
errChan := make(chan error, 1)
mutex.Lock()
go func() {
mutex.Unlock()
errChan <- a.boot(ctx)
}()
mutex.Lock()
cancel()
assert.Equal(t, <-errChan, context.Canceled)
}