2021-11-10 23:55:32 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 10:09:43 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:55:32 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 10:09:43 +08:00
|
|
|
//
|
2021-11-10 23:55:32 +08:00
|
|
|
// 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.
|
2021-04-19 10:09:43 +08:00
|
|
|
|
2021-06-22 14:40:07 +08:00
|
|
|
package proxy
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
import (
|
2022-08-04 11:04:34 +08:00
|
|
|
"context"
|
2023-08-30 10:52:26 +08:00
|
|
|
"encoding/json"
|
2022-08-04 11:04:34 +08:00
|
|
|
"fmt"
|
2022-04-29 13:35:49 +08:00
|
|
|
"strconv"
|
2022-08-04 11:04:34 +08:00
|
|
|
"strings"
|
2020-11-26 16:01:31 +08:00
|
|
|
"testing"
|
2022-10-12 15:17:23 +08:00
|
|
|
"time"
|
2022-08-19 19:42:50 +08:00
|
|
|
|
2023-08-30 14:47:00 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
|
2023-02-26 11:31:49 +08:00
|
|
|
"github.com/cockroachdb/errors"
|
2022-10-21 14:41:28 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-16 15:38:34 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-10-21 14:41:28 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/msgpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
2023-07-14 10:12:31 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/mocks"
|
2022-10-21 14:41:28 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/common"
|
2023-08-30 10:52:26 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/mq/msgstream"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/crypto"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/tsoutil"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2020-11-26 16:01:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateCollectionName(t *testing.T) {
|
2021-10-23 10:53:12 +08:00
|
|
|
assert.Nil(t, validateCollectionName("abc"))
|
|
|
|
assert.Nil(t, validateCollectionName("_123abc"))
|
2022-09-08 10:16:34 +08:00
|
|
|
assert.Nil(t, validateCollectionName("abc123_"))
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
longName := make([]byte, 256)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
"123abc",
|
|
|
|
"$abc",
|
2022-09-08 10:16:34 +08:00
|
|
|
"abc$",
|
2020-11-26 16:01:31 +08:00
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
2021-10-23 10:53:12 +08:00
|
|
|
assert.NotNil(t, validateCollectionName(name))
|
2021-09-18 11:13:51 +08:00
|
|
|
assert.NotNil(t, validateCollectionNameOrAlias(name, "name"))
|
|
|
|
assert.NotNil(t, validateCollectionNameOrAlias(name, "alias"))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:52:31 +08:00
|
|
|
func TestValidateResourceGroupName(t *testing.T) {
|
|
|
|
assert.Nil(t, ValidateResourceGroupName("abc"))
|
|
|
|
assert.Nil(t, ValidateResourceGroupName("_123abc"))
|
|
|
|
assert.Nil(t, ValidateResourceGroupName("abc123_"))
|
|
|
|
|
|
|
|
longName := make([]byte, 256)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
"123abc",
|
|
|
|
"$abc",
|
|
|
|
"abc$",
|
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
assert.NotNil(t, ValidateResourceGroupName(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func TestValidateDatabaseName(t *testing.T) {
|
|
|
|
assert.Nil(t, ValidateDatabaseName("dbname"))
|
|
|
|
assert.Nil(t, ValidateDatabaseName("_123abc"))
|
|
|
|
assert.Nil(t, ValidateDatabaseName("abc123_"))
|
|
|
|
|
|
|
|
longName := make([]byte, 512)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
"123abc",
|
|
|
|
"$abc",
|
|
|
|
"abc$",
|
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
assert.Error(t, ValidateDatabaseName(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:01:31 +08:00
|
|
|
func TestValidatePartitionTag(t *testing.T) {
|
2021-10-23 18:23:44 +08:00
|
|
|
assert.Nil(t, validatePartitionTag("abc", true))
|
|
|
|
assert.Nil(t, validatePartitionTag("123abc", true))
|
|
|
|
assert.Nil(t, validatePartitionTag("_123abc", true))
|
2022-09-08 10:16:34 +08:00
|
|
|
assert.Nil(t, validatePartitionTag("abc123_", true))
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
longName := make([]byte, 256)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
"$abc",
|
2022-09-08 10:16:34 +08:00
|
|
|
"abc$",
|
2020-11-26 16:01:31 +08:00
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
2021-10-23 18:23:44 +08:00
|
|
|
assert.NotNil(t, validatePartitionTag(name, true))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 18:23:44 +08:00
|
|
|
assert.Nil(t, validatePartitionTag("ab cd", false))
|
|
|
|
assert.Nil(t, validatePartitionTag("ab*", false))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateFieldName(t *testing.T) {
|
2021-10-23 18:25:37 +08:00
|
|
|
assert.Nil(t, validateFieldName("abc"))
|
|
|
|
assert.Nil(t, validateFieldName("_123abc"))
|
2022-09-08 10:16:34 +08:00
|
|
|
assert.Nil(t, validateFieldName("abc123_"))
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
longName := make([]byte, 256)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
"123abc",
|
|
|
|
"$abc",
|
2022-09-08 10:16:34 +08:00
|
|
|
"abc$",
|
2020-11-26 16:01:31 +08:00
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
2021-10-23 18:25:37 +08:00
|
|
|
assert.NotNil(t, validateFieldName(name))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateDimension(t *testing.T) {
|
2022-04-29 13:35:49 +08:00
|
|
|
fieldSchema := &schemapb.FieldSchema{
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-04-29 13:35:49 +08:00
|
|
|
Value: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Nil(t, validateDimension(fieldSchema))
|
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-12-07 18:01:19 +08:00
|
|
|
Value: Params.ProxyCfg.MaxDimension.GetValue(),
|
2022-04-29 13:35:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Nil(t, validateDimension(fieldSchema))
|
2020-11-26 16:01:31 +08:00
|
|
|
|
|
|
|
// invalid dim
|
2022-04-29 13:35:49 +08:00
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-04-29 13:35:49 +08:00
|
|
|
Value: "-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NotNil(t, validateDimension(fieldSchema))
|
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-12-07 18:01:19 +08:00
|
|
|
Value: strconv.Itoa(int(Params.ProxyCfg.MaxDimension.GetAsInt32() + 1)),
|
2022-04-29 13:35:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NotNil(t, validateDimension(fieldSchema))
|
|
|
|
|
|
|
|
fieldSchema.DataType = schemapb.DataType_BinaryVector
|
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-04-29 13:35:49 +08:00
|
|
|
Value: "8",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Nil(t, validateDimension(fieldSchema))
|
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-12-07 18:01:19 +08:00
|
|
|
Value: strconv.Itoa(Params.ProxyCfg.MaxDimension.GetAsInt()),
|
2022-04-29 13:35:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Nil(t, validateDimension(fieldSchema))
|
|
|
|
fieldSchema.TypeParams = []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2022-04-29 13:35:49 +08:00
|
|
|
Value: "9",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NotNil(t, validateDimension(fieldSchema))
|
2020-11-26 16:01:31 +08:00
|
|
|
}
|
2020-11-30 19:38:23 +08:00
|
|
|
|
|
|
|
func TestValidateVectorFieldMetricType(t *testing.T) {
|
|
|
|
field1 := &schemapb.FieldSchema{
|
|
|
|
Name: "",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
Description: "",
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_Int64,
|
2020-11-30 19:38:23 +08:00
|
|
|
TypeParams: nil,
|
|
|
|
IndexParams: nil,
|
|
|
|
}
|
2021-10-25 23:32:20 +08:00
|
|
|
assert.Nil(t, validateVectorFieldMetricType(field1))
|
2021-03-12 14:22:09 +08:00
|
|
|
field1.DataType = schemapb.DataType_FloatVector
|
2021-10-25 23:32:20 +08:00
|
|
|
assert.NotNil(t, validateVectorFieldMetricType(field1))
|
2020-11-30 19:38:23 +08:00
|
|
|
field1.IndexParams = []*commonpb.KeyValuePair{
|
2020-12-29 17:19:44 +08:00
|
|
|
{
|
2020-11-30 19:38:23 +08:00
|
|
|
Key: "abcdefg",
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
}
|
2021-10-25 23:32:20 +08:00
|
|
|
assert.NotNil(t, validateVectorFieldMetricType(field1))
|
2020-11-30 19:38:23 +08:00
|
|
|
field1.IndexParams = append(field1.IndexParams, &commonpb.KeyValuePair{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2020-11-30 19:38:23 +08:00
|
|
|
Value: "",
|
|
|
|
})
|
2021-10-25 23:32:20 +08:00
|
|
|
assert.Nil(t, validateVectorFieldMetricType(field1))
|
2020-11-30 19:38:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateDuplicatedFieldName(t *testing.T) {
|
|
|
|
fields := []*schemapb.FieldSchema{
|
|
|
|
{Name: "abc"},
|
|
|
|
{Name: "def"},
|
|
|
|
}
|
2021-10-26 10:38:41 +08:00
|
|
|
assert.Nil(t, validateDuplicatedFieldName(fields))
|
2020-11-30 19:38:23 +08:00
|
|
|
fields = append(fields, &schemapb.FieldSchema{
|
|
|
|
Name: "abc",
|
|
|
|
})
|
2021-10-26 10:38:41 +08:00
|
|
|
assert.NotNil(t, validateDuplicatedFieldName(fields))
|
2020-11-30 19:38:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatePrimaryKey(t *testing.T) {
|
2022-03-14 23:20:02 +08:00
|
|
|
boolField := &schemapb.FieldSchema{
|
|
|
|
Name: "boolField",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_Bool,
|
2020-11-30 19:38:23 +08:00
|
|
|
}
|
2022-03-14 23:20:02 +08:00
|
|
|
|
|
|
|
int64Field := &schemapb.FieldSchema{
|
|
|
|
Name: "int64Field",
|
2020-11-30 19:38:23 +08:00
|
|
|
IsPrimaryKey: false,
|
2022-03-14 23:20:02 +08:00
|
|
|
DataType: schemapb.DataType_Int64,
|
2020-11-30 19:38:23 +08:00
|
|
|
}
|
2022-03-14 23:20:02 +08:00
|
|
|
|
|
|
|
VarCharField := &schemapb.FieldSchema{
|
|
|
|
Name: "VarCharField",
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_VarChar,
|
|
|
|
TypeParams: []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MaxLengthKey,
|
2022-03-14 23:20:02 +08:00
|
|
|
Value: "100",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// test collection without pk field
|
|
|
|
assert.Error(t, validatePrimaryKey(&schemapb.CollectionSchema{
|
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{boolField},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// test collection with int64 field ad pk
|
|
|
|
int64Field.IsPrimaryKey = true
|
|
|
|
assert.Nil(t, validatePrimaryKey(&schemapb.CollectionSchema{
|
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{boolField, int64Field},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// test collection with varChar field as pk
|
|
|
|
VarCharField.IsPrimaryKey = true
|
|
|
|
assert.Nil(t, validatePrimaryKey(&schemapb.CollectionSchema{
|
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{boolField, VarCharField},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// test collection with multi pk field
|
|
|
|
assert.Error(t, validatePrimaryKey(&schemapb.CollectionSchema{
|
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{boolField, int64Field, VarCharField},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// test collection with varChar field as primary and autoID = true
|
|
|
|
VarCharField.AutoID = true
|
2023-06-16 17:00:40 +08:00
|
|
|
assert.Nil(t, validatePrimaryKey(&schemapb.CollectionSchema{
|
2022-03-14 23:20:02 +08:00
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: true,
|
|
|
|
Fields: []*schemapb.FieldSchema{boolField, VarCharField},
|
|
|
|
}))
|
2021-03-08 12:41:46 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 16:57:56 +08:00
|
|
|
func TestValidateFieldType(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
dt schemapb.DataType
|
|
|
|
validate bool
|
|
|
|
}
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Bool,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Int8,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Int16,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Int32,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Int64,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Float,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_Double,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_FloatVector,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_BinaryVector,
|
|
|
|
validate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dt: schemapb.DataType_None,
|
|
|
|
validate: false,
|
|
|
|
},
|
|
|
|
{
|
2022-03-14 23:20:02 +08:00
|
|
|
dt: schemapb.DataType_VarChar,
|
|
|
|
validate: true,
|
2022-03-03 16:57:56 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.dt.String(), func(t *testing.T) {
|
|
|
|
sch := &schemapb.CollectionSchema{
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
DataType: tc.dt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := validateFieldType(sch)
|
|
|
|
if tc.validate {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 12:41:46 +08:00
|
|
|
func TestValidateSchema(t *testing.T) {
|
|
|
|
coll := &schemapb.CollectionSchema{
|
|
|
|
Name: "coll1",
|
|
|
|
Description: "",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: nil,
|
|
|
|
}
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf1 := &schemapb.FieldSchema{
|
|
|
|
Name: "f1",
|
|
|
|
FieldID: 100,
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
Description: "",
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_Int64,
|
2021-03-08 12:41:46 +08:00
|
|
|
TypeParams: nil,
|
|
|
|
IndexParams: nil,
|
|
|
|
}
|
|
|
|
coll.Fields = append(coll.Fields, pf1)
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf1.IsPrimaryKey = true
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
pf1.DataType = schemapb.DataType_Int32
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
pf1.DataType = schemapb.DataType_Int64
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2 := &schemapb.FieldSchema{
|
|
|
|
Name: "f2",
|
|
|
|
FieldID: 101,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
Description: "",
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_Int64,
|
2021-03-08 12:41:46 +08:00
|
|
|
TypeParams: nil,
|
|
|
|
IndexParams: nil,
|
|
|
|
}
|
|
|
|
coll.Fields = append(coll.Fields, pf2)
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2.IsPrimaryKey = false
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2.Name = "f1"
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
pf2.Name = "f2"
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2.FieldID = 100
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2.FieldID = 101
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf2.DataType = -1
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
pf2.DataType = schemapb.DataType_FloatVector
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
pf2.DataType = schemapb.DataType_Int64
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
tp3Good := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "128",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tp3Bad1 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "asdfa",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tp3Bad2 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tp3Bad3 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
|
|
|
Key: "dimX",
|
|
|
|
Value: "128",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tp3Bad4 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "128",
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.DimKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "64",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ip3Good := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "IP",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ip3Bad1 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "JACCARD",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ip3Bad2 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "xxxxxx",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ip3Bad3 := []*commonpb.KeyValuePair{
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "L2",
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 17:41:22 +08:00
|
|
|
Key: common.MetricTypeKey,
|
2021-03-08 12:41:46 +08:00
|
|
|
Value: "IP",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pf3 := &schemapb.FieldSchema{
|
|
|
|
Name: "f3",
|
|
|
|
FieldID: 102,
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
Description: "",
|
2021-03-12 14:22:09 +08:00
|
|
|
DataType: schemapb.DataType_FloatVector,
|
2021-03-08 12:41:46 +08:00
|
|
|
TypeParams: tp3Good,
|
|
|
|
IndexParams: ip3Good,
|
|
|
|
}
|
|
|
|
|
|
|
|
coll.Fields = append(coll.Fields, pf3)
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.TypeParams = tp3Bad1
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.TypeParams = tp3Bad2
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.TypeParams = tp3Bad3
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.TypeParams = tp3Bad4
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.TypeParams = tp3Good
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.IndexParams = ip3Bad1
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.IndexParams = ip3Bad2
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.IndexParams = ip3Bad3
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.NotNil(t, validateSchema(coll))
|
2021-03-08 12:41:46 +08:00
|
|
|
|
|
|
|
pf3.IndexParams = ip3Good
|
2021-10-25 23:56:19 +08:00
|
|
|
assert.Nil(t, validateSchema(coll))
|
2020-11-30 19:38:23 +08:00
|
|
|
}
|
2021-12-06 10:03:34 +08:00
|
|
|
|
|
|
|
func TestValidateMultipleVectorFields(t *testing.T) {
|
|
|
|
// case1, no vector field
|
|
|
|
schema1 := &schemapb.CollectionSchema{}
|
|
|
|
assert.NoError(t, validateMultipleVectorFields(schema1))
|
|
|
|
|
|
|
|
// case2, only one vector field
|
|
|
|
schema2 := &schemapb.CollectionSchema{
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "case2",
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, validateMultipleVectorFields(schema2))
|
|
|
|
|
|
|
|
// case3, multiple vectors
|
|
|
|
schema3 := &schemapb.CollectionSchema{
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "case3_f",
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "case3_b",
|
|
|
|
DataType: schemapb.DataType_BinaryVector,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if enableMultipleVectorFields {
|
|
|
|
assert.NoError(t, validateMultipleVectorFields(schema3))
|
|
|
|
} else {
|
|
|
|
assert.Error(t, validateMultipleVectorFields(schema3))
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 14:27:25 +08:00
|
|
|
|
|
|
|
func TestFillFieldIDBySchema(t *testing.T) {
|
|
|
|
schema := &schemapb.CollectionSchema{}
|
|
|
|
columns := []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
FieldName: "TestFillFieldIDBySchema",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// length mismatch
|
|
|
|
assert.Error(t, fillFieldIDBySchema(columns, schema))
|
|
|
|
schema = &schemapb.CollectionSchema{
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "TestFillFieldIDBySchema",
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
FieldID: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, fillFieldIDBySchema(columns, schema))
|
|
|
|
assert.Equal(t, "TestFillFieldIDBySchema", columns[0].FieldName)
|
|
|
|
assert.Equal(t, schemapb.DataType_Int64, columns[0].Type)
|
|
|
|
assert.Equal(t, int64(1), columns[0].FieldId)
|
|
|
|
}
|
2022-04-11 19:49:34 +08:00
|
|
|
|
|
|
|
func TestValidateUsername(t *testing.T) {
|
|
|
|
// only spaces
|
|
|
|
res := ValidateUsername(" ")
|
|
|
|
assert.Error(t, res)
|
|
|
|
// starts with non-alphabet
|
|
|
|
res = ValidateUsername("1abc")
|
|
|
|
assert.Error(t, res)
|
|
|
|
// length gt 32
|
|
|
|
res = ValidateUsername("aaaaaaaaaabbbbbbbbbbccccccccccddddd")
|
|
|
|
assert.Error(t, res)
|
|
|
|
// illegal character which not alphabet, _, or number
|
|
|
|
res = ValidateUsername("a1^7*).,")
|
|
|
|
assert.Error(t, res)
|
|
|
|
// normal username that only contains alphabet, _, and number
|
|
|
|
res = ValidateUsername("a17_good")
|
|
|
|
assert.Nil(t, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidatePassword(t *testing.T) {
|
|
|
|
// only spaces
|
2022-04-20 13:03:40 +08:00
|
|
|
res := ValidatePassword("")
|
|
|
|
assert.NotNil(t, res)
|
2022-04-11 19:49:34 +08:00
|
|
|
//
|
|
|
|
res = ValidatePassword("1abc")
|
2022-04-20 13:03:40 +08:00
|
|
|
assert.NotNil(t, res)
|
2022-04-11 19:49:34 +08:00
|
|
|
//
|
|
|
|
res = ValidatePassword("a1^7*).,")
|
|
|
|
assert.Nil(t, res)
|
|
|
|
//
|
|
|
|
res = ValidatePassword("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmnnnnnnnnnnnooooooooooppppppppppqqqqqqqqqqrrrrrrrrrrsssssssssstttttttttttuuuuuuuuuuuvvvvvvvvvvwwwwwwwwwwwxxxxxxxxxxyyyyyyyyyzzzzzzzzzzz")
|
|
|
|
assert.Error(t, res)
|
|
|
|
}
|
2022-05-11 09:47:53 +08:00
|
|
|
|
|
|
|
func TestReplaceID2Name(t *testing.T) {
|
|
|
|
srcStr := "collection 432682805904801793 has not been loaded to memory or load failed"
|
|
|
|
dstStr := "collection default_collection has not been loaded to memory or load failed"
|
|
|
|
assert.Equal(t, dstStr, ReplaceID2Name(srcStr, int64(432682805904801793), "default_collection"))
|
|
|
|
}
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
func TestValidateName(t *testing.T) {
|
|
|
|
nameType := "Test"
|
|
|
|
validNames := []string{
|
|
|
|
"abc",
|
|
|
|
"_123abc",
|
|
|
|
}
|
|
|
|
for _, name := range validNames {
|
|
|
|
assert.Nil(t, validateName(name, nameType))
|
|
|
|
assert.Nil(t, ValidateRoleName(name))
|
|
|
|
assert.Nil(t, ValidateObjectName(name))
|
|
|
|
assert.Nil(t, ValidateObjectType(name))
|
|
|
|
assert.Nil(t, ValidatePrincipalName(name))
|
|
|
|
assert.Nil(t, ValidatePrincipalType(name))
|
|
|
|
assert.Nil(t, ValidatePrivilege(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
longName := make([]byte, 256)
|
|
|
|
for i := 0; i < len(longName); i++ {
|
|
|
|
longName[i] = 'a'
|
|
|
|
}
|
|
|
|
invalidNames := []string{
|
|
|
|
" ",
|
|
|
|
"123abc",
|
|
|
|
"$abc",
|
|
|
|
"_12 ac",
|
|
|
|
" ",
|
|
|
|
"",
|
|
|
|
string(longName),
|
|
|
|
"中文",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
assert.NotNil(t, validateName(name, nameType))
|
|
|
|
assert.NotNil(t, ValidateRoleName(name))
|
|
|
|
assert.NotNil(t, ValidateObjectType(name))
|
|
|
|
assert.NotNil(t, ValidatePrincipalName(name))
|
|
|
|
assert.NotNil(t, ValidatePrincipalType(name))
|
|
|
|
assert.NotNil(t, ValidatePrivilege(name))
|
|
|
|
}
|
|
|
|
assert.NotNil(t, ValidateObjectName(" "))
|
|
|
|
assert.NotNil(t, ValidateObjectName(string(longName)))
|
2022-08-15 16:40:48 +08:00
|
|
|
assert.Nil(t, ValidateObjectName("*"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsDefaultRole(t *testing.T) {
|
|
|
|
assert.Equal(t, true, IsDefaultRole(util.RoleAdmin))
|
|
|
|
assert.Equal(t, true, IsDefaultRole(util.RolePublic))
|
|
|
|
assert.Equal(t, false, IsDefaultRole("manager"))
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetContext(ctx context.Context, originValue string) context.Context {
|
|
|
|
authKey := strings.ToLower(util.HeaderAuthorize)
|
|
|
|
authValue := crypto.Base64Encode(originValue)
|
|
|
|
contextMap := map[string]string{
|
|
|
|
authKey: authValue,
|
|
|
|
}
|
|
|
|
md := metadata.New(contextMap)
|
|
|
|
return metadata.NewIncomingContext(ctx, md)
|
|
|
|
}
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func GetContextWithDB(ctx context.Context, originValue string, dbName string) context.Context {
|
|
|
|
authKey := strings.ToLower(util.HeaderAuthorize)
|
|
|
|
authValue := crypto.Base64Encode(originValue)
|
|
|
|
dbKey := strings.ToLower(util.HeaderDBName)
|
|
|
|
contextMap := map[string]string{
|
|
|
|
authKey: authValue,
|
|
|
|
dbKey: dbName,
|
|
|
|
}
|
|
|
|
md := metadata.New(contextMap)
|
|
|
|
return metadata.NewIncomingContext(ctx, md)
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
func TestGetCurUserFromContext(t *testing.T) {
|
|
|
|
_, err := GetCurUserFromContext(context.Background())
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
_, err = GetCurUserFromContext(metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{})))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
_, err = GetCurUserFromContext(GetContext(context.Background(), "123456"))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
root := "root"
|
|
|
|
password := "123456"
|
|
|
|
username, err := GetCurUserFromContext(GetContext(context.Background(), fmt.Sprintf("%s%s%s", root, util.CredentialSeperator, password)))
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Equal(t, "root", username)
|
|
|
|
}
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func TestGetCurDBNameFromContext(t *testing.T) {
|
|
|
|
dbName := GetCurDBNameFromContextOrDefault(context.Background())
|
|
|
|
assert.Equal(t, util.DefaultDBName, dbName)
|
|
|
|
|
|
|
|
dbName = GetCurDBNameFromContextOrDefault(metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{})))
|
|
|
|
assert.Equal(t, util.DefaultDBName, dbName)
|
|
|
|
|
|
|
|
dbNameKey := strings.ToLower(util.HeaderDBName)
|
|
|
|
dbNameValue := "foodb"
|
|
|
|
contextMap := map[string]string{
|
|
|
|
dbNameKey: dbNameValue,
|
|
|
|
}
|
|
|
|
md := metadata.New(contextMap)
|
|
|
|
|
|
|
|
dbName = GetCurDBNameFromContextOrDefault(metadata.NewIncomingContext(context.Background(), md))
|
|
|
|
assert.Equal(t, dbNameValue, dbName)
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:04:34 +08:00
|
|
|
func TestGetRole(t *testing.T) {
|
|
|
|
globalMetaCache = nil
|
|
|
|
_, err := GetRole("foo")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.Error(t, err)
|
2023-06-25 17:20:43 +08:00
|
|
|
mockCache := NewMockCache(t)
|
|
|
|
mockCache.On("GetUserRole",
|
|
|
|
mock.AnythingOfType("string"),
|
|
|
|
).Return(func(username string) []string {
|
|
|
|
if username == "root" {
|
|
|
|
return []string{"role1", "admin", "role2"}
|
|
|
|
}
|
|
|
|
return []string{"role1"}
|
|
|
|
})
|
|
|
|
globalMetaCache = mockCache
|
2022-08-04 11:04:34 +08:00
|
|
|
roles, err := GetRole("root")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Equal(t, 3, len(roles))
|
|
|
|
|
|
|
|
roles, err = GetRole("foo")
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-04 11:04:34 +08:00
|
|
|
assert.Equal(t, 1, len(roles))
|
|
|
|
}
|
2022-08-19 19:42:50 +08:00
|
|
|
|
|
|
|
func TestPasswordVerify(t *testing.T) {
|
|
|
|
username := "user-test00"
|
|
|
|
password := "PasswordVerify"
|
|
|
|
|
|
|
|
// credential does not exist within cache
|
|
|
|
credCache := make(map[string]*internalpb.CredentialInfo, 0)
|
|
|
|
invokedCount := 0
|
|
|
|
|
|
|
|
mockedRootCoord := newMockRootCoord()
|
|
|
|
mockedRootCoord.GetGetCredentialFunc = func(ctx context.Context, req *rootcoordpb.GetCredentialRequest) (*rootcoordpb.GetCredentialResponse, error) {
|
|
|
|
invokedCount++
|
|
|
|
return nil, fmt.Errorf("get cred not found credential")
|
|
|
|
}
|
|
|
|
|
|
|
|
metaCache := &MetaCache{
|
|
|
|
credMap: credCache,
|
|
|
|
rootCoord: mockedRootCoord,
|
|
|
|
}
|
|
|
|
ret, ok := credCache[username]
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Nil(t, ret)
|
|
|
|
assert.False(t, passwordVerify(context.TODO(), username, password, metaCache))
|
|
|
|
assert.Equal(t, 1, invokedCount)
|
|
|
|
|
|
|
|
// Sha256Password has not been filled into cache during establish connection firstly
|
|
|
|
encryptedPwd, err := crypto.PasswordEncrypt(password)
|
2023-06-08 15:36:36 +08:00
|
|
|
assert.NoError(t, err)
|
2022-08-19 19:42:50 +08:00
|
|
|
credCache[username] = &internalpb.CredentialInfo{
|
|
|
|
Username: username,
|
|
|
|
EncryptedPassword: encryptedPwd,
|
|
|
|
}
|
|
|
|
assert.True(t, passwordVerify(context.TODO(), username, password, metaCache))
|
|
|
|
ret, ok = credCache[username]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, ret)
|
|
|
|
assert.Equal(t, username, ret.Username)
|
|
|
|
assert.NotNil(t, ret.Sha256Password)
|
|
|
|
assert.Equal(t, 1, invokedCount)
|
|
|
|
|
|
|
|
// Sha256Password already exists within cache
|
|
|
|
assert.True(t, passwordVerify(context.TODO(), username, password, metaCache))
|
|
|
|
assert.Equal(t, 1, invokedCount)
|
|
|
|
}
|
2022-10-12 15:17:23 +08:00
|
|
|
|
2022-10-21 14:41:28 +08:00
|
|
|
func Test_isCollectionIsLoaded(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
CollectionIDs: []int64{collID, 10, 100},
|
|
|
|
}, nil)
|
2022-10-27 13:05:31 +08:00
|
|
|
loaded, err := isCollectionLoaded(ctx, qc, collID)
|
2022-10-21 14:41:28 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, loaded)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
CollectionIDs: []int64{collID},
|
|
|
|
}, errors.New("error"))
|
2022-10-27 13:05:31 +08:00
|
|
|
loaded, err := isCollectionLoaded(ctx, qc, collID)
|
2022-10-21 14:41:28 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, loaded)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowCollections(mock.Anything, mock.Anything).Return(&querypb.ShowCollectionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "fail reason",
|
|
|
|
},
|
|
|
|
CollectionIDs: []int64{collID},
|
|
|
|
}, nil)
|
2022-10-27 13:05:31 +08:00
|
|
|
loaded, err := isCollectionLoaded(ctx, qc, collID)
|
2022-10-21 14:41:28 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, loaded)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_isPartitionIsLoaded(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
|
|
|
partID := int64(2)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowPartitions(mock.Anything, mock.Anything).Return(&querypb.ShowPartitionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
PartitionIDs: []int64{partID},
|
|
|
|
}, nil)
|
2022-10-21 14:41:28 +08:00
|
|
|
loaded, err := isPartitionLoaded(ctx, qc, collID, []int64{partID})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, loaded)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
|
|
|
partID := int64(2)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowPartitions(mock.Anything, mock.Anything).Return(&querypb.ShowPartitionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
PartitionIDs: []int64{partID},
|
|
|
|
}, errors.New("error"))
|
2022-10-21 14:41:28 +08:00
|
|
|
loaded, err := isPartitionLoaded(ctx, qc, collID, []int64{partID})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, loaded)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail", func(t *testing.T) {
|
|
|
|
collID := int64(1)
|
|
|
|
partID := int64(2)
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := &mocks.MockQueryCoord{}
|
2023-02-16 15:38:34 +08:00
|
|
|
successStatus := &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}
|
|
|
|
qc.EXPECT().LoadCollection(mock.Anything, mock.Anything).Return(successStatus, nil)
|
|
|
|
qc.EXPECT().GetShardLeaders(mock.Anything, mock.Anything).Return(&querypb.GetShardLeadersResponse{
|
|
|
|
Status: successStatus,
|
|
|
|
Shards: []*querypb.ShardLeadersList{
|
|
|
|
{
|
|
|
|
ChannelName: "channel-1",
|
|
|
|
NodeIds: []int64{1, 2, 3},
|
|
|
|
NodeAddrs: []string{"localhost:9000", "localhost:9001", "localhost:9002"},
|
2022-10-21 14:41:28 +08:00
|
|
|
},
|
2023-02-16 15:38:34 +08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
qc.EXPECT().ShowPartitions(mock.Anything, mock.Anything).Return(&querypb.ShowPartitionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "fail reason",
|
|
|
|
},
|
|
|
|
PartitionIDs: []int64{partID},
|
|
|
|
}, nil)
|
2022-10-21 14:41:28 +08:00
|
|
|
loaded, err := isPartitionLoaded(ctx, qc, collID, []int64{partID})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, loaded)
|
|
|
|
})
|
|
|
|
}
|
2022-12-08 18:37:19 +08:00
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
func Test_InsertTaskfillFieldsDataBySchema(t *testing.T) {
|
2022-12-08 18:37:19 +08:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// schema is empty, though won't happen in system
|
|
|
|
case1 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
2023-05-15 16:15:21 +08:00
|
|
|
DbName: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
CollectionName: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
PartitionName: "TestInsertTask_fillFieldsDataBySchema",
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
err = fillFieldsDataBySchema(case1.schema, case1.insertMsg)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
// schema has two fields, msg has no field. fields will be filled in
|
2022-12-08 18:37:19 +08:00
|
|
|
case2 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "a",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "b",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-15 16:15:21 +08:00
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case2.schema, case2.insertMsg)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, len(case2.insertMsg.FieldsData), 2)
|
|
|
|
|
|
|
|
// schema has a pk can't fill in, and another can.
|
|
|
|
case3 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
AutoID: true,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b",
|
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
2023-05-15 16:15:21 +08:00
|
|
|
insertMsg: &BaseInsertTask{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
}
|
2023-05-15 16:15:21 +08:00
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case3.schema, case3.insertMsg)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.Equal(t, nil, err)
|
2023-05-15 16:15:21 +08:00
|
|
|
assert.Equal(t, len(case3.insertMsg.FieldsData), 1)
|
2022-12-08 18:37:19 +08:00
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
// schema has a pk can't fill in, and another can, but pk autoid == false
|
|
|
|
// means that data pass less
|
|
|
|
case4 := insertTask{
|
2022-12-08 18:37:19 +08:00
|
|
|
schema: &schemapb.CollectionSchema{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "a",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b",
|
|
|
|
AutoID: false,
|
2022-12-08 18:37:19 +08:00
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
2023-05-15 16:15:21 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case4.schema, case4.insertMsg)
|
|
|
|
assert.ErrorIs(t, merr.ErrParameterInvalid, err)
|
|
|
|
assert.Equal(t, len(case4.insertMsg.FieldsData), 1)
|
|
|
|
|
|
|
|
// pass more data field
|
|
|
|
case5 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "b",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
2023-05-15 16:15:21 +08:00
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
FieldName: "c",
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-15 16:15:21 +08:00
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case5.schema, case5.insertMsg)
|
|
|
|
assert.ErrorIs(t, merr.ErrParameterInvalid, err)
|
|
|
|
assert.Equal(t, len(case5.insertMsg.FieldsData), 3)
|
|
|
|
|
2023-05-31 20:32:31 +08:00
|
|
|
// duplicate field datas
|
|
|
|
case5.insertMsg.FieldsData = []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
FieldName: "a",
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
FieldName: "a",
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = fillFieldsDataBySchema(case5.schema, case5.insertMsg)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
// not pk, but autoid == true
|
|
|
|
case6 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b",
|
|
|
|
AutoID: true,
|
|
|
|
IsPrimaryKey: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-15 16:15:21 +08:00
|
|
|
err = fillFieldsDataBySchema(case6.schema, case6.insertMsg)
|
|
|
|
assert.ErrorIs(t, merr.ErrParameterInvalid, err)
|
|
|
|
assert.Equal(t, len(case6.insertMsg.FieldsData), 0)
|
|
|
|
|
|
|
|
// more than one pk
|
|
|
|
case7 := insertTask{
|
2022-12-08 18:37:19 +08:00
|
|
|
schema: &schemapb.CollectionSchema{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
2022-12-08 18:37:19 +08:00
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Name: "a",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "b",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-05-15 16:15:21 +08:00
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case7.schema, case7.insertMsg)
|
|
|
|
assert.ErrorIs(t, merr.ErrParameterInvalid, err)
|
|
|
|
assert.Equal(t, len(case7.insertMsg.FieldsData), 0)
|
|
|
|
|
|
|
|
// pk can not set default value
|
|
|
|
case8 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
Description: "TestInsertTask_fillFieldsDataBySchema",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "a",
|
|
|
|
AutoID: false,
|
|
|
|
IsPrimaryKey: true,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
DefaultValue: &schemapb.ValueField{
|
|
|
|
Data: &schemapb.ValueField_LongData{
|
|
|
|
LongData: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fillFieldsDataBySchema(case8.schema, case8.insertMsg)
|
|
|
|
assert.ErrorIs(t, merr.ErrParameterInvalid, err)
|
|
|
|
assert.Equal(t, len(case8.insertMsg.FieldsData), 0)
|
2022-12-08 18:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_InsertTaskCheckPrimaryFieldData(t *testing.T) {
|
|
|
|
// schema is empty, though won't happen in system
|
|
|
|
// num_rows(0) should be greater than 0
|
|
|
|
case1 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
DbName: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
CollectionName: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
PartitionName: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
},
|
|
|
|
},
|
2023-01-17 17:53:42 +08:00
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err := checkPrimaryFieldData(case1.schema, case1.result, case1.insertMsg, true)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// the num of passed fields is less than needed
|
|
|
|
case2 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AutoID: false,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
},
|
2023-03-04 23:21:50 +08:00
|
|
|
Version: msgpb.InsertDataVersion_RowBased,
|
2022-12-08 18:37:19 +08:00
|
|
|
},
|
|
|
|
},
|
2023-01-17 17:53:42 +08:00
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
}
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case2.schema, case2.result, case2.insertMsg, true)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// autoID == false, no primary field schema
|
|
|
|
// primary field is not found
|
|
|
|
case3 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-01-17 17:53:42 +08:00
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
}
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case3.schema, case3.result, case3.insertMsg, true)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// autoID == true, has primary field schema, but primary field data exist
|
|
|
|
// can not assign primary field data when auto id enabled int64Field
|
|
|
|
case4 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestInsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
FieldID: 1,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
FieldID: 2,
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2022-12-08 18:37:19 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
FieldName: "int64Field",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-01-17 17:53:42 +08:00
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
2022-12-08 18:37:19 +08:00
|
|
|
}
|
|
|
|
case4.schema.Fields[0].IsPrimaryKey = true
|
|
|
|
case4.schema.Fields[0].AutoID = true
|
|
|
|
case4.insertMsg.FieldsData[0] = newScalarFieldData(case4.schema.Fields[0], case4.schema.Fields[0].Name, 10)
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case4.schema, case4.result, case4.insertMsg, true)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// autoID == true, has primary field schema, but DataType don't match
|
|
|
|
// the data type of the data and the schema do not match
|
|
|
|
case4.schema.Fields[0].IsPrimaryKey = false
|
|
|
|
case4.schema.Fields[1].IsPrimaryKey = true
|
|
|
|
case4.schema.Fields[1].AutoID = true
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case4.schema, case4.result, case4.insertMsg, true)
|
2022-12-08 18:37:19 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|
2023-01-04 17:21:36 +08:00
|
|
|
|
|
|
|
func Test_UpsertTaskCheckPrimaryFieldData(t *testing.T) {
|
|
|
|
// schema is empty, though won't happen in system
|
|
|
|
// num_rows(0) should be greater than 0
|
|
|
|
case1 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
DbName: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
CollectionName: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
PartitionName: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err := checkPrimaryFieldData(case1.schema, case1.result, case1.insertMsg, false)
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// the num of passed fields is less than needed
|
|
|
|
case2 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
FieldID: 1,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
FieldID: 2,
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
FieldName: "int64Field",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case2.schema, case2.result, case2.insertMsg, false)
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// autoID == false, no primary field schema
|
|
|
|
// primary field is not found
|
|
|
|
case3 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case3.schema, case3.result, case3.insertMsg, false)
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// autoID == true, upsert don't support it
|
|
|
|
case4 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
FieldID: 1,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
FieldID: 2,
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
2023-05-15 16:15:21 +08:00
|
|
|
Type: schemapb.DataType_Float,
|
|
|
|
FieldName: "floatField",
|
2023-01-04 17:21:36 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case4.schema.Fields[0].IsPrimaryKey = true
|
|
|
|
case4.schema.Fields[0].AutoID = true
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case4.schema, case4.result, case4.insertMsg, false)
|
2023-09-15 10:09:21 +08:00
|
|
|
assert.Equal(t, commonpb.ErrorCode_UpsertAutoIDTrue, case4.result.GetStatus().GetErrorCode())
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// primary field data is nil, GetPrimaryFieldData fail
|
|
|
|
case5 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "int64Field",
|
|
|
|
FieldID: 1,
|
|
|
|
DataType: schemapb.DataType_Int64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
FieldID: 2,
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case5.schema.Fields[0].IsPrimaryKey = true
|
|
|
|
case5.schema.Fields[0].AutoID = false
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case5.schema, case5.result, case5.insertMsg, false)
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
// only support DataType Int64 or VarChar as PrimaryField
|
|
|
|
case6 := insertTask{
|
|
|
|
schema: &schemapb.CollectionSchema{
|
|
|
|
Name: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
Description: "TestUpsertTask_checkPrimaryFieldData",
|
|
|
|
AutoID: false,
|
|
|
|
Fields: []*schemapb.FieldSchema{
|
|
|
|
{
|
|
|
|
Name: "floatVectorField",
|
|
|
|
FieldID: 1,
|
|
|
|
DataType: schemapb.DataType_FloatVector,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "floatField",
|
|
|
|
FieldID: 2,
|
|
|
|
DataType: schemapb.DataType_Float,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
insertMsg: &BaseInsertTask{
|
2023-03-04 23:21:50 +08:00
|
|
|
InsertRequest: msgpb.InsertRequest{
|
2023-01-04 17:21:36 +08:00
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_Insert,
|
|
|
|
},
|
|
|
|
RowData: []*commonpb.Blob{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
FieldsData: []*schemapb.FieldData{
|
|
|
|
{
|
|
|
|
Type: schemapb.DataType_FloatVector,
|
|
|
|
FieldName: "floatVectorField",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: schemapb.DataType_Int64,
|
|
|
|
FieldName: "floatField",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case6.schema.Fields[0].IsPrimaryKey = true
|
|
|
|
case6.schema.Fields[0].AutoID = false
|
2023-01-17 17:53:42 +08:00
|
|
|
_, err = checkPrimaryFieldData(case6.schema, case6.result, case6.insertMsg, false)
|
2023-01-04 17:21:36 +08:00
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|
2023-05-30 21:01:29 +08:00
|
|
|
|
|
|
|
func Test_ParseGuaranteeTs(t *testing.T) {
|
|
|
|
strongTs := typeutil.Timestamp(0)
|
|
|
|
boundedTs := typeutil.Timestamp(2)
|
|
|
|
tsNow := tsoutil.GetCurrentTime()
|
|
|
|
tsMax := tsoutil.GetCurrentTime()
|
|
|
|
|
|
|
|
assert.Equal(t, tsMax, parseGuaranteeTs(strongTs, tsMax))
|
|
|
|
ratio := Params.CommonCfg.GracefulTime.GetAsDuration(time.Millisecond)
|
|
|
|
assert.Equal(t, tsoutil.AddPhysicalDurationOnTs(tsMax, -ratio), parseGuaranteeTs(boundedTs, tsMax))
|
|
|
|
assert.Equal(t, tsNow, parseGuaranteeTs(tsNow, tsMax))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ParseGuaranteeTsFromConsistency(t *testing.T) {
|
|
|
|
strong := commonpb.ConsistencyLevel_Strong
|
|
|
|
bounded := commonpb.ConsistencyLevel_Bounded
|
|
|
|
eventually := commonpb.ConsistencyLevel_Eventually
|
|
|
|
session := commonpb.ConsistencyLevel_Session
|
|
|
|
customized := commonpb.ConsistencyLevel_Customized
|
|
|
|
|
|
|
|
tsDefault := typeutil.Timestamp(0)
|
|
|
|
tsEventually := typeutil.Timestamp(1)
|
|
|
|
tsNow := tsoutil.GetCurrentTime()
|
|
|
|
tsMax := tsoutil.GetCurrentTime()
|
|
|
|
|
|
|
|
assert.Equal(t, tsMax, parseGuaranteeTsFromConsistency(tsDefault, tsMax, strong))
|
|
|
|
ratio := Params.CommonCfg.GracefulTime.GetAsDuration(time.Millisecond)
|
|
|
|
assert.Equal(t, tsoutil.AddPhysicalDurationOnTs(tsMax, -ratio), parseGuaranteeTsFromConsistency(tsDefault, tsMax, bounded))
|
|
|
|
assert.Equal(t, tsNow, parseGuaranteeTsFromConsistency(tsNow, tsMax, session))
|
|
|
|
assert.Equal(t, tsNow, parseGuaranteeTsFromConsistency(tsNow, tsMax, customized))
|
|
|
|
assert.Equal(t, tsEventually, parseGuaranteeTsFromConsistency(tsDefault, tsMax, eventually))
|
|
|
|
}
|
2023-06-07 10:38:36 +08:00
|
|
|
|
|
|
|
func Test_NQLimit(t *testing.T) {
|
|
|
|
paramtable.Init()
|
|
|
|
assert.Nil(t, validateNQLimit(16384))
|
|
|
|
assert.Nil(t, validateNQLimit(1))
|
|
|
|
assert.Error(t, validateNQLimit(16385))
|
|
|
|
assert.Error(t, validateNQLimit(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_TopKLimit(t *testing.T) {
|
|
|
|
paramtable.Init()
|
|
|
|
assert.Nil(t, validateTopKLimit(16384))
|
|
|
|
assert.Nil(t, validateTopKLimit(1))
|
|
|
|
assert.Error(t, validateTopKLimit(16385))
|
|
|
|
assert.Error(t, validateTopKLimit(0))
|
|
|
|
}
|
2023-06-15 11:14:39 +08:00
|
|
|
|
2023-06-25 14:42:43 +08:00
|
|
|
func Test_MaxQueryResultWindow(t *testing.T) {
|
|
|
|
paramtable.Init()
|
|
|
|
assert.Nil(t, validateMaxQueryResultWindow(0, 16384))
|
|
|
|
assert.Nil(t, validateMaxQueryResultWindow(0, 1))
|
|
|
|
assert.Error(t, validateMaxQueryResultWindow(0, 16385))
|
|
|
|
assert.Error(t, validateMaxQueryResultWindow(0, 0))
|
|
|
|
assert.Error(t, validateMaxQueryResultWindow(1, 0))
|
|
|
|
}
|
|
|
|
|
2023-06-15 11:14:39 +08:00
|
|
|
func Test_GetPartitionProgressFailed(t *testing.T) {
|
2023-07-14 10:12:31 +08:00
|
|
|
qc := mocks.NewMockQueryCoord(t)
|
2023-06-15 11:14:39 +08:00
|
|
|
qc.EXPECT().ShowPartitions(mock.Anything, mock.Anything).Return(&querypb.ShowPartitionsResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "Unexpected error",
|
|
|
|
},
|
|
|
|
}, nil)
|
2023-08-22 17:06:22 +08:00
|
|
|
_, _, err := getPartitionProgress(context.TODO(), qc, &commonpb.MsgBase{}, []string{}, "", 1, "")
|
2023-06-15 11:14:39 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2023-08-30 10:52:26 +08:00
|
|
|
|
2023-08-30 14:47:00 +08:00
|
|
|
func TestErrWithLog(t *testing.T) {
|
|
|
|
err := errors.New("test")
|
|
|
|
assert.ErrorIs(t, ErrWithLog(nil, "foo", err), err)
|
|
|
|
assert.ErrorIs(t, ErrWithLog(log.Ctx(context.Background()), "foo", err), err)
|
|
|
|
}
|
|
|
|
|
2023-08-30 10:52:26 +08:00
|
|
|
func Test_CheckDynamicFieldData(t *testing.T) {
|
|
|
|
t.Run("normal case", func(t *testing.T) {
|
|
|
|
jsonData := make([][]byte, 0)
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"bool": true,
|
|
|
|
"int": 100,
|
|
|
|
"float": 1.2,
|
|
|
|
"string": "abc",
|
|
|
|
"json": map[string]interface{}{
|
|
|
|
"int": 20,
|
|
|
|
"array": []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
jsonData = append(jsonData, jsonBytes)
|
|
|
|
jsonFieldData := autoGenDynamicFieldData(jsonData)
|
|
|
|
schema := newTestSchema()
|
|
|
|
insertMsg := &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
CollectionName: "collectionName",
|
|
|
|
FieldsData: []*schemapb.FieldData{jsonFieldData},
|
|
|
|
NumRows: 1,
|
|
|
|
Version: msgpb.InsertDataVersion_ColumnBased,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = checkDynamicFieldData(schema, insertMsg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
t.Run("key has $meta", func(t *testing.T) {
|
|
|
|
jsonData := make([][]byte, 0)
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"bool": true,
|
|
|
|
"int": 100,
|
|
|
|
"float": 1.2,
|
|
|
|
"string": "abc",
|
|
|
|
"json": map[string]interface{}{
|
|
|
|
"int": 20,
|
|
|
|
"array": []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
"$meta": "error key",
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
jsonData = append(jsonData, jsonBytes)
|
|
|
|
jsonFieldData := autoGenDynamicFieldData(jsonData)
|
|
|
|
schema := newTestSchema()
|
|
|
|
insertMsg := &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
CollectionName: "collectionName",
|
|
|
|
FieldsData: []*schemapb.FieldData{jsonFieldData},
|
|
|
|
NumRows: 1,
|
|
|
|
Version: msgpb.InsertDataVersion_ColumnBased,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = checkDynamicFieldData(schema, insertMsg)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("disable dynamic schema", func(t *testing.T) {
|
|
|
|
jsonData := make([][]byte, 0)
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"bool": true,
|
|
|
|
"int": 100,
|
|
|
|
"float": 1.2,
|
|
|
|
"string": "abc",
|
|
|
|
"json": map[string]interface{}{
|
|
|
|
"int": 20,
|
|
|
|
"array": []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
jsonData = append(jsonData, jsonBytes)
|
|
|
|
jsonFieldData := autoGenDynamicFieldData(jsonData)
|
|
|
|
schema := newTestSchema()
|
|
|
|
insertMsg := &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
CollectionName: "collectionName",
|
|
|
|
FieldsData: []*schemapb.FieldData{jsonFieldData},
|
|
|
|
NumRows: 1,
|
|
|
|
Version: msgpb.InsertDataVersion_ColumnBased,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
schema.EnableDynamicField = false
|
|
|
|
err = checkDynamicFieldData(schema, insertMsg)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("json data is string", func(t *testing.T) {
|
|
|
|
data := "abcdefg"
|
|
|
|
jsonFieldData := autoGenDynamicFieldData([][]byte{[]byte(data)})
|
|
|
|
schema := newTestSchema()
|
|
|
|
insertMsg := &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
CollectionName: "collectionName",
|
|
|
|
FieldsData: []*schemapb.FieldData{jsonFieldData},
|
|
|
|
NumRows: 1,
|
|
|
|
Version: msgpb.InsertDataVersion_ColumnBased,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := checkDynamicFieldData(schema, insertMsg)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("no json data", func(t *testing.T) {
|
|
|
|
schema := newTestSchema()
|
|
|
|
insertMsg := &msgstream.InsertMsg{
|
|
|
|
InsertRequest: msgpb.InsertRequest{
|
|
|
|
CollectionName: "collectionName",
|
|
|
|
FieldsData: []*schemapb.FieldData{},
|
|
|
|
NumRows: 1,
|
|
|
|
Version: msgpb.InsertDataVersion_ColumnBased,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := checkDynamicFieldData(schema, insertMsg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|