consul/agent/config/runtime_ce_test.go

170 lines
4.5 KiB
Go
Raw Permalink 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
//go:build !consulent
package config
import (
"fmt"
"net"
"os"
"testing"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require"
)
var testRuntimeConfigSanitizeExpectedFilename = "TestRuntimeConfig_Sanitize.golden"
func entFullRuntimeConfig(rt *RuntimeConfig) {}
var enterpriseReadReplicaWarnings = []string{enterpriseConfigKeyError{key: "read_replica (or the deprecated non_voting_server)"}.Error()}
var enterpriseConfigKeyWarnings = []string{
enterpriseConfigKeyError{key: "license_path"}.Error(),
enterpriseConfigKeyError{key: "read_replica (or the deprecated non_voting_server)"}.Error(),
enterpriseConfigKeyError{key: "autopilot.redundancy_zone_tag"}.Error(),
enterpriseConfigKeyError{key: "autopilot.upgrade_version_tag"}.Error(),
enterpriseConfigKeyError{key: "autopilot.disable_upgrade_migration"}.Error(),
enterpriseConfigKeyError{key: "dns_config.prefer_namespace"}.Error(),
enterpriseConfigKeyError{key: "acl.msp_disable_bootstrap"}.Error(),
enterpriseConfigKeyError{key: "acl.tokens.managed_service_provider"}.Error(),
enterpriseConfigKeyError{key: "audit"}.Error(),
enterpriseConfigKeyError{key: "reporting.license.enabled"}.Error(),
}
// CE-only equivalent of TestConfigFlagsAndEdgecases
// used for flags validated in ent-only code
func TestLoad_IntegrationWithFlags_CE(t *testing.T) {
dataDir := testutil.TempDir(t, "consul")
defer os.RemoveAll(dataDir)
tests := []testCase{
{
desc: "partition config on a client",
args: []string{
`-data-dir=` + dataDir,
`-server=false`,
},
json: []string{`{ "partition": "foo" }`},
hcl: []string{`partition = "foo"`},
expectedWarnings: []string{
`"partition" is a Consul Enterprise configuration and will have no effect`,
},
expected: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.ServerMode = false
},
},
{
desc: "partition config on a server",
args: []string{
`-data-dir=` + dataDir,
`-server`,
},
json: []string{`{ "partition": "foo" }`},
hcl: []string{`partition = "foo"`},
expectedWarnings: []string{
`"partition" is a Consul Enterprise configuration and will have no effect`,
},
expected: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.ServerMode = true
rt.TLS.ServerMode = true
rt.LeaveOnTerm = false
rt.SkipLeaveOnInt = true
rt.RPCConfig.EnableStreaming = true
rt.GRPCTLSPort = 8503
rt.GRPCTLSAddrs = []net.Addr{defaultGrpcTlsAddr}
},
},
}
for _, tc := range tests {
for _, format := range []string{"json", "hcl"} {
name := fmt.Sprintf("%v_%v", tc.desc, format)
t.Run(name, tc.run(format, dataDir))
}
}
}
func TestLoad_ReportingConfig(t *testing.T) {
dir := testutil.TempDir(t, t.Name())
t.Run("load from JSON defaults to false", func(t *testing.T) {
content := `{
"reporting": {}
}`
opts := LoadOpts{
FlagValues: FlagValuesTarget{Config: Config{
DataDir: &dir,
}},
Overrides: []Source{
FileSource{
Name: "reporting.json",
Format: "json",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 0)
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})
t.Run("load from HCL defaults to false", func(t *testing.T) {
content := `
reporting {}
`
opts := LoadOpts{
FlagValues: FlagValuesTarget{Config: Config{
DataDir: &dir,
}},
Overrides: []Source{
FileSource{
Name: "reporting.hcl",
Format: "hcl",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 0)
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})
t.Run("with value set returns warning and defaults to false", func(t *testing.T) {
content := `reporting {
license {
enabled = true
}
}`
opts := LoadOpts{
FlagValues: FlagValuesTarget{Config: Config{
DataDir: &dir,
}},
Overrides: []Source{
FileSource{
Name: "reporting.hcl",
Format: "hcl",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 1)
require.Contains(t, result.Warnings[0], "\"reporting.license.enabled\" is a Consul Enterprise configuration and will have no effect")
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})
}