AlexStocks commented on code in PR #947: URL: https://github.com/apache/dubbo-go-pixiu/pull/947#discussion_r3528720529
########## admin/initialize/router_opa_test.go: ########## @@ -0,0 +1,403 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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 initialize + +import ( + "bytes" + "encoding/json" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" +) + +import ( + "github.com/gin-gonic/gin" + + "github.com/golang-jwt/jwt/v4" +) + +import ( + adminconfig "github.com/apache/dubbo-go-pixiu/admin/config" + "github.com/apache/dubbo-go-pixiu/admin/controller/auth" +) + +// This file is the only place in the test suite that exercises the full +// admin startup pipeline that handles OPA requests: +// +// YAML → adminconfig.Bootstrap.OPA +// → initialize.Routers() registers /config/api/opa/policy +// → auth.JWTAuth() middleware validates "token" header +// → opa.PutOPAPolicy/GetOPAPolicy/DeleteOPAPolicy handlers +// → logic.BizPut/Get/Delete... → real HTTP call to OPA server +// +// We stand up an httptest server as the OPA backend, point +// adminconfig.Bootstrap.OPA.ServerURL at it, and sign JWTs with the +// SAME hardcoded SignKey ("dubbo-go-pixiu") the middleware reads from +// admin/controller/auth/auth.go:83. +// ----- helpers --------------------------------------------------------------- +type recordedRequest struct { + method string + path string + ctype string + auth string + body string +} + +type mockOPA struct { + srv *httptest.Server + mu sync.Mutex + recs []recordedRequest + // policies stores PUT'd bodies keyed by policy ID so GETs return them. + policies map[string]string + // nextStatus, if non-zero, overrides the success status on the next request. + nextStatus int + nextBody string +} + +func newMockOPA(t *testing.T) *mockOPA { + t.Helper() + m := &mockOPA{policies: map[string]string{}} + m.srv = httptest.NewServer(http.HandlerFunc(m.handle)) Review Comment: [P1] 新增 OPA 路由测试在当前环境下无法稳定命中 mock OPA。 我在本 PR head `15141102841c173dc9672e68fafcdc29d168f805` 上执行 `go test ./admin/config ./admin/controller/opa ./admin/initialize ./admin/logic`,`admin/config` 和 `admin/controller/opa` 通过,但 `admin/initialize` 与 `admin/logic` 失败。失败用例里的 `PUT /config/api/opa/policy` 都返回 `OPA connection failed: Put "http://127.0.0.1:<port>/v1/policies/...": dial tcp ... failed to respond`,说明请求没有到达这里创建的 `httptest` mock。`admin/logic/opa_test.go` 里已经通过 `setOPAHTTPClient(t, directOPAHTTPClient())` 禁掉代理并替换全局 `opaHTTPClient`,但这里和 `e2e_opa_test.go` 的 mock 没有做同样隔离;在带本机代理/特殊 HTTP transport 的环境下测试会直接失败。需要把同一套 HTTP client 隔离 helper 复用到 `admin/initialize` 的 router/e2e 测试,或把业务代码改为可注入 client,并确保 `go test ./admin/initialize ./admin/logic` 在无真实 OPA、无 Docker 的环境下稳定通 过。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
