Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
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.

88 lines
2.3 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 resource_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/proto/private/prototest"
)
func TestReferenceKey(t *testing.T) {
tenancy1 := &pbresource.Tenancy{}
tenancy1_actual := defaultTenancy()
tenancy2 := &pbresource.Tenancy{
Partition: "ap1",
Namespace: "ns-billing",
PeerName: "peer-dc4",
}
tenancy3 := &pbresource.Tenancy{
Partition: "ap2",
Namespace: "ns-intern",
PeerName: "peer-sea",
}
res1, err := demo.GenerateV2Artist()
require.NoError(t, err)
res1.Id.Tenancy = tenancy1
res2, err := demo.GenerateV2Artist()
require.NoError(t, err)
res2.Id.Tenancy = tenancy2
res3, err := demo.GenerateV2Artist()
require.NoError(t, err)
res3.Id.Tenancy = tenancy3
id1 := res1.Id
id2 := res2.Id
id3 := res3.Id
ref1 := resource.Reference(id1, "")
ref2 := resource.Reference(id2, "")
ref3 := resource.Reference(id3, "")
idRK1 := resource.NewReferenceKey(id1)
idRK2 := resource.NewReferenceKey(id2)
idRK3 := resource.NewReferenceKey(id3)
refRK1 := resource.NewReferenceKey(ref1)
refRK2 := resource.NewReferenceKey(ref2)
refRK3 := resource.NewReferenceKey(ref3)
require.Equal(t, idRK1, refRK1)
require.Equal(t, idRK2, refRK2)
require.Equal(t, idRK3, refRK3)
prototest.AssertDeepEqual(t, tenancy1_actual, idRK1.GetTenancy())
prototest.AssertDeepEqual(t, tenancy2, idRK2.GetTenancy())
prototest.AssertDeepEqual(t, tenancy3, idRK3.GetTenancy())
// Now that we tested the defaulting, swap out the tenancy in the id so
// that the comparisons work.
id1.Tenancy = tenancy1_actual
ref1.Tenancy = tenancy1_actual
prototest.AssertDeepEqual(t, id1, idRK1.ToID())
prototest.AssertDeepEqual(t, id2, idRK2.ToID())
prototest.AssertDeepEqual(t, id3, idRK3.ToID())
prototest.AssertDeepEqual(t, ref1, refRK1.ToReference())
prototest.AssertDeepEqual(t, ref2, refRK2.ToReference())
prototest.AssertDeepEqual(t, ref3, refRK3.ToReference())
}
func defaultTenancy() *pbresource.Tenancy {
return &pbresource.Tenancy{
Partition: "default",
Namespace: "default",
PeerName: "local",
}
}