driftctl/pkg/memstore/store_test.go

72 lines
1.6 KiB
Go
Raw Normal View History

2021-06-25 15:14:50 +00:00
package memstore
import (
2021-06-28 12:39:14 +00:00
"encoding/json"
2021-06-25 15:14:50 +00:00
"sync"
"testing"
"github.com/cloudskiff/driftctl/test/resource"
"github.com/stretchr/testify/assert"
)
func TestStore(t *testing.T) {
cases := []struct {
name string
2021-06-28 12:39:14 +00:00
bucket BucketName
2021-06-25 15:14:50 +00:00
values map[string]interface{}
expectedJSON string
}{
{
name: "test basic store usage",
2021-06-28 12:39:14 +00:00
bucket: 0,
2021-06-25 15:14:50 +00:00
values: map[string]interface{}{
"test-value_|)": 13,
"duration_key": "23",
"null": nil,
"res": &resource.FakeResource{Id: "id", Type: "type"},
},
expectedJSON: `{"duration_key":"23","null":null,"res":{"Id":"id","Type":"type","Attrs":null},"test-value_|)":13}`,
},
{
name: "test empty bucket",
2021-06-28 12:39:14 +00:00
bucket: 2,
2021-06-25 15:14:50 +00:00
values: map[string]interface{}{},
expectedJSON: `{}`,
},
{
name: "test bucket with nil values",
2021-06-28 12:39:14 +00:00
bucket: 1,
2021-06-25 15:14:50 +00:00
values: map[string]interface{}{
"version": nil,
"total_resources": nil,
"total_managed": nil,
},
expectedJSON: `{"total_managed":null,"total_resources":null,"version":null}`,
},
}
for _, tt := range cases {
kv := New()
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
for key, val := range tt.values {
wg.Add(1)
go func(key string, val interface{}, wg *sync.WaitGroup) {
defer wg.Done()
kv.Bucket(tt.bucket).Set(key, val)
assert.Equal(t, val, kv.Bucket(tt.bucket).Get(key))
2021-06-28 12:39:14 +00:00
assert.Equal(t, nil, kv.Bucket(tt.bucket+1).Get(key))
2021-06-25 15:14:50 +00:00
}(key, val, &wg)
}
wg.Wait()
2021-06-28 12:39:14 +00:00
b, err := json.Marshal(kv.Bucket(tt.bucket).Values())
2021-06-25 15:14:50 +00:00
assert.Nil(t, err)
assert.Equal(t, tt.expectedJSON, string(b))
})
}
}