-
Notifications
You must be signed in to change notification settings - Fork 83
bib: run "dnf" inside the container again #670
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3540536
bib: run "dnf" inside the container again
mvo5 a29239d
bib: add test to ensure InitDNF() works with subscribed content
mvo5 7125009
bib: add test for DNF solver inside the container for subscribed content
mvo5 cea0cd9
bib: add new `cntdnf` go module and move container dnf logic in there
mvo5 0f838c5
tmt: run new go tests to check container subscription access in tmt
mvo5 1362165
tmt: skip TestUploadAndRegisterProgressBar in testingfram
mvo5 50d684b
internal: move `cntdnf` code back into `container`
mvo5 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 hidden or 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,43 @@ | ||
--- | ||
name: Testing farm go unit tests | ||
|
||
on: | ||
pull_request_target: | ||
types: [opened, synchronize, reopened] | ||
|
||
# see testingfarm.yml | ||
jobs: | ||
testingfarm: | ||
name: "Run in testing farm" | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Get User Permission | ||
id: checkAccess | ||
uses: actions-cool/check-user-permission@v2 | ||
with: | ||
require: write | ||
username: ${{ github.triggering_actor }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Check User Permission | ||
if: steps.checkAccess.outputs.require-result == 'false' | ||
run: | | ||
echo "${{ github.triggering_actor }} does not have permissions on this repo." | ||
echo "Current permission level is ${{ steps.checkAccess.outputs.user-permission }}" | ||
echo "Job originally triggered by ${{ github.actor }}" | ||
exit 1 | ||
- name: Check out code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
- name: Run the tests | ||
uses: sclorg/testing-farm-as-github-action@v3 | ||
with: | ||
compose: Fedora-40 | ||
tmt_plan_filter: "unit-go.fmf" | ||
api_key: ${{ secrets.TF_API_KEY }} | ||
git_url: ${{ github.event.pull_request.head.repo.clone_url }} | ||
git_ref: ${{ github.event.pull_request.head.ref }} | ||
pull_request_status_name: "Testing farm" | ||
tf_scope: private | ||
secrets: "AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }};AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }};RHSM_ORG=${{ secrets.RHSM_ORG }};RHSM_ACTIVATION_KEY=${{ secrets.RHSM_ACTIVATION_KEY }} " |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
package container | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/osbuild/images/pkg/arch" | ||
"github.com/osbuild/images/pkg/dnfjson" | ||
|
||
"github.com/osbuild/bootc-image-builder/bib/internal/source" | ||
) | ||
|
||
// InitDNF initializes dnf in the container. This is necessary when | ||
// the caller wants to read the image's dnf repositories, but they are | ||
// not static, but rather configured by dnf dynamically. The primaru | ||
// use-case for this is RHEL and subscription-manager. | ||
// | ||
// The implementation is simple: We just run plain `dnf` in the | ||
// container so that the subscription-manager gets initialized. For | ||
// compatibility with both dnf and dnf5 we cannot just run "dnf" as | ||
// dnf5 will error and do nothing in this case. So we use "dnf check | ||
// --duplicates" as this is fast on both dnf4/dnf5 (just doing "dnf5 | ||
// check" without arguments takes around 25s so that is not a great | ||
// option). | ||
func (c *Container) InitDNF() error { | ||
if output, err := exec.Command("podman", "exec", c.id, "dnf", "check", "--duplicates").CombinedOutput(); err != nil { | ||
return fmt.Errorf("initializing dnf in %s container failed: %w\noutput:\n%s", c.id, err, string(output)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cnt *Container) injectDNFJson() ([]string, error) { | ||
if err := cnt.CopyInto("/usr/libexec/osbuild-depsolve-dnf", "/osbuild-depsolve-dnf"); err != nil { | ||
return nil, fmt.Errorf("cannot prepare depsolve in the container: %w", err) | ||
} | ||
// copy the python module too | ||
globPath := "/usr/lib/*/site-packages/osbuild" | ||
matches, err := filepath.Glob(globPath) | ||
if err != nil || len(matches) == 0 { | ||
return nil, fmt.Errorf("cannot find osbuild python module in %q: %w", globPath, err) | ||
} | ||
if len(matches) != 1 { | ||
return nil, fmt.Errorf("unexpected number of osbuild python module matches: %v", matches) | ||
} | ||
if err := cnt.CopyInto(matches[0], "/"); err != nil { | ||
return nil, fmt.Errorf("cannot prepare depsolve python-modules in the container: %w", err) | ||
} | ||
return append(cnt.ExecArgv(), "/osbuild-depsolve-dnf"), nil | ||
} | ||
|
||
func (cnt *Container) NewContainerSolver(cacheRoot string, architecture arch.Arch, sourceInfo *source.Info) (*dnfjson.Solver, error) { | ||
depsolverCmd, err := cnt.injectDNFJson() | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot inject depsolve into the container: %w", err) | ||
} | ||
|
||
solver := dnfjson.NewSolver( | ||
sourceInfo.OSRelease.PlatformID, | ||
sourceInfo.OSRelease.VersionID, | ||
architecture.String(), | ||
fmt.Sprintf("%s-%s", sourceInfo.OSRelease.ID, sourceInfo.OSRelease.VersionID), | ||
cacheRoot) | ||
solver.SetDNFJSONPath(depsolverCmd[0], depsolverCmd[1:]...) | ||
solver.SetRootDir("/") | ||
return solver, nil | ||
} |
This file contains hidden or 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,133 @@ | ||
package container_test | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/osbuild/images/pkg/arch" | ||
"github.com/osbuild/images/pkg/rpmmd" | ||
|
||
"github.com/osbuild/bootc-image-builder/bib/internal/container" | ||
"github.com/osbuild/bootc-image-builder/bib/internal/source" | ||
) | ||
|
||
const ( | ||
dnfTestingImageRHEL = "registry.access.redhat.com/ubi9:latest" | ||
dnfTestingImageCentos = "quay.io/centos/centos:stream9" | ||
) | ||
|
||
func TestDNFJsonWorks(t *testing.T) { | ||
if os.Geteuid() != 0 { | ||
t.Skip("skipping test; not running as root") | ||
} | ||
if _, err := os.Stat("/usr/libexec/osbuild-depsolve-dnf"); err != nil { | ||
t.Skip("cannot find /usr/libexec/osbuild-depsolve-dnf") | ||
} | ||
cacheRoot := t.TempDir() | ||
|
||
cnt, err := container.New(dnfTestingImageCentos) | ||
require.NoError(t, err) | ||
err = cnt.InitDNF() | ||
require.NoError(t, err) | ||
|
||
sourceInfo, err := source.LoadInfo(cnt.Root()) | ||
require.NoError(t, err) | ||
solver, err := cnt.NewContainerSolver(cacheRoot, arch.Current(), sourceInfo) | ||
require.NoError(t, err) | ||
res, err := solver.Depsolve([]rpmmd.PackageSet{ | ||
{ | ||
Include: []string{"coreutils"}, | ||
}, | ||
}, 0) | ||
require.NoError(t, err) | ||
assert.True(t, len(res.Packages) > 0) | ||
} | ||
|
||
func subscribeMachine(t *testing.T) (restore func()) { | ||
if _, err := exec.LookPath("subscription-manager"); err != nil { | ||
t.Skip("no subscription-manager found") | ||
return func() {} | ||
} | ||
|
||
matches, err := filepath.Glob("/etc/pki/entitlement/*.pem") | ||
if err == nil && len(matches) > 0 { | ||
return func() {} | ||
} | ||
|
||
rhsmOrg := os.Getenv("RHSM_ORG") | ||
rhsmActivationKey := os.Getenv("RHSM_ACTIVATION_KEY") | ||
if rhsmOrg == "" || rhsmActivationKey == "" { | ||
t.Skip("no RHSM_{ORG,ACTIVATION_KEY} env vars found") | ||
return func() {} | ||
} | ||
|
||
err = exec.Command("subscription-manager", "register", | ||
"--org", rhsmOrg, | ||
"--activationkey", rhsmActivationKey).Run() | ||
require.NoError(t, err) | ||
|
||
return func() { | ||
err := exec.Command("subscription-manager", "unregister").Run() | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
func TestDNFInitGivesAccessToSubscribedContent(t *testing.T) { | ||
if os.Geteuid() != 0 { | ||
t.Skip("skipping test; not running as root") | ||
} | ||
if runtime.GOARCH != "amd64" { | ||
t.Skip("skipping test; only runs on x86_64") | ||
} | ||
|
||
restore := subscribeMachine(t) | ||
defer restore() | ||
|
||
cnt, err := container.New(dnfTestingImageRHEL) | ||
require.NoError(t, err) | ||
err = cnt.InitDNF() | ||
require.NoError(t, err) | ||
|
||
content, err := cnt.ReadFile("/etc/yum.repos.d/redhat.repo") | ||
require.NoError(t, err) | ||
assert.Contains(t, string(content), "rhel-9-for-x86_64-baseos-rpms") | ||
} | ||
|
||
func TestDNFJsonWorkWithSubscribedContent(t *testing.T) { | ||
if os.Geteuid() != 0 { | ||
t.Skip("skipping test; not running as root") | ||
} | ||
if runtime.GOARCH != "amd64" { | ||
t.Skip("skipping test; only runs on x86_64") | ||
} | ||
if _, err := os.Stat("/usr/libexec/osbuild-depsolve-dnf"); err != nil { | ||
t.Skip("cannot find /usr/libexec/osbuild-depsolve-dnf") | ||
} | ||
cacheRoot := t.TempDir() | ||
|
||
restore := subscribeMachine(t) | ||
defer restore() | ||
|
||
cnt, err := container.New(dnfTestingImageRHEL) | ||
require.NoError(t, err) | ||
err = cnt.InitDNF() | ||
require.NoError(t, err) | ||
|
||
sourceInfo, err := source.LoadInfo(cnt.Root()) | ||
require.NoError(t, err) | ||
solver, err := cnt.NewContainerSolver(cacheRoot, arch.ARCH_X86_64, sourceInfo) | ||
require.NoError(t, err) | ||
res, err := solver.Depsolve([]rpmmd.PackageSet{ | ||
{ | ||
Include: []string{"coreutils"}, | ||
}, | ||
}, 0) | ||
require.NoError(t, err) | ||
assert.True(t, len(res.Packages) > 0) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.