Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 942f25e

Browse files
committed
Add test command to taskfile
Signed-off-by: Christian Dupuis <[email protected]>
1 parent 1304a66 commit 942f25e

File tree

7 files changed

+65
-37
lines changed

7 files changed

+65
-37
lines changed

Taskfile.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ vars:
44
IMAGE_NAME: docker/index-cli-plugin:local
55

66
tasks:
7+
go:test:
8+
cmds:
9+
- go test ./...
10+
711
go:build:
812
cmds:
913
- go build -o docker-index -ldflags="-w -s -X 'github.com/docker/index-cli-plugin/internal.version={{.GIT_COMMIT}}'"

sbom/detect/detect.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright © 2022 Docker, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package detect
18+
19+
import (
20+
"github.com/anchore/syft/syft/source"
21+
"github.com/docker/index-cli-plugin/types"
22+
)
23+
24+
type PackageDetector = func(packages []types.Package, image source.Source, lm types.LayerMapping) []types.Package
25+
26+
var detectors []PackageDetector
27+
28+
func init() {
29+
detectors = []PackageDetector{nodePackageDetector}
30+
}
31+
32+
func AdditionalPackages(packages []types.Package, image source.Source, lm types.LayerMapping) []types.Package {
33+
additionalPackages := make([]types.Package, 0)
34+
for _, d := range detectors {
35+
additionalPackages = append(additionalPackages, d(packages, image, lm)...)
36+
}
37+
return additionalPackages
38+
}

sbom/addition_test.go renamed to sbom/detect/detect_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package detect
1818

1919
import (
2020
"testing"
@@ -28,8 +28,10 @@ import (
2828

2929
func TestNodeDetector(t *testing.T) {
3030
cmd, _ := command.NewDockerCli()
31-
img, ociPath, _ := registry.SaveImage("node@sha256:2b00d259f3b07d8aa694b298a7dcf4655571aea2ab91375b5adb8e5a905d3ee2", cmd.Client())
32-
lm := createLayerMapping(img)
31+
_, ociPath, _ := registry.SaveImage("node@sha256:2b00d259f3b07d8aa694b298a7dcf4655571aea2ab91375b5adb8e5a905d3ee2", cmd.Client())
32+
lm := types.LayerMapping{
33+
ByDiffId: make(map[string]string),
34+
}
3335
i := source.Input{
3436
Scheme: source.ImageScheme,
3537
ImageSource: stereoscopeimage.OciDirectorySource,

sbom/addition.go renamed to sbom/detect/node.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package detect
1818

1919
import (
2020
"fmt"
@@ -24,22 +24,6 @@ import (
2424
"github.com/docker/index-cli-plugin/types"
2525
)
2626

27-
type PackageDetector = func(packages []types.Package, image source.Source, lm types.LayerMapping) []types.Package
28-
29-
var detectors []PackageDetector
30-
31-
func init() {
32-
detectors = []PackageDetector{nodePackageDetector}
33-
}
34-
35-
func detectAdditionalPackages(packages []types.Package, image source.Source, lm types.LayerMapping) []types.Package {
36-
additionalPackages := make([]types.Package, 0)
37-
for _, d := range detectors {
38-
additionalPackages = append(additionalPackages, d(packages, image, lm)...)
39-
}
40-
return additionalPackages
41-
}
42-
4327
func nodePackageDetector(_ []types.Package, image source.Source, lm types.LayerMapping) []types.Package {
4428
var path []string
4529
var nodeVersion string

sbom/syft.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"github.com/anchore/syft/syft/pkg/cataloger/deb"
3131
"github.com/anchore/syft/syft/pkg/cataloger/rpm"
3232
"github.com/anchore/syft/syft/source"
33+
"github.com/docker/index-cli-plugin/sbom/detect"
34+
"github.com/docker/index-cli-plugin/sbom/util"
3335
"github.com/docker/index-cli-plugin/types"
3436
"github.com/pkg/errors"
3537
)
@@ -69,7 +71,7 @@ func syftSbom(ociPath string, lm types.LayerMapping, resultChan chan<- types.Ind
6971
pm := make(packageMapping, 0)
7072
for _, layer := range src.Image.Layers {
7173
layerPkgs := make([]pkg2.Package, 0)
72-
res := newSingleLayerResolver(layer)
74+
res := util.NewSingleLayerResolver(layer)
7375
apkPkgs, _, err := apkdb.NewApkdbCataloger().Catalog(res)
7476
if err != nil {
7577
if err != nil {
@@ -108,7 +110,7 @@ func syftSbom(ociPath string, lm types.LayerMapping, resultChan chan<- types.Ind
108110
result.Packages = append(result.Packages, pkg...)
109111
}
110112

111-
result.Packages = append(result.Packages, detectAdditionalPackages(result.Packages, *src, lm)...)
113+
result.Packages = append(result.Packages, detect.AdditionalPackages(result.Packages, *src, lm)...)
112114
resultChan <- result
113115
}
114116

sbom/single_layer_resolver.go renamed to sbom/util/single_layer_resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package util
1818

1919
import (
2020
"io"
@@ -30,7 +30,7 @@ type singleLayerResolver struct {
3030
layer *image.Layer
3131
}
3232

33-
func newSingleLayerResolver(layer *image.Layer) *singleLayerResolver {
33+
func NewSingleLayerResolver(layer *image.Layer) *singleLayerResolver {
3434
return &singleLayerResolver{layer: layer}
3535
}
3636

sbom/index_test.go renamed to types/purl_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package types
1818

1919
import (
2020
"testing"
21-
22-
"github.com/docker/index-cli-plugin/types"
2321
)
2422

2523
func TestMergePackages(t *testing.T) {
26-
pkga := types.Package{
24+
pkga := Package{
2725
Purl: "pkg:maven/[email protected]",
28-
Files: []types.Location{{
26+
Files: []Location{{
2927
Path: "/bar",
3028
Digest: "sha256:1234",
3129
DiffId: "sha256:1234",
3230
}},
3331
}
34-
pkgb := types.Package{
32+
pkgb := Package{
3533
Purl: "pkg:maven/[email protected]",
36-
Files: []types.Location{{
34+
Files: []Location{{
3735
Path: "/bar",
3836
Digest: "sha256:1234",
3937
DiffId: "sha256:1234",
@@ -43,12 +41,12 @@ func TestMergePackages(t *testing.T) {
4341
DiffId: "sha256:5678",
4442
}},
4543
}
46-
packages := types.MergePackages(types.IndexResult{
47-
Status: types.Success,
48-
Packages: []types.Package{pkga},
49-
}, types.IndexResult{
50-
Status: types.Success,
51-
Packages: []types.Package{pkgb},
44+
packages := MergePackages(IndexResult{
45+
Status: Success,
46+
Packages: []Package{pkga},
47+
}, IndexResult{
48+
Status: Success,
49+
Packages: []Package{pkgb},
5250
})
5351
if len(packages) != 1 {
5452
t.Error("expected 1 package")

0 commit comments

Comments
 (0)