vault/helper/testhelpers/mysql/mysqlhelper.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

82 lines
2.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package mysqlhelper
import (
"context"
"database/sql"
"fmt"
"os"
"runtime"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/helper/docker"
)
type Config struct {
docker.ServiceHostPort
ConnString string
}
var _ docker.ServiceConfig = &Config{}
func PrepareTestContainer(t *testing.T, legacy bool, pw string) (func(), string) {
if os.Getenv("MYSQL_URL") != "" {
return func() {}, os.Getenv("MYSQL_URL")
}
// ARM64 is only supported on MySQL 8.0 and above. If we update
// our image and support to 8.0, we can unskip these tests.
if strings.Contains(runtime.GOARCH, "arm") {
t.Skip("Skipping, as MySQL 5.7 is not supported on ARM architectures")
}
imageVersion := "5.7"
if legacy {
imageVersion = "5.6"
}
runner, err := docker.NewServiceRunner(docker.RunOptions{
ContainerName: "mysql",
ImageRepo: "docker.mirror.hashicorp.services/library/mysql",
ImageTag: imageVersion,
Ports: []string{"3306/tcp"},
Env: []string{"MYSQL_ROOT_PASSWORD=" + pw},
})
if err != nil {
t.Fatalf("could not start docker mysql: %s", err)
}
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
hostIP := docker.NewServiceHostPort(host, port)
connString := fmt.Sprintf("root:%s@tcp(%s)/mysql?parseTime=true", pw, hostIP.Address())
db, err := sql.Open("mysql", connString)
if err != nil {
return nil, err
}
defer db.Close()
err = db.Ping()
if err != nil {
return nil, err
}
return &Config{ServiceHostPort: *hostIP, ConnString: connString}, nil
})
if err != nil {
t.Fatalf("could not start docker mysql: %s", err)
}
return svc.Cleanup, svc.Config.(*Config).ConnString
}
func TestCredsExist(t testing.TB, connURL, username, password string) error {
// Log in with the new creds
connURL = strings.Replace(connURL, "root:secret", fmt.Sprintf("%s:%s", username, password), 1)
db, err := sql.Open("mysql", connURL)
if err != nil {
return err
}
defer db.Close()
return db.Ping()
}