-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from rdooley/feelin-testy
Testing PR
- Loading branch information
Showing
13 changed files
with
383 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
vendor | ||
tags | ||
build | ||
|
||
coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: go | ||
|
||
go: | ||
- "1.12" | ||
|
||
script: | ||
- env GO111MODULE=on go build | ||
- env GO111MODULE=on go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
|
||
"github.com/rdooley/confidynt/service" | ||
) | ||
|
||
// Read a config from dynamo db | ||
func Read(table, key, value string, ds service.Dynamo, w io.Writer) { | ||
config, err := ds.Read(table, key, value) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case dynamodb.ErrCodeProvisionedThroughputExceededException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) | ||
case dynamodb.ErrCodeResourceNotFoundException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) | ||
case dynamodb.ErrCodeRequestLimitExceeded: | ||
fmt.Fprintln(w, dynamodb.ErrCodeRequestLimitExceeded, aerr.Error()) | ||
case dynamodb.ErrCodeInternalServerError: | ||
fmt.Fprintln(w, dynamodb.ErrCodeInternalServerError, aerr.Error()) | ||
default: | ||
fmt.Fprintln(w, aerr.Error()) | ||
} | ||
} else { | ||
// Print the error, cast err to awserr.Error to get the Code and | ||
// Message from an error. | ||
fmt.Fprintln(w, err.Error()) | ||
} | ||
return | ||
} | ||
|
||
fmt.Fprintf(w, "%s=%s\n", key, config[key]) | ||
for k, v := range config { | ||
if k != key { | ||
fmt.Fprintf(w, "%s=%s\n", k, v) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/rdooley/confidynt/service" | ||
"github.com/rdooley/confidynt/types" | ||
) | ||
|
||
func TestRead(t *testing.T) { | ||
t.Log("Testing Read") | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
table := "table" | ||
key := "key" | ||
val := "val" | ||
|
||
conf := types.Config{} | ||
conf[key] = val | ||
conf["other_key"] = "other_val" | ||
|
||
expected := "key=val\n" | ||
expected += "other_key=other_val\n" | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
mockDynamo := service.NewMockDynamo(mockCtrl) | ||
mockDynamo.EXPECT().Read(table, key, val).Return(conf, nil) | ||
Read(table, key, val, mockDynamo, buf) | ||
assert.Equal(t, buf.String(), expected) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cli | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
|
||
"github.com/rdooley/confidynt/service" | ||
"github.com/rdooley/confidynt/types" | ||
) | ||
|
||
var propRe = regexp.MustCompile(`^(\w+)=(.*)$`) | ||
|
||
// Write a given config file to a dynamo db table | ||
func Write(table string, path string, ds service.Dynamo, w io.Writer) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
config := types.Config{} | ||
var key string | ||
var value string | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if line == "" { | ||
// Ignore blank lines | ||
continue | ||
} | ||
if propRe.MatchString(line) { | ||
if key != "" { | ||
config[key] = value | ||
} | ||
matches := propRe.FindStringSubmatch(line) | ||
key = matches[1] | ||
value = matches[2] | ||
} else { | ||
// Continuation of a previous property "^ indented..." | ||
value += "\n" + strings.TrimRight(line, " \t") | ||
} | ||
} | ||
// Catch final prop | ||
if key != "" { | ||
config[key] = value | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
log.Fatal(err) | ||
} | ||
err = ds.Write(table, config) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case dynamodb.ErrCodeConditionalCheckFailedException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error()) | ||
case dynamodb.ErrCodeProvisionedThroughputExceededException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) | ||
case dynamodb.ErrCodeResourceNotFoundException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) | ||
case dynamodb.ErrCodeItemCollectionSizeLimitExceededException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error()) | ||
case dynamodb.ErrCodeTransactionConflictException: | ||
fmt.Fprintln(w, dynamodb.ErrCodeTransactionConflictException, aerr.Error()) | ||
case dynamodb.ErrCodeRequestLimitExceeded: | ||
fmt.Fprintln(w, dynamodb.ErrCodeRequestLimitExceeded, aerr.Error()) | ||
case dynamodb.ErrCodeInternalServerError: | ||
fmt.Fprintln(w, dynamodb.ErrCodeInternalServerError, aerr.Error()) | ||
default: | ||
fmt.Fprintln(w, aerr.Error()) | ||
} | ||
} else { | ||
// Print the error, cast err to awserr.Error to get the Code and | ||
// Message from an error. | ||
fmt.Fprintln(w, err.Error()) | ||
} | ||
return | ||
} | ||
fmt.Fprintf(w, "%s written to %s\n", path, table) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/rdooley/confidynt/service" | ||
"github.com/rdooley/confidynt/types" | ||
) | ||
|
||
func TestWrite(t *testing.T) { | ||
t.Log("Testing Write") | ||
path := "test.conf" | ||
mockCtrl := gomock.NewController(t) | ||
defer func() { | ||
os.Remove(path) | ||
mockCtrl.Finish() | ||
}() | ||
|
||
table := "table" | ||
key := "key" | ||
val := "val" | ||
|
||
conf := types.Config{} | ||
conf[key] = val | ||
conf["other_key"] = "other_val" | ||
|
||
text := "key=val\n" | ||
text += "other_key=other_val\n" | ||
ioutil.WriteFile(path, []byte(text), 0644) | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
mockDynamo := service.NewMockDynamo(mockCtrl) | ||
mockDynamo.EXPECT().Write(table, conf).Return(nil) | ||
|
||
Write(table, path, mockDynamo, buf) | ||
assert.Equal(t, buf.String(), "test.conf written to table\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.