@@ -6,47 +6,54 @@ import (
6
6
"os"
7
7
"time"
8
8
9
+ "github.com/antithesishq/antithesis-sdk-go/assert"
10
+ "github.com/antithesishq/antithesis-sdk-go/random"
9
11
clientv3 "go.etcd.io/etcd/client/v3"
10
12
)
11
13
12
- // Connect to an etcd node
13
14
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
18
16
19
- // Create an etcd client
17
+ hosts := [][]string {[]string {"etcd0:2379" }, []string {"etcd1:2379" }, []string {"etcd2:2379" }}
18
+ host := random .RandomChoice (hosts )
20
19
cli , err := clientv3 .New (clientv3.Config {
21
20
Endpoints : host ,
22
21
DialTimeout : 5 * time .Second ,
23
22
})
24
23
if err != nil {
25
24
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 })
26
27
os .Exit (1 )
27
28
}
28
29
return cli
29
30
}
30
31
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
36
32
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
+
37
39
ctx := context .Background ()
38
40
39
41
// Connect to an etcd node
40
42
cli := Connect ()
41
43
42
- // Get all keys with the prefix
44
+ // Get all keys
43
45
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
+
44
51
if err != nil {
45
52
log .Printf ("Client failed to get all keys: %v" , err )
46
53
os .Exit (0 )
47
54
}
48
55
49
- // Choose half of the keys to delete
56
+ // Choose half of the keys
50
57
var keys []string
51
58
for _ , k := range resp .Kvs {
52
59
keys = append (keys , string (k .Key ))
@@ -57,10 +64,12 @@ func DeleteKeys() {
57
64
// Connect to a new etcd node
58
65
cli = Connect ()
59
66
60
- // Delete half of the selected keys
67
+ // Delete half of the keys chosen
61
68
var deletedKeys []string
62
69
for _ , k := range halfKeys {
63
70
_ , 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 })
64
73
if err != nil {
65
74
log .Printf ("Failed to delete key %s: %v" , k , err )
66
75
} else {
@@ -73,26 +82,24 @@ func DeleteKeys() {
73
82
// Connect to a new etcd node
74
83
cli = Connect ()
75
84
76
- // Check if the deleted keys exist
85
+ // Check to see if those keys were deleted / exist
77
86
for _ , k := range deletedKeys {
78
87
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 })
79
90
if err != nil {
80
91
log .Printf ("Client failed to get key %s: %v" , k , err )
81
92
continue
82
93
}
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 })
90
96
}
91
97
cli .Close ()
92
98
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" )
94
101
}
95
102
96
103
func main () {
97
104
DeleteKeys ()
98
- }
105
+ }
0 commit comments