|
| 1 | +package sqldb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + testcontainers "github.com/testcontainers/testcontainers-go" |
| 12 | + testmysql "github.com/testcontainers/testcontainers-go/modules/mysql" |
| 13 | + "github.com/testcontainers/testcontainers-go/wait" |
| 14 | + |
| 15 | + "github.com/argoproj/argo-workflows/v4/config" |
| 16 | + "github.com/argoproj/argo-workflows/v4/util/logging" |
| 17 | +) |
| 18 | + |
| 19 | +type mysqlVariant struct { |
| 20 | + image string |
| 21 | + waitMessage string |
| 22 | +} |
| 23 | + |
| 24 | +var mysqlVariants = map[string]mysqlVariant{ |
| 25 | + "MySQL": {image: "mysql:8.4", waitMessage: "port: 3306 MySQL Community Server"}, |
| 26 | + "MariaDB": {image: "mariadb:11.4", waitMessage: "mariadbd: ready for connections"}, |
| 27 | +} |
| 28 | + |
| 29 | +func setupMySQLContainer(ctx context.Context, t *testing.T, v mysqlVariant) (config.DBConfig, func()) { |
| 30 | + t.Helper() |
| 31 | + |
| 32 | + c, err := testmysql.Run(ctx, |
| 33 | + v.image, |
| 34 | + testmysql.WithDatabase("argo"), |
| 35 | + testmysql.WithUsername("argo"), |
| 36 | + testmysql.WithPassword("argo"), |
| 37 | + testcontainers.WithWaitStrategy( |
| 38 | + wait.ForAll( |
| 39 | + wait.ForLog(v.waitMessage).WithStartupTimeout(60*time.Second), |
| 40 | + wait.ForListeningPort("3306/tcp"), |
| 41 | + )), |
| 42 | + ) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + host, err := c.Host(ctx) |
| 46 | + require.NoError(t, err) |
| 47 | + p, err := c.MappedPort(ctx, "3306/tcp") |
| 48 | + require.NoError(t, err) |
| 49 | + port, err := strconv.Atoi(p.Port()) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + return config.DBConfig{ |
| 53 | + MySQL: &config.MySQLConfig{ |
| 54 | + DatabaseConfig: config.DatabaseConfig{ |
| 55 | + Database: "argo", |
| 56 | + Host: host, |
| 57 | + Port: port, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, func() { testcontainers.TerminateContainer(c) } //nolint:errcheck |
| 61 | +} |
| 62 | + |
| 63 | +// TestMySQLSessionConnect verifies that CreateDBSessionWithCreds can connect |
| 64 | +// to both MySQL and MariaDB. MariaDB requires AllowNativePasswords. |
| 65 | +func TestMySQLSessionConnect(t *testing.T) { |
| 66 | + if runtime.GOOS == "windows" { |
| 67 | + t.Skip("test requires Linux container") |
| 68 | + } |
| 69 | + |
| 70 | + for name, variant := range mysqlVariants { |
| 71 | + t.Run(name, func(t *testing.T) { |
| 72 | + ctx := logging.TestContext(t.Context()) |
| 73 | + dbConfig, cleanup := setupMySQLContainer(ctx, t, variant) |
| 74 | + defer cleanup() |
| 75 | + |
| 76 | + sess, dbType, err := CreateDBSessionWithCreds(dbConfig, "argo", "argo") |
| 77 | + require.NoError(t, err) |
| 78 | + defer sess.Close() |
| 79 | + |
| 80 | + require.Equal(t, MySQL, dbType) |
| 81 | + require.NoError(t, sess.Ping()) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments