test/e2e: improve createArtifactFile()

There is no need whatsoever to run container to populate a random file,
this is just much slower than just writing some random bytes directly
without having to run a container and run dd in it.

Also the function accepted the number of bytes, however because dd uses
a minimum block size of 512 bytes it was actually numBytes * 1024 which
where written. That makes no sense so fix the two tests that depended on
the wrong number.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2025-02-25 18:49:51 +01:00
parent fe82fa85d2
commit f6e2d94409
2 changed files with 14 additions and 6 deletions

View File

@ -475,7 +475,7 @@ var _ = Describe("Podman artifact", func() {
a := podmanTest.InspectArtifact(artifact1Name) a := podmanTest.InspectArtifact(artifact1Name)
Expect(a.Manifest.Layers).To(HaveLen(1)) Expect(a.Manifest.Layers).To(HaveLen(1))
Expect(a.TotalSizeBytes()).To(Equal(int64(524288))) Expect(a.TotalSizeBytes()).To(Equal(int64(1024)))
}) })
It("podman artifact add file already exists in artifact", func() { It("podman artifact add file already exists in artifact", func() {
@ -506,7 +506,7 @@ var _ = Describe("Podman artifact", func() {
a := podmanTest.InspectArtifact(artifact1Name) a := podmanTest.InspectArtifact(artifact1Name)
Expect(a.Manifest.Layers).To(HaveLen(1)) Expect(a.Manifest.Layers).To(HaveLen(1))
Expect(a.TotalSizeBytes()).To(Equal(int64(1048576))) Expect(a.TotalSizeBytes()).To(Equal(int64(2048)))
}) })
It("podman artifact add with --append and --type", func() { It("podman artifact add with --append and --type", func() {

View File

@ -5,6 +5,7 @@ package integration
import ( import (
"bufio" "bufio"
"bytes" "bytes"
crand "crypto/rand"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -1390,6 +1391,7 @@ func writeYaml(content string, fileName string) error {
func GetPort() int { func GetPort() int {
portMin := 5000 portMin := 5000
portMax := 5999 portMax := 5999
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(time.Now().UnixNano()))
// Avoid dup-allocation races between parallel ginkgo processes // Avoid dup-allocation races between parallel ginkgo processes
@ -1617,16 +1619,22 @@ func setupRegistry(portOverride *int) (*lockfile.LockFile, string, error) {
} }
func createArtifactFile(numBytes int64) (string, error) { func createArtifactFile(numBytes int64) (string, error) {
GinkgoHelper()
artifactDir := filepath.Join(podmanTest.TempDir, "artifacts") artifactDir := filepath.Join(podmanTest.TempDir, "artifacts")
if err := os.MkdirAll(artifactDir, 0755); err != nil { if err := os.MkdirAll(artifactDir, 0755); err != nil {
return "", err return "", err
} }
filename := RandomString(8) filename := RandomString(8)
outFile := filepath.Join(artifactDir, filename) outFile := filepath.Join(artifactDir, filename)
session := podmanTest.Podman([]string{"run", "-v", fmt.Sprintf("%s:/artifacts:z", artifactDir), ALPINE, "dd", "if=/dev/urandom", fmt.Sprintf("of=%s", filepath.Join("/artifacts", filename)), "bs=1b", fmt.Sprintf("count=%d", numBytes)})
session.WaitWithDefaultTimeout() f, err := os.Create(filepath.Join(artifactDir, filename))
if session.ExitCode() != 0 { if err != nil {
return "", errors.New("unable to generate artifact file") return "", err
}
defer f.Close()
_, err = io.CopyN(f, crand.Reader, numBytes)
if err != nil {
return "", err
} }
return outFile, nil return outFile, nil
} }