1
1
package docker
2
2
3
3
import (
4
+ "io"
4
5
"strings"
5
6
"sync"
6
7
"testing"
7
8
9
+ image "github.com/docker/docker/api/types/image"
10
+ dockerClient "github.com/docker/docker/client"
8
11
"github.com/stretchr/testify/assert"
9
12
"google.golang.org/protobuf/types/known/anypb"
10
13
@@ -153,10 +156,32 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
153
156
},
154
157
}
155
158
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
+
156
182
for _ , tt := range dockerDaemonTestCases {
157
183
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
160
185
dockerConn := & sourcespb.Docker {
161
186
Credential : & sourcespb.Docker_Unauthenticated {
162
187
Unauthenticated : & credentialspb.Unauthenticated {},
@@ -165,7 +190,7 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
165
190
}
166
191
167
192
conn := & anypb.Any {}
168
- err : = conn .MarshalFrom (dockerConn )
193
+ err = conn .MarshalFrom (dockerConn )
169
194
assert .NoError (t , err )
170
195
171
196
s := & Source {}
0 commit comments