forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NET-6417] Add validation of MeshGateway name + listeners (hashicorp#…
…20425) * Add validation of MeshGateway name + listeners * Adds test for ValidateMeshGateway * Fixes data fetcher test for gatewayproxy --------- Co-authored-by: Nathan Coleman <[email protected]>
- Loading branch information
1 parent
b6f10bc
commit 7c00d39
Showing
3 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package types | ||
|
||
import ( | ||
"github.com/hashicorp/consul/internal/resource" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/internal/resource/resourcetest" | ||
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1" | ||
"github.com/hashicorp/consul/sdk/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateMeshGateway(t *testing.T) { | ||
type testcase struct { | ||
mgwName string | ||
mgw *pbmesh.MeshGateway | ||
expectErr string | ||
} | ||
|
||
run := func(t *testing.T, tc testcase) { | ||
res := resourcetest.Resource(pbmesh.MeshGatewayType, tc.mgwName). | ||
WithData(t, tc.mgw). | ||
Build() | ||
|
||
err := resource.DecodeAndValidate(validateMeshGateway)(res) | ||
|
||
if tc.expectErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
testutil.RequireErrorContains(t, err, tc.expectErr) | ||
} | ||
} | ||
|
||
cases := map[string]testcase{ | ||
"happy path": { | ||
mgwName: "mesh-gateway", | ||
mgw: &pbmesh.MeshGateway{ | ||
Listeners: []*pbmesh.MeshGatewayListener{ | ||
{ | ||
Name: "wan", | ||
}, | ||
}, | ||
}, | ||
expectErr: "", | ||
}, | ||
"wrong name for mesh-gateway": { | ||
mgwName: "my-mesh-gateway", | ||
mgw: &pbmesh.MeshGateway{ | ||
Listeners: []*pbmesh.MeshGatewayListener{ | ||
{ | ||
Name: "wan", | ||
}, | ||
}, | ||
}, | ||
expectErr: "invalid gateway name, must be \"mesh-gateway\"", | ||
}, | ||
"too many listeners on mesh-gateway": { | ||
mgwName: "mesh-gateway", | ||
mgw: &pbmesh.MeshGateway{ | ||
Listeners: []*pbmesh.MeshGatewayListener{ | ||
{ | ||
Name: "obi", | ||
}, | ||
{ | ||
Name: "wan", | ||
}, | ||
}, | ||
}, | ||
expectErr: "invalid listeners, must have exactly one listener", | ||
}, | ||
"zero listeners on mesh-gateway": { | ||
mgwName: "mesh-gateway", | ||
mgw: &pbmesh.MeshGateway{}, | ||
expectErr: "invalid listeners, must have exactly one listener", | ||
}, | ||
"incorrect listener name on mesh-gateway": { | ||
mgwName: "mesh-gateway", | ||
mgw: &pbmesh.MeshGateway{ | ||
Listeners: []*pbmesh.MeshGatewayListener{ | ||
{ | ||
Name: "kenobi", | ||
}, | ||
}, | ||
}, | ||
expectErr: "invalid listener name, must be \"wan\"", | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
run(t, tc) | ||
}) | ||
} | ||
} |