dragon188199 2019-09-03 11:53
浏览 58
已采纳

在Go中模拟Hashicorp保险库

Is there an easy way to mock Hashicorp vault in go tests ?

I created a service in Go that accesses Vault, and would like to create proper testing for it.

I didn't find a simple solution I like (like moto in python). I also tried using a vault in dev mode in docker (take the system test route) but I have trouble writing to it via API. Ideas ?

  • 写回答

2条回答 默认 最新

  • douxiong1994 2019-09-03 14:27
    关注

    Is there an easy way to mock HashiCorp Vault in Go tests?

    Don't. Use the real thing! HashiCorp helpfully provides utility functions for starting a server on the fly1. This makes your tests much more useful, and can often serve as an actionable guide for developers on how to setup local development servers too.

    Here is a very basic example. The testing framework is very flexible (which also makes them fairly complicated). Refer to the package documentations for more options, including running multiple servers in HA mode. I found Vault's own test cases very useful when setting up more complicated scenarios.

    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 They provide test servers for all of their projects, not just Vault. Mitchell Hashimoto explained the rational in his talk on advanced testing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。