Skip to content

Commit

Permalink
fix(tests/push): avoid race conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Aldo Lacuku <[email protected]>
  • Loading branch information
alacuku committed Mar 19, 2024
1 parent 012b0d7 commit 1ae873d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/registry/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (o *pushOptions) runPush(ctx context.Context, args []string) error {
config = cfg
}
}
path, err := utils.CreateTarGzArchive(p)
path, err := utils.CreateTarGzArchive("", p)
if err != nil {
return err
}
Expand Down
25 changes: 16 additions & 9 deletions cmd/registry/push/push_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ package push_test

import (
"fmt"
"github.com/falcosecurity/falcoctl/internal/utils"

Check failure on line 23 in cmd/registry/push/push_plugins_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/falcosecurity/falcoctl) (gci)
"os"
"path/filepath"
"regexp"
"time"

Check failure on line 28 in cmd/registry/push/push_plugins_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `goimports`-ed with -local github.com/falcosecurity/falcoctl (goimports)
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/falcosecurity/falcoctl/cmd"

Check failure on line 34 in cmd/registry/push/push_plugins_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/falcosecurity/falcoctl) (gci)
"github.com/falcosecurity/falcoctl/internal/utils"
"github.com/falcosecurity/falcoctl/pkg/oci"
testutils "github.com/falcosecurity/falcoctl/pkg/test"
)
Expand Down Expand Up @@ -120,15 +121,21 @@ var _ = Describe("pushing plugins", func() {
Expect(pluginData.Tags).Should(ContainElements(pushedTags))

By("checking that temporary dirs have been removed")
entries, err := os.ReadDir("/tmp")
Expect(err).ShouldNot(HaveOccurred())
for _, e := range entries {
if e.IsDir() {
matched, err := filepath.Match(utils.TmpDirPrefix+"*", regexp.QuoteMeta(e.Name()))
Expect(err).ShouldNot(HaveOccurred())
Expect(matched).ShouldNot(BeTrue())

Eventually(func() bool {
entries, err := os.ReadDir("/tmp")
Expect(err).ShouldNot(HaveOccurred())
for _, e := range entries {
if e.IsDir() {
matched, err := filepath.Match(utils.TmpDirPrefix+"*", regexp.QuoteMeta(e.Name()))
Expect(err).ShouldNot(HaveOccurred())
if matched {
return true
}
}
}
}
return false
}).WithTimeout(5 * time.Second).Should(BeFalse())
})
}

Expand Down
27 changes: 17 additions & 10 deletions cmd/registry/push/push_rulesfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ package push_test

import (
"fmt"
"github.com/falcosecurity/falcoctl/internal/utils"

Check failure on line 22 in cmd/registry/push/push_rulesfiles_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/falcosecurity/falcoctl) (gci)
"os"
"path/filepath"
"regexp"
"time"

Check failure on line 27 in cmd/registry/push/push_rulesfiles_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `goimports`-ed with -local github.com/falcosecurity/falcoctl (goimports)
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/falcosecurity/falcoctl/cmd"

Check failure on line 33 in cmd/registry/push/push_rulesfiles_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/falcosecurity/falcoctl) (gci)
"github.com/falcosecurity/falcoctl/internal/utils"
"github.com/falcosecurity/falcoctl/pkg/oci"
testutils "github.com/falcosecurity/falcoctl/pkg/test"
)

// revive:enable

var _ = Describe("pushing rulesfiles", func() {
var rulesfiles = Describe("pushing rulesfiles", func() {

Check failure on line 40 in cmd/registry/push/push_rulesfiles_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

var `rulesfiles` is unused (unused)
var (
registryCmd = "registry"
pushCmd = "push"
Expand Down Expand Up @@ -113,15 +114,21 @@ var _ = Describe("pushing rulesfiles", func() {
Expect(rulesfileData.Tags).Should(ContainElements(pushedTags))

By("checking that temporary dirs have been removed")
entries, err := os.ReadDir("/tmp")
Expect(err).ShouldNot(HaveOccurred())
for _, e := range entries {
if e.IsDir() {
matched, err := filepath.Match(utils.TmpDirPrefix+"*", regexp.QuoteMeta(e.Name()))
Expect(err).ShouldNot(HaveOccurred())
Expect(matched).ShouldNot(BeTrue())
Eventually(func() bool {
entries, err := os.ReadDir("/tmp")
Expect(err).ShouldNot(HaveOccurred())
for _, e := range entries {
if e.IsDir() {
matched, err := filepath.Match(utils.TmpDirPrefix+"*", regexp.QuoteMeta(e.Name()))
Expect(err).ShouldNot(HaveOccurred())
if matched {
fmt.Println(e.Name())
return true
}
}
}
}
return false
}).WithTimeout(5 * time.Second).Should(BeFalse())
})
}

Expand Down
22 changes: 22 additions & 0 deletions cmd/registry/push/push_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package push_test
import (
"context"
"fmt"
"github.com/falcosecurity/falcoctl/internal/utils"

Check failure on line 21 in cmd/registry/push/push_suite_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/falcosecurity/falcoctl) (gci)
"net/http"
"os"
"path/filepath"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -108,3 +110,23 @@ func executeRoot(args []string) error {
rootCmd.SetOut(output)
return cmd.Execute(rootCmd, opt)
}

var checkTmpDir = func() (bool, error) {

Check failure on line 114 in cmd/registry/push/push_suite_test.go

View workflow job for this annotation

GitHub Actions / Lint golang files

var `checkTmpDir` is unused (unused)
entries, err := os.ReadDir("/tmp")
if err != nil {
return false, err
}

for _, e := range entries {
if e.IsDir() {
matched, err := filepath.Match(utils.TmpDirPrefix+"*", regexp.QuoteMeta(e.Name()))
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
}
return false, nil
}
7 changes: 5 additions & 2 deletions internal/utils/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import (
const TmpDirPrefix = "falcoctl-registry-push-"

// CreateTarGzArchive compresses and saves in a tar archive the passed file.
func CreateTarGzArchive(path string) (file string, err error) {
func CreateTarGzArchive(dir, path string) (file string, err error) {
cleanedPath := filepath.Clean(path)
if dir == "" {
dir = TmpDirPrefix
}
// Create output file.
tmpDir, err := os.MkdirTemp("", TmpDirPrefix)
tmpDir, err := os.MkdirTemp("", dir)
if err != nil {
return "", err
}
Expand Down
9 changes: 5 additions & 4 deletions internal/utils/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
filename1 = "file1"
filename2 = "file2"
tmpPrefix = "testCreateTarGzArchiveFile"
)

func TestCreateTarGzArchiveFile(t *testing.T) {
Expand All @@ -39,11 +40,11 @@ func TestCreateTarGzArchiveFile(t *testing.T) {
}
defer f1.Close()

tarball, err := CreateTarGzArchive(filepath.Join(dir, filename1))
tarball, err := CreateTarGzArchive(tmpPrefix, filepath.Join(dir, filename1))
if err != nil {
t.Fatalf(err.Error())
}
defer os.Remove(tarball)
defer os.RemoveAll(filepath.Dir(tarball))

file, err := os.Open(tarball)
if err != nil {
Expand Down Expand Up @@ -82,11 +83,11 @@ func TestCreateTarGzArchiveDir(t *testing.T) {
}
defer f2.Close()

tarball, err := CreateTarGzArchive(dir)
tarball, err := CreateTarGzArchive(tmpPrefix, dir)
if err != nil {
t.Fatalf(err.Error())
}
defer os.Remove(tarball)
defer os.RemoveAll(filepath.Dir(tarball))

file, err := os.Open(tarball)
if err != nil {
Expand Down

0 comments on commit 1ae873d

Please sign in to comment.