-
Notifications
You must be signed in to change notification settings - Fork 511
/
convert_test.go
74 lines (64 loc) · 2.17 KB
/
convert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package integration
import (
"context"
"io/ioutil"
"os"
"testing"
"github.com/anchore/syft/cmd/syft/cli/convert"
"github.com/anchore/syft/internal/config"
"github.com/anchore/syft/internal/formats/cyclonedxjson"
"github.com/anchore/syft/internal/formats/cyclonedxxml"
"github.com/anchore/syft/internal/formats/spdx22json"
"github.com/anchore/syft/internal/formats/spdx22tagvalue"
"github.com/anchore/syft/internal/formats/syftjson"
"github.com/anchore/syft/internal/formats/table"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
"github.com/stretchr/testify/require"
)
var convertibleFormats = []sbom.Format{
syftjson.Format(),
spdx22json.Format(),
spdx22tagvalue.Format(),
cyclonedxjson.Format(),
cyclonedxxml.Format(),
}
// TestConvertCmd tests if the converted SBOM is a valid document according
// to spec.
// TODO: This test can, but currently does not, check the converted SBOM content. It
// might be useful to do that in the future, once we gather a better understanding of
// what users expect from the convert command.
func TestConvertCmd(t *testing.T) {
for _, format := range convertibleFormats {
t.Run(format.ID().String(), func(t *testing.T) {
sbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, false)
format := syft.FormatByID(syftjson.ID)
f, err := ioutil.TempFile("", "test-convert-sbom-")
require.NoError(t, err)
defer func() {
os.Remove(f.Name())
}()
err = format.Encode(f, sbom)
require.NoError(t, err)
ctx := context.Background()
app := &config.Application{Outputs: []string{format.ID().String()}}
// stdout reduction of test noise
rescue := os.Stdout // keep backup of the real stdout
os.Stdout, _ = os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
defer func() {
os.Stdout = rescue
}()
err = convert.Run(ctx, app, []string{f.Name()})
require.NoError(t, err)
file, err := ioutil.ReadFile(f.Name())
require.NoError(t, err)
formatFound := syft.IdentifyFormat(file)
if format.ID() == table.ID {
require.Nil(t, formatFound)
return
}
require.Equal(t, format.ID(), formatFound.ID())
})
}
}