github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/deploy/docker/port_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 docker
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/docker/docker/api/types/container"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    26  	schemautil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestAllocatePorts(t *testing.T) {
    32  	tests := []struct {
    33  		name      string
    34  		resources map[int]*latest.PortForwardResource // we map local port to resources for ease of testing
    35  	}{
    36  		{
    37  			name: "one port, one resource",
    38  			resources: map[int]*latest.PortForwardResource{
    39  				9000: {
    40  					Type: "container",
    41  					Port: schemautil.IntOrString{
    42  						IntVal: 9000,
    43  						StrVal: "9000",
    44  					},
    45  					Address:   "127.0.0.1",
    46  					LocalPort: 9000,
    47  				},
    48  			},
    49  		},
    50  		{
    51  			name: "two ports, two resources",
    52  			resources: map[int]*latest.PortForwardResource{
    53  				1234: {
    54  					Type: "container",
    55  					Port: schemautil.IntOrString{
    56  						IntVal: 20,
    57  						StrVal: "20",
    58  					},
    59  					Address:   "192.168.999.999",
    60  					LocalPort: 1234,
    61  				},
    62  				4321: {
    63  					Type: "container",
    64  					Port: schemautil.IntOrString{
    65  						IntVal: 8080,
    66  						StrVal: "8080",
    67  					},
    68  					Address:   "localhost",
    69  					LocalPort: 4321,
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		testutil.Run(t, test.name, func(t *testutil.T) {
    77  			t.Override(&GetAvailablePort, func(_ string, port int, _ *util.PortSet) int {
    78  				return port
    79  			})
    80  			pm := NewPortManager()
    81  			cfg := container.Config{}
    82  			m, err := pm.allocatePorts(test.name, collectResources(test.resources), &cfg, nil)
    83  			for containerPort := range cfg.ExposedPorts { // the image config's PortSet contains the local ports, so we grab the bindings keyed off these
    84  				bindings := m[containerPort]
    85  				t.CheckDeepEqual(len(bindings), 1) // we always have a 1-1 mapping of resource to binding
    86  				t.CheckError(false, err)           // shouldn't error, unless GetAvailablePort is broken
    87  				resource := resourceFromContainerPort(test.resources, containerPort.Int())
    88  				t.CheckDeepEqual(bindings[0].HostIP, resource.Address)
    89  				t.CheckDeepEqual(bindings[0].HostPort, strconv.Itoa(resource.LocalPort))
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func collectResources(resourceMap map[int]*latest.PortForwardResource) []*latest.PortForwardResource {
    96  	var resources []*latest.PortForwardResource
    97  	for _, r := range resourceMap {
    98  		resources = append(resources, r)
    99  	}
   100  	return resources
   101  }
   102  
   103  func resourceFromContainerPort(resourceMap map[int]*latest.PortForwardResource, containerPort int) *latest.PortForwardResource {
   104  	for _, resource := range resourceMap {
   105  		if resource.Port.IntVal == containerPort {
   106  			return resource
   107  		}
   108  	}
   109  	return nil
   110  }