1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-07 01:40:11 +00:00

use sha256 for checksumming when k8s version >= 1.31.6

This commit is contained in:
Jacob Lindgren
2025-02-11 14:01:21 -06:00
parent a274d25252
commit 019fa3d53c
3 changed files with 88 additions and 10 deletions

View File

@@ -1,6 +1,9 @@
package cluster
import (
"crypto/md5"
"crypto/sha256"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@@ -50,3 +53,57 @@ func Test_getUniqStringList(t *testing.T) {
})
}
}
func Test_getStringChecksum(t *testing.T) {
tests := []struct {
name string
config string
version string
expected string
}{
{
name: "version greater than 1.31.6, use sha256",
config: "test-config",
version: "v1.32.0-rancher0",
expected: fmt.Sprintf("%x", sha256.Sum256([]byte("test-config"))),
},
{
name: "version exactly 1.31.6, use sha256",
config: "test-config",
version: "v1.31.6-rancher0",
expected: fmt.Sprintf("%x", sha256.Sum256([]byte("test-config"))),
},
{
name: "version exactly 1.31.0, use md5",
config: "test-config",
version: "v1.31.0-rancher0",
expected: fmt.Sprintf("%x", md5.Sum([]byte("test-config"))),
},
{
name: "version less than 1.31, use md5",
config: "test-config",
version: "v1.30.0-rancher0",
expected: fmt.Sprintf("%x", md5.Sum([]byte("test-config"))),
},
{
name: "empty config",
config: "",
version: "v1.32.0-rancher0",
expected: fmt.Sprintf("%x", sha256.Sum256([]byte(""))),
},
{
name: "empty version",
config: "test-config",
version: "",
expected: fmt.Sprintf("%x", md5.Sum([]byte("test-config"))),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := getStringChecksum(tt.config, tt.version)
assert.Equal(t, tt.expected, result)
})
}
}