github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/docker/errors_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 docker
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    30  	"github.com/GoogleContainerTools/skaffold/testutil"
    31  )
    32  
    33  func TestDockerBuildError(t *testing.T) {
    34  	tests := []struct {
    35  		description    string
    36  		dockerfilepath string
    37  		expected       string
    38  		shouldErr      bool
    39  	}{
    40  		{
    41  			description:    "docker file present",
    42  			dockerfilepath: "Dockerfile",
    43  		},
    44  		{
    45  			description:    "docker file not present",
    46  			dockerfilepath: "DockerfileNotExist",
    47  			shouldErr:      true,
    48  			expected: `Dockerfile not found. Please check config \'dockerfile\' for artifact test-image.
    49  Refer https://skaffold.dev/docs/references/yaml/#build-artifacts-docker for details.`,
    50  		},
    51  	}
    52  	for _, test := range tests {
    53  		testutil.Run(t, test.description, func(t *testutil.T) {
    54  			t.NewTempDir().Touch("Dockerfile").Chdir()
    55  			dockerfilePath, _ := filepath.Abs("Dockerfile")
    56  			t.Override(&docker.EvalBuildArgs, func(_ config.RunMode, _ string, _ string, args map[string]*string, _ map[string]*string) (map[string]*string, error) {
    57  				return args, nil
    58  			})
    59  			t.Override(&util.DefaultExecCommand, testutil.CmdRun(
    60  				"docker build . --file "+dockerfilePath+" -t tag",
    61  			))
    62  			t.Override(&docker.DefaultAuthHelper, stubAuth{})
    63  			builder := NewArtifactBuilder(fakeLocalDaemonWithExtraEnv([]string{}), mockConfig{}, true, nil, false, mockArtifactResolver{make(map[string]string)}, nil)
    64  
    65  			artifact := &latest.Artifact{
    66  				ImageName: "test-image",
    67  				Workspace: ".",
    68  				ArtifactType: latest.ArtifactType{
    69  					DockerArtifact: &latest.DockerArtifact{
    70  						DockerfilePath: test.dockerfilepath,
    71  					},
    72  				},
    73  			}
    74  
    75  			_, err := builder.Build(context.Background(), ioutil.Discard, artifact, "tag", platform.Matcher{})
    76  			t.CheckError(test.shouldErr, err)
    77  			if test.shouldErr {
    78  				t.CheckErrorContains("", err)
    79  			}
    80  		})
    81  	}
    82  }