| 1 |
// Copyright 2025 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package protoc |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"os" |
| 20 |
"os/exec" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/execv" |
| 27 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/request" |
| 28 |
"github.com/google/go-cmp/cmp" |
| 29 |
) |
| 30 |
|
| 31 |
// mockConfigProvider is a mock implementation of the ConfigProvider interface for testing. |
| 32 |
type mockConfigProvider struct { |
| 33 |
gapicImportPath string |
| 34 |
serviceYAML string |
| 35 |
grpcServiceConfig string |
| 36 |
transport string |
| 37 |
releaseLevel string |
| 38 |
metadata bool |
| 39 |
diregapic bool |
| 40 |
restNumericEnums bool |
| 41 |
hasGoGRPC bool |
| 42 |
hasGAPIC bool |
| 43 |
hasLegacyGRPC bool |
| 44 |
} |
| 45 |
|
| 46 |
func (m *mockConfigProvider) GAPICImportPath() string { return m.gapicImportPath } |
| 47 |
func (m *mockConfigProvider) ServiceYAML() string { return m.serviceYAML } |
| 48 |
func (m *mockConfigProvider) GRPCServiceConfig() string { return m.grpcServiceConfig } |
| 49 |
func (m *mockConfigProvider) Transport() string { return m.transport } |
| 50 |
func (m *mockConfigProvider) ReleaseLevel() string { return m.releaseLevel } |
| 51 |
func (m *mockConfigProvider) HasMetadata() bool { return m.metadata } |
| 52 |
func (m *mockConfigProvider) HasDiregapic() bool { return m.diregapic } |
| 53 |
func (m *mockConfigProvider) HasRESTNumericEnums() bool { return m.restNumericEnums } |
| 54 |
func (m *mockConfigProvider) HasGoGRPC() bool { return m.hasGoGRPC } |
| 55 |
func (m *mockConfigProvider) HasGAPIC() bool { return m.hasGAPIC } |
| 56 |
func (m *mockConfigProvider) HasLegacyGRPC() bool { return m.hasLegacyGRPC } |
| 57 |
|
| 58 |
func TestBuild(t *testing.T) { |
| 59 |
// The testdata directory is a curated version of a valid protoc |
| 60 |
// import path that contains all the necessary proto definitions. |
| 61 |
sourceDir, err := filepath.Abs("../testdata/generate/source") |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("failed to get absolute path for sourceDir: %v", err) |
| 64 |
} |
| 65 |
tests := []struct { |
| 66 |
name string |
| 67 |
apiPath string |
| 68 |
apiServiceDir string |
| 69 |
reqID string |
| 70 |
config mockConfigProvider |
| 71 |
want []string |
| 72 |
}{ |
| 73 |
{ |
| 74 |
name: "go_grpc_library rule", |
| 75 |
apiPath: "google/cloud/workflows/v1", |
| 76 |
reqID: "workflows", |
| 77 |
config: mockConfigProvider{ |
| 78 |
gapicImportPath: "cloud.google.com/go/workflows/apiv1;workflows", |
| 79 |
transport: "grpc", |
| 80 |
grpcServiceConfig: "workflows_grpc_service_config.json", |
| 81 |
serviceYAML: "workflows_v1.yaml", |
| 82 |
releaseLevel: "ga", |
| 83 |
metadata: true, |
| 84 |
restNumericEnums: true, |
| 85 |
hasGoGRPC: true, |
| 86 |
hasGAPIC: true, |
| 87 |
}, |
| 88 |
want: []string{ |
| 89 |
"protoc", |
| 90 |
"--experimental_allow_proto3_optional", |
| 91 |
"--go_out=/output", |
| 92 |
"--go-grpc_out=/output", |
| 93 |
"--go-grpc_opt=require_unimplemented_servers=false", |
| 94 |
"--go_gapic_out=/output", |
| 95 |
"--go_gapic_opt=go-gapic-package=cloud.google.com/go/workflows/apiv1;workflows", |
| 96 |
"--go_gapic_opt=api-service-config=" + filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows_v1.yaml"), |
| 97 |
"--go_gapic_opt=grpc-service-config=" + filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows_grpc_service_config.json"), |
| 98 |
"--go_gapic_opt=transport=grpc", |
| 99 |
"--go_gapic_opt=release-level=ga", |
| 100 |
"--go_gapic_opt=metadata", |
| 101 |
"--go_gapic_opt=rest-numeric-enums", |
| 102 |
"-I=" + sourceDir, |
| 103 |
filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows.proto"), |
| 104 |
}, |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "go_proto_library rule with legacy gRPC", |
| 108 |
apiPath: "google/cloud/secretmanager/v1beta2", |
| 109 |
reqID: "secretmanager", |
| 110 |
config: mockConfigProvider{ |
| 111 |
gapicImportPath: "cloud.google.com/go/secretmanager/apiv1beta2;secretmanager", |
| 112 |
transport: "grpc", |
| 113 |
grpcServiceConfig: "secretmanager_grpc_service_config.json", |
| 114 |
serviceYAML: "secretmanager_v1beta2.yaml", |
| 115 |
releaseLevel: "ga", |
| 116 |
metadata: true, |
| 117 |
restNumericEnums: true, |
| 118 |
hasGoGRPC: false, |
| 119 |
hasGAPIC: true, |
| 120 |
hasLegacyGRPC: true, |
| 121 |
}, |
| 122 |
want: []string{ |
| 123 |
"protoc", |
| 124 |
"--experimental_allow_proto3_optional", |
| 125 |
"--go_v1_out=/output", |
| 126 |
"--go_v1_opt=plugins=grpc", |
| 127 |
"--go_gapic_out=/output", |
| 128 |
"--go_gapic_opt=go-gapic-package=cloud.google.com/go/secretmanager/apiv1beta2;secretmanager", |
| 129 |
"--go_gapic_opt=api-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_v1beta2.yaml"), |
| 130 |
"--go_gapic_opt=grpc-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_grpc_service_config.json"), |
| 131 |
"--go_gapic_opt=transport=grpc", |
| 132 |
"--go_gapic_opt=release-level=ga", |
| 133 |
"--go_gapic_opt=metadata", |
| 134 |
"--go_gapic_opt=rest-numeric-enums", |
| 135 |
"-I=" + sourceDir, |
| 136 |
filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager.proto"), |
| 137 |
}, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "go_proto_library rule without legacy gRPC", |
| 141 |
apiPath: "google/cloud/secretmanager/v1beta2", |
| 142 |
reqID: "secretmanager", |
| 143 |
config: mockConfigProvider{ |
| 144 |
gapicImportPath: "cloud.google.com/go/secretmanager/apiv1beta2;secretmanager", |
| 145 |
transport: "grpc", |
| 146 |
grpcServiceConfig: "secretmanager_grpc_service_config.json", |
| 147 |
serviceYAML: "secretmanager_v1beta2.yaml", |
| 148 |
releaseLevel: "ga", |
| 149 |
metadata: true, |
| 150 |
restNumericEnums: true, |
| 151 |
hasGoGRPC: false, |
| 152 |
hasGAPIC: true, |
| 153 |
}, |
| 154 |
want: []string{ |
| 155 |
"protoc", |
| 156 |
"--experimental_allow_proto3_optional", |
| 157 |
"--go_v1_out=/output", |
| 158 |
"--go_gapic_out=/output", |
| 159 |
"--go_gapic_opt=go-gapic-package=cloud.google.com/go/secretmanager/apiv1beta2;secretmanager", |
| 160 |
"--go_gapic_opt=api-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_v1beta2.yaml"), |
| 161 |
"--go_gapic_opt=grpc-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_grpc_service_config.json"), |
| 162 |
"--go_gapic_opt=transport=grpc", |
| 163 |
"--go_gapic_opt=release-level=ga", |
| 164 |
"--go_gapic_opt=metadata", |
| 165 |
"--go_gapic_opt=rest-numeric-enums", |
| 166 |
"-I=" + sourceDir, |
| 167 |
filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager.proto"), |
| 168 |
}, |
| 169 |
}, |
| 170 |
{ |
| 171 |
// Note: we don't have a separate test directory with a proto-only library; |
| 172 |
// the config is used to say "don't generate GAPIC". |
| 173 |
name: "proto-only", |
| 174 |
apiPath: "google/cloud/secretmanager/v1beta2", |
| 175 |
reqID: "secretmanager", |
| 176 |
config: mockConfigProvider{ |
| 177 |
hasGoGRPC: false, |
| 178 |
hasGAPIC: false, |
| 179 |
}, |
| 180 |
want: []string{ |
| 181 |
"protoc", |
| 182 |
"--experimental_allow_proto3_optional", |
| 183 |
"--go_v1_out=/output", |
| 184 |
"-I=" + sourceDir, |
| 185 |
filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager.proto"), |
| 186 |
}, |
| 187 |
}, |
| 188 |
} |
| 189 |
|
| 190 |
for _, tt := range tests { |
| 191 |
t.Run(tt.name, func(t *testing.T) { |
| 192 |
req := &request.Library{ |
| 193 |
ID: tt.reqID, |
| 194 |
} |
| 195 |
api := &request.API{ |
| 196 |
Path: tt.apiPath, |
| 197 |
} |
| 198 |
|
| 199 |
got, err := Build(req, api, filepath.Join(sourceDir, tt.apiPath), &tt.config, sourceDir, "/output") |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("Build() failed: %v", err) |
| 202 |
} |
| 203 |
|
| 204 |
if diff := cmp.Diff(tt.want, got); diff != "" { |
| 205 |
t.Errorf("Build() mismatch (-want +got):\n%s", diff) |
| 206 |
} |
| 207 |
}) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestRun_Integration(t *testing.T) { |
| 212 |
// Perform a "health check" on the required protoc plugins. Instead of just |
| 213 |
// checking for their existence (which could lead to a hanging test if the |
| 214 |
// binary is broken), we run them with a --version flag and a timeout. |
| 215 |
plugins := []string{"protoc-gen-go", "protoc-gen-go_gapic"} |
| 216 |
for _, plugin := range plugins { |
| 217 |
if _, err := exec.LookPath(plugin); err != nil { |
| 218 |
t.Skipf("%s not found in PATH, skipping integration test", plugin) |
| 219 |
} |
| 220 |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 221 |
defer cancel() |
| 222 |
if err := exec.CommandContext(ctx, plugin, "--version").Run(); err != nil { |
| 223 |
t.Skipf("%s is not responsive (--version failed or timed out), skipping integration test", plugin) |
| 224 |
} |
| 225 |
} |
| 226 |
if _, err := exec.LookPath("protoc"); err != nil { |
| 227 |
t.Skip("protoc not found in PATH, skipping integration test") |
| 228 |
} |
| 229 |
|
| 230 |
// Setup temporary directory for the output. |
| 231 |
outputDir := t.TempDir() |
| 232 |
|
| 233 |
// The testdata/generate/source directory is a miniature version of a valid protoc |
| 234 |
// import path that contains all the necessary proto definitions. |
| 235 |
sourceDir, err := filepath.Abs("../testdata/generate/source") |
| 236 |
if err != nil { |
| 237 |
t.Fatalf("failed to get absolute path for sourceDir: %v", err) |
| 238 |
} |
| 239 |
protoPath := filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows.proto") |
| 240 |
|
| 241 |
args := []string{ |
| 242 |
"protoc", |
| 243 |
"--go_out=" + outputDir, |
| 244 |
"-I=" + sourceDir, |
| 245 |
protoPath, |
| 246 |
} |
| 247 |
|
| 248 |
if err := execv.Run(context.Background(), args, outputDir); err != nil { |
| 249 |
t.Fatalf("Run() failed: %v", err) |
| 250 |
} |
| 251 |
|
| 252 |
// Check that a .pb.go file was created somewhere in the output directory. |
| 253 |
// We don't check the exact path because it's determined by the go_package |
| 254 |
// option in the proto file, and we don't want the test to be too brittle. |
| 255 |
var found bool |
| 256 |
err = filepath.Walk(outputDir, func(path string, info os.FileInfo, err error) error { |
| 257 |
if err != nil { |
| 258 |
return err |
| 259 |
} |
| 260 |
if !info.IsDir() && strings.HasSuffix(info.Name(), ".pb.go") { |
| 261 |
found = true |
| 262 |
} |
| 263 |
return nil |
| 264 |
}) |
| 265 |
if err != nil { |
| 266 |
t.Fatalf("failed to walk output directory: %v", err) |
| 267 |
} |
| 268 |
|
| 269 |
if !found { |
| 270 |
t.Error("Run() did not create any .pb.go files") |
| 271 |
} |
| 272 |
} |