|
| 1 | +// Copyright 2025 The etcd Authors |
| 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 coverage_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestInterfaceUse(t *testing.T) { |
| 25 | + files, err := os.ReadDir("testdata") |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + for _, file := range files { |
| 31 | + filename := file.Name() |
| 32 | + t.Run(filename, func(t *testing.T) { testInterfaceUse(t, filename) }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func testInterfaceUse(t *testing.T, filename string) { |
| 37 | + b, err := os.ReadFile(filepath.Join("testdata", filename)) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("read test data: %v", err) |
| 40 | + } |
| 41 | + var dump Dump |
| 42 | + err = json.Unmarshal(b, &dump) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("unmarshal testdata %s: %v", filename, err) |
| 45 | + } |
| 46 | + traces, apiserverOnly, etcdOnly := filterOutIncomplete(dump) |
| 47 | + if apiserverOnly > 0 { |
| 48 | + t.Logf("WARNING: some traces are present in Apiserver only: %d > 0", apiserverOnly) |
| 49 | + } |
| 50 | + if etcdOnly > 0 { |
| 51 | + t.Logf("WARNING: some traces are present in Etcd only: %d > 0", etcdOnly) |
| 52 | + } |
| 53 | + |
| 54 | + t.Run("interface_bypass", func(t *testing.T) { |
| 55 | + var interfaceBypass []Trace |
| 56 | + for _, trace := range traces { |
| 57 | + if !isContractCall(trace) { |
| 58 | + interfaceBypass = append(interfaceBypass, trace) |
| 59 | + } |
| 60 | + } |
| 61 | + t.Logf("Traces which did not go through the interface: %d / %d", len(interfaceBypass), len(traces)) |
| 62 | + |
| 63 | + knownBypass := map[string]bool{ |
| 64 | + "etcdserverpb.Lease/LeaseGrant": true, // Used to manage masterleases and events |
| 65 | + "etcdserverpb.Maintenance/Status": true, // Used to expose database size on apiserver's metrics endpoint |
| 66 | + "etcdserverpb.Watch/Watch": true, // Not part of the contract interface (yet) |
| 67 | + "etcdserverpb.KV/Compact": true, // Compaction should move to using internal Etcd mechanism |
| 68 | + } |
| 69 | + bypassByOperationName := make(map[string]int) |
| 70 | + for _, trace := range interfaceBypass { |
| 71 | + for _, span := range trace.Spans { |
| 72 | + if span.ServiceName != "etcd" { |
| 73 | + continue |
| 74 | + } |
| 75 | + opName := span.OperationName |
| 76 | + if opName == "etcdserverpb.KV/Txn" || opName == "etcdserverpb.KV/Range" { |
| 77 | + // TODO: Associate etcdserverpb.KV/{Txn,Range}s with known operations: |
| 78 | + // key == "/registry/health" -> healthcheck |
| 79 | + // key == "compact_rev_key" -> compaction |
| 80 | + // count_only -> count |
| 81 | + // strings.Count(key, "/") == 2, limit == 1 -> consistent read |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + bypassByOperationName[opName]++ |
| 86 | + |
| 87 | + // Print the first span to make it easier to troubleshoot the test. |
| 88 | + if bypassByOperationName[opName] == 1 && !knownBypass[opName] { |
| 89 | + t.Logf("%s: %+v", opName, span) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + t.Logf("contract bypass count by operation: %+v", bypassByOperationName) |
| 94 | + for op := range bypassByOperationName { |
| 95 | + if !knownBypass[op] { |
| 96 | + t.Errorf("operation not found in the set of known interface bypasses: %s", op) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + t.Run("contract_methods_set_rev_when_needed", func(t *testing.T) { |
| 101 | + for op, percentageWithRev := range map[string]float64{ |
| 102 | + "Get kubernetesEtcdContract": 0, // Conformance tests don't issue stale reads |
| 103 | + "OptimisticPut kubernetesEtcdContract": 0.7, |
| 104 | + "OptimisticDelete kubernetesEtcdContract": 1, |
| 105 | + "List kubernetesEtcdContract": 0.8, |
| 106 | + } { |
| 107 | + t.Run(op, func(t *testing.T) { |
| 108 | + operationSpans := make([]Span, 0, len(traces)) |
| 109 | + for _, trace := range traces { |
| 110 | + for _, span := range trace.Spans { |
| 111 | + if span.OperationName == op { |
| 112 | + operationSpans = append(operationSpans, span) |
| 113 | + break |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + t.Logf("Found %d traces matching %s operation", len(operationSpans), op) |
| 118 | + |
| 119 | + result := map[string]int{ |
| 120 | + "unset": 0, |
| 121 | + "set": 0, |
| 122 | + "missing": 0, // absent in collected spans belonging to the trace |
| 123 | + } |
| 124 | + for _, span := range operationSpans { |
| 125 | + result[revFromSpan(span)]++ |
| 126 | + } |
| 127 | + t.Logf("Distribution of revision values: %+v", result) |
| 128 | + |
| 129 | + if c := result["missing"]; c > 0 { |
| 130 | + t.Errorf("some traces are missing revision tag, count=%d", c) |
| 131 | + } |
| 132 | + total := float64(len(operationSpans)) |
| 133 | + if share := float64(result["set"]) / total; share > percentageWithRev+0.2 || share < percentageWithRev-0.2 { |
| 134 | + t.Errorf("expected rev>0 range calls %.2f to be close [±20%%] to %.2f", share, percentageWithRev) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | + }) |
| 139 | +} |
0 commit comments