在 Go 中模拟 Hashicorp 保险库
Mocking Hashicorp vault in Go
有没有简单的方法在 go 测试中模拟 Hashicorp 保险库?
我在 Go 中创建了一个访问 Vault 的服务,并想为它创建适当的测试。
我没有找到我喜欢的简单解决方案(比如 python 中的 moto)。
我还尝试在 docker 中以开发模式使用保管库(采用系统测试路线),但我无法通过 API 写入它。
想法?
因为 Go 是一种静态类型的语言,我们必须在编译时知道事物的类型。所以要进行这种类型的独立测试,我们需要在我们的代码中创建边界,以便在测试方面具有逻辑意义。
在 Go 中创建接口来定义这些边界,所以从这个意义上说,你想在你和这个第三方库之间创建一个接口,这样你就可以用 functions/methods/types 替换库的输入和输出您可以控制并定义条件的那些,以便您可以测试您的代码对第三方输出或条件的行为方式。
请参阅下面的接口示例。
https://gobyexample.com/interfaces
如果您分享一些示例代码,也许我们可以更具体一些,并为您提供一些代码示例。
Is there an easy way to mock HashiCorp Vault in Go tests?
不要。使用真实的东西! HashiCorp 提供有用的实用程序函数来动态启动服务器1。这使您的测试更有用,并且通常可以作为开发人员如何设置本地开发服务器的可操作指南。
这是一个非常基本的例子。测试框架非常灵活(这也使其相当复杂)。有关更多选项,请参阅包文档,包括 运行 HA 模式下的多个服务器。我发现 Vault 自己的测试用例在设置更复杂的场景时非常有用。
package main
import (
"net"
"testing"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
)
func TestVaultStuff(t *testing.T) {
ln, client := createTestVault(t)
defer ln.Close()
// Pass the client to the code under test.
myFunction(client)
}
func createTestVault(t *testing.T) (net.Listener, *api.Client) {
t.Helper()
// Create an in-memory, unsealed core (the "backend", if you will).
core, keyShares, rootToken := vault.TestCoreUnsealed(t)
_ = keyShares
// Start an HTTP server for the core.
ln, addr := http.TestServer(t, core)
// Create a client that talks to the server, initially authenticating with
// the root token.
conf := api.DefaultConfig()
conf.Address = addr
client, err := api.NewClient(conf)
if err != nil {
t.Fatal(err)
}
client.SetToken(rootToken)
// Setup required secrets, policies, etc.
_, err = client.Logical().Write("secret/foo", map[string]interface{}{
"secret": "bar",
})
if err != nil {
t.Fatal(err)
}
return ln, client
}
1 他们为所有项目提供测试服务器,而不仅仅是 Vault。 Mitchell Hashimoto 在 his talk on advanced testing.
中解释了理性
需要注意一件非常重要的事情:
TestCoreUnsealed
不使用常规 KV 后端,而是使用 PassthroughBackend
本质上模拟 kv 行为。
关注这个问题,我已经调试了 4 天
有没有简单的方法在 go 测试中模拟 Hashicorp 保险库?
我在 Go 中创建了一个访问 Vault 的服务,并想为它创建适当的测试。
我没有找到我喜欢的简单解决方案(比如 python 中的 moto)。 我还尝试在 docker 中以开发模式使用保管库(采用系统测试路线),但我无法通过 API 写入它。 想法?
因为 Go 是一种静态类型的语言,我们必须在编译时知道事物的类型。所以要进行这种类型的独立测试,我们需要在我们的代码中创建边界,以便在测试方面具有逻辑意义。
在 Go 中创建接口来定义这些边界,所以从这个意义上说,你想在你和这个第三方库之间创建一个接口,这样你就可以用 functions/methods/types 替换库的输入和输出您可以控制并定义条件的那些,以便您可以测试您的代码对第三方输出或条件的行为方式。
请参阅下面的接口示例。
https://gobyexample.com/interfaces
如果您分享一些示例代码,也许我们可以更具体一些,并为您提供一些代码示例。
Is there an easy way to mock HashiCorp Vault in Go tests?
不要。使用真实的东西! HashiCorp 提供有用的实用程序函数来动态启动服务器1。这使您的测试更有用,并且通常可以作为开发人员如何设置本地开发服务器的可操作指南。
这是一个非常基本的例子。测试框架非常灵活(这也使其相当复杂)。有关更多选项,请参阅包文档,包括 运行 HA 模式下的多个服务器。我发现 Vault 自己的测试用例在设置更复杂的场景时非常有用。
package main
import (
"net"
"testing"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
)
func TestVaultStuff(t *testing.T) {
ln, client := createTestVault(t)
defer ln.Close()
// Pass the client to the code under test.
myFunction(client)
}
func createTestVault(t *testing.T) (net.Listener, *api.Client) {
t.Helper()
// Create an in-memory, unsealed core (the "backend", if you will).
core, keyShares, rootToken := vault.TestCoreUnsealed(t)
_ = keyShares
// Start an HTTP server for the core.
ln, addr := http.TestServer(t, core)
// Create a client that talks to the server, initially authenticating with
// the root token.
conf := api.DefaultConfig()
conf.Address = addr
client, err := api.NewClient(conf)
if err != nil {
t.Fatal(err)
}
client.SetToken(rootToken)
// Setup required secrets, policies, etc.
_, err = client.Logical().Write("secret/foo", map[string]interface{}{
"secret": "bar",
})
if err != nil {
t.Fatal(err)
}
return ln, client
}
1 他们为所有项目提供测试服务器,而不仅仅是 Vault。 Mitchell Hashimoto 在 his talk on advanced testing.
中解释了理性需要注意一件非常重要的事情:
TestCoreUnsealed
不使用常规 KV 后端,而是使用 PassthroughBackend
本质上模拟 kv 行为。
关注这个问题,我已经调试了 4 天