Repository: prometheus/prometheus
File: cmd/promtool/unittest.go (Go)

Code before:
type testGroup struct {
	Interval        model.Duration     `yaml:"interval"`
	InputSeries     []series           `yaml:"input_series"`
	AlertRuleTests  []alertTestCase    `yaml:"alert_rule_test,omitempty"`
	PromqlExprTests []promqlTestCase   `yaml:"promql_expr_test,omitempty"`
	ExternalLabels  labels.Labels      `yaml:"external_labels,omitempty"`
	ExternalURL     string             `yaml:"external_url,omitempty"`
	TestGroupName   string             `yaml:"name,omitempty"`
	StartTimestamp  testStartTimestamp `yaml:"start_timestamp,omitempty"`
}

// test performs the unit tests.
func (tg *testGroup) test(testname string, evalInterval time.Duration, groupOrderMap map[string]int, queryOpts promqltest.LazyLoaderOpts, diffFlag, debug, ignoreUnknownFields, fuzzyCompare bool, ruleFiles ...string) (outErr []error) {
	if debug {
		testStart := time.Now()
		fmt.Printf("DEBUG: Starting test %s\n", testname)
		defer func() {
			fmt.Printf("DEBUG: Test %s finished, took %v\n", testname, time.Since(testStart))
		}()
	}
	// Setup testing suite.
	// Set the start time from the test group.
	if tg.StartTimestamp.IsZero() {
		queryOpts.StartTime = time.Unix(0, 0).UTC()
	} else {
		queryOpts.StartTime = tg.StartTimestamp.Time
	}
	suite, err := promqltest.NewLazyLoader(tg.seriesLoadingString(), queryOpts)
	if err != nil {
		return []error{err}
	}
	defer func() {
		err := suite.Close()
		if err != nil {
			outErr = append(outErr, err)
		}
	}()
	suite.SubqueryInterval = evalInterval

	// Load the rule files.
	opts := &rules.ManagerOptions{
		QueryFunc:  rules.EngineQueryFunc(suite.QueryEngine(), suite.Storage()),
		Appendable: suite.Storage(),
		Context:    context.Background(),
		NotifyFunc: func(context.Context, string, ...*rules.Alert) {},
		Logger:     promslog.NewNopLogger(),
	}
	m := rules.NewManager(opts)
	groupsMap, ers := m.LoadGroups(time.Duration(tg.Interval), tg.ExternalLabels, tg.ExternalURL, nil, ignoreUnknownFields, ruleFiles...)
	if ers != nil {
		return ers

Review comment (suggestion):
It feels like this could be simplified to just the line `queryOpts.StartTime = tg.StartTimestamp.Time` given queryOpts also goes through a IsZero check.

Code after:
type testGroup struct {
	Interval        model.Duration     `yaml:"interval"`
	InputSeries     []series           `yaml:"input_series"`
	AlertRuleTests  []alertTestCase    `yaml:"alert_rule_test,omitempty"`
	PromqlExprTests []promqlTestCase   `yaml:"promql_expr_test,omitempty"`
	ExternalLabels  labels.Labels      `yaml:"external_labels,omitempty"`
	ExternalURL     string             `yaml:"external_url,omitempty"`
	TestGroupName   string             `yaml:"name,omitempty"`
	StartTimestamp  testStartTimestamp `yaml:"start_timestamp,omitempty"`
}

// test performs the unit tests.
func (tg *testGroup) test(testname string, evalInterval time.Duration, groupOrderMap map[string]int, queryOpts promqltest.LazyLoaderOpts, diffFlag, debug, ignoreUnknownFields, fuzzyCompare bool, ruleFiles ...string) (outErr []error) {
	if debug {
		testStart := time.Now()
		fmt.Printf("DEBUG: Starting test %s\n", testname)
		defer func() {
			fmt.Printf("DEBUG: Test %s finished, took %v\n", testname, time.Since(testStart))
		}()
	}
	// Setup testing suite.
	// Set the start time from the test group.
	queryOpts.StartTime = tg.StartTimestamp.Time
	suite, err := promqltest.NewLazyLoader(tg.seriesLoadingString(), queryOpts)
	if err != nil {
		return []error{err}
	}
	defer func() {
		err := suite.Close()
		if err != nil {
			outErr = append(outErr, err)
		}
	}()
	suite.SubqueryInterval = evalInterval

	// Load the rule files.
	opts := &rules.ManagerOptions{
		QueryFunc:  rules.EngineQueryFunc(suite.QueryEngine(), suite.Storage()),
		Appendable: suite.Storage(),
		Context:    context.Background(),
		NotifyFunc: func(context.Context, string, ...*rules.Alert) {},
		Logger:     promslog.NewNopLogger(),
	}
	m := rules.NewManager(opts)
	groupsMap, ers := m.LoadGroups(time.Duration(tg.Interval), tg.ExternalLabels, tg.ExternalURL, nil, ignoreUnknownFields, ruleFiles...)
	if ers != nil {
		return ers
	}
	groups := orderedGroups(groupsMap, groupOrderMap)

	// Bounds for evaluating the rules.