github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/port_forward_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 integration 18 19 import ( 20 "testing" 21 22 "github.com/GoogleContainerTools/skaffold/v2/integration/skaffold" 23 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/config" 24 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/constants" 25 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubectl" 26 kubectx "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/context" 27 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/portforward" 28 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log" 29 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner/runcontext" 30 ) 31 32 func TestPortForward(t *testing.T) { 33 tests := []struct { 34 dir string 35 }{ 36 {dir: "examples/microservices"}, 37 {dir: "examples/multi-config-microservices"}, 38 } 39 for _, test := range tests { 40 MarkIntegrationTest(t, CanRunWithoutGcp) 41 ns, _ := SetupNamespace(t) 42 43 skaffold.Run().InDir(test.dir).InNs(ns.Name).RunOrFail(t) 44 45 cfg, err := kubectx.CurrentConfig() 46 failNowIfError(t, err) 47 48 kubectlCLI := kubectl.NewCLI(&runcontext.RunContext{ 49 KubeContext: cfg.CurrentContext, 50 Opts: config.SkaffoldOptions{ 51 Namespace: ns.Name, 52 }, 53 }, "") 54 55 portforward.SimulateDevCycle(t, kubectlCLI, ns.Name, log.TraceLevel) 56 } 57 } 58 59 func TestRunPortForward(t *testing.T) { 60 MarkIntegrationTest(t, CanRunWithoutGcp) 61 tests := []struct { 62 dir string 63 }{ 64 {dir: "examples/microservices"}, 65 {dir: "examples/multi-config-microservices"}, 66 } 67 for _, test := range tests { 68 ns, _ := SetupNamespace(t) 69 70 rpcAddr := randomPort() 71 skaffold.Run("--port-forward", "--rpc-port", rpcAddr).InDir(test.dir).InNs(ns.Name).RunBackground(t) 72 73 _, entries := apiEvents(t, rpcAddr) 74 75 address, localPort := getLocalPortFromPortForwardEvent(t, entries, "leeroy-app", "service", ns.Name) 76 assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse) 77 } 78 } 79 80 func TestRunUserPortForwardResource(t *testing.T) { 81 MarkIntegrationTest(t, CanRunWithoutGcp) 82 tests := []struct { 83 dir string 84 }{ 85 {dir: "examples/microservices"}, 86 {dir: "examples/multi-config-microservices"}, 87 } 88 for _, test := range tests { 89 ns, _ := SetupNamespace(t) 90 91 rpcAddr := randomPort() 92 skaffold.Run("--port-forward", "--rpc-port", rpcAddr).InDir(test.dir).InNs(ns.Name).RunBackground(t) 93 94 _, entries := apiEvents(t, rpcAddr) 95 96 address, localPort := getLocalPortFromPortForwardEvent(t, entries, "leeroy-web", "deployment", ns.Name) 97 assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse) 98 } 99 } 100 101 func TestRunPortForwardByPortName(t *testing.T) { 102 MarkIntegrationTest(t, CanRunWithoutGcp) 103 tests := []struct { 104 dir string 105 }{ 106 {dir: "examples/microservices"}, 107 {dir: "examples/multi-config-microservices"}, 108 } 109 for _, test := range tests { 110 ns, _ := SetupNamespace(t) 111 112 rpcAddr := randomPort() 113 skaffold.Run("--port-forward", "--rpc-port", rpcAddr).InDir(test.dir).InNs(ns.Name).RunBackground(t) 114 115 _, entries := apiEvents(t, rpcAddr) 116 117 address1, localPort1 := getLocalPortFromPortForwardEvent(t, entries, "leeroy-app", "deployment", ns.Name) 118 assertResponseFromPort(t, address1, localPort1, constants.LeeroyAppResponse) 119 } 120 } 121 122 // TestDevPortForwardDeletePod tests that port forwarding works 123 // as expected. Then, the test force deletes a pod, 124 // and tests that the pod eventually comes up at the same port again. 125 func TestDevPortForwardDeletePod(t *testing.T) { 126 MarkIntegrationTest(t, CanRunWithoutGcp) 127 tests := []struct { 128 dir string 129 }{ 130 {dir: "examples/microservices"}, 131 {dir: "examples/multi-config-microservices"}, 132 } 133 for _, test := range tests { 134 // pre-build images to avoid tripping the 1-minute timeout in getLocalPortFromPortForwardEvent() 135 skaffold.Build().InDir(test.dir).RunOrFail(t) 136 137 ns, client := SetupNamespace(t) 138 139 rpcAddr := randomPort() 140 skaffold.Dev("--port-forward", "--rpc-port", rpcAddr).InDir(test.dir).InNs(ns.Name).RunBackground(t) 141 client.WaitForDeploymentsToStabilize("leeroy-app") 142 143 _, entries := apiEvents(t, rpcAddr) 144 145 address, localPort := getLocalPortFromPortForwardEvent(t, entries, "leeroy-app", "service", ns.Name) 146 assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse) 147 148 // now, delete all pods in this namespace. 149 Run(t, ".", "kubectl", "delete", "pods", "--all", "-n", ns.Name) 150 151 assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse) 152 } 153 }