github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/fetcher_test.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildpacks
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"testing"
    23  
    24  	packimg "github.com/buildpacks/pack/pkg/image"
    25  	"github.com/docker/docker/client"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestFetcher(t *testing.T) {
    32  	tests := []struct {
    33  		description    string
    34  		imageExists    bool
    35  		pull           packimg.PullPolicy
    36  		expectedPulled []string
    37  	}{
    38  		{
    39  			description:    "pull",
    40  			pull:           packimg.PullAlways,
    41  			expectedPulled: []string{"image"},
    42  		},
    43  		{
    44  			description:    "pull-if-not-present but image present",
    45  			pull:           packimg.PullIfNotPresent,
    46  			imageExists:    true,
    47  			expectedPulled: nil,
    48  		},
    49  		{
    50  			description:    "pull-if-not-present",
    51  			pull:           packimg.PullIfNotPresent,
    52  			expectedPulled: []string{"image"},
    53  		},
    54  		{
    55  			description:    "don't pull",
    56  			pull:           packimg.PullNever,
    57  			expectedPulled: nil,
    58  		},
    59  	}
    60  	for _, test := range tests {
    61  		testutil.Run(t, test.description, func(t *testutil.T) {
    62  			api := &testutil.FakeAPIClient{}
    63  			if test.imageExists {
    64  				api.Add("image", "sha256:deadbeef")
    65  			}
    66  			docker := fakeLocalDaemon(api)
    67  
    68  			var out bytes.Buffer
    69  
    70  			f := newFetcher(&out, docker)
    71  			f.Fetch(context.Background(), "image", packimg.FetchOptions{Daemon: true, PullPolicy: test.pull})
    72  
    73  			t.CheckDeepEqual(test.expectedPulled, api.Pulled())
    74  		})
    75  	}
    76  }
    77  
    78  func fakeLocalDaemon(api client.CommonAPIClient) docker.LocalDaemon {
    79  	return docker.NewLocalDaemon(api, nil, false, nil)
    80  }