Skip to content

Commit 6dac565

Browse files
committed
Enhance Docker image scanning tests by ensuring local image availability
1 parent c10e0c6 commit 6dac565

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

pkg/sources/docker/docker_test.go

Lines changed: 28 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,32 @@ 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+
return
166+
}
167+
168+
resp, err := client.ImagePull(context.TODO(), img, image.PullOptions{})
169+
if err != nil {
170+
t.Errorf("Failed to load image %s: %v", img, err)
171+
return
172+
}
173+
174+
defer resp.Close()
175+
176+
// if we don't read the response, the image will not be available in the local Docker daemon
177+
_, err = io.ReadAll(resp)
178+
if err != nil {
179+
t.Errorf("Failed to read response body: %v", err)
180+
}
181+
156182
for _, tt := range dockerDaemonTestCases {
157183
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.
184+
// This test assumes the local Docker daemon is running
160185
dockerConn := &sourcespb.Docker{
161186
Credential: &sourcespb.Docker_Unauthenticated{
162187
Unauthenticated: &credentialspb.Unauthenticated{},
@@ -165,7 +190,7 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
165190
}
166191

167192
conn := &anypb.Any{}
168-
err := conn.MarshalFrom(dockerConn)
193+
err = conn.MarshalFrom(dockerConn)
169194
assert.NoError(t, err)
170195

171196
s := &Source{}

0 commit comments

Comments
 (0)