Debian Codegraph @

internal/antithesis/test_assert.go

raw
1 // Copyright 2022-2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // This file is used iff the `enable_antithesis_sdk` build tag is present
15 //go:build enable_antithesis_sdk
16
17 package antithesis
18
19 import (
20 "encoding/json"
21 "fmt"
22 "runtime/debug"
23 "testing"
24
25 "github.com/antithesishq/antithesis-sdk-go/assert"
26 )
27
28 // This file provides assertions utility functions suitable for use in tests.
29 // It is a thin wrapper around the Antithesis SDK.
30 //
31 // Notice that unlike other assertions libraries, a violation does not halt execution or fail the test.
32 // The effects of violating an assertion, are:
33 // 1. Print the violation message, prefixed by the test name in which it happened
34 // 2. Print the stack for the calling goroutine so each failed test has a clear trace of the failure
35 // 3. Invoke the underlying Antithesis assertion
36 //
37 // N.B. Enabling this module outside for tests running outside of Antithesis will enable 1 and 2 above, but not 3.
38 // therefore it can be useful to output additional test failure details when running tests locally or in CI.
39
40 // AssertUnreachable is used to flag code branches that should not get invoked ever.
41 // Example:
42 //
43 // pubAck, err := js.Publish(...)
44 //
45 // if err != nil {
46 // antithesis.AssertUnreachable(t, "Publish failed", map[string]any{"error": err.Error()})
47 // t.Fatalf("Publish failed with error: %s", err)
48 // }
49 func AssertUnreachable(t testing.TB, message string, details map[string]any) {
50 // Always print a message
51 fmt.Printf("{*} [%s] Assert Unreachable violation: %s\n", t.Name(), message)
52 if details != nil && len(details) > 0 {
53 fmt.Printf("{*} Details:\n")
54 jsonDetails, err := json.MarshalIndent(details, "", " ")
55 if err != nil {
56 panic(err)
57 }
58 fmt.Println(string(jsonDetails))
59 }
60
61 // Always print the stack trace
62 fmt.Printf("{*} Stack trace:\n")
63 debug.PrintStack()
64
65 // N.B. as of today, message (as-is) is the unique identifier of the event
66 // Therefore this will de-duplicate the same assertion failing in 2 different tests
67 // But not the same assertion failing at 2 different lines of the same test
68 messageWithTestName := fmt.Sprintf("[%s] %s", t.Name(), message)
69
70 // Fire assertion violation event (if Antithesis is enabled)
71 assert.Unreachable(messageWithTestName, details)
72 }
73
74 // Assert is used to check that some given condition is always true,
75 // Example:
76 //
77 // antithesis.Assert(t, sequence > lastSequence, "Non-monotonic stream sequence number", map[string]any{
78 // "stream": streamName,
79 // "connection_id": nc.Id(),
80 // "sequence": sequence,
81 // "lastSequence": lastSequence,
82 // })
83 func Assert(t testing.TB, condition bool, message string, details map[string]any) {
84 // Condition is true, nothing to do
85 if condition {
86 return
87 }
88
89 // Always print a message
90 fmt.Printf("{*} [%s] Assert violation: %s\n", t.Name(), message)
91 if details != nil && len(details) > 0 {
92 fmt.Printf("{*} Details:\n")
93 jsonDetails, err := json.MarshalIndent(details, "", " ")
94 if err != nil {
95 panic(err)
96 }
97 fmt.Println(string(jsonDetails))
98 }
99
100 // Always print the stack trace
101 fmt.Printf("{*} Stack trace:\n")
102 debug.PrintStack()
103
104 // N.B. as of today, message (as-is) is the unique identifier of the event
105 // Therefore this will de-duplicate the same assertion failing in 2 different tests
106 // But not the same assertion failing at 2 different lines of the same test
107 messageWithTestName := fmt.Sprintf("[%s] %s", t.Name(), message)
108
109 // Fire assertion violation event (if Antithesis is enabled)
110 assert.AlwaysOrUnreachable(false, messageWithTestName, details)
111 }