github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/cmd/legacy_command_test.go (about) 1 package cmd 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func Test_legacyCommands(t *testing.T) { 11 type testcase struct { 12 name string 13 inputLegacyCommand string 14 outputTP2Command string 15 msg string 16 } 17 testCases := []testcase{ 18 { 19 name: "swapDeploymentBasic", 20 inputLegacyCommand: "telepresence --swap-deployment myserver --expose 9090 --run python3 -m http.server 9090", 21 outputTP2Command: "intercept myserver --port 9090 -- python3 -m http.server 9090", 22 }, 23 { 24 name: "swapDeploymentMethod", 25 inputLegacyCommand: "telepresence --swap-deployment myserver --method inject-tcp --expose 9090 --run python3 -m http.server 9090", 26 outputTP2Command: "intercept myserver --port 9090 -- python3 -m http.server 9090", 27 msg: "Telepresence doesn't have proxying methods. You can use --docker-run for container, otherwise it works similarly to vpn-tcp\n", 28 }, 29 { 30 name: "swapDeploymentUnsupportedParam", 31 inputLegacyCommand: "telepresence --swap-deployment myserver --expose 9090 --not-real-param --run python3 -m http.server 9090", 32 outputTP2Command: "intercept myserver --port 9090 -- python3 -m http.server 9090", 33 msg: "The following flags used don't have a direct translation to Telepresence: --not-real-param\n", 34 }, 35 { 36 // This name isn't the greatest but basically, if we have an unsupported 37 // parameter in the process, that's fine because telepresence doesn't 38 // care about parameters associated with a process a user is running. 39 name: "swapDeploymentUnsupportedParamInProcess", 40 inputLegacyCommand: "telepresence --swap-deployment myserver --expose 9090 --run python3 -m http.server 9090 --not-real-param", 41 outputTP2Command: "intercept myserver --port 9090 -- python3 -m http.server 9090 --not-real-param", 42 }, 43 { 44 name: "swapDeploymentMappedPort", 45 inputLegacyCommand: "telepresence --swap-deployment myserver --expose 9090:80 --run python3 -m http.server 9090", 46 outputTP2Command: "intercept myserver --port 9090:80 -- python3 -m http.server 9090", 47 }, 48 { 49 name: "swapDeploymentRunShell", 50 inputLegacyCommand: "telepresence --swap-deployment myserver --run-shell", 51 outputTP2Command: "intercept myserver -- bash", 52 }, 53 { 54 name: "swapDeploymentBasicDockerRun", 55 inputLegacyCommand: "telepresence --swap-deployment myserver --expose 80 --docker-run -i -t nginx:latest", 56 outputTP2Command: "intercept myserver --port 80 --docker-run -- -i -t nginx:latest", 57 }, 58 { 59 name: "swapDeploymentGlobalFlag", 60 inputLegacyCommand: "telepresence --as system:serviceaccount:default:telepresence-test-developer --swap-deployment myserver --expose 9090 --run python3 -m http.server 9090", 61 outputTP2Command: "intercept myserver --port 9090 --as system:serviceaccount:default:telepresence-test-developer -- python3 -m http.server 9090", 62 }, 63 { 64 name: "runCommand", 65 inputLegacyCommand: "telepresence --run curl http://myservice:8080/", 66 outputTP2Command: "connect -- curl http://myservice:8080/", 67 }, 68 { 69 name: "runShell", 70 inputLegacyCommand: "telepresence --run-shell", 71 outputTP2Command: "connect -- bash", 72 }, 73 { 74 name: "runShellNewDeployment", 75 inputLegacyCommand: "telepresence --new-deployment myserver --run-shell", 76 outputTP2Command: "connect -- bash", 77 msg: "This flag is ignored since Telepresence uses one traffic-manager deployed in the ambassador namespace.\n", 78 }, 79 { 80 name: "runShellIgnoreExtraArgs", 81 inputLegacyCommand: "telepresence --expose 8080 --run-shell", 82 outputTP2Command: "connect -- bash", 83 }, 84 } 85 86 for _, tc := range testCases { 87 tcName := tc.name 88 tc := tc 89 t.Run(tcName, func(t *testing.T) { 90 inputArgs := strings.Split(tc.inputLegacyCommand, " ") 91 genTPCmd, msg, _, err := translateLegacy(inputArgs) 92 if err != nil { 93 t.Fatal(err) 94 } 95 assert.Equal(t, tc.msg, msg) 96 assert.Equal(t, tc.outputTP2Command, genTPCmd) 97 }) 98 } 99 }