try to read license from env and mapped to container (#12854)

This commit is contained in:
Dhia Ayachi 2022-04-25 11:58:29 -04:00 committed by GitHub
parent fe22a002e1
commit e3eab78fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

View File

@ -31,6 +31,10 @@ type consulContainerNode struct {
// NewConsulContainer starts a Consul node in a container with the given config. // NewConsulContainer starts a Consul node in a container with the given config.
func NewConsulContainer(ctx context.Context, config Config) (Node, error) { func NewConsulContainer(ctx context.Context, config Config) (Node, error) {
license, err := readLicense()
if err != nil {
return nil, err
}
name := utils.RandName("consul-") name := utils.RandName("consul-")
tmpDir, err := ioutils.TempDir("", name) tmpDir, err := ioutils.TempDir("", name)
if err != nil { if err != nil {
@ -59,6 +63,7 @@ func NewConsulContainer(ctx context.Context, config Config) (Node, error) {
Mounts: testcontainers.ContainerMounts{testcontainers.ContainerMount{Source: testcontainers.DockerBindMountSource{HostPath: configFile}, Target: "/consul/config/config.hcl"}}, Mounts: testcontainers.ContainerMounts{testcontainers.ContainerMount{Source: testcontainers.DockerBindMountSource{HostPath: configFile}, Target: "/consul/config/config.hcl"}},
Cmd: config.Cmd, Cmd: config.Cmd,
SkipReaper: skipReaper, SkipReaper: skipReaper,
Env: map[string]string{"CONSUL_LICENSE": license},
} }
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req, ContainerRequest: req,
@ -127,3 +132,18 @@ func isRYUKDisabled() bool {
} }
return skipReaper return skipReaper
} }
func readLicense() (string, error) {
license := os.Getenv("CONSUL_LICENSE")
if license == "" {
licensePath := os.Getenv("CONSUL_LICENSE_PATH")
if licensePath != "" {
licenseBytes, err := os.ReadFile(licensePath)
if err != nil {
return "", err
}
license = string(licenseBytes)
}
}
return license, nil
}