File: resource_name_test.go

package info (click to toggle)
golang-github-hashicorp-go-gcp-common 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176 kB
  • sloc: makefile: 2
file content (197 lines) | stat: -rw-r--r-- 5,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gcputil

import (
	"testing"
)

func TestParseSelfLink(t *testing.T) {
	testCases := map[string]struct {
		Expected    *SelfLink
		ShouldError bool
	}{
		"":                                    {ShouldError: true},
		"not/a/real/link":                     {ShouldError: true},
		"//fullresourcename.google.com/foo/A": {ShouldError: true},
		"https://aprojectlesslink.com/v1/foo/A/bar/B": {ShouldError: true},
		"https://validlink.com/v1/projects/A/foos/B": {
			ShouldError: false,
			Expected: &SelfLink{
				Prefix: "https://validlink.com/v1/",
				RelativeResourceName: &RelativeResourceName{
					Name:    "foos",
					TypeKey: "projects/foos",
					IdTuples: map[string]string{
						"projects": "A",
						"foos":     "B",
					},
					OrderedCollectionIds: []string{"projects", "foos"},
				},
			},
		},
	}

	for k, testCase := range testCases {
		actual, err := ParseProjectResourceSelfLink(k)

		if testCase.ShouldError && err == nil {
			t.Errorf("input '%s' should have returned error, instead got: %v", k, actual)
		} else if !testCase.ShouldError {
			if err != nil {
				t.Errorf("input '%s' returned error: %s", k, err)
			} else {
				if testCase.Expected.Prefix != actual.Prefix {
					t.Errorf("input '%s' prefix, expected %s, got %s", k, testCase.Expected.Prefix, actual.Prefix)
					continue
				}
				checkRelativeName(t, k, testCase.Expected.RelativeResourceName, actual.RelativeResourceName)
			}
		}
	}
}

func TestParseFullResourceName(t *testing.T) {
	testCases := map[string]struct {
		Expected    *FullResourceName
		ShouldError bool
	}{
		"":                                     {ShouldError: true},
		"not/a/real/link":                      {ShouldError: true},
		"https://aselflink.com/v1/foo/A/bar/B": {ShouldError: true},
		"https://a.fake.service.com/v1/foo/A/bar/B": {ShouldError: true},
		"//aservice.googleapis.com/foos/A/bars/B": {
			ShouldError: false,
			Expected: &FullResourceName{
				Service: "aservice",
				RelativeResourceName: &RelativeResourceName{
					Name:    "bars",
					TypeKey: "foos/bars",
					IdTuples: map[string]string{
						"foos": "A",
						"bars": "B",
					},
					OrderedCollectionIds: []string{"foos", "bars"},
				},
			},
		},
	}

	for k, testCase := range testCases {
		actual, err := ParseFullResourceName(k)

		if testCase.ShouldError && err == nil {
			t.Errorf("input '%s' should have returned error, instead got: %v", k, actual)
		} else if !testCase.ShouldError {
			if err != nil {
				t.Errorf("input '%s' returned error: %s", k, err)
			} else {
				if testCase.Expected.Service != actual.Service {
					t.Errorf("input '%s' service, expected %s, got %s", k, testCase.Expected.Service, actual.Service)
					continue
				}
				checkRelativeName(t, k, testCase.Expected.RelativeResourceName, actual.RelativeResourceName)
			}
		}
	}
}

func TestParseRelativeName_noErrors(t *testing.T) {
	testCases := map[string]*RelativeResourceName{
		"projects/my-project": {
			Name:    "projects",
			TypeKey: "projects",
			IdTuples: map[string]string{
				"projects": "my-project",
			},
			OrderedCollectionIds: []string{"projects"},
		},
		"foos/bar@": {
			Name:    "foos",
			TypeKey: "foos",
			IdTuples: map[string]string{
				"foos": "bar@",
			},
			OrderedCollectionIds: []string{"foos"},
		},
		"foos/1/bars/2": {
			Name:    "bars",
			TypeKey: "foos/bars",
			IdTuples: map[string]string{
				"foos": "1",
				"bars": "2",
			},
			OrderedCollectionIds: []string{"foos", "bars"},
		},
		"foo/1/global/bar/2": {
			Name:    "bar",
			TypeKey: "foo/bar",
			IdTuples: map[string]string{
				"foo": "1",
				"bar": "2",
			},
			OrderedCollectionIds: []string{"foo", "bar"},
		},
		"foos/{foosId}/global/bars/{barsId}": {
			Name:    "bars",
			TypeKey: "foos/bars",
			IdTuples: map[string]string{
				"foos": "{foosId}",
				"bars": "{barsId}",
			},
			OrderedCollectionIds: []string{"foos", "bars"},
		},
	}

	for k, expected := range testCases {
		actual, err := ParseRelativeName(k)
		if err != nil {
			t.Errorf("passing input '%s' returned error: %s", k, err)
		} else {
			checkRelativeName(t, k, expected, actual)
		}
	}
}

func checkRelativeName(t *testing.T, input string, expected, actual *RelativeResourceName) {
	if expected.TypeKey != actual.TypeKey {
		t.Errorf("Input '%s': expected output type key '%s', actual: '%s'", input, expected.TypeKey, actual.TypeKey)
	} else {
		if len(expected.IdTuples) != len(actual.IdTuples) {
			t.Errorf("Input '%s': IdMap length mismatch: Expected: '%+v', Actual: '%+v'", input, expected.IdTuples, actual.IdTuples)
		}
		for colId, expectedV := range expected.IdTuples {
			if expectedV != actual.IdTuples[colId] {
				t.Errorf("Input '%s': IdMap '%s' mismatch: expected: '%s', actual: '%s'", input, colId, expectedV, actual.IdTuples[colId])
			}
		}
		if len(expected.OrderedCollectionIds) != len(actual.OrderedCollectionIds) {
			t.Errorf("Input '%s': OrderedCollectionIds length mismatch: Expected: '%+v', Actual: '%+v'", input, expected.OrderedCollectionIds, actual.OrderedCollectionIds)
		}
		for i, expectedV := range expected.OrderedCollectionIds {
			if expectedV != actual.OrderedCollectionIds[i] {
				t.Errorf("Input '%s': OrderedCollectionIds['%d'] mismatch: expected: '%s', actual: '%s'", input, i, expectedV, actual.OrderedCollectionIds[i])
			}
		}
	}
}

func TestParseRelativeName_shouldError(t *testing.T) {
	testCases := []string{
		"",
		"/",
		"//",
		"projects",
		"//not.a.real.relname",
		"//cloudresourcemanager.googleapis.com/",
		"cloudresourcemanager.googleapis.com",
		"projects/X/global",
		"projects/X/global/serviceAccounts/",
		"projects//serviceAccounts/X@serviceaccounts.com",
	}

	for _, k := range testCases {
		relName, err := ParseRelativeName(k)
		if err == nil {
			t.Errorf("expected error for incorrect input '%s', actually got %v", k, relName)
		}
	}
}