11package v1
22
33import (
4- "crypto/sha256"
5- "encoding/json"
6- "fmt"
74 "testing"
85
9- "github.com/stretchr/testify/assert"
106 "github.com/stretchr/testify/require"
117)
128
@@ -21,51 +17,3 @@ func TestValidate(t *testing.T) {
2117 require .Error (t , err )
2218 require .ErrorContains (t , err , "Spec.Resources.CPU error: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$'" )
2319}
24-
25- func TestGetQdrantClusterCrdForHash (t * testing.T ) {
26- qc := QdrantCluster {}
27- hash , err := getQdrantClusterCrdHash (qc )
28- require .NoError (t , err )
29- assert .Equal (t , "523a8cb" , hash )
30-
31- falseVal := false
32- qc .Spec .ServicePerNode = & falseVal
33- hash , err = getQdrantClusterCrdHash (qc )
34- require .NoError (t , err )
35- assert .Equal (t , "523a8cb" , hash )
36-
37- trueVal := true
38- qc .Spec .ServicePerNode = & trueVal
39- hash , err = getQdrantClusterCrdHash (qc )
40- require .NoError (t , err )
41- assert .Equal (t , "523a8cb" , hash )
42- }
43-
44- // getQdrantClusterCrdHash created a hash for the provided QdrantCluster,
45- // however a subset only, see GetQdrantClusterCrdForHash for details.
46- func getQdrantClusterCrdHash (qc QdrantCluster ) (string , error ) {
47- inspect := GetQdrantClusterCrdForHash (qc )
48- // Get the hash, so we can diff later
49- hash , err := getHash (inspect )
50- if err != nil {
51- return "" , fmt .Errorf ("failed to get hash for QdrantCluster: %w" , err )
52- }
53- return hash , err
54- }
55-
56- // Get hash of provided value.
57- // Returns the first 7 characters of the hash (like GitHub).
58- func getHash (v any ) (string , error ) {
59- json , err := json .Marshal (v )
60- if err != nil {
61- return "" , fmt .Errorf ("marshal failed: %w" , err )
62- }
63- // Initialize hash
64- hash := sha256 .New ()
65- // add the serialized content
66- hash .Write (json )
67- // close hash
68- sum := hash .Sum (nil )
69- // Return first 7 characters
70- return fmt .Sprintf ("%x" , sum )[:7 ], nil
71- }
0 commit comments