ANALYSIS stage can falsely succeed without running any metric queries when duration <= interval
What happened:
In the pipedv1 analysis plugin, an ANALYSIS stage can complete with status SUCCESS without ever running any metric query or evaluating any threshold.
This happens when the stage's duration is shorter than (or expires before) the metric check's interval. For example, configuring an analysis stage with duration: 30s and a metric with interval: 1m (or when a shared template sets an interval longer than the stage duration):
AnalysisStageOptions.Validate()accepts the configuration without verifying thatduration >= interval.- At runtime,
metricsAnalyzer.run(ctx)initializes a ticker:
In Go,ticker := time.NewTicker(a.cfg.Interval.Duration()) defer ticker.Stop()time.NewTickerdoes not fire immediately; it waits for the fullintervalbefore sending the first event onticker.C. - When the stage timeout expires, the context is cancelled and
run(ctx)catches<-ctx.Done():case <-ctx.Done(): return nil - It returns
nilwithout having executed a single query or evaluation. - In
analysis.go, the orchestratingerrgroupsees no errors (eg.Wait() == nil), so it logs:e.logPersister.Success("All analyses were successful") return status // sdk.StageStatusSuccess
Because no error is returned, the deployment safety gate passes unconditionally—even if the underlying service is throwing errors or metric values violate the configured threshold.
What you expected to happen:
An analysis stage configured to guard a deployment should never pass without performing an actual check:
- Configuration validation should reject stages where
duration < interval(both during initial stage validation and after template resolution). - At runtime, the analyzer should ensure at least one evaluation was performed before considering the stage successful, or run the first query on startup before ticking.
How to reproduce it:
A focused unit test in executestage reproduces the behavior deterministically:
- Configure an
AnalysisStageOptionswith adurationof20msand a metric check withinterval: 1s, thresholdmax: 1.0, andfailureLimit: 0. - Provide a mock metrics provider that returns
Value: 100(which would immediately fail the threshold if evaluated). - Call
stage.Validate(): it succeeds with no error. - Call
analyzer.run(ctx): it returnsnilwithout making any calls to the provider (p.calls == 0).
Reproduction unit test
package executestage
import (
"context"
"testing"
"time"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/analysis/analysisprovider/metrics"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/analysis/config"
"github.com/pipe-cd/piped-plugin-sdk-go/unit"
"go.uber.org/zap"
)
type countingFailingProvider struct{ calls int }
func (p *countingFailingProvider) Type() string { return "test" }
func (p *countingFailingProvider) QueryPoints(context.Context, string, metrics.QueryRange) ([]metrics.DataPoint, error) {
p.calls++
return []metrics.DataPoint{{Value: 100}}, nil // Should fail max: 1.0 threshold
}
func TestAnalysisCanSucceedWithoutQuery(t *testing.T) {
max := 1.0
cfg := config.AnalysisMetrics{
Provider: "test",
Query: "test",
Strategy: config.AnalysisStrategyThreshold,
Deviation: "EITHER",
Interval: unit.Duration(time.Second),
Expected: config.AnalysisExpected{Max: &max},
}
stage := config.AnalysisStageOptions{
Duration: unit.Duration(20 * time.Millisecond),
Metrics: []config.TemplatableAnalysisMetrics{{AnalysisMetrics: cfg}},
}
if err := stage.Validate(); err != nil {
t.Fatalf("configuration rejected: %v", err)
}
p := &countingFailingProvider{}
a := &metricsAnalyzer{
id: "test",
cfg: cfg,
provider: p,
logger: zap.NewNop(),
logPersister: &fakeLogPersister{},
}
ctx, cancel := context.WithTimeout(context.Background(), stage.Duration.Duration())
defer cancel()
if err := a.run(ctx); err != nil {
t.Fatalf("analyzer failed: %v", err)
}
if p.calls != 0 {
t.Fatalf("expected 0 calls due to duration < interval, got %d", p.calls)
}
// Analyzer returned nil (success) despite zero queries and a provider that would violate thresholds!
}
Relevant Code:
- Stage validation doesn't check duration against metric intervals
- Ticker loop returns nil on context cancellation even if zero queries ran
- Executor marks stage successful when analyzers return nil
Environment:
pipedversion:mastercontrol-planeversion:master- OS: Linux
Possible Solution / Recommendations:
- Validation: Validate that
stage.Duration >= metric.Intervalinstage.go(and validate resolved configurations if using templates). - Initial Check / Execution Count: In
metricsAnalyzer(and genericanalyzer), run an immediate evaluation on startup before waiting for subsequent ticks, or track the number of successful checks performed and return an error ifevaluatedCount == 0whenctx.Done()is reached.