Skip to content

Commit

Permalink
Add metrics to controller tests
Browse files Browse the repository at this point in the history
Fix: #387
  • Loading branch information
stefanprodan committed Feb 8, 2020
1 parent e4e92b3 commit 8f99e58
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
38 changes: 29 additions & 9 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,26 @@ func newTestCanary() *flaggerv1.Canary {
MaxWeight: 50,
Metrics: []flaggerv1.CanaryMetric{
{
Name: "istio_requests_total",
Name: "request-success-rate",
Threshold: 99,
Interval: "1m",
},
{
Name: "istio_request_duration_seconds_bucket",
Threshold: 500,
Interval: "1m",
Name: "request-duration",
ThresholdRange: &flaggerv1.CanaryThresholdRange{
Min: toFloatPtr(0),
Max: toFloatPtr(500000),
},
Interval: "1m",
},
{
Name: "custom",
ThresholdRange: &flaggerv1.CanaryThresholdRange{
Min: toFloatPtr(0),
Max: toFloatPtr(500000),
},
Interval: "1m",
Query: "fake",
},
},
},
Expand All @@ -266,6 +278,11 @@ func newTestCanary() *flaggerv1.Canary {
return cd
}

func toFloatPtr(val int) *float64 {
v := float64(val)
return &v
}

func newTestCanaryMirror() *flaggerv1.Canary {
cd := newTestCanary()
cd.Spec.CanaryAnalysis.Mirror = true
Expand Down Expand Up @@ -305,13 +322,16 @@ func newTestCanaryAB() *flaggerv1.Canary {
},
Metrics: []flaggerv1.CanaryMetric{
{
Name: "istio_requests_total",
Threshold: 99,
Interval: "1m",
Name: "request-success-rate",
ThresholdRange: &flaggerv1.CanaryThresholdRange{
Min: toFloatPtr(99),
Max: toFloatPtr(100),
},
Interval: "1m",
},
{
Name: "istio_request_duration_seconds_bucket",
Threshold: 500,
Name: "request-duration",
Threshold: 500000,
Interval: "1m",
},
},
Expand Down
36 changes: 33 additions & 3 deletions pkg/controller/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,45 @@ func TestScheduler_Rollback(t *testing.T) {
mocks.ctrl.advanceCanary("podinfo", "default", true)

// update failed checks to max
err := mocks.deployer.SyncStatus(mocks.canary, flaggerv1.CanaryStatus{Phase: flaggerv1.CanaryPhaseProgressing, FailedChecks: 11})
err := mocks.deployer.SyncStatus(mocks.canary, flaggerv1.CanaryStatus{Phase: flaggerv1.CanaryPhaseProgressing, FailedChecks: 10})
if err != nil {
t.Fatal(err.Error())
}

// detect changes
// set a metric check to fail
c, err := mocks.flaggerClient.FlaggerV1beta1().Canaries("default").Get("podinfo", metav1.GetOptions{})
if err != nil {
t.Fatal(err.Error())
}
cd := c.DeepCopy()
cd.Spec.CanaryAnalysis.Metrics = append(c.Spec.CanaryAnalysis.Metrics, flaggerv1.CanaryMetric{
Name: "fail",
Interval: "1m",
ThresholdRange: &flaggerv1.CanaryThresholdRange{
Min: toFloatPtr(0),
Max: toFloatPtr(50),
},
Query: "fail",
})
_, err = mocks.flaggerClient.FlaggerV1beta1().Canaries("default").Update(cd)
if err != nil {
t.Fatal(err.Error())
}

// run metric checks
mocks.ctrl.advanceCanary("podinfo", "default", true)
if err != nil {
t.Fatal(err.Error())
}

c, err := mocks.flaggerClient.FlaggerV1beta1().Canaries("default").Get("podinfo", metav1.GetOptions{})
// finalise analysis
mocks.ctrl.advanceCanary("podinfo", "default", true)
if err != nil {
t.Fatal(err.Error())
}

// check status
c, err = mocks.flaggerClient.FlaggerV1beta1().Canaries("default").Get("podinfo", metav1.GetOptions{})
if err != nil {
t.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/providers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewPrometheusProvider(provider flaggerv1.MetricTemplateProvider, credential

// RunQuery executes the promQL query and returns the the first result as float64
func (p *PrometheusProvider) RunQuery(query string) (float64, error) {
if p.url.Host == "fake" {
if p.url.String() == "fake" {
return 100, nil
}

Expand Down

0 comments on commit 8f99e58

Please sign in to comment.