Skip to content

Commit 061e59a

Browse files
authored
Merge pull request #19767 from abdurrehman107/abdur-rehman/antithesis-itegration-pr-3
Add Antithesis package imports
2 parents aa2a025 + 5ac6079 commit 061e59a

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

tests/antithesis/test-template/go-delete-keys/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ toolchain go1.24.2
77
require go.etcd.io/etcd/client/v3 v3.5.21
88

99
require (
10+
github.com/antithesishq/antithesis-sdk-go v0.4.3
1011
github.com/coreos/go-semver v0.3.0 // indirect
1112
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
1213
github.com/gogo/protobuf v1.3.2 // indirect

tests/antithesis/test-template/go-delete-keys/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/antithesishq/antithesis-sdk-go v0.4.3 h1:a2hGdDogClzHzFu20r1z0tzD6zwSWUipiaerAjZVP90=
2+
github.com/antithesishq/antithesis-sdk-go v0.4.3/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl3v2yvUZjmKncl7U91fup7E=
13
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
24
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
35
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=

tests/antithesis/test-template/go-delete-keys/serial_driver_delete_keys.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,54 @@ import (
66
"os"
77
"time"
88

9+
"github.com/antithesishq/antithesis-sdk-go/assert"
10+
"github.com/antithesishq/antithesis-sdk-go/random"
911
clientv3 "go.etcd.io/etcd/client/v3"
1012
)
1113

12-
// Connect to an etcd node
1314
func Connect() *clientv3.Client {
14-
hosts := [][]string{[]string{"etcd0:2379"}, []string{"etcd1:2379"}, []string{"etcd2:2379"}}
15-
16-
// Randomly choose one host to connect to (just for simplicity)
17-
host := hosts[0] // You can implement a random choice function here if needed
15+
// This function returns a client connection to an etcd node
1816

19-
// Create an etcd client
17+
hosts := [][]string{[]string{"etcd0:2379"}, []string{"etcd1:2379"}, []string{"etcd2:2379"}}
18+
host := random.RandomChoice(hosts)
2019
cli, err := clientv3.New(clientv3.Config{
2120
Endpoints: host,
2221
DialTimeout: 5 * time.Second,
2322
})
2423
if err != nil {
2524
log.Fatalf("Failed to connect to etcd: %v", err)
25+
// Antithesis Assertion: client should always be able to connect to an etcd host
26+
assert.Unreachable("Client failed to connect to an etcd host", map[string]interface{}{"host": host, "error": err})
2627
os.Exit(1)
2728
}
2829
return cli
2930
}
3031

31-
// This function will:
32-
// 1. Get all keys
33-
// 2. Select half of the keys received
34-
// 3. Attempt to delete the keys selected
35-
// 4. Check that the keys were deleted
3632
func DeleteKeys() {
33+
// This function will:
34+
// 1. Get all keys
35+
// 2. Select half of the keys received
36+
// 3. Attempt to delete the keys selected
37+
// 4. Check that the keys were deleted
38+
3739
ctx := context.Background()
3840

3941
// Connect to an etcd node
4042
cli := Connect()
4143

42-
// Get all keys with the prefix
44+
// Get all keys
4345
resp, err := cli.Get(ctx, "", clientv3.WithPrefix())
46+
47+
// Antithesis Assertion: sometimes get with prefix requests are successful. A failed request is OK since we expect them to happen.
48+
assert.Sometimes(err == nil, "Client can make successful get all requests", map[string]interface{}{"error": err})
49+
cli.Close()
50+
4451
if err != nil {
4552
log.Printf("Client failed to get all keys: %v", err)
4653
os.Exit(0)
4754
}
4855

49-
// Choose half of the keys to delete
56+
// Choose half of the keys
5057
var keys []string
5158
for _, k := range resp.Kvs {
5259
keys = append(keys, string(k.Key))
@@ -57,10 +64,12 @@ func DeleteKeys() {
5764
// Connect to a new etcd node
5865
cli = Connect()
5966

60-
// Delete half of the selected keys
67+
// Delete half of the keys chosen
6168
var deletedKeys []string
6269
for _, k := range halfKeys {
6370
_, err := cli.Delete(ctx, k)
71+
// Antithesis Assertion: sometimes delete requests are successful. A failed request is OK since we expect them to happen.
72+
assert.Sometimes(err == nil, "Client can make successful delete requests", map[string]interface{}{"error": err})
6473
if err != nil {
6574
log.Printf("Failed to delete key %s: %v", k, err)
6675
} else {
@@ -73,26 +82,24 @@ func DeleteKeys() {
7382
// Connect to a new etcd node
7483
cli = Connect()
7584

76-
// Check if the deleted keys exist
85+
// Check to see if those keys were deleted / exist
7786
for _, k := range deletedKeys {
7887
resp, err := cli.Get(ctx, k)
88+
// Antithesis Assertion: sometimes get requests are successful. A failed request is OK since we expect them to happen.
89+
assert.Sometimes(err == nil, "Client can make successful get requests", map[string]interface{}{"error": err})
7990
if err != nil {
8091
log.Printf("Client failed to get key %s: %v", k, err)
8192
continue
8293
}
83-
84-
// Assert that the key was deleted correctly (now simply check if the key count is 0)
85-
if resp.Count != 0 {
86-
log.Printf("Key %s was not deleted correctly", k)
87-
} else {
88-
log.Printf("Key %s was deleted correctly", k)
89-
}
94+
// Antithesis Assertion: if we deleted a key, we should not get a value
95+
assert.Always(resp.Count == 0, "Key was deleted correctly", map[string]interface{}{"key": k})
9096
}
9197
cli.Close()
9298

93-
log.Printf("Completion of key deleting check")
99+
assert.Reachable("Completion of a key deleting check", nil)
100+
log.Printf("Completion of a key deleting check")
94101
}
95102

96103
func main() {
97104
DeleteKeys()
98-
}
105+
}

0 commit comments

Comments
 (0)