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

     1  /*
     2  Copyright 2021 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 graph
    18  
    19  import (
    20  	"context"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp/cmpopts"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestSourceDependenciesCache(t *testing.T) {
    32  	testutil.Run(t, "TestTransitiveSourceDependenciesCache", func(t *testutil.T) {
    33  		g := map[string]*latest.Artifact{
    34  			"img1": {ImageName: "img1", Dependencies: []*latest.ArtifactDependency{{ImageName: "img2"}}},
    35  			"img2": {ImageName: "img2", Dependencies: []*latest.ArtifactDependency{{ImageName: "img3"}, {ImageName: "img4"}}},
    36  			"img3": {ImageName: "img3", Dependencies: []*latest.ArtifactDependency{{ImageName: "img4"}}},
    37  			"img4": {ImageName: "img4"},
    38  		}
    39  		deps := map[string][]string{
    40  			"img1": {"file11", "file12"},
    41  			"img2": {"file21", "file22"},
    42  			"img3": {"file31", "file32"},
    43  			"img4": {"file41", "file42"},
    44  		}
    45  		counts := map[string]int{"img1": 0, "img2": 0, "img3": 0, "img4": 0}
    46  		t.Override(&getDependenciesFunc, func(_ context.Context, a *latest.Artifact, _ docker.Config, _ docker.ArtifactResolver) ([]string, error) {
    47  			counts[a.ImageName]++
    48  			return deps[a.ImageName], nil
    49  		})
    50  
    51  		r := NewSourceDependenciesCache(nil, nil, g)
    52  		d, err := r.TransitiveArtifactDependencies(context.Background(), g["img1"])
    53  		t.CheckNoError(err)
    54  		expectedDeps := []string{"file11", "file12", "file21", "file22", "file31", "file32", "file41", "file42", "file41", "file42"}
    55  		t.CheckDeepEqual(expectedDeps, d)
    56  		for _, v := range counts {
    57  			t.CheckDeepEqual(v, 1)
    58  		}
    59  	})
    60  }
    61  
    62  func TestSourceDependenciesForArtifact(t *testing.T) {
    63  	tmpDir := testutil.NewTempDir(t).Touch(
    64  		"foo.java",
    65  		"bar.go",
    66  		"dir1/baz.java",
    67  		"dir2/frob.go",
    68  	)
    69  	tests := []struct {
    70  		description            string
    71  		artifact               *latest.Artifact
    72  		dockerConfig           docker.Config
    73  		dockerArtifactResolver docker.ArtifactResolver
    74  		expectedPaths          []string
    75  	}{
    76  		{
    77  			description: "ko default dependencies",
    78  			artifact: &latest.Artifact{
    79  				ArtifactType: latest.ArtifactType{
    80  					KoArtifact: &latest.KoArtifact{},
    81  				},
    82  				Workspace: tmpDir.Root(),
    83  			},
    84  			expectedPaths: []string{
    85  				filepath.Join(tmpDir.Root(), "dir2/frob.go"),
    86  				filepath.Join(tmpDir.Root(), "bar.go"),
    87  			},
    88  		},
    89  	}
    90  	for _, test := range tests {
    91  		testutil.Run(t, test.description, func(t *testutil.T) {
    92  			paths, err := sourceDependenciesForArtifact(context.Background(), test.artifact, test.dockerConfig, test.dockerArtifactResolver)
    93  			t.CheckNoError(err)
    94  			t.CheckDeepEqual(test.expectedPaths, paths,
    95  				cmpopts.SortSlices(func(x, y string) bool { return x < y }))
    96  		})
    97  	}
    98  }