From a16f6106dc7c0a158d2da9f3940143859e53a44e Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Mon, 26 Jul 2021 17:48:30 +0300 Subject: [PATCH] Started tested health indexes for the missing/broken cases --- engine/datamanager.go | 2 +- engine/z_libindex_health_test.go | 113 +++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/engine/datamanager.go b/engine/datamanager.go index d8215e61f..0f9d8be53 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -3103,7 +3103,7 @@ func (dm *DataManager) GetIndexes(idxItmType, tntCtx, idxKey string, err = utils.CastRPCErr(err) if err == utils.ErrNotFound && cacheWrite && idxKey != utils.EmptyString && dm.dataDB.GetStorageType() != utils.INTERNAL { if errCh := Cache.Set(idxItmType, utils.ConcatenatedKey(tntCtx, idxKey), nil, []string{tntCtx}, - true, utils.NonTransactional); errCh != nil { + true, utils.NonTransactional); errCh != nil { return nil, errCh } } diff --git a/engine/z_libindex_health_test.go b/engine/z_libindex_health_test.go index 7d266ae21..5cc55025f 100644 --- a/engine/z_libindex_health_test.go +++ b/engine/z_libindex_health_test.go @@ -388,3 +388,116 @@ func TestHealthReverseFilter(t *testing.T) { } } } + +func TestHealthIndexThreshold(t *testing.T) { + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + // we will set this threshold but without indexing + thPrf := &ThresholdProfileWithAPIOpts{ + ThresholdProfile: &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TestHealthIndexThreshold", + FilterIDs: []string{"*string:~*opts.*eventType:AccountUpdate", + "*string:~*asm.ID:1002", + "*suffix:BrokenFilter:Invalid"}, + MaxHits: 1, + }, + } + if err := dm.SetThresholdProfile(thPrf.ThresholdProfile, false); err != nil { + t.Error(err) + } + + args := &IndexHealthArgsWith3Ch{} + exp := &FilterIHReply{ + MissingIndexes: map[string][]string{ + "cgrates.org:*string:*asm.ID:1002": {"TestHealthIndexThreshold"}, + "cgrates.org:*string:*opts.*eventType:AccountUpdate": {"TestHealthIndexThreshold"}, + }, + BrokenIndexes: map[string][]string{}, + MissingFilters: map[string][]string{}, + } + if rply, err := GetFltrIdxHealth(dm, + ltcache.NewCache(args.FilterCacheLimit, args.FilterCacheTTL, args.FilterCacheStaticTTL, nil), + ltcache.NewCache(args.IndexCacheLimit, args.IndexCacheTTL, args.IndexCacheStaticTTL, nil), + ltcache.NewCache(args.ObjectCacheLimit, args.ObjectCacheTTL, args.ObjectCacheStaticTTL, nil), + utils.CacheThresholdFilterIndexes); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, rply) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(exp), utils.ToJSON(rply)) + } + + indexes := map[string]utils.StringSet{ + "*prefix:req.InvalidIdx:10": { // obj exist but the index don't + "TestHealthIndexThreshold": {}, + }, + "*string:*req.Destination:123": { // index is valid but the obj does not exist + "InexistingThreshold": {}, + }, + } + + // we will set manually some indexes that points to an nil object or index is valid but the obj is missing + if err := dm.SetIndexes(utils.CacheThresholdFilterIndexes, "cgrates.org", + indexes, true, utils.NonTransactional); err != nil { + t.Error(err) + } + exp = &FilterIHReply{ + MissingObjects: []string{"cgrates.org:InexistingThreshold"}, + MissingIndexes: map[string][]string{ + "cgrates.org:*string:*asm.ID:1002": {"TestHealthIndexThreshold"}, + "cgrates.org:*string:*opts.*eventType:AccountUpdate": {"TestHealthIndexThreshold"}, + }, + BrokenIndexes: map[string][]string{ + "cgrates.org:*prefix:req.InvalidIdx:10": {"TestHealthIndexThreshold"}, + }, + MissingFilters: map[string][]string{}, + } + if rply, err := GetFltrIdxHealth(dm, + ltcache.NewCache(args.FilterCacheLimit, args.FilterCacheTTL, args.FilterCacheStaticTTL, nil), + ltcache.NewCache(args.IndexCacheLimit, args.IndexCacheTTL, args.IndexCacheStaticTTL, nil), + ltcache.NewCache(args.ObjectCacheLimit, args.ObjectCacheTTL, args.ObjectCacheStaticTTL, nil), + utils.CacheThresholdFilterIndexes); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, rply) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(exp), utils.ToJSON(rply)) + } + + //we will use an inexisting Filter(not inline) for the same ThresholdProfile + thPrf = &ThresholdProfileWithAPIOpts{ + ThresholdProfile: &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TestHealthIndexThreshold", + FilterIDs: []string{"*string:~*opts.*eventType:AccountUpdate", + "*string:~*asm.ID:1002", + "FLTR_1_DOES_NOT_EXIST"}, + MaxHits: 1, + }, + } + if err := dm.SetThresholdProfile(thPrf.ThresholdProfile, false); err != nil { + t.Error(err) + } + exp = &FilterIHReply{ + MissingObjects: []string{"cgrates.org:InexistingThreshold"}, + MissingIndexes: map[string][]string{ + "cgrates.org:*string:*asm.ID:1002": {"TestHealthIndexThreshold"}, + "cgrates.org:*string:*opts.*eventType:AccountUpdate": {"TestHealthIndexThreshold"}, + }, + BrokenIndexes: map[string][]string{ + "cgrates.org:*prefix:req.InvalidIdx:10": {"TestHealthIndexThreshold"}, + }, + MissingFilters: map[string][]string{ + "cgrates.org:FLTR_1_DOES_NOT_EXIST": {"TestHealthIndexThreshold"}, + }, + } + if rply, err := GetFltrIdxHealth(dm, + ltcache.NewCache(args.FilterCacheLimit, args.FilterCacheTTL, args.FilterCacheStaticTTL, nil), + ltcache.NewCache(args.IndexCacheLimit, args.IndexCacheTTL, args.IndexCacheStaticTTL, nil), + ltcache.NewCache(args.ObjectCacheLimit, args.ObjectCacheTTL, args.ObjectCacheStaticTTL, nil), + utils.CacheThresholdFilterIndexes); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, rply) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(exp), utils.ToJSON(rply)) + } +}