-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(utils): add hasher pkg and add more uts for toml #5992
Merged
csuzhangxc
merged 3 commits into
pingcap:feature/v2
from
liubog2008:liubo02/add-toml-ut
Dec 20, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this function only works for TOML, should we include
toml
in the name?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but may add it when a new similar function is added?