2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-02-03 06:18:10 +00:00
|
|
|
package xdscommon
|
2020-07-09 22:04:51 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-05 19:19:23 +00:00
|
|
|
"fmt"
|
|
|
|
"slices"
|
2020-07-09 22:04:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
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"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2020-07-09 22:04:51 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2020-07-09 22:04:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDetermineEnvoyVersionFromNode(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
2021-02-26 22:23:15 +00:00
|
|
|
node *envoy_core_v3.Node
|
2020-07-09 22:04:51 +00:00
|
|
|
expect *version.Version
|
|
|
|
}{
|
|
|
|
"empty": {
|
2021-02-26 22:23:15 +00:00
|
|
|
node: &envoy_core_v3.Node{},
|
2020-07-09 22:04:51 +00:00
|
|
|
expect: nil,
|
|
|
|
},
|
|
|
|
"user agent build version but no user agent": {
|
2021-02-26 22:23:15 +00:00
|
|
|
node: &envoy_core_v3.Node{
|
2020-07-09 22:04:51 +00:00
|
|
|
UserAgentName: "",
|
2021-02-26 22:23:15 +00:00
|
|
|
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
|
|
|
|
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
|
|
|
|
Version: &envoy_type_v3.SemanticVersion{
|
2020-07-09 22:04:51 +00:00
|
|
|
MajorNumber: 1,
|
|
|
|
MinorNumber: 14,
|
|
|
|
Patch: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: nil,
|
|
|
|
},
|
|
|
|
"user agent build version with user agent": {
|
2021-02-26 22:23:15 +00:00
|
|
|
node: &envoy_core_v3.Node{
|
2020-07-09 22:04:51 +00:00
|
|
|
UserAgentName: "envoy",
|
2021-02-26 22:23:15 +00:00
|
|
|
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
|
|
|
|
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
|
|
|
|
Version: &envoy_type_v3.SemanticVersion{
|
2020-07-09 22:04:51 +00:00
|
|
|
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) {
|
2023-06-08 14:26:11 +00:00
|
|
|
got := DetermineEnvoyVersionFromNode(tc.node)
|
2020-07-09 22:04:51 +00:00
|
|
|
if tc.expect != nil {
|
|
|
|
require.Equal(t, tc.expect, got)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 22:09:13 +00:00
|
|
|
|
|
|
|
func TestDetermineSupportedProxyFeaturesFromString(t *testing.T) {
|
2024-07-05 19:19:23 +00:00
|
|
|
const errTooOld = "is too old and is not supported by Consul"
|
2020-10-06 22:09:13 +00:00
|
|
|
|
|
|
|
type testcase struct {
|
2024-07-05 19:19:23 +00:00
|
|
|
name string
|
2023-02-03 06:18:10 +00:00
|
|
|
expect SupportedProxyFeatures
|
2020-10-06 22:09:13 +00:00
|
|
|
expectErr string
|
|
|
|
}
|
2024-07-05 19:19:23 +00:00
|
|
|
var cases []testcase
|
2020-10-06 22:09:13 +00:00
|
|
|
|
2024-07-05 19:19:23 +00:00
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
}
|
2020-10-06 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 19:19:23 +00:00
|
|
|
// 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{},
|
|
|
|
})
|
|
|
|
}
|
2020-10-06 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 19:19:23 +00:00
|
|
|
for _, tc := range cases {
|
2020-10-06 22:09:13 +00:00
|
|
|
tc := tc
|
2024-07-05 19:19:23 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
sf, err := DetermineSupportedProxyFeaturesFromString(tc.name)
|
2020-10-06 22:09:13 +00:00
|
|
|
if tc.expectErr == "" {
|
2023-04-24 18:23:24 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-06 22:09:13 +00:00
|
|
|
require.Equal(t, tc.expect, sf)
|
|
|
|
} else {
|
|
|
|
testutil.RequireErrorContains(t, err, tc.expectErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|