Skip to content

Commit

Permalink
Add test suite (closes #63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Oct 3, 2016
1 parent 78ecae0 commit 51bc0e0
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ _testmain.go
# This project's binaries
sql-runner
*.tmp
coverage.out
coverage.html

# Vagrant
.vagrant/
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ go:
- 1.7
- tip

before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover

before_script:
- ./integration/setup_travis.sh

script:
- go test -i ./...
- go test ./...
- test -z "$(go fmt ./...)"
- go build -o sql-runner ./sql_runner/
- ./integration/run_tests.sh
- go test ./sql_runner/ -v -covermode=count -coverprofile=coverage.out
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci

before_deploy:
- go get github.com/mitchellh/gox
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SQL Runner

[ ![Build Status] [travis-image] ] [travis] [ ![Release] [release-image] ] [releases] [ ![License] [license-image] ] [license]
[![Build Status] [travis-image]][travis] [![Coveralls][coveralls-image]][coveralls] [![Release][release-image]][releases] [![License][license-image]][license]

## Overview

Expand Down Expand Up @@ -44,6 +44,9 @@ limitations under the License.
[license-image]: http://img.shields.io/badge/license-Apache--2-blue.svg?style=flat
[license]: http://www.apache.org/licenses/LICENSE-2.0

[coveralls-image]: https://coveralls.io/repos/github/snowplow/sql-runner/badge.svg?branch=master
[coveralls]: https://coveralls.io/github/snowplow/sql-runner?branch=master

[snowplow]: https://github.com/snowplow/snowplow

[analysts-guide]: https://github.com/snowplow/sql-runner/wiki/Guide-for-analysts
Expand Down
37 changes: 37 additions & 0 deletions integration/setup_aws.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Copyright (c) 2015-2016 Snowplow Analytics Ltd. All rights reserved.
#
# This program is licensed to you under the Apache License Version 2.0,
# and you may not use this file except in compliance with the Apache License Version 2.0.
# You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the Apache License Version 2.0 is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.

set -e



# -----------------------------------------------------------------------------
# CONSTANTS
# -----------------------------------------------------------------------------

home=${HOME}
aws_dir=${home}/.aws
creds_file=${aws_dir}/credentials



# -----------------------------------------------------------------------------
# EXECUTION
# -----------------------------------------------------------------------------

mkdir -p ${aws_dir}
touch ${creds_file}

echo "[default]" >> ${creds_file}
echo "aws_access_key_id=some-aws-key" >> ${creds_file}
echo "aws_secret_access_key=some-aws-secret" >> ${creds_file}
1 change: 1 addition & 0 deletions integration/setup_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'sql_runner_test
psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = 'sql_runner_tests_2'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE sql_runner_tests_2"

${root}/integration/setup_consul.sh
${root}/integration/setup_aws.sh

printf "Ready for integration tests!\n"
1 change: 1 addition & 0 deletions integration/setup_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ psql -c 'create database sql_runner_tests_1' -U postgres
psql -c 'create database sql_runner_tests_2' -U postgres

./integration/setup_consul.sh
./integration/setup_aws.sh

printf "Ready for integration tests!\n"
68 changes: 68 additions & 0 deletions sql_runner/aws_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Copyright (c) 2015-2016 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License Version 2.0.
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
//
package main

import (
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestAwsEnvCredentials(t *testing.T) {
assert := assert.New(t)

str, err := awsEnvCredentials()
assert.NotNil(err)
assert.NotNil(str)
assert.Equal("EnvAccessKeyNotFound: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY not found in environment", err.Error())
assert.Equal("CREDENTIALS 'aws_access_key_id=;aws_secret_access_key='", str)

os.Setenv("AWS_ACCESS_KEY_ID", "some-aws-key")
os.Setenv("AWS_SECRET_ACCESS_KEY", "some-aws-secret")

str, err = awsEnvCredentials()
assert.NotNil(str)
assert.Nil(err)
assert.Equal("CREDENTIALS 'aws_access_key_id=some-aws-key;aws_secret_access_key=some-aws-secret'", str)
}

func TestAwsProfileCredentials(t *testing.T) {
assert := assert.New(t)

str, err := awsProfileCredentials("fake-profile")
assert.NotNil(err)
assert.NotNil(str)
assert.Equal("CREDENTIALS 'aws_access_key_id=;aws_secret_access_key='", str)

str, err = awsProfileCredentials("default")
assert.NotNil(str)
assert.Nil(err)
assert.Equal("CREDENTIALS 'aws_access_key_id=some-aws-key;aws_secret_access_key=some-aws-secret'", str)
}

func TestAwsChainCredentials(t *testing.T) {
assert := assert.New(t)

os.Setenv("AWS_ACCESS_KEY_ID", "some-aws-key")
os.Setenv("AWS_SECRET_ACCESS_KEY", "some-aws-secret")

str, err := awsChainCredentials("fake-profile")
assert.NotNil(str)
assert.Nil(err)
assert.Equal("CREDENTIALS 'aws_access_key_id=some-aws-key;aws_secret_access_key=some-aws-secret'", str)

str, err = awsChainCredentials("default")
assert.NotNil(str)
assert.Nil(err)
assert.Equal("CREDENTIALS 'aws_access_key_id=some-aws-key;aws_secret_access_key=some-aws-secret'", str)
}
22 changes: 5 additions & 17 deletions sql_runner/consul_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ func GetConsulClient(address string) (*api.Client, error) {
// GetBytesFromConsul attempts to return the bytes
// of a key stored in a Consul server
func GetBytesFromConsul(address string, key string) ([]byte, error) {
client, err := GetConsulClient(address)
if err != nil {
return nil, err
}

client, _ := GetConsulClient(address)
kv := client.KV()

// Get the KV Pair from consul
Expand Down Expand Up @@ -64,16 +60,12 @@ func GetStringValueFromConsul(address string, key string) (string, error) {
// PutBytesToConsul attempts to push a new
// KV pair to a Consul Server
func PutBytesToConsul(address string, key string, value []byte) error {
client, err := GetConsulClient(address)
if err != nil {
return err
}

client, _ := GetConsulClient(address)
kv := client.KV()

// Put a new KV pair to consul
p := &api.KVPair{Key: key, Value: value}
_, err = kv.Put(p, nil)
_, err := kv.Put(p, nil)
return err
}

Expand All @@ -86,14 +78,10 @@ func PutStringValueToConsul(address string, key string, value string) error {
// DeleteValueFromConsul attempts to delete a
// KV pair from a Consul Server
func DeleteValueFromConsul(address string, key string) error {
client, err := GetConsulClient(address)
if err != nil {
return err
}

client, _ := GetConsulClient(address)
kv := client.KV()

// Delete the KV pair
_, err = kv.Delete(key, nil)
_, err := kv.Delete(key, nil)
return err
}
47 changes: 47 additions & 0 deletions sql_runner/consul_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright (c) 2015-2016 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License Version 2.0.
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
//
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestPutGetDelStringValueFromConsul_Failure(t *testing.T) {
assert := assert.New(t)

err := PutStringValueToConsul("localhost", "somekey", "somevalue")
assert.NotNil(err)

str, err := GetStringValueFromConsul("localhost", "somekey")
assert.Equal("", str)
assert.NotNil(err)

err = DeleteValueFromConsul("localhost", "somekey")
assert.NotNil(err)
}

func TestPutGetDelStringValueFromConsul_Success(t *testing.T) {
assert := assert.New(t)

err := PutStringValueToConsul("localhost:8500", "somekey", "somevalue")
assert.Nil(err)

str, err := GetStringValueFromConsul("localhost:8500", "somekey")
assert.Nil(err)
assert.NotNil(str)
assert.Equal("somevalue", str)

err = DeleteValueFromConsul("localhost:8500", "somekey")
assert.Nil(err)
}
32 changes: 32 additions & 0 deletions sql_runner/file_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) 2015-2016 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License Version 2.0.
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
//
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestLoadLocalFile(t *testing.T) {
assert := assert.New(t)

bytes, err := loadLocalFile("/this/path/does/not/exist")
assert.Nil(bytes)
assert.NotNil(err)
assert.Equal("open /this/path/does/not/exist: no such file or directory", err.Error())

bytes, err = loadLocalFile("../VERSION")
assert.NotNil(bytes)
assert.Nil(err)
assert.Equal(CLI_VERSION, string(bytes))
}
Loading

0 comments on commit 51bc0e0

Please sign in to comment.