-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
options.go
75 lines (63 loc) · 2.6 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package tests contains test cases. To run the tests go to tests directory and run:
// RUN_TESTBED=1 go test -v
package testbed // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
import (
"time"
)
// ResourceSpec is a resource consumption specification.
type ResourceSpec struct {
// Percentage of one core the process is expected to consume at most.
// Test is aborted and failed if consumption during
// ResourceCheckPeriod exceeds this number. If 0 the CPU
// consumption is not monitored and does not affect the test result.
ExpectedMaxCPU uint32
// Maximum RAM in MiB the process is expected to consume.
// Test is aborted and failed if consumption exceeds this number.
// If 0 memory consumption is not monitored and does not affect
// the test result.
ExpectedMaxRAM uint32
// Period during which CPU and RAM of the process are measured.
// Bigger numbers will result in more averaging of short spikes.
ResourceCheckPeriod time.Duration
// The number of consecutive violations necessary to trigger a failure.
// This is useful for tests which can tolerate transitory violations.
MaxConsecutiveFailures uint32
}
// isSpecified returns true if any part of ResourceSpec is specified,
// i.e. has non-zero value.
func (rs *ResourceSpec) isSpecified() bool {
return rs != nil && (rs.ExpectedMaxCPU != 0 || rs.ExpectedMaxRAM != 0)
}
// TestCaseOption defines a TestCase option.
type TestCaseOption func(t *TestCase)
// WithSkipResults disables writing out results file for a TestCase.
func WithSkipResults() TestCaseOption {
return func(tc *TestCase) {
tc.skipResults = true
}
}
// WithResourceLimits sets expected limits for resource consmption.
// Error is signaled if consumption during ResourceCheckPeriod exceeds the limits.
// Limits are modified only for non-zero fields of resourceSpec, all zero-value fields
// fo resourceSpec are ignored and their previous values remain in effect.
func WithResourceLimits(resourceSpec ResourceSpec) TestCaseOption {
return func(tc *TestCase) {
if resourceSpec.ExpectedMaxCPU > 0 {
tc.resourceSpec.ExpectedMaxCPU = resourceSpec.ExpectedMaxCPU
}
if resourceSpec.ExpectedMaxRAM > 0 {
tc.resourceSpec.ExpectedMaxRAM = resourceSpec.ExpectedMaxRAM
}
if resourceSpec.ResourceCheckPeriod > 0 {
tc.resourceSpec.ResourceCheckPeriod = resourceSpec.ResourceCheckPeriod
}
}
}
// WithDecision enables our mock backend to behave sporadically
func WithDecisionFunc(decision decisionFunc) TestCaseOption {
return func(tc *TestCase) {
tc.decision = decision
}
}