github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/custom/dependencies_test.go (about)

     1  /*
     2  Copyright 2019 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 custom
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    26  	"github.com/GoogleContainerTools/skaffold/testutil"
    27  )
    28  
    29  func TestGetDependenciesDockerfile(t *testing.T) {
    30  	tmpDir := testutil.NewTempDir(t)
    31  
    32  	// Directory structure:
    33  	//   foo
    34  	//   bar
    35  	// - baz
    36  	//     file
    37  	//   Dockerfile
    38  	tmpDir.Touch("foo", "bar", "baz/file")
    39  	tmpDir.Write("Dockerfile", "FROM scratch \n ARG file \n COPY $file baz/file .")
    40  
    41  	customArtifact := &latest.CustomArtifact{
    42  		Dependencies: &latest.CustomDependencies{
    43  			Dockerfile: &latest.DockerfileDependency{
    44  				Path: "Dockerfile",
    45  				BuildArgs: map[string]*string{
    46  					"file": util.StringPtr("foo"),
    47  				},
    48  			},
    49  		},
    50  	}
    51  
    52  	expected := []string{"Dockerfile", filepath.FromSlash("baz/file"), "foo"}
    53  	deps, err := GetDependencies(context.Background(), tmpDir.Root(), "test", customArtifact, nil)
    54  
    55  	testutil.CheckErrorAndDeepEqual(t, false, err, expected, deps)
    56  }
    57  
    58  func TestGetDependenciesCommand(t *testing.T) {
    59  	testutil.Run(t, "", func(t *testutil.T) {
    60  		workspace := "test/workspace"
    61  
    62  		t.Override(&util.DefaultExecCommand, testutil.CmdRunDirOut(
    63  			"echo [\"file1\",\"file2\",\"file3\"]",
    64  			workspace,
    65  			"[\"file1\",\"file2\",\"file3\"]",
    66  		))
    67  
    68  		customArtifact := &latest.CustomArtifact{
    69  			Dependencies: &latest.CustomDependencies{
    70  				Command: "echo [\"file1\",\"file2\",\"file3\"]",
    71  			},
    72  		}
    73  
    74  		expected := []string{"file1", "file2", "file3"}
    75  		deps, err := GetDependencies(context.Background(), workspace, "test", customArtifact, nil)
    76  
    77  		t.CheckNoError(err)
    78  		t.CheckDeepEqual(expected, deps)
    79  	})
    80  }
    81  
    82  func TestGetDependenciesPaths(t *testing.T) {
    83  	tests := []struct {
    84  		description string
    85  		ignore      []string
    86  		paths       []string
    87  		expected    []string
    88  		shouldErr   bool
    89  	}{
    90  		{
    91  			description: "watch everything",
    92  			paths:       []string{"."},
    93  			expected:    []string{"bar", filepath.FromSlash("baz/file"), "foo"},
    94  		},
    95  		{
    96  			description: "watch nothing",
    97  		},
    98  		{
    99  			description: "ignore some paths",
   100  			paths:       []string{"."},
   101  			ignore:      []string{"b*"},
   102  			expected:    []string{"foo"},
   103  		},
   104  		{
   105  			description: "glob",
   106  			paths:       []string{"**"},
   107  			expected:    []string{"bar", filepath.FromSlash("baz/file"), "foo"},
   108  		},
   109  		{
   110  			description: "error",
   111  			paths:       []string{"unknown"},
   112  			shouldErr:   true,
   113  		},
   114  	}
   115  	for _, test := range tests {
   116  		testutil.Run(t, test.description, func(t *testutil.T) {
   117  			// Directory structure:
   118  			//   foo
   119  			//   bar
   120  			// - baz
   121  			//     file
   122  			tmpDir := t.NewTempDir().
   123  				Touch("foo", "bar", "baz/file")
   124  
   125  			deps, err := GetDependencies(context.Background(), tmpDir.Root(), "test", &latest.CustomArtifact{
   126  				Dependencies: &latest.CustomDependencies{
   127  					Paths:  test.paths,
   128  					Ignore: test.ignore,
   129  				},
   130  			}, nil)
   131  
   132  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deps)
   133  		})
   134  	}
   135  }