From 613debd193f11a127504a5c95b0014ef21e9d76b Mon Sep 17 00:00:00 2001 From: Jason Keller Date: Thu, 15 Feb 2024 14:44:50 -0500 Subject: [PATCH] added async error return unit test --- pkg/remoteimageasync/synchronizer_test.go | 25 +++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/remoteimageasync/synchronizer_test.go b/pkg/remoteimageasync/synchronizer_test.go index 8b01ca9..9087cc2 100644 --- a/pkg/remoteimageasync/synchronizer_test.go +++ b/pkg/remoteimageasync/synchronizer_test.go @@ -42,6 +42,20 @@ func TestChannelStructContent(t *testing.T) { assert.NotNil(t, val2.err, "pass by reference does update value") } +func TestAsyncPullErrorReturn(t *testing.T) { + ctx, dontCare := context.WithTimeout(context.TODO(), 5*time.Second) + defer dontCare() + puller := StartAsyncPuller(ctx, 100, 20) + + err := pullImage(puller, "nginx:exists", 1, 5, 5) + assert.Nil(t, err, "no error should be returned for successful pull") + + err = pullImage(puller, nonExistentImage, 1, 5, 5) + assert.NotNil(t, err, "error should be returned for non-existent image") + + <-ctx.Done() +} + func TestPullDuration(t *testing.T) { ctx, dontCare := context.WithTimeout(context.TODO(), 5*time.Second) defer dontCare() @@ -197,16 +211,23 @@ func (p pullerMock) Pull(ctx context.Context) (err error) { } fmt.Printf("pullerMock: starting to pull image %s\n", p.image) + if p.image == nonExistentImage { + err = fmt.Errorf("pullerMock: non-existent image specified, returning this error\n") + fmt.Println(err.Error()) + return err + } select { case <-time.After(dur): fmt.Printf("pullerMock: finshed pulling image %s\n", p.image) - return + return nil case <-ctx.Done(): fmt.Printf("pullerMock: context cancelled\n") - return + return nil } } func (p pullerMock) ImageSize(ctx context.Context) int { return 0 } + +const nonExistentImage = "docker.io/warmmetal/container-image-csi-driver-test:simple-fs-doesnt-exist"