From a3666c843f6bb15cac60080137b42fa68c4063ea Mon Sep 17 00:00:00 2001 From: groot Date: Thu, 9 Sep 2021 17:12:15 +0800 Subject: [PATCH] Add unittest for chunk manager (#7628) Signed-off-by: yhmo --- internal/storage/local_chunk_manager_test.go | 51 ++++++++++++++ internal/storage/minio_chunk_manager_test.go | 70 +++++++++++++++++++ internal/storage/vector_chunk_manager_test.go | 2 +- 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 internal/storage/local_chunk_manager_test.go create mode 100644 internal/storage/minio_chunk_manager_test.go diff --git a/internal/storage/local_chunk_manager_test.go b/internal/storage/local_chunk_manager_test.go new file mode 100644 index 0000000000..c1af3428e5 --- /dev/null +++ b/internal/storage/local_chunk_manager_test.go @@ -0,0 +1,51 @@ +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed under the License +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +// or implied. See the License for the specific language governing permissions and limitations under the License. + +package storage + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestLocalChunkManager_GetPath(t *testing.T) { + lcm := NewLocalChunkManager(localPath) + path, err := lcm.GetPath("invalid") + assert.Empty(t, path) + assert.Error(t, err) +} + +func TestLocalChunkManager_Write(t *testing.T) { + lcm := NewLocalChunkManager(localPath) + + bin := []byte{1, 2, 3} + err := lcm.Write("1", bin) + assert.Nil(t, err) +} + +func TestLocalChunkManager_Read(t *testing.T) { + lcm := NewLocalChunkManager(localPath) + bin, err := lcm.Read("invalid") + assert.Nil(t, bin) + assert.Error(t, err) + + content := make([]byte, 8) + n, err := lcm.ReadAt("invalid", content, 0) + assert.Zero(t, n) + assert.Error(t, err) + + bin = []byte{1, 2, 3} + lcm.Write("1", bin) + res, err := lcm.Read("1") + assert.Nil(t, err) + assert.Equal(t, len(res), len(bin)) +} diff --git a/internal/storage/minio_chunk_manager_test.go b/internal/storage/minio_chunk_manager_test.go new file mode 100644 index 0000000000..66c8a7ad24 --- /dev/null +++ b/internal/storage/minio_chunk_manager_test.go @@ -0,0 +1,70 @@ +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software distributed under the License +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +// or implied. See the License for the specific language governing permissions and limitations under the License. + +package storage + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMinioChunkManager_GetPath(t *testing.T) { + bucketName := "minio-chunk-manager" + kv, err := newMinIOKVClient(context.TODO(), bucketName) + assert.Nil(t, err) + + minioMgr := NewMinioChunkManager(kv) + path, err := minioMgr.GetPath("invalid") + assert.Empty(t, path) + assert.Error(t, err) +} + +func TestMinioChunkManager_ReadAt(t *testing.T) { + bucketName := "minio-chunk-manager" + kv, err := newMinIOKVClient(context.TODO(), bucketName) + assert.Nil(t, err) + + minioMgr := NewMinioChunkManager(kv) + + key := "1" + bin := []byte{1, 2, 3} + content := make([]byte, 8) + minioMgr.minio.Remove(key) + + n, err := minioMgr.ReadAt(key, content, 0) + assert.Equal(t, n, -1) + assert.Error(t, err) + + err = minioMgr.Write(key, bin) + assert.Nil(t, err) + + n, err = minioMgr.ReadAt(key, content, -1) + assert.Equal(t, n, 0) + assert.Error(t, err) + + offset := int64(1) + n, err = minioMgr.ReadAt(key, content, offset) + assert.Error(t, err) + assert.Equal(t, n, len(bin)-int(offset)) + for i := offset; i < int64(len(bin)); i++ { + assert.Equal(t, content[i-offset], bin[i]) + } + + content = make([]byte, 2) + n, err = minioMgr.ReadAt(key, content, offset) + assert.Nil(t, err) + assert.Equal(t, n, len(bin)-int(offset)) + for i := offset; i < int64(len(bin)); i++ { + assert.Equal(t, content[i-offset], bin[i]) + } +} diff --git a/internal/storage/vector_chunk_manager_test.go b/internal/storage/vector_chunk_manager_test.go index 6fad782cd0..4e985f29cd 100644 --- a/internal/storage/vector_chunk_manager_test.go +++ b/internal/storage/vector_chunk_manager_test.go @@ -154,7 +154,7 @@ func buildVectorChunkManager(t *testing.T, localPath string, localCacheEnable bo } var Params paramtable.BaseTable -var localPath string = "/tmp/milvus/data" +var localPath string = "/tmp/milvus/test_data" func TestMain(m *testing.M) { Params.Init()