Skip to content

Commit 6865bc0

Browse files
committed
Enhance Docker image scanning tests by ensuring local image availability
1 parent 726a586 commit 6865bc0

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

pkg/sources/docker/docker_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package docker
22

33
import (
4+
"io"
45
"strings"
56
"sync"
67
"testing"
78

9+
image "github.com/docker/docker/api/types/image"
10+
dockerClient "github.com/docker/docker/client"
811
"github.com/stretchr/testify/assert"
912
"google.golang.org/protobuf/types/known/anypb"
1013

@@ -153,10 +156,30 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
153156
},
154157
}
155158

159+
// pull the image here to ensure it exists locally
160+
img := "docker.io/trufflesecurity/secrets:latest"
161+
162+
client, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv, dockerClient.WithAPIVersionNegotiation())
163+
if err != nil {
164+
t.Errorf("Failed to create Docker client: %v", err)
165+
}
166+
167+
resp, err := client.ImagePull(context.TODO(), img, image.PullOptions{})
168+
if err != nil {
169+
t.Errorf("Failed to load image %s: %v", img, err)
170+
}
171+
172+
defer resp.Close()
173+
174+
// if we don't read the response, the image will not be available in the local Docker daemon
175+
_, err = io.ReadAll(resp)
176+
if err != nil {
177+
t.Errorf("Failed to read response body: %v", err)
178+
}
179+
156180
for _, tt := range dockerDaemonTestCases {
157181
t.Run(tt.name, func(t *testing.T) {
158-
// This test assumes the local Docker daemon is running and the image exists locally.
159-
// You may need to pull or build the image "trufflesecurity/secrets" locally before running this test.
182+
// This test assumes the local Docker daemon is running
160183
dockerConn := &sourcespb.Docker{
161184
Credential: &sourcespb.Docker_Unauthenticated{
162185
Unauthenticated: &credentialspb.Unauthenticated{},
@@ -165,7 +188,7 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
165188
}
166189

167190
conn := &anypb.Any{}
168-
err := conn.MarshalFrom(dockerConn)
191+
err = conn.MarshalFrom(dockerConn)
169192
assert.NoError(t, err)
170193

171194
s := &Source{}

0 commit comments

Comments
 (0)