diff --git a/components/configstores/nacos/configstore_test.go b/components/configstores/nacos/configstore_test.go index 439d3e6a71..2e4187f811 100644 --- a/components/configstores/nacos/configstore_test.go +++ b/components/configstores/nacos/configstore_test.go @@ -15,6 +15,7 @@ package nacos import ( "context" + "encoding/json" "fmt" "sync" "testing" @@ -34,6 +35,32 @@ const ( address = "127.0.0.1:8848" ) +//var ( +// group string +// key string +// label string +// content string +// keyWithLabel string +// tags map[string]string +// defaultStore *ConfigStore +// marshal string +//) + +//func TestMain() { +//defaultStore = setup(t, nil) +//key = "key" +//group = "group" +//label = "label" +//content = "content" +//keyWithLabel = defaultStore.concatenateKey(key, label) +//tags = map[string]string{ +// "key1": "value1", +// "key2": "value2", +//} +//bytes, _ := json.Marshal(tags) +//marshal = string(bytes) +//} + func getMockNacosClient(t *testing.T) *mock.MockNacosConfigClient { ctrl := gomock.NewController(t) return mock.NewMockNacosConfigClient(ctrl) @@ -66,86 +93,174 @@ func setup(t *testing.T, client NacosConfigClient) *ConfigStore { } func TestNacosConfigStore_Delete(t *testing.T) { - mockClient := getMockNacosClient(t) - mockClient.EXPECT().DeleteConfig(gomock.Any()).Return(true, nil) - - store := setup(t, mockClient) - params := &configstores.DeleteRequest{ - Group: "group", - Keys: []string{"key"}, - AppId: "test-delete-app", - } - + defaultStore := setup(t, nil) + key, label := "key", "label" + keyWithLabel := defaultStore.concatenateKey(key, label) + group := "group" + appId := "test-delete-app" t.Run("delete success", func(t *testing.T) { - err := store.Delete(context.Background(), params) + params := &configstores.DeleteRequest{ + Group: group, + Keys: []string{key, keyWithLabel}, + AppId: appId, + } + mockClient := getMockNacosClient(t) + + // delete config + mockClient.EXPECT().DeleteConfig(gomock.Eq(vo.ConfigParam{ + DataId: keyWithLabel, + Group: group, + AppName: appId, + })).Return(true, nil).MaxTimes(1) + + mockClient.EXPECT().DeleteConfig(gomock.Eq(vo.ConfigParam{ + DataId: key, + Group: group, + AppName: appId, + })).Return(true, nil).MaxTimes(1) + + // delete tags + mockClient.EXPECT().DeleteConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, keyWithLabel), + Group: defaultStore.tagGroup, + AppName: appId, + })).Return(true, nil).MaxTimes(1) + + mockClient.EXPECT().DeleteConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, key), + Group: defaultStore.tagGroup, + AppName: appId, + })).Return(true, nil).MaxTimes(1) + + nacosStore := setup(t, mockClient) + err := nacosStore.Delete(context.Background(), params) assert.Nil(t, err) }) t.Run("delete without app_id", func(t *testing.T) { - row := params.AppId - params.AppId = "" - err := store.Delete(context.Background(), params) + params := &configstores.DeleteRequest{ + Group: group, + Keys: []string{key, keyWithLabel}, + AppId: "", + } + nacosStore := setup(t, nil) + err := nacosStore.Delete(context.Background(), params) assert.Error(t, err) - params.AppId = row }) t.Run("delete without group", func(t *testing.T) { - row := params.Group - params.Group = "" - err := store.Delete(context.Background(), params) + params := &configstores.DeleteRequest{ + Group: "", + Keys: []string{key, keyWithLabel}, + AppId: appId, + } + + nacosStore := setup(t, nil) + err := nacosStore.Delete(context.Background(), params) assert.Error(t, err) - params.Group = row }) t.Run("delete with empty keys", func(t *testing.T) { - row := params.Keys - params.Keys = []string{} - err := store.Delete(context.Background(), params) + params := &configstores.DeleteRequest{ + Group: group, + Keys: []string{}, + AppId: appId, + } + nacosStore := setup(t, nil) + err := nacosStore.Delete(context.Background(), params) assert.Error(t, err) - params.Keys = row }) } func TestNacosConfigStore_Get(t *testing.T) { + defaultStore := setup(t, nil) + content := "content" + key := "key" + label := "label" + keyWithLabel := defaultStore.concatenateKey(key, label) + group := "group" + tags := map[string]string{ + "key1": "value", + } + marshal, _ := json.Marshal(tags) // Only support get configs from the app_id has been set in store. - t.Run("test get with other app id", func(t *testing.T) { + t.Run("test get with other app id ", func(t *testing.T) { mockClient := getMockNacosClient(t) params := &configstores.GetRequest{ AppId: "test-app1", // different from app stored in the nacos instance - Group: "test-get-group", - Keys: []string{"test-get-key1"}, + Group: group, + Keys: []string{key}, + Label: label, } - content := "content" + // get config content mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, + DataId: keyWithLabel, + Group: group, AppName: appName, // app name that stored in the store instance - })).Return(content, nil) + })).Return(content, nil).MaxTimes(1) + + // get config tags + keyForTag := defaultStore.concatenateKeyForTag(params.Group, keyWithLabel) + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: keyForTag, + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: appName, // app name that stored in the store instance + })).Return(string(marshal), nil).MaxTimes(1) + store := setup(t, mockClient) get, err := store.Get(context.Background(), params) assert.Nil(t, err) - assert.EqualValues(t, content, get[0].Content) + expect := []*configstores.ConfigurationItem{ + { + Key: key, + Label: label, + Content: content, + Group: group, + Tags: tags, + Metadata: nil, + }, + } + assert.EqualValues(t, expect, get) }) t.Run("test success with key level", func(t *testing.T) { mockClient := getMockNacosClient(t) params := &configstores.GetRequest{ - AppId: appName, // different from app stored in the nacos instance - Group: "test-get-group", - Keys: []string{"test-get-key1"}, + //AppId: appName, + Group: group, + Keys: []string{key}, + // without label } - content := "content" mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, + DataId: key, + Group: group, AppName: appName, // app name that stored in the store instance })).Return(content, nil) + + // without tags + keyForTag := defaultStore.concatenateKeyForTag(params.Group, params.Keys[0]) + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: keyForTag, + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: appName, // app name that stored in the store instance + })).Return("", nil).MaxTimes(1) + store := setup(t, mockClient) get, err := store.Get(context.Background(), params) assert.Nil(t, err) - assert.EqualValues(t, content, get[0].Content) + expect := []*configstores.ConfigurationItem{ + { + Key: key, + Label: "", + Content: content, + Group: group, + Tags: nil, + Metadata: nil, + }, + } + assert.EqualValues(t, expect, get) }) t.Run("test success with app level", func(t *testing.T) { @@ -154,37 +269,73 @@ func TestNacosConfigStore_Get(t *testing.T) { AppId: appName, // different from app stored in the nacos instance } - content := "content" mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ Search: "accurate", AppName: appName, // app name that stored in the store instance })).Return(&model.ConfigPage{ PageItems: []model.ConfigItem{ { - DataId: "data_id", - Group: "group", + DataId: keyWithLabel, + Group: group, + Appname: appName, + Content: content, + }, + { + DataId: key, // without label + Group: group, Appname: appName, Content: content, }, }, - }, nil) + }, nil).MaxTimes(1) + + // mock get tags + // get config tags + + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, keyWithLabel), + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: params.AppId, // app name that stored in the store instance + })).Return(string(marshal), nil).MaxTimes(1) + + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, key), // without label + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: params.AppId, // app name that stored in the store instance + })).Return(string(marshal), nil).MaxTimes(1) + store := setup(t, mockClient) get, err := store.Get(context.Background(), params) assert.Nil(t, err) - assert.EqualValues(t, content, get[0].Content) - assert.EqualValues(t, "group", get[0].Group) - assert.EqualValues(t, "data_id", get[0].Key) + expect := []*configstores.ConfigurationItem{ + { + Key: key, + Label: label, + Group: group, + Content: content, + Tags: tags, + Metadata: nil, + }, + { + Key: key, + Label: "", + Group: group, + Content: content, + Tags: tags, + Metadata: nil, + }, + } + + assert.EqualValues(t, expect, get) }) t.Run("test success with group level", func(t *testing.T) { mockClient := getMockNacosClient(t) params := &configstores.GetRequest{ - AppId: appName, // different from app stored in the nacos instance - Group: "test-get-group", - //Keys: []string{"test-get-key1"}, // without keys + AppId: appName, + Group: group, } - content := "content" mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ Search: "accurate", AppName: appName, // app name that stored in the store instance @@ -192,19 +343,57 @@ func TestNacosConfigStore_Get(t *testing.T) { })).Return(&model.ConfigPage{ PageItems: []model.ConfigItem{ { - DataId: "data_id", + DataId: keyWithLabel, + Group: params.Group, + Appname: appName, + Content: content, + }, + { + DataId: key, // without label Group: params.Group, Appname: appName, Content: content, }, }, - }, nil) + }, nil).MaxTimes(1) + + // mock get tags + // get config tags + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(params.Group, keyWithLabel), + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: params.AppId, // app name that stored in the store instance + })).Return(string(marshal), nil).MaxTimes(1) + + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(params.Group, key), // without label + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: params.AppId, // app name that stored in the store instance + })).Return(string(marshal), nil).MaxTimes(1) + store := setup(t, mockClient) get, err := store.Get(context.Background(), params) assert.Nil(t, err) - assert.EqualValues(t, content, get[0].Content) - assert.EqualValues(t, params.Group, get[0].Group) - assert.EqualValues(t, "data_id", get[0].Key) + expect := []*configstores.ConfigurationItem{ + { + Key: key, + Label: label, + Group: group, + Content: content, + Tags: tags, + Metadata: nil, + }, + { + Key: key, + Label: "", + Group: group, + Content: content, + Tags: tags, + Metadata: nil, + }, + } + + assert.EqualValues(t, expect, get) }) t.Run("test success with illegal params", func(t *testing.T) { @@ -212,19 +401,38 @@ func TestNacosConfigStore_Get(t *testing.T) { params := &configstores.GetRequest{ AppId: appName, // different from app stored in the nacos instance //Group: "test-get-group", // without group - Keys: []string{"test-get-key1"}, + Keys: []string{key}, + // without label } - content := "content" mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ DataId: params.Keys[0], Group: defaultGroup, // use default group AppName: appName, // app name that stored in the store instance - })).Return(content, nil) + })).Return(content, nil).MaxTimes(1) + + // without tags + keyForTag := defaultStore.concatenateKeyForTag(defaultGroup, params.Keys[0]) + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: keyForTag, + Group: defaultStore.tagGroup, // use the tag group to store the tag info. + AppName: appName, // app name that stored in the store instance + })).Return("", nil).MaxTimes(1) + store := setup(t, mockClient) get, err := store.Get(context.Background(), params) assert.Nil(t, err) - assert.EqualValues(t, content, get[0].Content) + expect := []*configstores.ConfigurationItem{ + { + Key: key, + Group: defaultGroup, + Content: content, + Label: "", + Tags: nil, + Metadata: nil, + }, + } + assert.EqualValues(t, expect, get) }) } @@ -249,7 +457,6 @@ func TestNacosConfigStore_Init(t *testing.T) { timeout = "10" // seconds ) - store := NewStore() // test success without all params config := &configstores.StoreConfig{} config.Metadata = map[string]string{ @@ -261,6 +468,7 @@ func TestNacosConfigStore_Init(t *testing.T) { config.TimeOut = timeout t.Run("test success", func(t *testing.T) { + store := NewStore() err := store.Init(config) assert.Nil(t, err) // check config params @@ -319,56 +527,170 @@ func TestNacosConfigStore_Init(t *testing.T) { } func TestNacosConfigStore_Set(t *testing.T) { - mockClient := getMockNacosClient(t) - mockClient.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) - - store := setup(t, mockClient) - params := &configstores.SetRequest{ - AppId: "test-set-app", - Items: []*configstores.ConfigurationItem{ - { - Group: "test-set-group", - Content: "content", - Key: "test-set-key", - }, - }, + defaultStore := setup(t, nil) + key := "test-set-key" + label := "label" + group := "test-set-group" + content := "content" + appId := "test-set-app" + tags := map[string]string{ + "key1": "value1", + "key2": "value2", } + marshal, _ := json.Marshal(tags) t.Run("set success", func(t *testing.T) { + params := &configstores.SetRequest{ + AppId: appId, + Items: []*configstores.ConfigurationItem{ + { + Group: group, + Content: content, + Key: key, + Label: label, + Tags: tags, + Metadata: nil, + }, + { + Group: group, + Content: content, + Key: key, + Label: "", // without label + Tags: tags, + Metadata: nil, + }, + }, + } + mockClient := getMockNacosClient(t) + // mock set config content + keyWithLabel := defaultStore.concatenateKey(key, label) + mockClient.EXPECT().PublishConfig(gomock.Eq(vo.ConfigParam{ + DataId: keyWithLabel, + Group: group, + AppName: appId, + Content: content, + })).Return(true, nil).MaxTimes(1) + + mockClient.EXPECT().PublishConfig(gomock.Eq(vo.ConfigParam{ + DataId: key, + Group: group, + AppName: appId, + Content: content, + })).Return(true, nil).MaxTimes(1) + + // mock set config tags + mockClient.EXPECT().PublishConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, keyWithLabel), + Group: defaultStore.tagGroup, + AppName: appId, + Content: string(marshal), + })).Return(true, nil).MaxTimes(1) + + mockClient.EXPECT().PublishConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, key), + Group: defaultStore.tagGroup, + AppName: appId, + Content: string(marshal), + })).Return(true, nil).MaxTimes(1) + + store := setup(t, mockClient) err := store.Set(context.Background(), params) assert.Nil(t, err) }) t.Run("set without app_id", func(t *testing.T) { - row := params.AppId - params.AppId = "" + params := &configstores.SetRequest{ + AppId: "", + Items: []*configstores.ConfigurationItem{ + { + Group: group, + Content: content, + Key: key, + Label: label, + Tags: tags, + Metadata: nil, + }, + }, + } + store := setup(t, nil) err := store.Set(context.Background(), params) - assert.Error(t, err) - params.AppId = row + assert.EqualValues(t, errParamsMissingField("AppId"), err) }) t.Run("set with empty items", func(t *testing.T) { - row := params.Items - params.Items = nil + params := &configstores.SetRequest{ + AppId: appId, + Items: []*configstores.ConfigurationItem{}, + } + store := setup(t, nil) err := store.Set(context.Background(), params) - assert.Error(t, err) - params.Items = row + assert.EqualValues(t, errParamsMissingField("Items"), err) }) t.Run("set without group", func(t *testing.T) { - row := params.Items[0].Group - params.Items[0].Group = "" + params := &configstores.SetRequest{ + AppId: appId, + Items: []*configstores.ConfigurationItem{ + { + Group: "", + Content: content, + Key: key, + Label: label, + Tags: tags, + Metadata: nil, + }, + }, + } + store := setup(t, nil) err := store.Set(context.Background(), params) - assert.Error(t, err) - params.Items[0].Group = row + assert.EqualValues(t, errParamsMissingField("Group"), err) + }) + + t.Run("key contains delimiter", func(t *testing.T) { + params := &configstores.SetRequest{ + AppId: appId, + Items: []*configstores.ConfigurationItem{ + { + Group: group, + Content: content, + Key: key + defaultStore.delimiter, + Label: label, + Tags: tags, + Metadata: nil, + }, + }, + } + store := setup(t, nil) + err := store.Set(context.Background(), params) + assert.EqualValues(t, InvalidKey, err) + }) + + t.Run("label contains delimiter", func(t *testing.T) { + params := &configstores.SetRequest{ + AppId: appId, + Items: []*configstores.ConfigurationItem{ + { + Group: group, + Content: content, + Key: key, + Label: label + defaultStore.delimiter, + Tags: tags, + Metadata: nil, + }, + }, + } + store := setup(t, nil) + err := store.Set(context.Background(), params) + assert.EqualValues(t, InvalidLabel, err) }) } func TestNacosConfigStore_StopSubscribe(t *testing.T) { + defaultStore := setup(t, nil) req := &configstores.SubscribeReq{ AppId: appName, Group: "test-stop-subscribe-group", - Keys: []string{"1", "2", "3"}, + Keys: []string{"1", "2", "3", defaultStore.concatenateKey("key", "label")}, } client := getMockNacosClient(t) @@ -415,197 +737,161 @@ func (c *configParamMatcher) String() string { } func TestNacosConfigStore_Subscribe(t *testing.T) { - // Only support get configs from the app_id has been set in store. - t.Run("test subscribe with other app id", func(t *testing.T) { - mockClient := getMockNacosClient(t) - params := &configstores.SubscribeReq{ - AppId: "test-app1", // different from app stored in the nacos instance - Group: "test-get-group", - Keys: []string{"test-get-key1"}, - } - - ch := make(chan *configstores.SubscribeResp) - content := "content" - mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, - AppName: appName, // app name that stored in the store instance - })).Return(content, nil) - - mockClient.EXPECT().ListenConfig(EqConfigParam(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, - AppName: appName, // app name that stored in the store instance - OnChange: nil, // Ignore the impact of the OnChange function - })).Return(nil) - store := setup(t, mockClient) - err := store.Subscribe(params, ch) - assert.Nil(t, err) - }) + defaultStore := setup(t, nil) + key := "key" + group := "group" + label := "label" + content := "content" + keyWithLabel := defaultStore.concatenateKey(key, label) + tags := map[string]string{ + "key1": "value1", + "key2": "value2", + } + marshal, _ := json.Marshal(tags) + // The testing of group level and app level is reflected in the Get single test t.Run("test success with key level", func(t *testing.T) { mockClient := getMockNacosClient(t) params := &configstores.SubscribeReq{ - AppId: "test-app1", // different from app stored in the nacos instance - Group: "test-get-group", - Keys: []string{"test-get-key1"}, + //AppId: "test-app1", + Group: group, + Keys: []string{key}, + Label: label, + Metadata: nil, } ch := make(chan *configstores.SubscribeResp) - content := "content" mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, + DataId: keyWithLabel, + Group: group, AppName: appName, // app name that stored in the store instance - })).Return(content, nil) + })).Return(content, nil).MaxTimes(1) mockClient.EXPECT().ListenConfig(EqConfigParam(vo.ConfigParam{ - DataId: params.Keys[0], - Group: params.Group, + DataId: keyWithLabel, + Group: group, AppName: appName, // app name that stored in the store instance OnChange: nil, // Ignore the impact of the OnChange function - })).Return(nil) - store := setup(t, mockClient) - err := store.Subscribe(params, ch) - assert.Nil(t, err) - }) - - t.Run("test success with app level", func(t *testing.T) { - mockClient := getMockNacosClient(t) - params := &configstores.SubscribeReq{ - AppId: appName, // different from app stored in the nacos instance - //Group: "test-get-group", // without group - //Keys: []string{"test-get-key1"}, // without keys - } + })).Return(nil).MaxTimes(1) - mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ - Search: "accurate", - AppName: appName, // app name that stored in the store instance - })).Return(&model.ConfigPage{ - PageItems: []model.ConfigItem{ - { - DataId: "data_id", - Group: "group", - Appname: appName, - Content: "content", - }, - }, - }, nil) - - ch := make(chan *configstores.SubscribeResp) - mockClient.EXPECT().ListenConfig(EqConfigParam(vo.ConfigParam{ - DataId: "data_id", - Group: "group", - AppName: appName, // app name that stored in the store instance - OnChange: nil, // Ignore the impact of the OnChange function - })).Return(nil) store := setup(t, mockClient) err := store.Subscribe(params, ch) assert.Nil(t, err) }) - t.Run("test success with group level", func(t *testing.T) { + t.Run("test default subscribe", func(t *testing.T) { mockClient := getMockNacosClient(t) - params := &configstores.SubscribeReq{ - AppId: appName, // different from app stored in the nacos instance - Group: "test-get-group", - //Keys: []string{"test-get-key1"}, // without keys - } - - mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ - Search: "accurate", + mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ + DataId: defaultStore.concatenateKeyForTag(group, keyWithLabel), + Group: defaultStore.tagGroup, AppName: appName, // app name that stored in the store instance - Group: params.Group, - })).Return(&model.ConfigPage{ - PageItems: []model.ConfigItem{ - { - DataId: "data_id", - Group: params.Group, - Appname: appName, - Content: "content", - }, - }, - }, nil) + })).Return(string(marshal), nil).MaxTimes(1) - ch := make(chan *configstores.SubscribeResp) - mockClient.EXPECT().ListenConfig(EqConfigParam(vo.ConfigParam{ - DataId: "data_id", - Group: params.Group, - AppName: appName, // app name that stored in the store instance - OnChange: nil, // Ignore the impact of the OnChange function - })).Return(nil) - store := setup(t, mockClient) - err := store.Subscribe(params, ch) - assert.Nil(t, err) - }) - - t.Run("test success with illegal params", func(t *testing.T) { - mockClient := getMockNacosClient(t) - params := &configstores.SubscribeReq{ - AppId: appName, // different from app stored in the nacos instance - //Group: "test-get-group", // without group - Keys: []string{"test-get- key1"}, - } - - ch := make(chan *configstores.SubscribeResp) - content := "content" mockClient.EXPECT().GetConfig(gomock.Eq(vo.ConfigParam{ - DataId: params.Keys[0], - Group: defaultGroup, + DataId: defaultStore.concatenateKeyForTag(group, key), + Group: defaultStore.tagGroup, AppName: appName, // app name that stored in the store instance - })).Return(content, nil) + })).Return(string(marshal), nil).MaxTimes(1) - mockClient.EXPECT().ListenConfig(EqConfigParam(vo.ConfigParam{ - DataId: params.Keys[0], - Group: defaultGroup, - AppName: appName, // app name that stored in the store instance - OnChange: nil, // Ignore the impact of the OnChange function - })).Return(nil) store := setup(t, mockClient) - err := store.Subscribe(params, ch) - assert.Nil(t, err) - }) - - t.Run("test default subscribe", func(t *testing.T) { - store := setup(t, nil) ch := make(chan *configstores.SubscribeResp, 2) fn := store.subscribeOnChange(ch) - expected := &configstores.SubscribeResp{ - StoreName: store.storeName, - AppId: store.appName, - Items: []*configstores.ConfigurationItem{ - { - Key: "data_id", - Content: "content", - Group: "group", - }, - }, - } var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() - i := 0 - for v := range ch { - i++ - assert.EqualValues(t, expected, v) + v := <-ch + expected := &configstores.SubscribeResp{ + StoreName: store.storeName, + AppId: store.appName, + Items: []*configstores.ConfigurationItem{ + { + Key: key, + Content: content, + Group: group, + Label: label, + Tags: tags, + }, + }, } - assert.EqualValues(t, i, 3) + assert.EqualValues(t, expected.StoreName, v.StoreName) + assert.EqualValues(t, expected.AppId, v.AppId) + assert.EqualValues(t, 1, len(expected.Items)) + assert.EqualValues(t, len(expected.Items), len(v.Items)) + assert.EqualValues(t, expected.Items[0], v.Items[0]) + v = <-ch + expected = &configstores.SubscribeResp{ + StoreName: store.storeName, + AppId: store.appName, + Items: []*configstores.ConfigurationItem{ + { + Key: key, + Content: content, + Group: group, + Label: "", + Tags: tags, + }, + }, + } + assert.EqualValues(t, expected.StoreName, v.StoreName) + assert.EqualValues(t, expected.AppId, v.AppId) + assert.EqualValues(t, 1, len(expected.Items)) + assert.EqualValues(t, len(expected.Items), len(v.Items)) + assert.EqualValues(t, expected.Items[0], v.Items[0]) + _, ok := <-ch // close + assert.False(t, ok) }() - fn(store.namespaceId, "group", "data_id", "content") - fn(store.namespaceId, "group", "data_id", "content") - fn(store.namespaceId, "group", "data_id", "content") + fn(store.namespaceId, group, keyWithLabel, content) + fn(store.namespaceId, group, key, content) close(ch) wg.Wait() }) - - t.Run("test ", func(t *testing.T) { - - }) } func TestNewStore(t *testing.T) { store := NewStore() + nacosStore := store.(*ConfigStore) assert.NotNil(t, store) + assert.EqualValues(t, defaultDelimiter, nacosStore.delimiter) + assert.EqualValues(t, defaultTagGroup, nacosStore.tagGroup) +} + +func TestConcatenateKey(t *testing.T) { + store := setup(t, nil) + t.Run("test with label", func(t *testing.T) { + k, l := "key", "label" + keyWithLabel := store.concatenateKey(k, l) + assert.EqualValues(t, k+store.delimiter+l, keyWithLabel) + key, label := store.splitKey(keyWithLabel) + assert.EqualValues(t, k, key) + assert.EqualValues(t, l, label) + g := "group" + keyForTag := store.concatenateKeyForTag(g, keyWithLabel) + assert.EqualValues(t, g+store.delimiter+keyWithLabel, keyForTag) + }) + + t.Run("test without label", func(t *testing.T) { + k, l := "key", "" // empty label + keyWithLabel := store.concatenateKey(k, l) + assert.EqualValues(t, k, keyWithLabel) + key, label := store.splitKey(keyWithLabel) + assert.EqualValues(t, k, key) + assert.EqualValues(t, l, label) + g := "group" + keyForTag := store.concatenateKeyForTag(g, keyWithLabel) + assert.EqualValues(t, g+store.delimiter+keyWithLabel, keyForTag) + }) + + t.Run("test without label and key", func(t *testing.T) { + k, l := "", "" + keyWithLabel := store.concatenateKey(k, l) + assert.EqualValues(t, k, keyWithLabel) + key, label := store.splitKey(keyWithLabel) + assert.EqualValues(t, k, key) + assert.EqualValues(t, l, label) + g := "group" + keyForTag := store.concatenateKeyForTag(g, keyWithLabel) + assert.EqualValues(t, keyWithLabel, keyForTag) + }) }