-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): add hasher pkg and add more uts for toml
Signed-off-by: liubo02 <[email protected]>
- Loading branch information
1 parent
fce4a79
commit 077098c
Showing
8 changed files
with
588 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package hasher | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"hash/fnv" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
|
||
"github.com/pingcap/tidb-operator/apis/core/v1alpha1" | ||
hashutil "github.com/pingcap/tidb-operator/third_party/kubernetes/pkg/util/hash" | ||
) | ||
|
||
// GenerateHash takes a TOML string as input, unmarshals it into a map, | ||
// and generates a hash of the resulting configuration. The hash is then | ||
// encoded into a safe string format and returned. | ||
// If the order of keys in the TOML string is different, the hash will be the same. | ||
func GenerateHash(tomlStr v1alpha1.ConfigFile) (string, error) { | ||
var config map[string]any | ||
if err := toml.NewDecoder(bytes.NewReader([]byte(tomlStr))).Decode(&config); err != nil { | ||
return "", fmt.Errorf("failed to unmarshal toml string %s: %w", tomlStr, err) | ||
} | ||
hasher := fnv.New32a() | ||
hashutil.DeepHashObject(hasher, config) | ||
return rand.SafeEncodeString(fmt.Sprint(hasher.Sum32())), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package hasher | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pingcap/tidb-operator/apis/core/v1alpha1" | ||
) | ||
|
||
func TestGenerateHash(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
tomlStr v1alpha1.ConfigFile | ||
semanticallyEquivalentStr v1alpha1.ConfigFile | ||
wantHash string | ||
wantError bool | ||
}{ | ||
{ | ||
name: "Valid TOML string", | ||
tomlStr: v1alpha1.ConfigFile(`foo = 'bar' | ||
[log] | ||
k1 = 'v1' | ||
k2 = 'v2'`), | ||
semanticallyEquivalentStr: v1alpha1.ConfigFile(`foo = 'bar' | ||
[log] | ||
k2 = 'v2' | ||
k1 = 'v1'`), | ||
wantHash: "5dbbcf4574", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Different config value", | ||
tomlStr: v1alpha1.ConfigFile(`foo = 'foo' | ||
[log] | ||
k2 = 'v2' | ||
k1 = 'v1'`), | ||
wantHash: "f5bc46cb9", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "multiple sections with blank line", | ||
tomlStr: v1alpha1.ConfigFile(`[a] | ||
k1 = 'v1' | ||
[b] | ||
k2 = 'v2'`), | ||
semanticallyEquivalentStr: v1alpha1.ConfigFile(`[a] | ||
k1 = 'v1' | ||
[b] | ||
k2 = 'v2'`), | ||
wantHash: "79598d5977", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Empty TOML string", | ||
tomlStr: v1alpha1.ConfigFile(``), | ||
wantHash: "7d6fc488b7", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Invalid TOML string", | ||
tomlStr: v1alpha1.ConfigFile(`key1 = "value1" | ||
key2 = value2`), // Missing quotes around value2 | ||
wantHash: "", | ||
wantError: true, | ||
}, | ||
{ | ||
name: "Nested tables", | ||
tomlStr: v1alpha1.ConfigFile(`[parent] | ||
child1 = "value1" | ||
child2 = "value2" | ||
[parent.child] | ||
grandchild1 = "value3" | ||
grandchild2 = "value4"`), | ||
semanticallyEquivalentStr: v1alpha1.ConfigFile(`[parent] | ||
child2 = "value2" | ||
child1 = "value1" | ||
[parent.child] | ||
grandchild2 = "value4" | ||
grandchild1 = "value3"`), | ||
wantHash: "7bf645ccb4", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Array of tables", | ||
tomlStr: v1alpha1.ConfigFile(`[[products]] | ||
name = "Hammer" | ||
sku = 738594937 | ||
[[products]] | ||
name = "Nail" | ||
sku = 284758393 | ||
color = "gray"`), | ||
semanticallyEquivalentStr: v1alpha1.ConfigFile(`[[products]] | ||
sku = 738594937 | ||
name = "Hammer" | ||
[[products]] | ||
sku = 284758393 | ||
name = "Nail" | ||
color = "gray"`), | ||
wantHash: "7549cf87f4", | ||
wantError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotHash, err := GenerateHash(tt.tomlStr) | ||
if tt.wantError { | ||
assert.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.wantHash, gotHash) | ||
|
||
if string(tt.semanticallyEquivalentStr) != "" { | ||
reorderedHash, err := GenerateHash(tt.semanticallyEquivalentStr) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.wantHash, reorderedHash) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.