From f9b5ed8f67e9d9f4684b10d995d279151830c66c Mon Sep 17 00:00:00 2001 From: Bryce Lampe Date: Wed, 27 Mar 2024 13:53:41 -0700 Subject: [PATCH] Use in-memory SSH agent --- examples/main_test.go | 45 +++++++++++++++++++++++++++++++++++ provider/internal/ssh_test.go | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 examples/main_test.go diff --git a/examples/main_test.go b/examples/main_test.go new file mode 100644 index 0000000..981e7ae --- /dev/null +++ b/examples/main_test.go @@ -0,0 +1,45 @@ +//go:build nodejs || python || dotnet || java || all +// +build nodejs python dotnet java all + +package examples + +import ( + "net" + "os" + "path/filepath" + "testing" + + "golang.org/x/crypto/ssh/agent" +) + +func TestMain(m *testing.M) { + if os.Getenv("SSH_AUTH_SOCK") == "" { + sock := sshagent() + os.Setenv("SSH_AUTH_SOCK", sock) + } + + os.Exit(m.Run()) +} + +func sshagent() string { + dir := os.TempDir() + sock := filepath.Join(dir, "test.sock") + + _ = os.Remove(sock) + + l, err := net.Listen("unix", sock) + if err != nil { + panic(err) + } + + go func() { + conn, err := l.Accept() + if err != nil { + panic(err) + } + a := agent.NewKeyring() + agent.ServeAgent(a, conn) + }() + + return sock +} diff --git a/provider/internal/ssh_test.go b/provider/internal/ssh_test.go index 224591d..a93ec60 100644 --- a/provider/internal/ssh_test.go +++ b/provider/internal/ssh_test.go @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS,