Skip to content

Commit

Permalink
Merge pull request #1303 from ccojocar/fix_compliance_results
Browse files Browse the repository at this point in the history
fix:(compliance) check the status of the compliance before retrieving the results
  • Loading branch information
rawlingsj authored Jul 18, 2018
2 parents 5153d1f + 71e6fa2 commit 848dae6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 38 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 53 additions & 37 deletions pkg/jx/cmd/compliance_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (

"github.com/heptio/sonobuoy/pkg/client"
"github.com/heptio/sonobuoy/pkg/client/results"
"github.com/heptio/sonobuoy/pkg/plugin/aggregation"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/onsi/ginkgo/reporters"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

var (
Expand Down Expand Up @@ -68,45 +70,52 @@ func (o *ComplianceResultsOptions) Run() error {
return errors.Wrap(err, "could not create the compliance client")
}

status, err := cc.GetStatus(complianceNamespace)
if err != nil {
return errors.Wrap(err, "failed to retrieve the compliance status")
}

if status.Status != aggregation.CompleteStatus {
log.Info("Compliance results not ready. Run `jx compliance status` for status.")
return nil
}

cfg := &client.RetrieveConfig{
Namespace: complianceNamespace,
}

reader, errch := cc.RetrieveResults(cfg)

// TODO Refactor? Seems like there should be a nicer way to handle this...
go func() {
for {
select {
case err := <-errch:
if err != nil {
log.Fatalf("could not retrieve the compliance results\n %v", err)
o.Exit(1)
}
}
eg := &errgroup.Group{}
eg.Go(func() error { return <-errch })
eg.Go(func() error {
resultsReader, errch := untarResults(reader)
gzr, err := gzip.NewReader(resultsReader)
if err != nil {
return errors.Wrap(err, "could not create a gzip reader for compliance results ")
}
}()

resultsReader, err := untarResults(reader)
if err != nil {
return errors.Wrap(err, "could not extract the compliance results from archive")
}

gzr, err := gzip.NewReader(resultsReader)
if err != nil {
return errors.Wrap(err, "could not create a gzip reader for compliance results ")
}
testResults, err := cc.GetTests(gzr, "all")
if err != nil {
return errors.Wrap(err, "could not get the results of the compliance tests from the archive")
}
testResults = filterTests(
func(tc reporters.JUnitTestCase) bool {
return !results.Skipped(tc)
}, testResults)
sort.Sort(StatusSortedTestCases(testResults))
o.printResults(testResults)

err = <-errch
if err != nil {
return errors.Wrap(err, "could not extract the compliance results from archive")
}
return nil
})

testResults, err := cc.GetTests(gzr, "all")
err = eg.Wait()
if err != nil {
return errors.Wrap(err, "could not get the results of the compliance tests from the archive")
return errors.Wrap(err, "failed to retrieve the results")
}
testResults = filterTests(
func(tc reporters.JUnitTestCase) bool {
return !results.Skipped(tc)
}, testResults)
sort.Sort(StatusSortedTestCases(testResults))
o.printResults(testResults)
return nil
}

Expand Down Expand Up @@ -156,27 +165,34 @@ func status(junitResult reporters.JUnitTestCase) string {
}
}

func untarResults(src io.Reader) (io.Reader, error) {
func untarResults(src io.Reader) (io.Reader, <-chan error) {
ec := make(chan error, 1)
tarReader := tar.NewReader(src)
for {
header, err := tarReader.Next()
if err != nil {
if err != io.EOF {
return nil, err
ec <- err
return nil, ec
} else {
ec <- errors.New("no compliance results archive found")
return nil, ec
}
break
}
if strings.HasSuffix(header.Name, ".tar.gz") {
reader, writer := io.Pipe()
//TODO propagate the error out of the goroutine
go func(writer *io.PipeWriter) {
go func(writer *io.PipeWriter, ec chan error) {
defer writer.Close()
io.Copy(writer, tarReader)
}(writer)
defer close(ec)
_, err := io.Copy(writer, tarReader)
if err != nil {
ec <- err
}
}(writer, ec)
return reader, nil
}
}
return nil, errors.New("no compliance results archive found")
return nil, ec
}

func filterTests(predicate func(testCase reporters.JUnitTestCase) bool, testCases []reporters.JUnitTestCase) []reporters.JUnitTestCase {
Expand Down
3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/sync/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/sync/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/sync/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/golang.org/x/sync/errgroup/errgroup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 848dae6

Please sign in to comment.