gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/storage/disk_test.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "os" 6 "path" 7 "reflect" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 "gitlab.com/beacon-software/gadget/generator" 14 ) 15 16 var RootPath = getRootPath() 17 18 const TestRoot = "disk_test" 19 20 type TestStruct struct { 21 Foo string 22 Bar int 23 } 24 25 func TestMain(m *testing.M) { 26 res := m.Run() 27 cleanup() 28 os.Exit(res) 29 } 30 31 func getRootPath() string { 32 uuid := generator.String(10) 33 rootPath := fmt.Sprintf("/tmp/%s/%s", TestRoot, strings.TrimSpace(uuid)) 34 return rootPath 35 } 36 37 func cleanup() { 38 wd, _ := os.Getwd() 39 os.Chdir("/tmp") 40 os.RemoveAll(TestRoot) 41 os.Chdir(wd) 42 } 43 44 func TestInitialize(t *testing.T) { 45 _, err := NewDisk(RootPath, 0777) 46 if err != nil { 47 t.Error(err.Error()) 48 } 49 // relative path 50 _, err = NewDisk("foo/bar", 0777) 51 if err == nil { 52 t.Error("Relative path should fail.") 53 } 54 // empty path 55 _, err = NewDisk("", 0777) 56 if err == nil { 57 t.Error("Empty path should fail.") 58 } 59 } 60 61 func TestGetFullPath(t *testing.T) { 62 provider, _ := NewDisk(RootPath, 0777) 63 impl := provider.(*disk) 64 // empty key 65 _, err := impl.getFullPath("") 66 if err == nil { 67 t.Error("Empty partition should fail.") 68 } 69 70 key := generator.String(20) 71 expected := RootPath + "/" + keyToFilename(key) + ".gob" 72 actual, err := impl.getFullPath(key) 73 if err != nil { 74 t.Error(err) 75 } 76 if path.Clean(actual) != path.Clean(expected) { 77 t.Errorf("'%s' =/= '%s'", actual, expected) 78 } 79 } 80 81 func TestSymmetricWrite(t *testing.T) { 82 expected := &TestStruct{Foo: "Foo", Bar: 20} 83 provider, _ := NewDisk(RootPath, 0777) 84 key := generator.String(20) 85 err := provider.Write(key, expected) 86 if err != nil { 87 t.Error(err) 88 } 89 var actual = &TestStruct{} 90 err = provider.Read(key, actual) 91 if err != nil { 92 t.Error(err) 93 t.FailNow() 94 } 95 96 if !reflect.DeepEqual(actual, expected) { 97 t.Errorf("%v != %v", actual, expected) 98 } 99 } 100 101 func TestExists(t *testing.T) { 102 s := &TestStruct{Foo: "foo", Bar: 20} 103 provider := &disk{RootPath: RootPath, Mode: 0777} 104 key := generator.String(20) 105 if provider.Exists(key) { 106 t.Error("Resource should not exist.") 107 } 108 provider.Write(key, s) 109 if !provider.Exists(key) { 110 t.Error("Resource should exist.") 111 } 112 } 113 114 func TestDelete(t *testing.T) { 115 target := &TestStruct{Foo: "foo", Bar: 20} 116 provider := &disk{RootPath: RootPath, Mode: 0777} 117 key := generator.String(20) 118 provider.Write(key, target) 119 provider.Delete(key) 120 if provider.Exists(key) { 121 t.Error("Resource should have been removed.") 122 } 123 } 124 125 func Test_diskProvider_Shard(t *testing.T) { 126 assert := assert.New(t) 127 storage, err := NewDisk(RootPath, 0777) 128 assert.NoError(err) 129 pkey := generator.String(20) 130 pvalue := generator.String(20) 131 skey := generator.String(20) 132 svalue := generator.String(20) 133 assert.NoError(storage.Write(pkey, pvalue)) 134 shardName := generator.String(20) 135 shard, err := storage.Shard(shardName) 136 assert.NoError(err) 137 assert.False(shard.Exists(pkey)) 138 assert.NoError(shard.Write(skey, svalue)) 139 assert.False(storage.Exists(skey)) 140 shard, err = storage.Shard(shardName) 141 assert.NoError(err) 142 assert.False(shard.Exists(pkey)) 143 assert.True(shard.Exists(skey)) 144 }