mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-04-28 11:30:15 +00:00
94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package dockeragent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/moby/moby/api/types/image"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func TestAgent_getImageRepoDigest_Error(t *testing.T) {
|
|
agent := &Agent{
|
|
docker: &fakeDockerClient{
|
|
imageInspectWithRawFn: func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
return image.InspectResponse{}, nil, errors.New("inspect failed")
|
|
},
|
|
},
|
|
logger: zerolog.New(io.Discard),
|
|
}
|
|
|
|
got, _, _, _ := agent.getImageRepoDigest(context.Background(), "image-id", "nginx:latest")
|
|
if got != "" {
|
|
t.Fatalf("expected empty digest on error, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAgent_getImageRepoDigest_NoRepoDigests(t *testing.T) {
|
|
agent := &Agent{
|
|
docker: &fakeDockerClient{
|
|
imageInspectWithRawFn: func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
return image.InspectResponse{RepoDigests: nil}, nil, nil
|
|
},
|
|
},
|
|
logger: zerolog.New(io.Discard),
|
|
}
|
|
|
|
got, _, _, _ := agent.getImageRepoDigest(context.Background(), "image-id", "nginx:latest")
|
|
if got != "" {
|
|
t.Fatalf("expected empty digest for no RepoDigests, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAgent_getImageRepoDigest_Match(t *testing.T) {
|
|
agent := &Agent{
|
|
docker: &fakeDockerClient{
|
|
imageInspectWithRawFn: func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
return image.InspectResponse{RepoDigests: []string{"docker.io/library/nginx@sha256:abc"}}, nil, nil
|
|
},
|
|
},
|
|
logger: zerolog.New(io.Discard),
|
|
}
|
|
|
|
got, _, _, _ := agent.getImageRepoDigest(context.Background(), "image-id", "nginx:latest")
|
|
if got != "sha256:abc" {
|
|
t.Fatalf("expected matching digest, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAgent_getImageRepoDigest_FallbackToFirst(t *testing.T) {
|
|
agent := &Agent{
|
|
docker: &fakeDockerClient{
|
|
imageInspectWithRawFn: func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
return image.InspectResponse{RepoDigests: []string{
|
|
"docker.io/library/redis@sha256:first",
|
|
"docker.io/library/nginx@sha256:second",
|
|
}}, nil, nil
|
|
},
|
|
},
|
|
logger: zerolog.New(io.Discard),
|
|
}
|
|
|
|
got, _, _, _ := agent.getImageRepoDigest(context.Background(), "image-id", "custom:latest")
|
|
if got != "sha256:first" {
|
|
t.Fatalf("expected fallback digest, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAgent_getImageRepoDigest_InvalidRepoDigest(t *testing.T) {
|
|
agent := &Agent{
|
|
docker: &fakeDockerClient{
|
|
imageInspectWithRawFn: func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
return image.InspectResponse{RepoDigests: []string{"invalid-digest"}}, nil, nil
|
|
},
|
|
},
|
|
logger: zerolog.New(io.Discard),
|
|
}
|
|
|
|
got, _, _, _ := agent.getImageRepoDigest(context.Background(), "image-id", "nginx:latest")
|
|
if got != "" {
|
|
t.Fatalf("expected empty digest for invalid repo digest, got %q", got)
|
|
}
|
|
}
|