github.com/thediveo/morbyd@v0.11.1/image_has_test.go (about)

     1  // Copyright 2024 Harald Albrecht.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package morbyd
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"time"
    21  
    22  	image "github.com/docker/docker/api/types/image"
    23  	mock "go.uber.org/mock/gomock"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	. "github.com/onsi/gomega/gleak"
    28  	. "github.com/thediveo/success"
    29  )
    30  
    31  var _ = Describe("image presence", Ordered, func() {
    32  
    33  	BeforeEach(func(ctx context.Context) {
    34  		goodgos := Goroutines()
    35  		DeferCleanup(func() {
    36  			Eventually(Goroutines).Within(2 * time.Second).ProbeEvery(250 * time.Millisecond).
    37  				ShouldNot(HaveLeaked(goodgos))
    38  		})
    39  	})
    40  
    41  	It("reports whether an image is locally available", func(ctx context.Context) {
    42  		const imgref = "busybox:latestandgreatest"
    43  
    44  		ctrl := mock.NewController(GinkgoT())
    45  		sess := Successful(NewSession(ctx,
    46  			WithMockController(ctrl, "ImageList")))
    47  		DeferCleanup(func(ctx context.Context) {
    48  			sess.Close(ctx)
    49  		})
    50  		rec := sess.Client().(*MockClient).EXPECT()
    51  		rec.ImageList(Any, Any).Return([]image.Summary{}, nil)
    52  		rec.ImageList(Any, Any).Return([]image.Summary{
    53  			{ /*doesn't matter what, just needs to exist*/ },
    54  		}, nil)
    55  
    56  		Expect(sess.HasImage(ctx, imgref)).To(BeFalse())
    57  		Expect(sess.HasImage(ctx, imgref)).To(BeTrue())
    58  	})
    59  
    60  	It("reports image listing errors", func(ctx context.Context) {
    61  		ctrl := mock.NewController(GinkgoT())
    62  		sess := Successful(NewSession(ctx,
    63  			WithMockController(ctrl, "ImageList")))
    64  		DeferCleanup(func(ctx context.Context) {
    65  			sess.Close(ctx)
    66  		})
    67  		rec := sess.Client().(*MockClient).EXPECT()
    68  		rec.ImageList(Any, Any).Return(nil, errors.New("error IJK305I"))
    69  
    70  		Expect(sess.HasImage(ctx, "busybox:absolutelygreatest")).Error().To(HaveOccurred())
    71  	})
    72  
    73  })