栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Go中模拟Hashicorp保险库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Go中模拟Hashicorp保险库

有没有一种简单的方法可以在Go测试中模拟HashiCorp Vault?

别。使用真实的东西!HashiCorp有助于提供实用程序功能,以快速启动服务器1。这使您的测试更加有用,并且经常可以作为开发人员如何设置本地开发服务器的可行指南。

这是一个非常基本的例子。测试框架非常灵活(这也使其相当复杂)。有关更多选项,请参阅软件包文档,包括在HA模式下运行多个服务器。在设置更复杂的场景时,我发现Vault自己的测试用例非常有用。

package mainimport (    "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 pre 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)在有关高级测试的演讲中解释了其合理性。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413168.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号