Skip to content
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

fix:(compliance) check the status of the compliance before retrieving the results #1303

Merged
merged 4 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.