github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/portforward/port_forward_integration.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 portforward
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"k8s.io/apimachinery/pkg/util/wait"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    30  	schemautil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    32  )
    33  
    34  // SimulateDevCycle is used for testing a port forward + stop + restart in a simulated dev cycle
    35  func SimulateDevCycle(t *testing.T, kubectlCLI *kubectl.CLI, namespace string, level log.Level) {
    36  	log.SetLevel(level)
    37  	em := NewEntryManager(NewKubectlForwarder(kubectlCLI))
    38  	portForwardEventHandler := portForwardEvent
    39  	defer func() { portForwardEvent = portForwardEventHandler }()
    40  	portForwardEvent = func(entry *portForwardEntry) {}
    41  	ctx := context.Background()
    42  	localPort := retrieveAvailablePort(util.Loopback, 9000, &em.forwardedPorts)
    43  	pfe := newPortForwardEntry(0, latest.PortForwardResource{
    44  		Type:      "deployment",
    45  		Name:      "leeroy-web",
    46  		Namespace: namespace,
    47  		Port:      schemautil.FromInt(8080),
    48  	}, "", "dummy container", "", "", localPort, false)
    49  	defer em.Stop()
    50  	em.forwardPortForwardEntry(ctx, os.Stdout, pfe)
    51  	em.Stop()
    52  
    53  	log.Entry(ctx).Info("waiting for the same port to become available...")
    54  	if err := wait.Poll(100*time.Millisecond, 5*time.Second, func() (done bool, err error) {
    55  		nextPort := retrieveAvailablePort(util.Loopback, localPort, &em.forwardedPorts)
    56  
    57  		log.Entry(ctx).Infof("next port %d", nextPort)
    58  
    59  		// theoretically we should be able to bind to the very same port
    60  		// this might get flaky when multiple tests are ran. However
    61  		// we shouldn't collide with our own process because of poor cleanup
    62  		return nextPort == localPort, nil
    63  	}); err != nil {
    64  		t.Fatalf("port is not released after portforwarding stopped: %d", localPort)
    65  	}
    66  
    67  	em.forwardPortForwardEntry(ctx, os.Stdout, pfe)
    68  }