|
| 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 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/olekukonko/tablewriter" |
| 27 | + "github.com/olekukonko/tablewriter/tw" |
| 28 | + traceservice "go.opentelemetry.io/proto/otlp/collector/trace/v1" |
| 29 | + tracev1 "go.opentelemetry.io/proto/otlp/trace/v1" |
| 30 | + "google.golang.org/protobuf/encoding/protojson" |
| 31 | +) |
| 32 | + |
| 33 | +func TestInterfaceUse(t *testing.T) { |
| 34 | + files, err := os.ReadDir("testdata") |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + for _, file := range files { |
| 40 | + filename := file.Name() |
| 41 | + if filename == ".gitignore" { |
| 42 | + continue |
| 43 | + } |
| 44 | + t.Run(filename, func(t *testing.T) { testInterfaceUse(t, filename) }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func testInterfaceUse(t *testing.T, filename string) { |
| 49 | + b, err := os.ReadFile(filepath.Join("testdata", filename)) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("read test data: %v", err) |
| 52 | + } |
| 53 | + var dump Dump |
| 54 | + err = json.Unmarshal(b, &dump) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("unmarshal testdata %s: %v", filename, err) |
| 57 | + } |
| 58 | + if dump.Result == nil { |
| 59 | + t.Fatalf("missing result data") |
| 60 | + } |
| 61 | + traces := dump.Result |
| 62 | + |
| 63 | + callsByOperationName := make(map[string]int) |
| 64 | + for _, trace := range traces.GetResourceSpans() { |
| 65 | + serviceName := getServiceName(trace) |
| 66 | + if serviceName != "etcd" { |
| 67 | + continue |
| 68 | + } |
| 69 | + opName := getOperationName(trace) |
| 70 | + callsByOperationName[opName]++ |
| 71 | + } |
| 72 | + t.Logf("\n%s", printableCallTable(callsByOperationName)) |
| 73 | + |
| 74 | + knownMethodsUsedByKubernetes := map[string]bool{ |
| 75 | + "etcdserverpb.KV/Range": true, // All calls should go through etcd-k8s interface |
| 76 | + "etcdserverpb.KV/Txn": true, // All calls should go through etcd-k8s interface |
| 77 | + "etcdserverpb.KV/Compact": true, // Compaction should move to using internal Etcd mechanism |
| 78 | + "etcdserverpb.Watch/Watch": true, // Not part of the contract interface (yet) |
| 79 | + "etcdserverpb.Lease/LeaseGrant": true, // Used to manage masterleases and events |
| 80 | + "etcdserverpb.Maintenance/Status": true, // Used to expose database size on apiserver's metrics endpoint |
| 81 | + } |
| 82 | + for method := range knownMethodsUsedByKubernetes { |
| 83 | + t.Run(method, func(t *testing.T) { |
| 84 | + if _, ok := callsByOperationName[method]; !ok { |
| 85 | + t.Errorf("expected %q method to be called at least once", method) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | + t.Run("only_expected_methods_were_called", func(t *testing.T) { |
| 90 | + for opName := range callsByOperationName { |
| 91 | + if !knownMethodsUsedByKubernetes[opName] { |
| 92 | + t.Errorf("method called outside the list: %s", opName) |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +type Traces struct { |
| 99 | + traceservice.ExportTraceServiceRequest |
| 100 | +} |
| 101 | + |
| 102 | +func (t *Traces) UnmarshalJSON(b []byte) error { |
| 103 | + return protojson.Unmarshal(b, &t.ExportTraceServiceRequest) |
| 104 | +} |
| 105 | + |
| 106 | +type Dump struct { |
| 107 | + Result *Traces `json:"result"` |
| 108 | +} |
| 109 | + |
| 110 | +func getServiceName(trace *tracev1.ResourceSpans) string { |
| 111 | + for _, kv := range trace.GetResource().GetAttributes() { |
| 112 | + if kv.GetKey() == "service.name" { |
| 113 | + return kv.GetValue().GetStringValue() |
| 114 | + } |
| 115 | + } |
| 116 | + return "" |
| 117 | +} |
| 118 | + |
| 119 | +func getOperationName(trace *tracev1.ResourceSpans) string { |
| 120 | + for _, scopeSpan := range trace.GetScopeSpans() { |
| 121 | + for _, span := range scopeSpan.GetSpans() { |
| 122 | + name := span.GetName() |
| 123 | + if strings.HasPrefix(name, "etcdserverpb") { |
| 124 | + return name |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return "" |
| 129 | +} |
| 130 | + |
| 131 | +func printableCallTable(callsByOperationName map[string]int) string { |
| 132 | + buf := new(bytes.Buffer) |
| 133 | + cfgBuilder := tablewriter.NewConfigBuilder().WithRowAlignment(tw.AlignRight) |
| 134 | + table := tablewriter.NewTable(buf, tablewriter.WithConfig(cfgBuilder.Build())) |
| 135 | + table.Header("method", "calls", "percent") |
| 136 | + |
| 137 | + totalCalls := 0 |
| 138 | + for _, c := range callsByOperationName { |
| 139 | + totalCalls += c |
| 140 | + } |
| 141 | + |
| 142 | + for opName, callCount := range callsByOperationName { |
| 143 | + table.Append(opName, callCount, fmt.Sprintf("%.2f%%", float64(callCount*100)/float64(totalCalls))) |
| 144 | + } |
| 145 | + table.Footer("total", totalCalls, "100.00%") |
| 146 | + |
| 147 | + table.Render() |
| 148 | + return buf.String() |
| 149 | +} |
0 commit comments