2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
|
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) {
|
|
|
|
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 {
|
2020-06-02 16:41:25 +00:00
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
svc.Check = *tc.in
|
|
|
|
checks, err := svc.CheckTypes()
|
|
|
|
require.Error(t, err, tc.err.Error())
|
|
|
|
require.Len(t, checks, 0)
|
|
|
|
})
|
|
|
|
|
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) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
service := TestServiceDefinition(t)
|
|
|
|
tc.Modify(service)
|
|
|
|
|
|
|
|
err := service.Validate()
|
2018-09-12 16:07:47 +00:00
|
|
|
if tc.Err == "" {
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-12 16:07:47 +00:00
|
|
|
} else {
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
2018-05-04 21:10:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|