| 1 | |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "flag" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "syscall" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | . "github.com/onsi/ginkgo" |
| 16 | . "github.com/onsi/gomega" |
| 17 | "github.com/sirupsen/logrus" |
| 18 | ) |
| 19 | |
| 20 | const ( |
| 21 | WM_QUIT = 0x12 |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | tmpDir string |
| 26 | binDir string |
| 27 | keyFile string |
| 28 | winSshProxy string |
| 29 | tidFile string |
| 30 | ) |
| 31 | |
| 32 | func TestSuite(t *testing.T) { |
| 33 | RegisterFailHandler(Fail) |
| 34 | RunSpecs(t, "win-sshproxy suite") |
| 35 | } |
| 36 | |
| 37 | func init() { |
| 38 | flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory") |
| 39 | flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries") |
| 40 | _ = os.MkdirAll(tmpDir, 0755) |
| 41 | keyFile = filepath.Join(tmpDir, "id.key") |
| 42 | _ = os.WriteFile(keyFile, []byte(fakeHostKey), 0600) |
| 43 | winSshProxy = filepath.Join(binDir, "win-sshproxy.exe") |
| 44 | tidFile = filepath.Join(tmpDir, "win-sshproxy.tid") |
| 45 | } |
| 46 | |
| 47 | var _ = BeforeSuite(func() { |
| 48 | startMockServer() |
| 49 | }) |
| 50 | |
| 51 | var _ = AfterSuite(func() { |
| 52 | stopMockServer() |
| 53 | }) |
| 54 | |
| 55 | func startProxy() error { |
| 56 | os.Remove(tidFile) |
| 57 | cmd := exec.Command(winSshProxy, "-debug", "test", tmpDir, "npipe:////./pipe/fake_docker_engine", "ssh://localhost:2134/run/podman/podman.sock", keyFile) |
| 58 | return cmd.Start() |
| 59 | } |
| 60 | |
| 61 | func readTid() (uint32, uint32, error) { |
| 62 | contents, err := os.ReadFile(tidFile) |
| 63 | if err != nil { |
| 64 | return 0, 0, err |
| 65 | } |
| 66 | |
| 67 | var pid, tid uint32 |
| 68 | _, err = fmt.Sscanf(string(contents), "%d:%d", &pid, &tid) |
| 69 | if err != nil { |
| 70 | return 0, 0, err |
| 71 | } |
| 72 | return pid, tid, nil |
| 73 | } |
| 74 | |
| 75 | func sendQuit(tid uint32) { |
| 76 | user32 := syscall.NewLazyDLL("user32.dll") |
| 77 | postMessage := user32.NewProc("PostThreadMessageW") |
| 78 | _, _, err := postMessage.Call(uintptr(tid), WM_QUIT, 0, 0) |
| 79 | if err != nil { |
| 80 | logrus.Errorf("Error posting quit message: %v", err) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func stopProxy(noKill bool) error { |
| 85 | pid, tid, err := readTid() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | proc, err := os.FindProcess(int(pid)) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | sendQuit(tid) |
| 95 | state := waitTimeout(proc, 20*time.Second) |
| 96 | if state == nil || !state.Exited() { |
| 97 | if noKill { |
| 98 | return fmt.Errorf("proxy did not exit on request") |
| 99 | } |
| 100 | _ = proc.Kill() |
| 101 | state = waitTimeout(proc, 20*time.Second) |
| 102 | } |
| 103 | |
| 104 | if state == nil || !state.Exited() { |
| 105 | return fmt.Errorf("Stop proxy failed: %d", pid) |
| 106 | } |
| 107 | |
| 108 | _ = os.Remove(tidFile) |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | func waitTimeout(proc *os.Process, timeout time.Duration) *os.ProcessState { |
| 113 | return doTimeout(func(complete chan *os.ProcessState) { |
| 114 | state, _ := proc.Wait() |
| 115 | complete <- state |
| 116 | }, timeout) |
| 117 | } |
| 118 | |
| 119 | func doTimeout(action func(complete chan *os.ProcessState), timeout time.Duration) *os.ProcessState { |
| 120 | complete := make(chan *os.ProcessState) |
| 121 | |
| 122 | go action(complete) |
| 123 | select { |
| 124 | case <-time.After(timeout): |
| 125 | return nil |
| 126 | |
| 127 | case state := <-complete: |
| 128 | return state |
| 129 | } |
| 130 | } |