Beispiel #1
0
func TestInsertMetricValuesFromJSON(t *testing.T) {
	c := initTestChecker()
	c.NewScopeAndPackage()
	initMetricsJson()
	c.InsertMetricValuesFromJSON()
	if f, _ := exact.Float64Val(c.sc.Lookup("testGauge2_value").(*types.Const).Val()); f != float64(200) {
		t.Errorf("Did not insert gauge2 correctly")
	}
	if f, _ := exact.Float64Val(c.sc.Lookup("testGauge3_value").(*types.Const).Val()); f != float64(300) {
		t.Errorf("Did not insert gauge3 correctly")
	}
	if f, _ := exact.Float64Val(c.sc.Lookup("testGauge4_value").(*types.Const).Val()); f != float64(400) {
		t.Errorf("Did not insert gauge4 correctly")
	}
	if f, _ := exact.Float64Val(c.sc.Lookup("testGauge5_value").(*types.Const).Val()); f != float64(500) {
		t.Errorf("Did not insert gauge5 correctly")
	}
}
Beispiel #2
0
//ranges through config file and checks all expressions.
// prints result messages to stdout
func (c *checker) CheckAll() ([]CheckResult, error) {
	result := []CheckResult{}
	for _, section := range c.config.GetSections() {
		if section == "default" {
			continue
		}
		expr, err := c.config.GetString(section, "expr")
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		fset := token.NewFileSet()
		fset.AddFile("", fset.Base(), len(expr)).SetLinesForContent([]byte(expr))
		tv, err := types.Eval(fset, c.pkg, 0, expr)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			fmt.Fprintln(os.Stderr, expr)
			continue
		}
		cr := &CheckResult{
			Name: section,
		}
		var m string
		if exact.BoolVal(tv.Value) {
			m, err = c.config.GetString(section, "true")
			if err != nil {
				continue
			}
		} else {
			m, err = c.config.GetString(section, "false")
			if err != nil {
				continue
			}
		}
		val, err := c.config.GetString(section, "val")
		if err == nil {
			fset := token.NewFileSet()
			fset.AddFile("", fset.Base(), len(val)).SetLinesForContent([]byte(val))
			tv, err := types.Eval(fset, c.pkg, 0, val)
			if err == nil {
				if types.Identical(tv.Type, types.Typ[types.UntypedFloat]) || types.Identical(tv.Type, types.Typ[types.Float64]) {
					x, _ := exact.Float64Val(tv.Value)
					cr.Value = x
				}
			}
		}
		tags, err := c.config.GetString(section, "tags")
		if err == nil {
			cr.Tags = tags
		} else {
			cr.Tags = "unknown"
		}

		fset = token.NewFileSet()
		fset.AddFile("", fset.Base(), len(m)).SetLinesForContent([]byte(m))
		tv, err = types.Eval(fset, c.pkg, 0, m)
		if err != nil {
			cr.Message = m
		} else {
			cr.Message = exact.StringVal(tv.Value)
		}
		result = append(result, *cr)
	}
	return result, nil
}