consul/command/services/config_test.go

239 lines
6.0 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package services
2018-09-28 06:52:17 +00:00
import (
"testing"
"time"
2018-09-28 06:52:17 +00:00
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/structs"
2018-09-28 06:52:17 +00:00
"github.com/hashicorp/consul/api"
)
// 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
opts := config.LoadOpts{
DevMode: &devMode,
HCL: []string{`node_name = "dummy"`},
}
result, err := config.Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 0)
require.Len(t, result.RuntimeConfig.Services, 0)
}
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.")
}
func TestStructsToAgentService(t *testing.T) {
t.Parallel()
2018-09-28 06:52:17 +00:00
cases := []struct {
Name string
Input *structs.ServiceDefinition
2018-09-28 06:52:17 +00:00
Output *api.AgentServiceRegistration
}{
{
"Basic service with port",
&structs.ServiceDefinition{
Name: "web",
2018-09-28 06:52:17 +00:00
Tags: []string{"leader"},
Port: 1234,
2018-09-28 06:52:17 +00:00
},
&api.AgentServiceRegistration{
Name: "web",
Tags: []string{"leader"},
Port: 1234,
},
},
{
"Service with a check",
&structs.ServiceDefinition{
Name: "web",
Check: structs.CheckType{
Name: "ping",
},
},
&api.AgentServiceRegistration{
Name: "web",
Check: &api.AgentServiceCheck{
Name: "ping",
},
},
},
{
"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,
},
},
{
"Service with checks",
&structs.ServiceDefinition{
Name: "web",
Checks: structs.CheckTypes{
&structs.CheckType{
Name: "ping",
},
&structs.CheckType{
Name: "pong",
},
},
},
&api.AgentServiceRegistration{
Name: "web",
Checks: api.AgentServiceChecks{
&api.AgentServiceCheck{
Name: "ping",
},
&api.AgentServiceCheck{
Name: "pong",
},
},
},
},
{
"Proxy service",
&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),
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(),
Mode: api.ProxyModeTransparent,
Config: map[string]interface{}{
"foo": "bar",
},
},
},
},
{
"TProxy proxy service w/ access logs",
&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,
},
AccessLogs: structs.AccessLogsConfig{
Enabled: true,
DisableListenerLogs: true,
Type: structs.FileLogSinkType,
Path: "/var/logs/envoy.logs",
TextFormat: "MY START TIME %START_TIME%",
},
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,
},
AccessLogs: &api.AccessLogsConfig{
Enabled: true,
DisableListenerLogs: true,
Type: api.FileLogSinkType,
Path: "/var/logs/envoy.logs",
TextFormat: "MY START TIME %START_TIME%",
},
Config: map[string]interface{}{
"foo": "bar",
},
},
},
},
2018-09-28 06:52:17 +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) {
t.Parallel()
actual, err := serviceToAgentService(tc.Input)
require.NoError(t, err)
require.Equal(t, tc.Output, actual)
2018-09-28 06:52:17 +00:00
})
}
}