Skip to content

Commit

Permalink
mysql/rdsmysql: add integration tests (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen authored and vangent committed Oct 12, 2018
1 parent 164487f commit 5734240
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
98 changes: 98 additions & 0 deletions mysql/rdsmysql/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Harness for MySQL tests.

terraform {
required_version = "~>0.11"
}

provider "aws" {
version = "~> 1.22"
region = "${var.region}"
}

provider "random" {
version = "~> 1.3"
}

variable "region" {
type = "string"
description = "Region to create resources in. See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html for valid values."
}

resource "aws_security_group" "main" {
name_prefix = "testdb"
description = "Security group for Go Cloud MySQL test database."

ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Public MySQL access"
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "All outgoing traffic allowed"
}
}

resource "random_string" "db_password" {
keepers = {
region = "${var.region}"
}

special = false
length = 20
}

resource "aws_db_instance" "main" {
identifier_prefix = "go-cloud-test"
engine = "mysql"
engine_version = "5.6.39"
instance_class = "db.t2.micro"
allocated_storage = 20
username = "root"
password = "${random_string.db_password.result}"
name = "testdb"
publicly_accessible = true
vpc_security_group_ids = ["${aws_security_group.main.id}"]
skip_final_snapshot = true
}

output "endpoint" {
value = "${aws_db_instance.main.endpoint}"
description = "The RDS instance's host/port."
}

output "username" {
value = "root"
description = "The MySQL username to connect with."
}

output "password" {
value = "${random_string.db_password.result}"
sensitive = true
description = "The RDS instance password for the user."
}

output "database" {
value = "testdb"
description = "The name of the database inside the RDS instance."
}
60 changes: 60 additions & 0 deletions mysql/rdsmysql/rdsmysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rdsmysql

import (
"context"
"testing"

"github.com/google/go-cloud/internal/testing/terraform"
)

func TestOpen(t *testing.T) {
// This test will be skipped unless the project is set up with Terraform.
// Before running go test, run in this directory:
//
// terraform init
// terraform apply

tfOut, err := terraform.ReadOutput(".")
if err != nil {
t.Skipf("Could not obtain harness info: %v", err)
}
endpoint, _ := tfOut["endpoint"].Value.(string)
username, _ := tfOut["username"].Value.(string)
password, _ := tfOut["password"].Value.(string)
databaseName, _ := tfOut["database"].Value.(string)
if endpoint == "" || username == "" || databaseName == "" {
t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q username=%q database=%q", endpoint, username, databaseName)
}

ctx := context.Background()
cf := new(CertFetcher)
db, _, err := Open(ctx, cf, &Params{
Endpoint: endpoint,
User: username,
Password: password,
Database: databaseName,
})
if err != nil {
t.Fatal(err)
}
if err := db.Ping(); err != nil {
t.Error("Ping:", err)
}
if err := db.Close(); err != nil {
t.Error("Close:", err)
}
}

0 comments on commit 5734240

Please sign in to comment.