dnf-json: replace dnf hash function

Replace the dnf-json `Hash()` function in
favour of a hash calculated using the
`rpmmd.RepConfig.Hash()` function. The
`repoHash` field is populated when converting
a `rpmmd.RepoConfig` to `dnfjson.repoConfig`
object. The `dnfson.repoConfig.Hash()` function
then returns the `repoHash` field instead of
re-calculating the hash.
This commit is contained in:
Gianluca Zuccarelli 2023-04-24 12:27:53 +01:00 committed by Gianluca Zuccarelli
parent f59e248bc8
commit e313a76103
2 changed files with 39 additions and 24 deletions

View file

@ -268,6 +268,7 @@ func (s *Solver) reposFromRPMMD(rpmRepos []rpmmd.RepoConfig) ([]repoConfig, erro
GPGKeys: rr.GPGKeys,
IgnoreSSL: rr.IgnoreSSL,
MetadataExpire: rr.MetadataExpire,
repoHash: rr.Hash(),
}
if rr.CheckGPG != nil {
@ -311,25 +312,15 @@ type repoConfig struct {
SSLClientKey string `json:"sslclientkey,omitempty"`
SSLClientCert string `json:"sslclientcert,omitempty"`
MetadataExpire string `json:"metadata_expire,omitempty"`
// set the repo hass from `rpmmd.RepoConfig.Hash()` function
// rather than re-calculating it
repoHash string
}
// Hash calculates an ID string that uniquely represents a repository
// configuration. The Name and ImageTypeTags fields are not considered in the
// calculation.
// Copied from rpmmd/repository.go
// use the hash calculated by the `rpmmd.RepoConfig.Hash()`
// function rather than re-implementing the same code
func (r *repoConfig) Hash() string {
bts := func(b bool) string {
return fmt.Sprintf("%T", b)
}
ats := func(s []string) string {
return strings.Join(s, "")
}
return fmt.Sprintf("%x", sha256.Sum256([]byte(ats(r.BaseURLs)+
r.Metalink+
r.MirrorList+
ats(r.GPGKeys)+
bts(r.IgnoreSSL)+
r.MetadataExpire)))
return r.repoHash
}
// Helper function for creating a depsolve request payload.

View file

@ -115,11 +115,13 @@ func TestMakeDepsolveRequest(t *testing.T) {
ID: baseOS.Hash(),
Name: "baseos",
BaseURLs: []string{"https://example.org/baseos"},
repoHash: "b6b5cb4aae14a9ccc5f925876857b5cbfd757713a7c60e4c287502db80d8ecae",
},
{
ID: appstream.Hash(),
Name: "appstream",
BaseURLs: []string{"https://example.org/appstream"},
repoHash: "de4ae5f6f8288c8e6c745cac22bf3d923d3c738cc68c4b6046acac9a5eb3b8da",
},
},
},
@ -152,16 +154,19 @@ func TestMakeDepsolveRequest(t *testing.T) {
ID: baseOS.Hash(),
Name: "baseos",
BaseURLs: []string{"https://example.org/baseos"},
repoHash: "b6b5cb4aae14a9ccc5f925876857b5cbfd757713a7c60e4c287502db80d8ecae",
},
{
ID: appstream.Hash(),
Name: "appstream",
BaseURLs: []string{"https://example.org/appstream"},
repoHash: "de4ae5f6f8288c8e6c745cac22bf3d923d3c738cc68c4b6046acac9a5eb3b8da",
},
{
ID: userRepo.Hash(),
Name: "user-repo",
BaseURLs: []string{"https://example.org/user-repo"},
repoHash: "127d1ae749d1b673dd8701871483ae93e82bff052ef2b9bf0b668ed1aca89f76",
},
},
},
@ -194,11 +199,13 @@ func TestMakeDepsolveRequest(t *testing.T) {
ID: baseOS.Hash(),
Name: "baseos",
BaseURLs: []string{"https://example.org/baseos"},
repoHash: "b6b5cb4aae14a9ccc5f925876857b5cbfd757713a7c60e4c287502db80d8ecae",
},
{
ID: appstream.Hash(),
Name: "appstream",
BaseURLs: []string{"https://example.org/appstream"},
repoHash: "de4ae5f6f8288c8e6c745cac22bf3d923d3c738cc68c4b6046acac9a5eb3b8da",
},
},
},
@ -239,16 +246,19 @@ func TestMakeDepsolveRequest(t *testing.T) {
ID: baseOS.Hash(),
Name: "baseos",
BaseURLs: []string{"https://example.org/baseos"},
repoHash: "b6b5cb4aae14a9ccc5f925876857b5cbfd757713a7c60e4c287502db80d8ecae",
},
{
ID: appstream.Hash(),
Name: "appstream",
BaseURLs: []string{"https://example.org/appstream"},
repoHash: "de4ae5f6f8288c8e6c745cac22bf3d923d3c738cc68c4b6046acac9a5eb3b8da",
},
{
ID: userRepo.Hash(),
Name: "user-repo",
BaseURLs: []string{"https://example.org/user-repo"},
repoHash: "127d1ae749d1b673dd8701871483ae93e82bff052ef2b9bf0b668ed1aca89f76",
},
},
},
@ -290,21 +300,25 @@ func TestMakeDepsolveRequest(t *testing.T) {
ID: baseOS.Hash(),
Name: "baseos",
BaseURLs: []string{"https://example.org/baseos"},
repoHash: "b6b5cb4aae14a9ccc5f925876857b5cbfd757713a7c60e4c287502db80d8ecae",
},
{
ID: appstream.Hash(),
Name: "appstream",
BaseURLs: []string{"https://example.org/appstream"},
repoHash: "de4ae5f6f8288c8e6c745cac22bf3d923d3c738cc68c4b6046acac9a5eb3b8da",
},
{
ID: userRepo.Hash(),
Name: "user-repo",
BaseURLs: []string{"https://example.org/user-repo"},
repoHash: "127d1ae749d1b673dd8701871483ae93e82bff052ef2b9bf0b668ed1aca89f76",
},
{
ID: userRepo2.Hash(),
Name: "user-repo-2",
BaseURLs: []string{"https://example.org/user-repo-2"},
repoHash: "ca314d924e273218585cd60d7a5ffd91574df7ddf5b0574b31cf558971466170",
},
},
},
@ -551,18 +565,28 @@ func TestErrorRepoInfo(t *testing.T) {
}
func TestRepoConfigHash(t *testing.T) {
rc := repoConfig{
ID: "repoid-1",
Name: "A test repository",
BaseURLs: []string{"https://arepourl/"},
IgnoreSSL: false,
repos := []rpmmd.RepoConfig{
{
Id: "repoid-1",
Name: "A test repository",
BaseURLs: []string{"https://arepourl/"},
IgnoreSSL: false,
},
{
BaseURLs: []string{"https://adifferenturl/"},
},
}
hash := rc.Hash()
solver := NewSolver("f36", "36", "x86_64", "fedora-36", "/tmp/cache")
solver.SetDNFJSONPath("../../dnf-json")
rcs, err := solver.reposFromRPMMD(repos)
assert.Nil(t, err)
hash := rcs[0].Hash()
assert.Equal(t, 64, len(hash))
rc.BaseURLs = []string{"https://adifferenturl/"}
assert.NotEqual(t, hash, rc.Hash())
assert.NotEqual(t, hash, rcs[1].Hash())
}
func TestRequestHash(t *testing.T) {