|
| 1 | +// Copyright 2026 apstndb |
| 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 mycli_test |
| 16 | + |
| 17 | +// Dispatch-level READONLY guard regression for the extracted CQL family (#757, |
| 18 | +// re-homed by #778). These live in the external mycli_test package so they can |
| 19 | +// import feature/cql (which imports mycli) without a cycle, and prove the guard |
| 20 | +// fires through the real dispatch path: build the statement from the merged def |
| 21 | +// table exactly as production does, then execute it against a READONLY session. |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/apstndb/spanner-mycli/internal/mycli" |
| 27 | + "github.com/apstndb/spanner-mycli/internal/mycli/feature/cql" |
| 28 | +) |
| 29 | + |
| 30 | +func buildCQL(t *testing.T, cqlText string) mycli.Statement { |
| 31 | + t.Helper() |
| 32 | + defs := mycli.MergedStatementDefs(cql.Feature()) |
| 33 | + stmt, err := mycli.BuildStatementWithDefs(defs, "CQL "+cqlText) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("BuildStatementWithDefs(CQL %s) error = %v", cqlText, err) |
| 36 | + } |
| 37 | + return stmt |
| 38 | +} |
| 39 | + |
| 40 | +// TestReadOnlyGuardBlocksMutatingCQL verifies that mutating CQL is rejected by |
| 41 | +// Session.ExecuteStatement in READONLY mode before the statement can reach the |
| 42 | +// Cassandra adapter. These statements short-circuit at the guard before |
| 43 | +// CQLStatement.Execute, so no adapter/proxy is built. |
| 44 | +func TestReadOnlyGuardBlocksMutatingCQL(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + for _, tt := range []struct { |
| 48 | + desc string |
| 49 | + cql string |
| 50 | + }{ |
| 51 | + {desc: "DELETE blocked", cql: "DELETE FROM t WHERE id = 1"}, |
| 52 | + {desc: "INSERT blocked", cql: "INSERT INTO t (id) VALUES (1)"}, |
| 53 | + {desc: "unrecognized keyword blocked", cql: "FROBNICATE t"}, |
| 54 | + } { |
| 55 | + t.Run(tt.desc, func(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + session := mycli.NewReadOnlySessionForTest(t) |
| 58 | + _, err := session.ExecuteStatement(t.Context(), buildCQL(t, tt.cql)) |
| 59 | + if !mycli.IsReadOnlyError(err) { |
| 60 | + t.Errorf("%s in READONLY mode: got error %v, want READONLY error", tt.desc, err) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TestReadOnlyGuardAllowsReadOnlyCQL verifies, at the dispatch level, that a |
| 67 | +// read-only CQL SELECT is not classified as a static MutationStatement and that |
| 68 | +// the DISPATCH-BUILT statement's conditional classifier reports non-mutating, |
| 69 | +// so the READONLY guard lets it through. Asserting the classification on the |
| 70 | +// statement produced by the real def table (not one built via the constructor) |
| 71 | +// catches a HandleGroups regression that returns a zero-value CQLStatement: |
| 72 | +// with the fail-closed nil-Classify default, that regression would silently |
| 73 | +// block CQL SELECT in READONLY mode (#782 review nit). |
| 74 | +// |
| 75 | +// Unlike the BigQuery counterpart, this does NOT run the SELECT through |
| 76 | +// Session.ExecuteStatement: a passing SELECT proceeds to CQLStatement.Execute, |
| 77 | +// which builds the Cassandra adapter via spancql.NewCluster. That call panics |
| 78 | +// when the adapter client cannot be constructed (no live Spanner backend in |
| 79 | +// tests), so it cannot "fail later for a non-READONLY reason" the way BigQuery's |
| 80 | +// project-not-configured guard does. |
| 81 | +func TestReadOnlyGuardAllowsReadOnlyCQL(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + stmt := buildCQL(t, "SELECT * FROM t") |
| 85 | + if _, isMutation := stmt.(mycli.MutationStatement); isMutation { |
| 86 | + t.Fatal("CQLStatement must not be a static MutationStatement") |
| 87 | + } |
| 88 | + conditional, mutating := mycli.ClassifyForTest(stmt) |
| 89 | + if !conditional { |
| 90 | + t.Fatal("dispatch-built CQLStatement must be a ConditionallyMutatingStatement") |
| 91 | + } |
| 92 | + if mutating { |
| 93 | + t.Fatal("dispatch-built CQL SELECT classifies as mutating; READONLY mode would wrongly block it (HandleGroups regression?)") |
| 94 | + } |
| 95 | + |
| 96 | + // The mutating counterpart, classified on the dispatch-built statement. |
| 97 | + del := buildCQL(t, "DELETE FROM t WHERE id = 1") |
| 98 | + conditional, mutating = mycli.ClassifyForTest(del) |
| 99 | + if !conditional || !mutating { |
| 100 | + t.Fatalf("dispatch-built CQL DELETE: conditional=%v mutating=%v, want true/true", conditional, mutating) |
| 101 | + } |
| 102 | +} |
0 commit comments