-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
62 lines (55 loc) · 1.38 KB
/
main_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-containerregistry/pkg/authn"
gname "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
const testImage = "sclevine/sbom-test-app"
func TestSBoM(t *testing.T) {
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
in := filepath.Join(dir, "in")
out := filepath.Join(dir, "base."+getDigestHex(t, testImage)[:8]+".test.json")
if err := ioutil.WriteFile(in, []byte("test"), 0777); err != nil {
t.Fatal(err)
}
if err := addBaseSBoM(testImage, in, ".test.json"); err != nil {
t.Fatal(err)
}
if err := getSBoM(testImage, dir); err != nil {
// TODO: test with app SBoM
if !strings.Contains(err.Error(), "could not retrieve app SBoM: cannot parse hash") {
t.Fatal(err)
}
}
sbom, err := ioutil.ReadFile(out)
if err != nil {
t.Fatal(err)
}
if tt := string(sbom); tt != "test" {
t.Fatalf("found: %s, expected test string", tt)
}
}
func getDigestHex(t *testing.T, image string) string {
ref, err := gname.ParseReference(image, gname.WeakValidation)
if err != nil {
t.Fatal(err)
}
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
t.Fatal(err)
}
digest, err := img.Digest()
if err != nil {
t.Fatal(err)
}
return digest.Hex
}