mirror of https://github.com/hashicorp/consul
Eric Haberkorn
2 years ago
committed by
GitHub
35 changed files with 1194 additions and 1020 deletions
@ -1,83 +0,0 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package proxycfgglue |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/hashicorp/go-hclog" |
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/hashicorp/consul/acl" |
||||
"github.com/hashicorp/consul/agent/consul/state" |
||||
"github.com/hashicorp/consul/agent/consul/stream" |
||||
"github.com/hashicorp/consul/agent/proxycfg" |
||||
"github.com/hashicorp/consul/agent/structs" |
||||
"github.com/hashicorp/consul/agent/submatview" |
||||
"github.com/hashicorp/consul/proto/private/pbsubscribe" |
||||
"github.com/hashicorp/consul/sdk/testutil" |
||||
) |
||||
|
||||
func TestServerIntentions_Enterprise(t *testing.T) { |
||||
// This test asserts that we also subscribe to the wildcard namespace intention.
|
||||
const ( |
||||
serviceName = "web" |
||||
index = 1 |
||||
) |
||||
|
||||
logger := hclog.NewNullLogger() |
||||
|
||||
ctx, cancel := context.WithCancel(context.Background()) |
||||
t.Cleanup(cancel) |
||||
|
||||
store := submatview.NewStore(logger) |
||||
go store.Run(ctx) |
||||
|
||||
publisher := stream.NewEventPublisher(10 * time.Second) |
||||
publisher.RegisterHandler(pbsubscribe.Topic_ServiceIntentions, |
||||
func(stream.SubscribeRequest, stream.SnapshotAppender) (uint64, error) { return index, nil }, |
||||
false) |
||||
go publisher.Run(ctx) |
||||
|
||||
intentions := ServerIntentions(ServerDataSourceDeps{ |
||||
ACLResolver: newStaticResolver(acl.ManageAll()), |
||||
ViewStore: store, |
||||
EventPublisher: publisher, |
||||
Logger: logger, |
||||
}) |
||||
|
||||
eventCh := make(chan proxycfg.UpdateEvent) |
||||
require.NoError(t, intentions.Notify(ctx, &structs.ServiceSpecificRequest{ |
||||
EnterpriseMeta: *acl.DefaultEnterpriseMeta(), |
||||
ServiceName: serviceName, |
||||
}, "", eventCh)) |
||||
|
||||
testutil.RunStep(t, "initial snapshot", func(t *testing.T) { |
||||
getEventResult[structs.Intentions](t, eventCh) |
||||
}) |
||||
|
||||
testutil.RunStep(t, "publish a namespace-wildcard partition", func(t *testing.T) { |
||||
publisher.Publish([]stream.Event{ |
||||
{ |
||||
Topic: pbsubscribe.Topic_ServiceIntentions, |
||||
Index: index + 1, |
||||
Payload: state.EventPayloadConfigEntry{ |
||||
Op: pbsubscribe.ConfigEntryUpdate_Upsert, |
||||
Value: &structs.ServiceIntentionsConfigEntry{ |
||||
Name: structs.WildcardSpecifier, |
||||
EnterpriseMeta: *acl.WildcardEnterpriseMeta(), |
||||
Sources: []*structs.SourceIntention{ |
||||
{Name: structs.WildcardSpecifier, Action: structs.IntentionActionAllow, Precedence: 1}, |
||||
}, |
||||
}, |
||||
}, |
||||
}, |
||||
}) |
||||
|
||||
result := getEventResult[structs.Intentions](t, eventCh) |
||||
require.Len(t, result, 1) |
||||
}) |
||||
} |
@ -0,0 +1,74 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//go:build !consulent
|
||||
// +build !consulent
|
||||
|
||||
package structs |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
|
||||
"github.com/hashicorp/consul/sdk/testutil" |
||||
) |
||||
|
||||
func TestEnterprise_ServiceIntentionsConfigEntry(t *testing.T) { |
||||
type testcase struct { |
||||
entry *ServiceIntentionsConfigEntry |
||||
legacy bool |
||||
normalizeErr string |
||||
validateErr string |
||||
// check is called between normalize and validate
|
||||
check func(t *testing.T, entry *ServiceIntentionsConfigEntry) |
||||
} |
||||
|
||||
cases := map[string]testcase{ |
||||
"No sameness groups": { |
||||
entry: &ServiceIntentionsConfigEntry{ |
||||
Kind: ServiceIntentions, |
||||
Name: "test", |
||||
Sources: []*SourceIntention{ |
||||
{ |
||||
Name: "foo", |
||||
SamenessGroup: "blah", |
||||
Action: IntentionActionAllow, |
||||
}, |
||||
}, |
||||
}, |
||||
validateErr: `Sources[0].SamenessGroup: Sameness groups are a Consul Enterprise feature.`, |
||||
}, |
||||
} |
||||
for name, tc := range cases { |
||||
tc := tc |
||||
t.Run(name, func(t *testing.T) { |
||||
var err error |
||||
if tc.legacy { |
||||
err = tc.entry.LegacyNormalize() |
||||
} else { |
||||
err = tc.entry.Normalize() |
||||
} |
||||
if tc.normalizeErr != "" { |
||||
testutil.RequireErrorContains(t, err, tc.normalizeErr) |
||||
return |
||||
} |
||||
require.NoError(t, err) |
||||
|
||||
if tc.check != nil { |
||||
tc.check(t, tc.entry) |
||||
} |
||||
|
||||
if tc.legacy { |
||||
err = tc.entry.LegacyValidate() |
||||
} else { |
||||
err = tc.entry.Validate() |
||||
} |
||||
if tc.validateErr != "" { |
||||
testutil.RequireErrorContains(t, err, tc.validateErr) |
||||
return |
||||
} |
||||
require.NoError(t, err) |
||||
}) |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue