Skip to content

Commit

Permalink
Merge pull request #631 from bcrochet/coverage-engine
Browse files Browse the repository at this point in the history
Enhance test coverage for internal/engine package
  • Loading branch information
bcrochet authored May 16, 2022
2 parents 7fc513a + 8067024 commit 16e00a2
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 1,206 deletions.
50 changes: 0 additions & 50 deletions certification/internal/cli/podman.go

This file was deleted.

5 changes: 5 additions & 0 deletions certification/internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"time"

"github.com/google/go-containerregistry/pkg/crane"
Expand Down Expand Up @@ -93,6 +94,8 @@ func (c *CraneEngine) ExecuteChecks(ctx context.Context) error {

// export/flatten, and extract
log.Debug("exporting and flattening image")
wg := sync.WaitGroup{}
wg.Add(1)
r, w := io.Pipe()
go func() {
defer w.Close()
Expand All @@ -105,12 +108,14 @@ func (c *CraneEngine) ExecuteChecks(ctx context.Context) error {
// an error, which requires watching multiple error streams.
log.Error("unable to export and flatten container filesystem:", err)
}
wg.Done()
}()

log.Debug("extracting container filesystem to ", containerFSPath)
if err := untar(containerFSPath, r); err != nil {
return fmt.Errorf("%w: %s", errors.ErrExtractingTarball, err)
}
wg.Wait()

reference, err := name.ParseReference(c.Image)
if err != nil {
Expand Down
102 changes: 102 additions & 0 deletions certification/internal/engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,112 @@
package engine

import (
"context"
"errors"
"fmt"
"net/http/httptest"
"net/url"
"os"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/registry"
"github.com/google/go-containerregistry/pkg/v1/random"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-openshift-ecosystem/openshift-preflight/certification"
"github.com/spf13/viper"
)

var _ = Describe("Execute Checks tests", func() {
var src string
var engine CraneEngine
BeforeEach(func() {
// Set up a fake registry.
s := httptest.NewServer(registry.New())
DeferCleanup(func() {
s.Close()
})
u, err := url.Parse(s.URL)
Expect(err).ToNot(HaveOccurred())

src = fmt.Sprintf("%s/test/crane", u.Host)

// Expected values.
img, err := random.Image(1024, 5)
Expect(err).ToNot(HaveOccurred())

err = crane.Push(img, src)
Expect(err).ToNot(HaveOccurred())

tmpDir, err := os.MkdirTemp("", "preflight-engine-test-*")
DeferCleanup(os.RemoveAll, tmpDir)
viper.Set("artifacts", tmpDir)

goodCheck := certification.NewGenericCheck(
"testcheck",
func(context.Context, certification.ImageReference) (bool, error) {
return true, nil
},
certification.Metadata{},
certification.HelpText{},
)

errorCheck := certification.NewGenericCheck(
"errorCheck",
func(context.Context, certification.ImageReference) (bool, error) {
return false, errors.New("errorCheck")
},
certification.Metadata{},
certification.HelpText{},
)

failedCheck := certification.NewGenericCheck(
"failedCheck",
func(context.Context, certification.ImageReference) (bool, error) {
return false, nil
},
certification.Metadata{},
certification.HelpText{},
)

engine = CraneEngine{
Image: src,
Checks: []certification.Check{
goodCheck,
errorCheck,
failedCheck,
},
IsBundle: false,
IsScratch: false,
}
})
Context("Run the checks", func() {
It("should succeed", func() {
err := engine.ExecuteChecks(context.TODO())
Expect(err).ToNot(HaveOccurred())
Expect(engine.results.Passed).To(HaveLen(1))
Expect(engine.results.Failed).To(HaveLen(1))
Expect(engine.results.Errors).To(HaveLen(1))
Expect(engine.results.CertificationHash).To(BeEmpty())
})
Context("it is a bundle", func() {
It("should succeed and generate a bundle hash", func() {
engine.IsBundle = true
err := engine.ExecuteChecks(context.TODO())
Expect(err).ToNot(HaveOccurred())
Expect(engine.results.CertificationHash).ToNot(BeEmpty())
})
})
Context("the image is invalid", func() {
It("should throw a crane error on pull", func() {
engine.Image = "does.not/exist/anywhere:ever"
err := engine.ExecuteChecks(context.TODO())
Expect(err).To(HaveOccurred())
})
})
})
})

var _ = Describe("Source RPM name function", func() {
Context("With a source rpm name", func() {
Context("And a normal source rpm name", func() {
Expand Down
Loading

0 comments on commit 16e00a2

Please sign in to comment.