vault/helper/namespace/namespace_test.go
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* 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 https://hashi.co/bsl-blog, 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 BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

175 lines
2.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package namespace
import (
"testing"
)
func TestSplitIDFromString(t *testing.T) {
tcases := []struct {
input string
id string
prefix string
}{
{
"foo",
"",
"foo",
},
{
"foo.id",
"id",
"foo",
},
{
"foo.foo.id",
"id",
"foo.foo",
},
{
"foo.foo/foo.id",
"id",
"foo.foo/foo",
},
{
"foo.foo/.id",
"id",
"foo.foo/",
},
{
"foo.foo/foo",
"",
"foo.foo/foo",
},
{
"foo.foo/f",
"",
"foo.foo/f",
},
{
"foo.foo/",
"",
"foo.foo/",
},
{
"b.foo",
"",
"b.foo",
},
{
"s.foo",
"",
"s.foo",
},
{
"t.foo",
"foo",
"t",
},
}
for _, c := range tcases {
pre, id := SplitIDFromString(c.input)
if pre != c.prefix || id != c.id {
t.Fatalf("bad test case: %s != %s, %s != %s", pre, c.prefix, id, c.id)
}
}
}
func TestHasParent(t *testing.T) {
// Create ns1
ns1 := &Namespace{
ID: "id1",
Path: "ns1/",
}
// Create ns1/ns2
ns2 := &Namespace{
ID: "id2",
Path: "ns1/ns2/",
}
// Create ns1/ns2/ns3
ns3 := &Namespace{
ID: "id3",
Path: "ns1/ns2/ns3/",
}
// Create ns4
ns4 := &Namespace{
ID: "id4",
Path: "ns4/",
}
// Create ns4/ns5
ns5 := &Namespace{
ID: "id5",
Path: "ns4/ns5/",
}
tests := []struct {
name string
parent *Namespace
ns *Namespace
expected bool
}{
{
"is root an ancestor of ns1",
RootNamespace,
ns1,
true,
},
{
"is ns1 an ancestor of ns2",
ns1,
ns2,
true,
},
{
"is ns2 an ancestor of ns3",
ns2,
ns3,
true,
},
{
"is ns1 an ancestor of ns3",
ns1,
ns3,
true,
},
{
"is root an ancestor of ns3",
RootNamespace,
ns3,
true,
},
{
"is ns4 an ancestor of ns3",
ns4,
ns3,
false,
},
{
"is ns5 an ancestor of ns3",
ns5,
ns3,
false,
},
{
"is ns1 an ancestor of ns5",
ns1,
ns5,
false,
},
}
for _, test := range tests {
actual := test.ns.HasParent(test.parent)
if actual != test.expected {
t.Fatalf("bad ancestor calculation; name: %q, actual: %t, expected: %t", test.name, actual, test.expected)
}
}
}