-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcommand_tree_builder_test.go
More file actions
242 lines (207 loc) · 6.77 KB
/
command_tree_builder_test.go
File metadata and controls
242 lines (207 loc) · 6.77 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcloud
import (
"fmt"
"path"
"slices"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/librarian/internal/sidekick/api"
"github.com/googleapis/librarian/internal/sidekick/parser/httprule"
"github.com/googleapis/librarian/internal/surfer/gcloud/provider"
)
func TestCommandTreeBuilder_Build_EmptyDefaultHost(t *testing.T) {
service := &api.Service{
Name: "parallelstore.googleapis.com",
DefaultHost: "",
Package: "google.cloud.parallelstore.v1",
}
model := &api.API{
Title: "Parallelstore API",
Services: []*api.Service{service},
}
if _, err := newCommandTreeBuilder(model, &provider.Config{}).build(); err == nil {
t.Error("newCommandTreeBuilder().build() error = nil, want error")
}
}
func TestCommandTreeBuilder_Build_Structure(t *testing.T) {
service := mockService("parallelstore.googleapis.com",
mockMethod("CreateInstance", "v1/{parent=projects/*/locations/*}/instances"),
mockMethod("ListInstances", "v1/{parent=projects/*/locations/*}/instances"),
mockMethod("GetOperation", "v1/{name=projects/*/locations/*/operations/*}"),
)
model := &api.API{
Name: "parallelstore",
PackageName: "google.cloud.parallelstore.v1",
Title: "Parallelstore API",
Services: []*api.Service{service},
}
config := &provider.Config{
GenerateOperations: boolPtr(true),
APIs: []provider.API{
{
Name: "parallelstore",
},
},
}
root, err := newCommandTreeBuilder(model, config).build()
if err != nil {
t.Fatalf("build() failed: %v", err)
}
got := flattenTree(root.GA)
want := []string{
"parallelstore/instances/create",
"parallelstore/instances/list",
"parallelstore/operations/describe",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("flattenTree() mismatch (-want +got):\n%s", diff)
}
}
func TestCommandTreeBuilder_Build_Operations_Disabled(t *testing.T) {
service := mockService("parallelstore.googleapis.com", mockMethod("GetOperation", "v1/{name=projects/*/locations/*/operations/*}"))
model := &api.API{
Name: "parallelstore",
Title: "Parallelstore API",
Services: []*api.Service{service},
}
root, err := newCommandTreeBuilder(model, &provider.Config{GenerateOperations: boolPtr(false)}).build()
if err != nil {
t.Fatalf("build() failed: %v", err)
}
got := flattenTree(root.GA)
if len(got) != 0 {
t.Errorf("flattenTree() = %v, want empty when GenerateOperations is false", got)
}
}
func TestCommandTreeBuilder_Build_Operations_Enabled(t *testing.T) {
service := mockService("parallelstore.googleapis.com", mockMethod("GetOperation", "v1/{name=projects/*/locations/*/operations/*}"))
model := &api.API{
Name: "parallelstore",
Title: "Parallelstore API",
Services: []*api.Service{service},
}
root, err := newCommandTreeBuilder(model, &provider.Config{GenerateOperations: boolPtr(true)}).build()
if err != nil {
t.Fatalf("build() failed: %v", err)
}
got := flattenTree(root.GA)
want := []string{
"parallelstore/operations/describe",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("flattenTree() mismatch (-want +got) when GenerateOperations is true:\n%s", diff)
}
}
func TestCommandTreeBuilder_Build_MultipleServices(t *testing.T) {
serviceOne := mockService("ParallelstoreService", mockMethod("CreateInstance", "v1/{parent=projects/*/locations/*}/instances"))
serviceTwo := mockService("OtherParallelstoreService", mockMethod("CreateOtherInstance", "v1/{parent=projects/*/locations/*}/otherInstances"))
model := &api.API{
Name: "parallelstore",
Title: "Parallelstore API",
Services: []*api.Service{serviceOne, serviceTwo},
}
root, err := newCommandTreeBuilder(model, &provider.Config{GenerateOperations: boolPtr(true)}).build()
if err != nil {
t.Fatalf("build() failed: %v", err)
}
got := flattenTree(root.GA)
want := []string{
"parallelstore/instances/create",
"parallelstore/otherInstances/create",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("flattenTree() mismatch (-want +got):\n%s", diff)
}
}
func TestCommandTreeBuilder_Build_MultipleReleaseTracks(t *testing.T) {
serviceOne := mockService("ParallelstoreService", mockMethod("CreateInstance", "v1/{parent=projects/*/locations/*}/instances"))
serviceTwo := mockService("ParallelstoreService", mockMethod("CreateInstance", "v1alpha/{parent=projects/*/locations/*}/instances"))
serviceTwo.Package = "google.cloud.parallelstore.v1alpha"
model := &api.API{
Name: "parallelstore",
Title: "Parallelstore API",
Services: []*api.Service{serviceOne, serviceTwo},
}
root, err := newCommandTreeBuilder(model, &provider.Config{GenerateOperations: boolPtr(true)}).build()
if err != nil {
t.Fatalf("build() failed: %v", err)
}
// GA release track
got := flattenTree(root.GA)
want := []string{
"parallelstore/instances/create",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("flattenTree() mismatch (-want +got):\n%s", diff)
}
// Alpha release track
got = flattenTree(root.ALPHA)
want = []string{
"parallelstore/instances/create",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("flattenTree() mismatch (-want +got):\n%s", diff)
}
}
func flattenTree(g *CommandGroup) []string {
var paths []string
var walk func(prefix string, current *CommandGroup)
walk = func(prefix string, current *CommandGroup) {
for name := range current.Commands {
paths = append(paths, path.Join(prefix, current.Name, name))
}
for _, sub := range current.Groups {
walk(path.Join(prefix, current.Name), sub)
}
}
walk("", g)
slices.Sort(paths)
return paths
}
func mockMethod(name, path string) *api.Method {
pt, err := httprule.ParseResourcePattern(path)
if err != nil {
panic(fmt.Sprintf("failed to parse path %q: %v", path, err))
}
return &api.Method{
Name: name,
PathInfo: &api.PathInfo{
Bindings: []*api.PathBinding{
{
PathTemplate: pt,
},
},
},
InputType: &api.Message{
Fields: []*api.Field{},
},
}
}
func mockService(name string, methods ...*api.Method) *api.Service {
s := &api.Service{
Name: name,
DefaultHost: "parallelstore.googleapis.com",
Package: "google.cloud.parallelstore.v1",
Methods: methods,
}
for _, m := range methods {
m.Service = s
}
return s
}
func boolPtr(b bool) *bool {
return &b
}