test: Improve image list checks

We had similar code to check if image exists or not. Add
checkImageNotExists() helper and refactor listImages() helper to make it
easier to use.

listImages() fails the test so the caller do no not need to check for
errors. It returns list of images so the caller does not have to parse
the output.

When parsing the output of `minikube image ls` use scanner to split the
output to lines. Previously we search images in rr.Output() (formatted
message with contents of stdout and stderr) instead of
rr.Stdout.String(), which could lead to returning a wrong result.
pull/21405/head
Nir Soffer 2025-08-23 15:34:29 +03:00
parent 12b7ead6ed
commit ac378f46f5
1 changed files with 28 additions and 17 deletions

View File

@ -34,6 +34,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"testing"
"time"
@ -409,14 +410,7 @@ func validateImageCommands(ctx context.Context, t *testing.T, profile string) {
t.Fatalf("removing image from minikube: %v\n%s", err, rr.Output())
}
// make sure the image was removed
rr, err = listImages(ctx, t, profile)
if err != nil {
t.Fatalf("listing images: %v\n%s", err, rr.Output())
}
if strings.Contains(rr.Output(), taggedImage) {
t.Fatalf("expected %q to be removed from minikube but still exists", taggedImage)
}
checkImageNotExists(ctx, t, profile, taggedImage)
})
// docs: Make sure image loading from file works by `minikube image load`
@ -452,18 +446,35 @@ func validateImageCommands(ctx context.Context, t *testing.T, profile string) {
}
func checkImageExists(ctx context.Context, t *testing.T, profile string, image string) {
// make sure the image was correctly loaded
rr, err := listImages(ctx, t, profile)
if err != nil {
t.Fatalf("listing images: %v\n%s", err, rr.Output())
}
if !strings.Contains(rr.Output(), image) {
t.Fatalf("expected %q to be loaded into minikube but the image is not there", image)
images := listImages(ctx, t, profile)
if !slices.Contains(images, image) {
t.Fatalf("expected %q to exist in minikube but the image is not there", image)
}
}
func listImages(ctx context.Context, t *testing.T, profile string) (*RunResult, error) {
return Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "ls"))
func checkImageNotExists(ctx context.Context, t *testing.T, profile string, image string) {
images := listImages(ctx, t, profile)
if slices.Contains(images, image) {
t.Fatalf("expected %q to not exist in minikube but the image is there", image)
}
}
func listImages(ctx context.Context, t *testing.T, profile string) []string {
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "ls"))
if err != nil {
t.Fatalf("failed to list images: %v\n%s", err, rr.Output())
}
scanner := bufio.NewScanner(rr.Stdout)
var images []string
for scanner.Scan() {
images = append(images, scanner.Text())
}
if err := scanner.Err(); err != nil {
t.Fatalf("failed to scan lines: %v: %q", err, rr.Stdout.String())
}
return images
}
// check functionality of minikube after evaluating docker-env