forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo_fake_test.go
126 lines (103 loc) · 4.26 KB
/
dynamo_fake_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package nbs
import (
"bytes"
"testing"
"github.com/attic-labs/noms/go/constants"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/assert"
)
type fakeDDB struct {
data map[string]interface{}
t *testing.T
numPuts, numGets int
}
type record struct {
lock, root []byte
vers, specs string
}
func makeFakeDDB(t *testing.T) *fakeDDB {
return &fakeDDB{
data: map[string]interface{}{},
t: t,
}
}
func (m *fakeDDB) readerForTable(name addr) chunkReader {
if i, present := m.data[fmtTableName(name)]; present {
buff, ok := i.([]byte)
assert.True(m.t, ok)
return newTableReader(parseTableIndex(buff), tableReaderAtFromBytes(buff), fileBlockSize)
}
return nil
}
func (m *fakeDDB) GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
key := input.Key[dbAttr].S
assert.NotNil(m.t, key, "key should have been a String: %+v", input.Key[dbAttr])
item := map[string]*dynamodb.AttributeValue{}
if e, present := m.data[*key]; present {
item[dbAttr] = &dynamodb.AttributeValue{S: key}
switch e := e.(type) {
case record:
item[nbsVersAttr] = &dynamodb.AttributeValue{S: aws.String(StorageVersion)}
item[versAttr] = &dynamodb.AttributeValue{S: aws.String(e.vers)}
item[rootAttr] = &dynamodb.AttributeValue{B: e.root}
item[lockAttr] = &dynamodb.AttributeValue{B: e.lock}
if e.specs != "" {
item[tableSpecsAttr] = &dynamodb.AttributeValue{S: aws.String(e.specs)}
}
case []byte:
item[dataAttr] = &dynamodb.AttributeValue{B: e}
}
}
m.numGets++
return &dynamodb.GetItemOutput{Item: item}, nil
}
func (m *fakeDDB) putRecord(k string, l, r []byte, v string, s string) {
m.data[k] = record{l, r, v, s}
}
func (m *fakeDDB) putData(k string, d []byte) {
m.data[k] = d
}
func (m *fakeDDB) PutItem(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) {
assert.NotNil(m.t, input.Item[dbAttr], "%s should have been present", dbAttr)
assert.NotNil(m.t, input.Item[dbAttr].S, "key should have been a String: %+v", input.Item[dbAttr])
key := *input.Item[dbAttr].S
if input.Item[dataAttr] != nil {
assert.NotNil(m.t, input.Item[dataAttr].B, "data should have been a blob: %+v", input.Item[dataAttr])
m.putData(key, input.Item[dataAttr].B)
return &dynamodb.PutItemOutput{}, nil
}
assert.NotNil(m.t, input.Item[nbsVersAttr], "%s should have been present", nbsVersAttr)
assert.NotNil(m.t, input.Item[nbsVersAttr].S, "nbsVers should have been a String: %+v", input.Item[nbsVersAttr])
assert.Equal(m.t, StorageVersion, *input.Item[nbsVersAttr].S)
assert.NotNil(m.t, input.Item[versAttr], "%s should have been present", versAttr)
assert.NotNil(m.t, input.Item[versAttr].S, "nbsVers should have been a String: %+v", input.Item[versAttr])
assert.Equal(m.t, constants.NomsVersion, *input.Item[versAttr].S)
assert.NotNil(m.t, input.Item[lockAttr], "%s should have been present", lockAttr)
assert.NotNil(m.t, input.Item[lockAttr].B, "lock should have been a blob: %+v", input.Item[lockAttr])
lock := input.Item[lockAttr].B
assert.NotNil(m.t, input.Item[rootAttr], "%s should have been present", rootAttr)
assert.NotNil(m.t, input.Item[rootAttr].B, "root should have been a blob: %+v", input.Item[rootAttr])
root := input.Item[rootAttr].B
specs := ""
if attr, present := input.Item[tableSpecsAttr]; present {
assert.NotNil(m.t, attr.S, "specs should have been a String: %+v", input.Item[tableSpecsAttr])
specs = *attr.S
}
mustNotExist := *(input.ConditionExpression) == valueNotExistsOrEqualsExpression
current, present := m.data[key]
if mustNotExist && present {
return nil, mockAWSError("ConditionalCheckFailedException")
} else if !mustNotExist && !checkCondition(current.(record), input.ExpressionAttributeValues) {
return nil, mockAWSError("ConditionalCheckFailedException")
}
m.putRecord(key, lock, root, constants.NomsVersion, specs)
m.numPuts++
return &dynamodb.PutItemOutput{}, nil
}
func checkCondition(current record, expressionAttrVals map[string]*dynamodb.AttributeValue) bool {
return current.vers == *expressionAttrVals[":vers"].S && bytes.Equal(current.lock, expressionAttrVals[":prev"].B)
}