Skip to content

Commit 5734240

Browse files
zombiezenvangent
authored andcommitted
mysql/rdsmysql: add integration tests (#540)
1 parent 164487f commit 5734240

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

‎mysql/rdsmysql/main.tf‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Harness for MySQL tests.
16+
17+
terraform {
18+
required_version = "~>0.11"
19+
}
20+
21+
provider "aws" {
22+
version = "~> 1.22"
23+
region = "${var.region}"
24+
}
25+
26+
provider "random" {
27+
version = "~> 1.3"
28+
}
29+
30+
variable "region" {
31+
type = "string"
32+
description = "Region to create resources in. See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html for valid values."
33+
}
34+
35+
resource "aws_security_group" "main" {
36+
name_prefix = "testdb"
37+
description = "Security group for Go Cloud MySQL test database."
38+
39+
ingress {
40+
from_port = 3306
41+
to_port = 3306
42+
protocol = "tcp"
43+
cidr_blocks = ["0.0.0.0/0"]
44+
description = "Public MySQL access"
45+
}
46+
47+
egress {
48+
from_port = 0
49+
to_port = 0
50+
protocol = "-1"
51+
cidr_blocks = ["0.0.0.0/0"]
52+
description = "All outgoing traffic allowed"
53+
}
54+
}
55+
56+
resource "random_string" "db_password" {
57+
keepers = {
58+
region = "${var.region}"
59+
}
60+
61+
special = false
62+
length = 20
63+
}
64+
65+
resource "aws_db_instance" "main" {
66+
identifier_prefix = "go-cloud-test"
67+
engine = "mysql"
68+
engine_version = "5.6.39"
69+
instance_class = "db.t2.micro"
70+
allocated_storage = 20
71+
username = "root"
72+
password = "${random_string.db_password.result}"
73+
name = "testdb"
74+
publicly_accessible = true
75+
vpc_security_group_ids = ["${aws_security_group.main.id}"]
76+
skip_final_snapshot = true
77+
}
78+
79+
output "endpoint" {
80+
value = "${aws_db_instance.main.endpoint}"
81+
description = "The RDS instance's host/port."
82+
}
83+
84+
output "username" {
85+
value = "root"
86+
description = "The MySQL username to connect with."
87+
}
88+
89+
output "password" {
90+
value = "${random_string.db_password.result}"
91+
sensitive = true
92+
description = "The RDS instance password for the user."
93+
}
94+
95+
output "database" {
96+
value = "testdb"
97+
description = "The name of the database inside the RDS instance."
98+
}

‎mysql/rdsmysql/rdsmysql_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package rdsmysql
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/google/go-cloud/internal/testing/terraform"
22+
)
23+
24+
func TestOpen(t *testing.T) {
25+
// This test will be skipped unless the project is set up with Terraform.
26+
// Before running go test, run in this directory:
27+
//
28+
// terraform init
29+
// terraform apply
30+
31+
tfOut, err := terraform.ReadOutput(".")
32+
if err != nil {
33+
t.Skipf("Could not obtain harness info: %v", err)
34+
}
35+
endpoint, _ := tfOut["endpoint"].Value.(string)
36+
username, _ := tfOut["username"].Value.(string)
37+
password, _ := tfOut["password"].Value.(string)
38+
databaseName, _ := tfOut["database"].Value.(string)
39+
if endpoint == "" || username == "" || databaseName == "" {
40+
t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q username=%q database=%q", endpoint, username, databaseName)
41+
}
42+
43+
ctx := context.Background()
44+
cf := new(CertFetcher)
45+
db, _, err := Open(ctx, cf, &Params{
46+
Endpoint: endpoint,
47+
User: username,
48+
Password: password,
49+
Database: databaseName,
50+
})
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if err := db.Ping(); err != nil {
55+
t.Error("Ping:", err)
56+
}
57+
if err := db.Close(); err != nil {
58+
t.Error("Close:", err)
59+
}
60+
}

0 commit comments

Comments
 (0)