github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/pwlib/vault_docker_test.go (about) 1 package pwlib 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "time" 8 9 "github.com/tommi2day/gomodules/test" 10 11 "github.com/tommi2day/gomodules/common" 12 13 "github.com/ory/dockertest/v3" 14 "github.com/ory/dockertest/v3/docker" 15 ) 16 17 const repo = "docker.io/hashicorp/vault" 18 const repoTag = "1.15.4" 19 const containerTimeout = 120 20 const rootToken = "pwlib-test" 21 22 var containerName string 23 24 // prepareVaultContainer create an Oracle Docker Container 25 func prepareVaultContainer() (container *dockertest.Resource, err error) { 26 if os.Getenv("SKIP_VAULT") != "" { 27 err = fmt.Errorf("skipping Vault Container in CI environment") 28 return 29 } 30 containerName = os.Getenv("CONTAINER_NAME") 31 if containerName == "" { 32 containerName = "pwlib-vault" 33 } 34 pool, err := common.GetDockerPool() 35 if err != nil { 36 err = fmt.Errorf("cannot attach to docker: %v", err) 37 return 38 } 39 40 vendorImagePrefix := os.Getenv("VENDOR_IMAGE_PREFIX") 41 repoString := vendorImagePrefix + repo 42 43 fmt.Printf("Try to start docker container for %s:%s\n", repoString, repoTag) 44 container, err = pool.RunWithOptions(&dockertest.RunOptions{ 45 Repository: repoString, 46 Tag: repoTag, 47 Env: []string{ 48 "VAULT_DEV_ROOT_TOKEN_ID=" + rootToken, 49 "VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200", 50 }, 51 Hostname: containerName, 52 Name: containerName, 53 CapAdd: []string{"IPC_LOCK"}, 54 Cmd: []string{}, 55 // ExposedPorts: []string{"8200"}, 56 /* 57 PortBindings: map[docker.Port][]docker.PortBinding{ 58 "8200": { 59 {HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", port)}, 60 }, 61 }, 62 */ 63 Mounts: []string{ 64 test.TestDir + "/docker/vault_provision:/vault_provision/", 65 }, 66 }, func(config *docker.HostConfig) { 67 // set AutoRemove to true so that stopped container goes away by itself 68 config.AutoRemove = true 69 config.RestartPolicy = docker.RestartPolicy{Name: "no"} 70 }) 71 72 if err != nil { 73 err = fmt.Errorf("error starting vault docker container: %v", err) 74 return 75 } 76 77 pool.MaxWait = containerTimeout * time.Second 78 host, port := common.GetContainerHostAndPort(container, "8200/tcp") 79 address := fmt.Sprintf("http://%s:%d", host, port) 80 fmt.Printf("Wait to successfully connect to Vault with %s (max %ds)...\n", address, containerTimeout) 81 start := time.Now() 82 if err = pool.Retry(func() error { 83 var resp *http.Response 84 //nolint gosec 85 resp, err = http.Get(address) 86 if err != nil { 87 return err 88 } 89 if resp.StatusCode != http.StatusOK { 90 return fmt.Errorf("status code not OK:%s", resp.Status) 91 } 92 return nil 93 }); err != nil { 94 fmt.Printf("Could not connect to Vault Container: %s", err) 95 return 96 } 97 98 // wait 5s to init container 99 time.Sleep(5 * time.Second) 100 elapsed := time.Since(start) 101 fmt.Printf("vault Container is available after %s\n", elapsed.Round(time.Millisecond)) 102 103 // provision 104 cmdout := "" 105 cmd := []string{"/vault_provision/vault_init.sh"} 106 cmdout, _, err = common.ExecDockerCmd(container, cmd) 107 if err != nil { 108 fmt.Printf("Exec Error %s", err) 109 } else { 110 fmt.Printf("Cmd:%v\n %s", cmd, cmdout) 111 } 112 err = nil 113 return 114 }