vault/helper/testhelpers/mssql/mssqlhelper.go
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

85 lines
2.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package mssqlhelper
import (
"context"
"database/sql"
"fmt"
"net/url"
"os"
"runtime"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/helper/docker"
)
const mssqlPassword = "yourStrong(!)Password"
// This constant is used in retrying the mssql container restart, since
// intermittently the container starts but mssql within the container
// is unreachable.
const numRetries = 3
func PrepareMSSQLTestContainer(t *testing.T) (cleanup func(), retURL string) {
if strings.Contains(runtime.GOARCH, "arm") {
t.Skip("Skipping, as this image is not supported on ARM architectures")
}
if os.Getenv("MSSQL_URL") != "" {
return func() {}, os.Getenv("MSSQL_URL")
}
var err error
for i := 0; i < numRetries; i++ {
var svc *docker.Service
runner, err := docker.NewServiceRunner(docker.RunOptions{
ContainerName: "sqlserver",
ImageRepo: "mcr.microsoft.com/mssql/server",
ImageTag: "2017-latest-ubuntu",
Env: []string{"ACCEPT_EULA=Y", "SA_PASSWORD=" + mssqlPassword},
Ports: []string{"1433/tcp"},
LogConsumer: func(s string) {
if t.Failed() {
t.Logf("container logs: %s", s)
}
},
})
if err != nil {
t.Fatalf("Could not start docker MSSQL: %s", err)
}
svc, err = runner.StartService(context.Background(), connectMSSQL)
if err == nil {
return svc.Cleanup, svc.Config.URL().String()
}
}
t.Fatalf("Could not start docker MSSQL: %s", err)
return nil, ""
}
func connectMSSQL(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
u := url.URL{
Scheme: "sqlserver",
User: url.UserPassword("sa", mssqlPassword),
Host: fmt.Sprintf("%s:%d", host, port),
}
// Attempt to address connection flakiness within tests such as "Failed to initialize: error verifying connection ..."
u.Query().Add("Connection Timeout", "30")
db, err := sql.Open("mssql", u.String())
if err != nil {
return nil, err
}
defer db.Close()
err = db.Ping()
if err != nil {
return nil, err
}
return docker.NewServiceURL(u), nil
}