-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdiscovery_test.go
82 lines (72 loc) · 2.55 KB
/
discovery_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sparta
import (
"encoding/base64"
"encoding/json"
"os"
"testing"
"github.com/rs/zerolog"
)
var discoveryDataNoTags = `
{
"ResourceID": "mainhelloWorldGETLambda3d6fd4fce31e46927fb59e0cfe2f20461a69490a",
"Region": "us-west-2",
"StackID": "arn:aws:cloudformation:us-west-2:123412341234:stack/SpartaDDB-mweagle/c4ada6d0-d697-11e7-9b91-50d5ca789e82",
"StackName": "SpartaDDB-mweagle",
"Resources": {
"DynamoDBad8db2fc80a1af0b5bacfbc66b5ae671301d5e96": {
"ResourceID": "DynamoDBad8db2fc80a1af0b5bacfbc66b5ae671301d5e96",
"ResourceRef": "SpartaDDB-mweagle-DynamoDBad8db2fc80a1af0b5bacfbc66b5ae671301d5e96-1EU295I6O4XJH",
"ResourceType": "AWS::DynamoDB::Table",
"Properties": {
"StreamArn": "arn:aws:dynamodb:us-west-2:123412341234:table/SpartaDDB-mweagle-DynamoDBad8db2fc80a1af0b5bacfbc66b5ae671301d5e96-1EU295I6O4XJH/stream/2017-12-03T15:37:38.943"
}
}
}
}
`
func TestDiscoveryInitialized(t *testing.T) {
// Ensure that sparta.Discover() can only be called from a lambda function
logger, _ := NewLogger(zerolog.WarnLevel.String())
// Encode the data, stuff it into the environment variable
encodedString := base64.StdEncoding.EncodeToString([]byte(discoveryDataNoTags))
os.Setenv(envVarDiscoveryInformation, encodedString)
// Initialize the data
initializeDiscovery(logger)
configuration, err := Discover()
t.Logf("Configuration: %#v", configuration)
t.Logf("Error: %#v", err)
if err != nil {
t.Errorf("sparta.Discover() failed to initialize from environment")
}
t.Logf("Properly unmarshaled environment data")
}
func TestDiscoveryNotInitialized(t *testing.T) {
configuration, err := Discover()
t.Logf("Configuration: %#v", configuration)
t.Logf("Error: %#v", err)
if err != nil {
t.Errorf("sparta.Discover() failed to error when not initialized")
}
t.Logf("Properly rejected unintialized discovery data")
}
func TestDiscoveryUnmarshalNoTags(t *testing.T) {
// Ensure that sparta.Discover() can only be called from a lambda function
var info DiscoveryInfo
err := json.Unmarshal([]byte(discoveryDataNoTags), &info)
if nil != err {
t.Errorf("Failed to unmarshal discovery data without tags")
}
if len(info.Resources) != 1 {
t.Errorf("Failed to unmarshal single resource")
}
t.Logf("Discovery Info: %#v", info)
}
func TestDiscoveryEmptyMetadata(t *testing.T) {
// Ensure that sparta.Discover() can only be called from a lambda function
var info DiscoveryInfo
err := json.Unmarshal([]byte("{}"), &info)
if nil != err {
t.Errorf("Failed to unmarshal empty discovery data")
}
t.Logf("Discovery Info: %#v", info)
}