-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract fake controller to use it in other tests
Signed-off-by: Lucas Thiesen <[email protected]>
- Loading branch information
Showing
2 changed files
with
57 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package collector | ||
|
||
import ( | ||
"time" | ||
|
||
autoscalingv2 "k8s.io/api/autoscaling/v2beta2" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/metrics/pkg/apis/custom_metrics" | ||
) | ||
|
||
type FakeCollectorPlugin struct { | ||
metrics []CollectedMetric | ||
config map[string]string | ||
} | ||
|
||
type FakeCollector struct { | ||
metrics []CollectedMetric | ||
interval time.Duration | ||
stub func() ([]CollectedMetric, error) | ||
} | ||
|
||
func (c *FakeCollector) GetMetrics() ([]CollectedMetric, error) { | ||
if c.stub != nil { | ||
v, err := c.stub() | ||
return v, err | ||
} | ||
|
||
return c.metrics, nil | ||
} | ||
|
||
func (FakeCollector) Interval() time.Duration { | ||
return time.Minute | ||
} | ||
|
||
func (p *FakeCollectorPlugin) NewCollector( | ||
hpa *autoscalingv2.HorizontalPodAutoscaler, | ||
config *MetricConfig, | ||
interval time.Duration, | ||
) (Collector, error) { | ||
|
||
p.config = config.Config | ||
return &FakeCollector{metrics: p.metrics, interval: interval}, nil | ||
} | ||
|
||
func makePlugin(metric int) *FakeCollectorPlugin { | ||
return &FakeCollectorPlugin{ | ||
metrics: []CollectedMetric{ | ||
{ | ||
Custom: custom_metrics.MetricValue{Value: *resource.NewQuantity(int64(metric), resource.DecimalSI)}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func makeCollectorWithStub(f func() ([]CollectedMetric, error)) *FakeCollector { | ||
return &FakeCollector{stub: f} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters