github.com/GuanceCloud/cliutils@v1.1.21/pipeline/manager/scriptstore_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package manager
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  	"sync"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/GuanceCloud/cliutils/point"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func whichStore(c *Manager, cat point.Category) *ScriptStore {
    21  	v, _ := c.whichStore(cat)
    22  	return v
    23  }
    24  
    25  func TestScriptLoadFunc(t *testing.T) {
    26  	m := NewManager(ManagerCfg{})
    27  	case1 := map[point.Category]map[string]string{
    28  		point.Logging: {
    29  			"abcd": "if true {}",
    30  		},
    31  		point.Metric: {
    32  			"abc": "if true {}",
    33  			"def": "if true {}",
    34  		},
    35  	}
    36  
    37  	m.LoadScripts(NSDefault, nil, nil)
    38  	m.LoadScripts(NSGitRepo, nil, nil)
    39  	m.LoadScripts(NSRemote, nil, nil)
    40  
    41  	m.LoadScripts(NSDefault, case1, nil)
    42  	for category, v := range case1 {
    43  		for name := range v {
    44  			if y, ok := m.QueryScript(category, name); !ok {
    45  				t.Error(category, " ", name, y)
    46  				if y, ok := m.QueryScript(category, name); !ok {
    47  					t.Error(y)
    48  				}
    49  			}
    50  		}
    51  	}
    52  
    53  	m.LoadScripts(NSDefault, nil, nil)
    54  	m.LoadScripts(NSGitRepo, nil, nil)
    55  	m.LoadScripts(NSRemote, nil, nil)
    56  	for k, v := range case1 {
    57  		m.LoadScriptWithCat(k, NSDefault, v, nil)
    58  	}
    59  	for category, v := range case1 {
    60  		for name := range v {
    61  			if _, ok := m.QueryScript(category, name); !ok {
    62  				t.Error(category, " ", name)
    63  			}
    64  		}
    65  	}
    66  
    67  	m.LoadScripts(NSDefault, nil, nil)
    68  	m.LoadScripts(NSGitRepo, nil, nil)
    69  	m.LoadScripts(NSRemote, nil, nil)
    70  	for category, v := range case1 {
    71  		for name := range v {
    72  			if _, ok := m.QueryScript(category, name); ok {
    73  				t.Error(category, " ", name)
    74  			}
    75  		}
    76  	}
    77  
    78  	m.LoadScripts(NSDefault, nil, nil)
    79  	m.LoadScripts(NSGitRepo, nil, nil)
    80  	m.LoadScripts(NSRemote, nil, nil)
    81  
    82  	for k, v := range case1 {
    83  		m.LoadScriptWithCat(k, "DefaultScriptNS", v, nil)
    84  		whichStore(m, k).UpdateScriptsWithNS(NSRemote, v, nil)
    85  	}
    86  	for category, v := range case1 {
    87  		for name := range v {
    88  			if s, ok := m.QueryScript(category, name); !ok || s.NS() != NSRemote {
    89  				t.Error(category, " ", name)
    90  			}
    91  		}
    92  	}
    93  
    94  	m.LoadScripts(NSDefault, nil, nil)
    95  	m.LoadScripts(NSGitRepo, nil, nil)
    96  	m.LoadScripts(NSRemote, nil, nil)
    97  
    98  	_ = os.WriteFile("/tmp/nginx-time123.p", []byte(`
    99  		json(_, time)
   100  		set_tag(bb, "aa0")
   101  		default_time(time)
   102  		`), os.FileMode(0o755))
   103  	ss, _ := ReadScripts("/tmp")
   104  	whichStore(m, point.Logging).UpdateScriptsWithNS(
   105  		NSDefault, ss, nil)
   106  	_ = os.Remove("/tmp/nginx-time123.p")
   107  }
   108  
   109  func TestCmpCategory(t *testing.T) {
   110  	cats := map[point.Category]struct{}{}
   111  	for _, k := range point.AllCategories() {
   112  		if k == point.DynamicDWCategory {
   113  			continue
   114  		}
   115  		cats[k] = struct{}{}
   116  	}
   117  
   118  	assert.Equal(t, cats, func() map[point.Category]struct{} {
   119  		ret := map[point.Category]struct{}{}
   120  		for k := range CategoryDirName() {
   121  			ret[k] = struct{}{}
   122  		}
   123  		return ret
   124  	}())
   125  }
   126  
   127  func BenchmarkIndexMap(b *testing.B) {
   128  	b.Run("sync.Map", func(b *testing.B) {
   129  		type cachemap struct {
   130  			m sync.Map
   131  		}
   132  
   133  		m := cachemap{}
   134  		m.m.Store("abc.p", &PlScript{})
   135  		m.m.Store("def.p", &PlScript{})
   136  
   137  		var x1, x2, x3 *PlScript
   138  		for i := 0; i < b.N; i++ {
   139  			if v, ok := m.m.Load("abc.p"); ok {
   140  				x1 = v.(*PlScript)
   141  			}
   142  			if v, ok := m.m.Load("def.p"); ok {
   143  				x2 = v.(*PlScript)
   144  			}
   145  			if v, ok := m.m.Load("ddd"); ok {
   146  				x3 = v.(*PlScript)
   147  			}
   148  		}
   149  		b.Log(x1, x2, x3, false)
   150  	})
   151  
   152  	b.Run("map", func(b *testing.B) {
   153  		type cachemap struct {
   154  			m     map[string]*PlScript
   155  			mlock sync.RWMutex
   156  		}
   157  
   158  		m := cachemap{
   159  			m: map[string]*PlScript{
   160  				"abc.p": {},
   161  				"def.p": {},
   162  			},
   163  		}
   164  
   165  		var x1, x2, x3 *PlScript
   166  		var ok bool
   167  		for i := 0; i < b.N; i++ {
   168  			m.mlock.RLock()
   169  			x1, ok = m.m["abc.p"]
   170  			if !ok {
   171  				b.Log()
   172  			}
   173  			m.mlock.RUnlock()
   174  
   175  			m.mlock.RLock()
   176  			x2, ok = m.m["def.p"]
   177  			if !ok {
   178  				b.Log()
   179  			}
   180  			m.mlock.RUnlock()
   181  
   182  			m.mlock.RLock()
   183  			x3, ok = m.m["ddd"]
   184  			if ok {
   185  				b.Log()
   186  			}
   187  			m.mlock.RUnlock()
   188  		}
   189  		b.Log(x1, x2, x3, ok)
   190  	})
   191  }
   192  
   193  func TestPlScriptStore(t *testing.T) {
   194  	store := NewScriptStore(point.Logging, ManagerCfg{})
   195  
   196  	store.indexUpdate(nil)
   197  
   198  	err := store.UpdateScriptsWithNS(NSDefault, map[string]string{
   199  		"abc.p": "default_time(time) ;set_tag(a, \"1\")",
   200  	}, nil)
   201  	if err != nil {
   202  		t.Error(err)
   203  	}
   204  
   205  	err = store.UpdateScriptsWithNS(NSDefault, map[string]string{
   206  		"abc.p": "default_time(time)",
   207  	}, nil)
   208  	if err != nil {
   209  		t.Error(err)
   210  	}
   211  
   212  	err = store.UpdateScriptsWithNS(NSDefault, map[string]string{
   213  		"abc.p": "default_time(time); set_tag(a, 1)",
   214  	}, nil)
   215  	if err == nil {
   216  		t.Error("should not be nil")
   217  	}
   218  
   219  	err = store.UpdateScriptsWithNS(NSDefault, map[string]string{
   220  		"abc.p": "default_time(time)",
   221  	}, nil)
   222  	if err != nil {
   223  		t.Error(err)
   224  	}
   225  
   226  	err = store.UpdateScriptsWithNS(NSGitRepo, map[string]string{
   227  		"abc.p": "default_time(time)",
   228  	}, nil)
   229  	if err != nil {
   230  		t.Error(err)
   231  	}
   232  
   233  	assert.Equal(t, store.Count(), 2)
   234  
   235  	err = store.UpdateScriptsWithNS(NSConfd, map[string]string{
   236  		"abc.p": "default_time(time)",
   237  	}, nil)
   238  	if err != nil {
   239  		t.Error(err)
   240  	}
   241  
   242  	err = store.UpdateScriptsWithNS(NSRemote, map[string]string{
   243  		"abc.p": "default_time(time)",
   244  	}, nil)
   245  	if err != nil {
   246  		t.Error(err)
   247  	}
   248  
   249  	for i, ns := range nsSearchOrder {
   250  		store.UpdateScriptsWithNS(ns, nil, nil)
   251  		if i < len(nsSearchOrder)-1 {
   252  			sInfo, ok := store.IndexGet("abc.p")
   253  			if !ok {
   254  				t.Error(fmt.Errorf("!ok"))
   255  				return
   256  			}
   257  			if sInfo.ns != nsSearchOrder[i+1] {
   258  				t.Error(sInfo.ns, nsSearchOrder[i+1])
   259  			}
   260  		} else {
   261  			_, ok := store.IndexGet("abc.p")
   262  			if ok {
   263  				t.Error(fmt.Errorf("shoud not be ok"))
   264  				return
   265  			}
   266  		}
   267  	}
   268  }
   269  
   270  func TestPlDirStruct(t *testing.T) {
   271  	bPath := fmt.Sprintf("/tmp/%d/pipeline/", time.Now().UnixNano())
   272  	_ = os.MkdirAll(bPath, os.FileMode(0o755))
   273  
   274  	expt := map[point.Category]map[string]string{}
   275  	for category, dirName := range CategoryDirName() {
   276  		if _, ok := expt[category]; !ok {
   277  			expt[category] = map[string]string{}
   278  		}
   279  		expt[category][dirName+"-xx.p"] = filepath.Join(bPath, dirName, dirName+"-xx.p")
   280  	}
   281  
   282  	_ = os.WriteFile(filepath.Join(bPath, "nginx-xx.p"), []byte(`
   283  	json(_, time)
   284  	set_tag(bb, "aa0")
   285  	default_time(time)
   286  	`), os.FileMode(0o755))
   287  
   288  	expt[point.Logging]["nginx-xx.p"] = filepath.Join(bPath, "nginx-xx.p")
   289  
   290  	for _, dirName := range CategoryDirName() {
   291  		_ = os.MkdirAll(filepath.Join(bPath, dirName), os.FileMode(0o755))
   292  		_ = os.WriteFile(filepath.Join(bPath, dirName, dirName+"-xx.p"), []byte(`
   293  		json(_, time)
   294  		set_tag(bb, "aa0")
   295  		default_time(time)
   296  		`), os.FileMode(0o755))
   297  	}
   298  	act := SearchWorkspaceScripts(bPath)
   299  
   300  	assert.Equal(t, expt, act)
   301  }