2017-06-15 16:46:06 +00:00
|
|
|
package structs
|
2014-10-15 17:08:13 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-10 23:54:06 +00:00
|
|
|
"fmt"
|
2018-05-04 21:10:03 +00:00
|
|
|
"strings"
|
2014-10-15 17:08:13 +00:00
|
|
|
"testing"
|
2015-01-29 20:11:42 +00:00
|
|
|
"time"
|
2017-10-10 23:54:06 +00:00
|
|
|
|
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2018-05-04 21:10:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-10-15 17:08:13 +00:00
|
|
|
)
|
|
|
|
|
2015-01-29 20:11:42 +00:00
|
|
|
func TestAgentStructs_CheckTypes(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2015-01-29 20:11:42 +00:00
|
|
|
svc := new(ServiceDefinition)
|
|
|
|
|
|
|
|
// Singular Check field works
|
|
|
|
svc.Check = CheckType{
|
2018-05-08 22:31:53 +00:00
|
|
|
ScriptArgs: []string{"/foo/bar"},
|
|
|
|
Interval: 10 * time.Second,
|
2015-01-29 20:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns HTTP checks
|
|
|
|
svc.Checks = append(svc.Checks, &CheckType{
|
|
|
|
HTTP: "http://foo/bar",
|
|
|
|
Interval: 10 * time.Second,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Returns Script checks
|
|
|
|
svc.Checks = append(svc.Checks, &CheckType{
|
2018-05-08 22:31:53 +00:00
|
|
|
ScriptArgs: []string{"/foo/bar"},
|
|
|
|
Interval: 10 * time.Second,
|
2015-01-29 20:11:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Returns TTL checks
|
|
|
|
svc.Checks = append(svc.Checks, &CheckType{
|
|
|
|
TTL: 10 * time.Second,
|
|
|
|
})
|
|
|
|
|
2017-10-10 23:54:06 +00:00
|
|
|
// Validate checks
|
|
|
|
cases := []struct {
|
|
|
|
in *CheckType
|
|
|
|
err error
|
|
|
|
desc string
|
|
|
|
}{
|
|
|
|
{&CheckType{HTTP: "http://foo/baz"}, fmt.Errorf("Interval must be > 0 for Script, HTTP, or TCP checks"), "Missing interval"},
|
|
|
|
{&CheckType{TTL: -1}, fmt.Errorf("TTL must be > 0 for TTL checks"), "Negative TTL"},
|
|
|
|
{&CheckType{TTL: 20 * time.Second, Interval: 10 * time.Second}, fmt.Errorf("Interval and TTL cannot both be specified"), "Interval and TTL both set"},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
svc.Check = *tc.in
|
|
|
|
checks, err := svc.CheckTypes()
|
|
|
|
verify.Values(t, tc.desc, err.Error(), tc.err.Error())
|
|
|
|
if len(checks) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", svc)
|
|
|
|
}
|
2015-01-29 20:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-04 21:10:03 +00:00
|
|
|
|
|
|
|
func TestServiceDefinitionValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Modify func(*ServiceDefinition)
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
|
|
|
func(x *ServiceDefinition) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"managed proxy with a port set",
|
|
|
|
func(x *ServiceDefinition) {
|
|
|
|
x.Port = 8080
|
2018-06-05 17:51:05 +00:00
|
|
|
x.Connect = &ServiceConnect{
|
2018-05-04 21:10:03 +00:00
|
|
|
Proxy: &ServiceDefinitionConnectProxy{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"managed proxy with no port set",
|
|
|
|
func(x *ServiceDefinition) {
|
2018-06-05 17:51:05 +00:00
|
|
|
x.Connect = &ServiceConnect{
|
2018-05-04 21:10:03 +00:00
|
|
|
Proxy: &ServiceDefinitionConnectProxy{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"must have a port",
|
|
|
|
},
|
2018-06-04 05:20:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"managed proxy with native set",
|
|
|
|
func(x *ServiceDefinition) {
|
|
|
|
x.Port = 8080
|
2018-06-05 17:51:05 +00:00
|
|
|
x.Connect = &ServiceConnect{
|
2018-06-04 05:20:16 +00:00
|
|
|
Native: true,
|
|
|
|
Proxy: &ServiceDefinitionConnectProxy{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"may not have a proxy",
|
|
|
|
},
|
2018-05-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
service := TestServiceDefinition(t)
|
|
|
|
tc.Modify(service)
|
|
|
|
|
|
|
|
err := service.Validate()
|
|
|
|
t.Logf("error: %s", err)
|
|
|
|
require.Equal(err != nil, tc.Err != "")
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|