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,30 @@ 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
+ }
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
+
156
180
for _ , tt := range dockerDaemonTestCases {
157
181
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
160
183
dockerConn := & sourcespb.Docker {
161
184
Credential : & sourcespb.Docker_Unauthenticated {
162
185
Unauthenticated : & credentialspb.Unauthenticated {},
@@ -165,7 +188,7 @@ func TestDockerImageScanFromLocalDaemon(t *testing.T) {
165
188
}
166
189
167
190
conn := & anypb.Any {}
168
- err : = conn .MarshalFrom (dockerConn )
191
+ err = conn .MarshalFrom (dockerConn )
169
192
assert .NoError (t , err )
170
193
171
194
s := & Source {}
0 commit comments