Skip to content

Commit 270aa53

Browse files
committed
Add Kubernetes API coverage test consuming traces
It pulls a file from testdata with traces collected by Jaeger and then iterates over them in order to group them into those which use the contract interface, are known (and usually valid) uses outside of it. Later it can further analyze the parameters used over the run of Kubernetes conformance tests. It requires patched Kubernetes with: AwesomePatrol/kubernetes@5a18a66 or at: https://github.com/AwesomePatrol/kubernetes/tree/add-kubernetes-etcd-contract-tracker
1 parent 52ce705 commit 270aa53

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 "strings"
18+
19+
type Tag struct {
20+
Key string `json:"key"`
21+
Type string `json:"type"`
22+
Value any `json:"value"`
23+
}
24+
25+
type Log struct {
26+
Timestamp int64 `json:"timestamp"`
27+
Fields []Tag `json:"fields"`
28+
}
29+
30+
type Span struct {
31+
TraceID string `json:"traceID"`
32+
SpanID string `json:"spanID"`
33+
OperationName string `json:"operationName"`
34+
Tags []Tag `json:"tags"`
35+
ProcessID string `json:"processID"`
36+
Logs []Log `json:"logs"`
37+
ServiceName string
38+
}
39+
40+
type Process struct {
41+
ServiceName string `json:"serviceName"`
42+
}
43+
44+
type Trace struct {
45+
TraceID string `json:"traceID"`
46+
Spans []Span `json:"spans"`
47+
Processes map[string]Process `json:"processes"`
48+
}
49+
50+
type Dump struct {
51+
Data []Trace `json:"data"`
52+
}
53+
54+
func filterOutIncomplete(dump Dump) (ret []Trace, apiserverOnly, etcdOnly int) {
55+
// TODO: Make request directly to Jaeger (OTLP /api/v3) and use OTLP types
56+
ret = make([]Trace, 0, len(dump.Data))
57+
for _, trace := range dump.Data {
58+
var associatedToEtcd, associatedToApiserver bool
59+
for i := range trace.Spans {
60+
span := &trace.Spans[i]
61+
span.ServiceName = trace.Processes[span.ProcessID].ServiceName
62+
if span.ServiceName == "etcd" {
63+
associatedToEtcd = true
64+
}
65+
if span.ServiceName == "apiserver" {
66+
associatedToApiserver = true
67+
}
68+
}
69+
if !associatedToEtcd {
70+
apiserverOnly++
71+
continue
72+
}
73+
if !associatedToApiserver {
74+
etcdOnly++
75+
continue
76+
}
77+
ret = append(ret, trace)
78+
}
79+
return
80+
}
81+
82+
func isContractCall(trace Trace) bool {
83+
for _, span := range trace.Spans {
84+
if strings.HasSuffix(span.OperationName, "kubernetesEtcdContract") {
85+
return true
86+
}
87+
}
88+
return false
89+
}
90+
91+
func revFromSpan(span Span) string {
92+
for _, tag := range span.Tags {
93+
if tag.Key == "rev" {
94+
if tag.Type != "int64" {
95+
continue
96+
}
97+
if tag.Value.(float64) == 0 {
98+
return "unset"
99+
}
100+
if tag.Value.(float64) > 0 {
101+
return "set"
102+
}
103+
}
104+
}
105+
return "missing"
106+
}

0 commit comments

Comments
 (0)