github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/docker/debugger/debug_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 debugger
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/docker/docker/api/types/container"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug/types"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    27  	"github.com/GoogleContainerTools/skaffold/testutil"
    28  )
    29  
    30  func TestZeroValue(t *testing.T) {
    31  	t.Run("nil DebugManager should not raise nil pointer dereference", func(t *testing.T) {
    32  		var d *DebugManager
    33  		d.Start(context.TODO())
    34  		d.Stop()
    35  		d.SupportMounts()
    36  		d.TransformImage(context.TODO(), graph.Artifact{}, nil)
    37  	})
    38  }
    39  
    40  func TestConfigurationsAndImages(t *testing.T) {
    41  	tests := []struct {
    42  		name      string
    43  		artifacts []graph.Artifact
    44  	}{
    45  		{
    46  			name:      "no artifacts doesn't error",
    47  			artifacts: []graph.Artifact{},
    48  		},
    49  		{
    50  			name: "one artifact, one configuration",
    51  			artifacts: []graph.Artifact{
    52  				{ImageName: "foo", Tag: "foo:bar"},
    53  			},
    54  		},
    55  		{
    56  			name: "two artifacts, two configurations",
    57  			artifacts: []graph.Artifact{
    58  				{ImageName: "foo", Tag: "foo:bar"},
    59  				{ImageName: "another", Tag: "another:image"},
    60  			},
    61  		},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		testutil.Run(t, test.name, func(t *testutil.T) {
    66  			t.Override(&TransformImage, func(_ context.Context, a graph.Artifact, _ *container.Config, _ map[string]bool, _ string) (map[string]types.ContainerDebugConfiguration, []*container.Config, error) {
    67  				m := make(map[string]types.ContainerDebugConfiguration)
    68  				m[a.ImageName] = types.ContainerDebugConfiguration{
    69  					Artifact: a.ImageName,
    70  				}
    71  				return m, nil, nil
    72  			})
    73  
    74  			m := NewDebugManager(nil, "")
    75  
    76  			for _, a := range test.artifacts {
    77  				m.TransformImage(context.TODO(), a, &container.Config{Image: a.ImageName})
    78  			}
    79  
    80  			for _, a := range test.artifacts {
    81  				if !findArtifactInImageList(m.images, a) {
    82  					t.Errorf("unable to find artifact %+v in image list: %v", a, m.images)
    83  				}
    84  
    85  				if !validateConfiguration(m.configurations, a) {
    86  					t.Errorf("unable to find configuration for artifact %+v in map: %+v", a, m.configurations)
    87  				}
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func findArtifactInImageList(list []string, target graph.Artifact) bool {
    94  	var found bool
    95  	for _, item := range list {
    96  		if item == target.ImageName {
    97  			found = true
    98  		}
    99  	}
   100  	return found
   101  }
   102  
   103  func validateConfiguration(config map[string]types.ContainerDebugConfiguration, a graph.Artifact) bool {
   104  	var validated bool
   105  	for i, c := range config {
   106  		if i == a.ImageName {
   107  			validated = c.Artifact == a.ImageName
   108  		}
   109  	}
   110  	return validated
   111  }