blob: 4d806ec9c36f7bcd7a2fc6d9164d8ce439c12068 [file] [log] [blame]
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tpgresource
import "testing"
func TestCompareSelfLinkOrResourceName(t *testing.T) {
cases := map[string]struct {
Old, New string
Expect bool
}{
"name only, same": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "a-network",
Expect: true,
},
"name only, different": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "another-network",
Expect: false,
},
"partial path, same": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/your-project/global/networks/a-network",
Expect: true,
},
"partial path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/your-project/global/networks/another-network",
Expect: false,
},
"partial path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "projects/another-project/global/networks/a-network",
Expect: false,
},
"full path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/another-network",
Expect: false,
},
"full path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/v1/projects/another-project/global/networks/a-network",
Expect: false,
},
"beta full path, same": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/a-network",
Expect: true,
},
"beta full path, different name": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/your-project/global/networks/another-network",
Expect: false,
},
"beta full path, different project": {
Old: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
New: "https://www.googleapis.com/compute/beta/projects/another-project/global/networks/a-network",
Expect: false,
},
}
for tn, tc := range cases {
if CompareSelfLinkOrResourceName("", tc.Old, tc.New, nil) != tc.Expect {
t.Errorf("bad: %s, expected %t for old = %q and new = %q", tn, tc.Expect, tc.Old, tc.New)
}
}
}
func TestGetResourceNameFromSelfLink(t *testing.T) {
cases := map[string]struct {
SelfLink, ExpectedName string
}{
"name is extracted from self_link": {
SelfLink: "http://something.com/one/two/three",
ExpectedName: "three",
},
"name is returned if the self_link only contains the name": {
SelfLink: "resource_name",
ExpectedName: "resource_name",
},
}
for tn, tc := range cases {
if n := GetResourceNameFromSelfLink(tc.SelfLink); n != tc.ExpectedName {
t.Errorf("%s: expected resource name %q; got %q", tn, tc.ExpectedName, n)
}
}
}
func TestSelfLinkNameHash(t *testing.T) {
cases := map[string]struct {
SelfLink, Name string
Expect bool
}{
"same": {
SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
Name: "a-network",
Expect: true,
},
"different": {
SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network",
Name: "another-network",
Expect: false,
},
}
for tn, tc := range cases {
if (SelfLinkNameHash(tc.SelfLink) == SelfLinkNameHash(tc.Name)) != tc.Expect {
t.Errorf("%s: expected %t for whether hashes matched for self link = %q, name = %q", tn, tc.Expect, tc.SelfLink, tc.Name)
}
}
}
func TestGetRegionFromRegionalSelfLink(t *testing.T) {
cases := map[string]string{
"projects/foo/locations/europe-north1/datasets/bar/operations/foobar": "europe-north1",
"projects/REDACTED/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "europe-north1",
}
for input, expected := range cases {
if result := GetRegionFromRegionalSelfLink(input); result != expected {
t.Errorf("expected to get %q from %q, got %q", expected, input, result)
}
}
}