Skip to content
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

Make sat reproducible #86

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use_repo(
"com_github_spf13_cobra",
"io_k8s_sigs_yaml",
"org_golang_x_crypto",
"org_golang_x_exp",
)

# deps only needed for the repo internals
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/bazelbuild/buildtools v0.0.0-20240823132350-3488089d3661
github.com/bazelbuild/buildtools v0.0.0-20250110114635-13fa61383b99
github.com/crillab/gophersat v1.4.0
github.com/onsi/gomega v1.36.2
github.com/sassoftware/go-rpmutils v0.2.0
Expand All @@ -22,6 +22,7 @@ require (
github.com/klauspost/compress v1.11.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/bazelbuild/buildtools v0.0.0-20240823132350-3488089d3661 h1:acJJwAuD2t36RHnvxf3oh9lhg5LISUYXunx+om2ONZw=
github.com/bazelbuild/buildtools v0.0.0-20240823132350-3488089d3661/go.mod h1:yBQGNvRAGhcBTxe4MHiW3Ul7DwoBim4XsKUaXnW1LWc=
github.com/bazelbuild/buildtools v0.0.0-20250110114635-13fa61383b99 h1:3Wvirn7fGxAk5XMO5OV9goElw2ZKya/erxxHChPSPQM=
github.com/bazelbuild/buildtools v0.0.0-20250110114635-13fa61383b99/go.mod h1:PLNUetjLa77TCCziPsz0EI8a6CUxgC+1jgmWv0H25tg=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/crillab/gophersat v1.4.0 h1:irf9ajKmNnEURjgPU4oz+ouqIXXLQ59ZNd3NC+hULMc=
github.com/crillab/gophersat v1.4.0/go.mod h1:gDzeMEBrqJR20IL9JW25tFHNGLU5+GDeJzr0zpi3mxs=
Expand Down Expand Up @@ -47,6 +49,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
Expand All @@ -60,6 +64,7 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
Expand Down
16 changes: 6 additions & 10 deletions pkg/rpm/rpm.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package rpm

import (
"cmp"
"strconv"
"strings"

"github.com/rmohr/bazeldnf/pkg/api"
)

func Compare(a api.Version, b api.Version) int {
if res := compare(a.Epoch, b.Epoch); res != 0 {
return res
}
if res := compare(a.Ver, b.Ver); res != 0 {
return res
}
if res := compare(a.Rel, b.Rel); res != 0 {
return res
}
return 0
return cmp.Or(
compare(a.Epoch, b.Epoch),
compare(a.Ver, b.Ver),
compare(a.Rel, b.Rel),
)
}

func compare(a string, b string) int {
Expand Down
15 changes: 15 additions & 0 deletions pkg/sat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ go_library(
"@com_github_crillab_gophersat//explain",
"@com_github_crillab_gophersat//maxsat",
"@com_github_sirupsen_logrus//:logrus",
"@org_golang_x_exp//maps",
"@org_golang_x_exp//slices",
],
)

# gazelle:go_test file

go_test(
name = "sat_test",
srcs = ["sat_test.go"],
Expand All @@ -26,3 +30,14 @@ go_test(
"@com_github_onsi_gomega//:gomega",
],
)

go_test(
name = "sat_determinsitic_test",
srcs = ["sat_determinsitic_test.go"],
data = glob(["testdata/**"]),
embed = [":sat"],
deps = [
"//pkg/api",
"@com_github_onsi_gomega//:gomega",
],
)
34 changes: 29 additions & 5 deletions pkg/sat/sat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sat

import (
"bufio"
"cmp"
"fmt"
"io"
"regexp"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/rmohr/bazeldnf/pkg/reducer"
"github.com/rmohr/bazeldnf/pkg/rpm"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

type VarType string
Expand All @@ -34,6 +37,13 @@ type VarContext struct {
Version api.Version
}

func varContextSort(a VarContext, b VarContext) int {
return cmp.Or(
cmp.Compare(a.Package, b.Package),
rpm.Compare(a.Version, b.Version),
)
}

type Var struct {
satVarName string
varType VarType
Expand Down Expand Up @@ -129,8 +139,12 @@ func (r *Resolver) LoadInvolvedPackages(packages []*api.Package, ignoreRegex []s
deduplicated[pkg.String()] = packages[i]
}
}

deduplicatedKeys := maps.Keys(deduplicated)
slices.Sort(deduplicatedKeys)

packages = nil
for k, _ := range deduplicated {
for _, k := range deduplicatedKeys {
reducer.FixPackages(deduplicated[k])
packages = append(packages, deduplicated[k])
}
Expand All @@ -146,8 +160,10 @@ func (r *Resolver) LoadInvolvedPackages(packages []*api.Package, ignoreRegex []s

if !r.nobest {
packages = nil
for _, v := range r.bestPackages {
packages = append(packages, v)
bestPackagesKeys := maps.Keys(r.bestPackages)
slices.Sort(bestPackagesKeys)
for _, v := range bestPackagesKeys {
packages = append(packages, r.bestPackages[v])
}
}
// Generate variables
Expand All @@ -161,17 +177,25 @@ func (r *Resolver) LoadInvolvedPackages(packages []*api.Package, ignoreRegex []s
}
}

for x, _ := range r.packages {
packagesKeys := maps.Keys(r.packages)
slices.Sort(packagesKeys)

for _, x := range packagesKeys {
sort.SliceStable(r.packages[x], func(i, j int) bool {
return rpm.Compare(r.packages[x][i].Package.Version, r.packages[x][j].Package.Version) < 0
})
}

logrus.Infof("Loaded %v packages.", len(r.pkgProvides))

pkgProvideKeys := maps.Keys(r.pkgProvides)
slices.SortFunc(pkgProvideKeys, varContextSort)

// Generate imply rules
for _, resourceVars := range r.pkgProvides {
for _, provided := range pkgProvideKeys {
// Create imply rules for every package and add them to the formula
// one provided dependency implies all dependencies from that package
resourceVars := r.pkgProvides[provided]
bfVar := bf.And(toBFVars(resourceVars)...)
var ands []bf.Formula
for _, res := range resourceVars {
Expand Down
85 changes: 85 additions & 0 deletions pkg/sat/sat_determinsitic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package sat

import (
"encoding/xml"
"fmt"
"os"
"testing"

. "github.com/onsi/gomega"
"github.com/rmohr/bazeldnf/pkg/api"
)

func TestDeterministicOutput(t *testing.T) {
tt := struct {
requires []string
installs []string
repofile string
}{
requires: []string{
"shadow-utils",
},
installs: []string{
"glibc-all-langpacks-0:2.28-161.el8",
"libsepol-0:2.9-2.el8",
"ncurses-base-0:6.1-9.20180224.el8",
"libacl-0:2.2.53-1.el8",
"libattr-0:2.4.48-3.el8",
"basesystem-0:11-5.el8",
"bash-0:4.4.20-1.el8_4",
"filesystem-0:3.8-6.el8",
"centos-gpg-keys-1:8-2.el8",
"libsemanage-0:2.9-6.el8",
"libxcrypt-0:4.1.1-6.el8",
"libcap-0:2.26-4.el8",
"libselinux-0:2.9-5.el8",
"ncurses-libs-0:6.1-9.20180224.el8",
"bzip2-libs-0:1.0.6-26.el8",
"coreutils-single-0:8.30-10.el8",
"tzdata-0:2021a-1.el8",
"shadow-utils-2:4.6-13.el8",
"glibc-0:2.28-161.el8",
"glibc-common-0:2.28-161.el8",
"libcap-ng-0:0.7.11-1.el8",
"pcre2-0:10.32-2.el8",
"audit-libs-0:3.0-0.17.20191104git1c2f876.el8",
"setup-0:2.12.2-6.el8",
"centos-stream-release-0:8.5-3.el8",
"centos-stream-repos-0:8-2.el8",
},
repofile: "testdata/libguestfs-el8.xml",
}

for i := range 10 {
t.Run(fmt.Sprintf("iteration: %d", i), func(t *testing.T) {
g := NewGomegaWithT(t)
f, err := os.Open(tt.repofile)
g.Expect(err).ToNot(HaveOccurred())
defer f.Close()
repo := &api.Repository{}
err = xml.NewDecoder(f).Decode(repo)
g.Expect(err).ToNot(HaveOccurred())

resolver := NewResolver(false)
packages := []*api.Package{}
for i, _ := range repo.Packages {
packages = append(packages, &repo.Packages[i])
}
err = resolver.LoadInvolvedPackages(packages, nil)
g.Expect(err).ToNot(HaveOccurred())
err = resolver.ConstructRequirements(tt.requires)
g.Expect(err).ToNot(HaveOccurred())
install, _, _, err := resolver.Resolve()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(installToString(install)).To(ConsistOf(tt.installs))
})
}

}

func installToString(given []*api.Package) (resolved []string) {
for _, p := range given {
resolved = append(resolved, p.String())
}
return
}
Loading