2023-03-28 19:12:30 +00:00
// Copyright (c) HashiCorp, Inc.
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
2023-03-28 19:12:30 +00:00
2018-10-01 15:27:59 +00:00
package services
2018-09-28 06:52:17 +00:00
import (
"testing"
2019-11-15 15:06:33 +00:00
"time"
2018-09-28 06:52:17 +00:00
2020-12-21 18:25:32 +00:00
"github.com/stretchr/testify/require"
2018-10-02 19:45:00 +00:00
"github.com/hashicorp/consul/agent/config"
2018-10-01 15:05:57 +00:00
"github.com/hashicorp/consul/agent/structs"
2018-09-28 06:52:17 +00:00
"github.com/hashicorp/consul/api"
)
2018-10-02 19:45:00 +00:00
// This test ensures that dev mode doesn't register services by default.
// We depend on this behavior for ServiesFromFiles so we want to fail
// tests if that ever changes.
func TestDevModeHasNoServices ( t * testing . T ) {
devMode := true
2021-06-01 15:58:03 +00:00
opts := config . LoadOpts {
DevMode : & devMode ,
HCL : [ ] string { ` node_name = "dummy" ` } ,
}
result , err := config . Load ( opts )
2020-12-21 18:46:41 +00:00
require . NoError ( t , err )
require . Len ( t , result . Warnings , 0 )
require . Len ( t , result . RuntimeConfig . Services , 0 )
2018-10-02 19:45:00 +00:00
}
2021-06-01 15:58:03 +00:00
func TestInvalidNodeNameWarning ( t * testing . T ) {
devMode := true
opts := config . LoadOpts {
DevMode : & devMode ,
HCL : [ ] string { ` node_name = "dummy.local" ` } ,
}
result , err := config . Load ( opts )
require . NoError ( t , err )
require . Len ( t , result . Warnings , 1 )
require . Contains ( t , result . Warnings [ 0 ] , "will not be discoverable via DNS due to invalid characters. Valid characters include all alpha-numerics and dashes." )
}
2018-10-01 15:05:57 +00:00
func TestStructsToAgentService ( t * testing . T ) {
2018-10-02 19:45:00 +00:00
t . Parallel ( )
2018-09-28 06:52:17 +00:00
cases := [ ] struct {
Name string
2018-10-01 15:05:57 +00:00
Input * structs . ServiceDefinition
2018-09-28 06:52:17 +00:00
Output * api . AgentServiceRegistration
} {
{
"Basic service with port" ,
2018-10-01 15:05:57 +00:00
& structs . ServiceDefinition {
Name : "web" ,
2018-09-28 06:52:17 +00:00
Tags : [ ] string { "leader" } ,
2018-10-01 15:05:57 +00:00
Port : 1234 ,
2018-09-28 06:52:17 +00:00
} ,
& api . AgentServiceRegistration {
Name : "web" ,
Tags : [ ] string { "leader" } ,
Port : 1234 ,
} ,
} ,
2018-10-01 02:17:45 +00:00
{
"Service with a check" ,
2018-10-01 15:05:57 +00:00
& structs . ServiceDefinition {
Name : "web" ,
Check : structs . CheckType {
Name : "ping" ,
2018-10-01 02:17:45 +00:00
} ,
} ,
& api . AgentServiceRegistration {
Name : "web" ,
Check : & api . AgentServiceCheck {
Name : "ping" ,
} ,
} ,
} ,
2019-11-15 15:06:33 +00:00
{
"Service with an unnamed check" ,
& structs . ServiceDefinition {
Name : "web" ,
Check : structs . CheckType {
TTL : 5 * time . Second ,
} ,
} ,
& api . AgentServiceRegistration {
Name : "web" ,
Check : & api . AgentServiceCheck {
TTL : "5s" ,
} ,
} ,
} ,
{
"Service with a zero-value check" ,
& structs . ServiceDefinition {
Name : "web" ,
Check : structs . CheckType { } ,
} ,
& api . AgentServiceRegistration {
Name : "web" ,
Check : nil ,
} ,
} ,
2018-10-01 02:17:45 +00:00
{
"Service with checks" ,
2018-10-01 15:05:57 +00:00
& structs . ServiceDefinition {
Name : "web" ,
Checks : structs . CheckTypes {
& structs . CheckType {
Name : "ping" ,
2018-10-01 02:17:45 +00:00
} ,
2018-10-01 15:05:57 +00:00
& structs . CheckType {
Name : "pong" ,
2018-10-01 02:17:45 +00:00
} ,
} ,
} ,
& api . AgentServiceRegistration {
Name : "web" ,
Checks : api . AgentServiceChecks {
& api . AgentServiceCheck {
Name : "ping" ,
} ,
& api . AgentServiceCheck {
Name : "pong" ,
} ,
} ,
} ,
} ,
2018-10-04 13:08:12 +00:00
{
"Proxy service" ,
2022-12-22 20:18:15 +00:00
& structs . ServiceDefinition {
Name : "web-proxy" ,
Kind : structs . ServiceKindConnectProxy ,
Tags : [ ] string { "leader" } ,
Port : 1234 ,
Proxy : & structs . ConnectProxyConfig {
DestinationServiceID : "web1" ,
DestinationServiceName : "web" ,
LocalServiceAddress : "127.0.0.1" ,
LocalServicePort : 8181 ,
2023-03-22 18:56:18 +00:00
Upstreams : structs . TestUpstreams ( t , false ) ,
2022-12-22 20:18:15 +00:00
Mode : structs . ProxyModeTransparent ,
Config : map [ string ] interface { } {
"foo" : "bar" ,
} ,
} ,
} ,
& api . AgentServiceRegistration {
Name : "web-proxy" ,
Tags : [ ] string { "leader" } ,
Port : 1234 ,
Kind : api . ServiceKindConnectProxy ,
Proxy : & api . AgentServiceConnectProxyConfig {
DestinationServiceID : "web1" ,
DestinationServiceName : "web" ,
LocalServiceAddress : "127.0.0.1" ,
LocalServicePort : 8181 ,
2023-03-22 18:56:18 +00:00
Upstreams : structs . TestUpstreams ( t , false ) . ToAPI ( ) ,
2022-12-22 20:18:15 +00:00
Mode : api . ProxyModeTransparent ,
Config : map [ string ] interface { } {
"foo" : "bar" ,
} ,
} ,
} ,
} ,
{
"TProxy proxy service w/ access logs" ,
2018-10-04 13:08:12 +00:00
& structs . ServiceDefinition {
Name : "web-proxy" ,
Kind : structs . ServiceKindConnectProxy ,
Tags : [ ] string { "leader" } ,
Port : 1234 ,
Proxy : & structs . ConnectProxyConfig {
DestinationServiceID : "web1" ,
DestinationServiceName : "web" ,
LocalServiceAddress : "127.0.0.1" ,
LocalServicePort : 8181 ,
2023-03-22 18:56:18 +00:00
Upstreams : structs . TestUpstreams ( t , false ) ,
2021-04-13 22:08:41 +00:00
Mode : structs . ProxyModeTransparent ,
TransparentProxy : structs . TransparentProxyConfig {
OutboundListenerPort : 808 ,
} ,
2022-12-22 20:18:15 +00:00
AccessLogs : structs . AccessLogsConfig {
Enabled : true ,
DisableListenerLogs : true ,
Type : structs . FileLogSinkType ,
Path : "/var/logs/envoy.logs" ,
TextFormat : "MY START TIME %START_TIME%" ,
} ,
2018-10-04 13:08:12 +00:00
Config : map [ string ] interface { } {
"foo" : "bar" ,
} ,
} ,
} ,
& api . AgentServiceRegistration {
Name : "web-proxy" ,
Tags : [ ] string { "leader" } ,
Port : 1234 ,
Kind : api . ServiceKindConnectProxy ,
Proxy : & api . AgentServiceConnectProxyConfig {
DestinationServiceID : "web1" ,
DestinationServiceName : "web" ,
LocalServiceAddress : "127.0.0.1" ,
LocalServicePort : 8181 ,
2023-03-22 18:56:18 +00:00
Upstreams : structs . TestUpstreams ( t , false ) . ToAPI ( ) ,
2021-04-13 22:08:41 +00:00
Mode : api . ProxyModeTransparent ,
TransparentProxy : & api . TransparentProxyConfig {
OutboundListenerPort : 808 ,
} ,
2022-12-22 20:18:15 +00:00
AccessLogs : & api . AccessLogsConfig {
Enabled : true ,
DisableListenerLogs : true ,
Type : api . FileLogSinkType ,
Path : "/var/logs/envoy.logs" ,
TextFormat : "MY START TIME %START_TIME%" ,
} ,
2018-10-04 13:08:12 +00:00
Config : map [ string ] interface { } {
"foo" : "bar" ,
} ,
} ,
} ,
} ,
2018-09-28 06:52:17 +00:00
}
2018-10-04 13:08:12 +00:00
for _ , tt := range cases {
// Capture the loop variable locally otherwise parallel will cause us to run
// N copies of the last test case but with different names!!
tc := tt
2018-09-28 06:52:17 +00:00
t . Run ( tc . Name , func ( t * testing . T ) {
2018-10-02 19:45:00 +00:00
t . Parallel ( )
2018-10-01 15:05:57 +00:00
actual , err := serviceToAgentService ( tc . Input )
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 )
require . Equal ( t , tc . Output , actual )
2018-09-28 06:52:17 +00:00
} )
}
}