You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/envoyextensions/xdscommon/envoy_versioning_test.go

131 lines
3.6 KiB

// 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>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package xdscommon
import (
"fmt"
"slices"
"testing"
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
"github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/sdk/testutil"
)
func TestDetermineEnvoyVersionFromNode(t *testing.T) {
cases := map[string]struct {
node *envoy_core_v3.Node
expect *version.Version
}{
"empty": {
node: &envoy_core_v3.Node{},
expect: nil,
},
"user agent build version but no user agent": {
node: &envoy_core_v3.Node{
UserAgentName: "",
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
Version: &envoy_type_v3.SemanticVersion{
MajorNumber: 1,
MinorNumber: 14,
Patch: 4,
},
},
},
},
expect: nil,
},
"user agent build version with user agent": {
node: &envoy_core_v3.Node{
UserAgentName: "envoy",
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
Version: &envoy_type_v3.SemanticVersion{
MajorNumber: 1,
MinorNumber: 14,
Patch: 4,
},
},
},
},
expect: version.Must(version.NewVersion("1.14.4")),
},
}
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
got := DetermineEnvoyVersionFromNode(tc.node)
if tc.expect != nil {
require.Equal(t, tc.expect, got)
} else {
require.Nil(t, got)
}
})
}
}
func TestDetermineSupportedProxyFeaturesFromString(t *testing.T) {
const errTooOld = "is too old and is not supported by Consul"
type testcase struct {
name string
expect SupportedProxyFeatures
expectErr string
}
var cases []testcase
// Bad versions.
minMajorVersion := version.Must(version.NewVersion(getMinEnvoyVersion()))
minMajorVersionMajorPart := minMajorVersion.Segments()[len(minMajorVersion.Segments())-2]
for major := 9; major < minMajorVersionMajorPart; major++ {
for minor := 0; minor < 10; minor++ {
cases = append(cases, testcase{
name: version.Must(version.NewVersion(fmt.Sprintf("1.%d.%d", major, minor))).String(),
expectErr: errTooOld,
})
}
}
// Good versions.
// Sort ascending so test output is ordered like bad cases above.
var supportedVersionsAscending []string
supportedVersionsAscending = append(supportedVersionsAscending, EnvoyVersions...)
slices.Reverse(supportedVersionsAscending)
for _, v := range supportedVersionsAscending {
envoyVersion := version.Must(version.NewVersion(v))
// e.g. this is 27 in 1.27.4
versionMajorPart := envoyVersion.Segments()[len(envoyVersion.Segments())-2]
// e.g. this is 4 in 1.27.4
versionMinorPart := envoyVersion.Segments()[len(envoyVersion.Segments())-1]
// Create synthetic minor versions from .0 through the actual configured version.
for minor := 0; minor <= versionMinorPart; minor++ {
minorVersion := version.Must(version.NewVersion(fmt.Sprintf("1.%d.%d", versionMajorPart, minor)))
cases = append(cases, testcase{
name: minorVersion.String(),
expect: SupportedProxyFeatures{},
})
}
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
sf, err := DetermineSupportedProxyFeaturesFromString(tc.name)
if tc.expectErr == "" {
require.NoError(t, err)
require.Equal(t, tc.expect, sf)
} else {
testutil.RequireErrorContains(t, err, tc.expectErr)
}
})
}
}