Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Fernandez <mariofer@redhat.com>
  • Loading branch information
marioferh committed Aug 9, 2023
1 parent 5ef1694 commit 05c1dbc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 53 deletions.
26 changes: 13 additions & 13 deletions test/e2e/framework/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// default ForeverTestTimeout is 30, some test fails because they take more than 30s
// change to custom in order to let the test finish withouth errors
const CustomForeverTestTimeout = 40 * time.Second
const DefaultTestTimeout = 40 * time.Second

type AssertOption struct {
PollInterval time.Duration
Expand All @@ -46,11 +46,11 @@ func WithPollInterval(d time.Duration) OptionFn {
}
}

// AssertResourceNeverExists asserts that a statefulset is never created for the duration of customForeverTestTimeout
// AssertResourceNeverExists asserts that a statefulset is never created for the duration of DefaultTestTimeout
func (f *Framework) AssertResourceNeverExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
option := AssertOption{
PollInterval: 5 * time.Second,
WaitTimeout: CustomForeverTestTimeout,
WaitTimeout: DefaultTestTimeout,
}
for _, fn := range fns {
fn(&option)
Expand All @@ -62,12 +62,12 @@ func (f *Framework) AssertResourceNeverExists(name, namespace string, resource c
Name: name,
Namespace: namespace,
}
if err := f.K8sClient.Get(ctx, key, resource); errors.IsNotFound(err) {
if err := f.K8sClient.Get(context.Background(), key, resource); errors.IsNotFound(err) {
return false, nil
}

return true, fmt.Errorf("resource %s/%s should not have been created", namespace, name)
}); wait.Interrupted(err) {
}); !wait.Interrupted(err) {
t.Fatal(err)
}
}
Expand All @@ -77,7 +77,7 @@ func (f *Framework) AssertResourceNeverExists(name, namespace string, resource c
func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
option := AssertOption{
PollInterval: 5 * time.Second,
WaitTimeout: CustomForeverTestTimeout,
WaitTimeout: DefaultTestTimeout,
}
for _, fn := range fns {
fn(&option)
Expand All @@ -103,16 +103,16 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...OptionFn) func(t *testing.T) {
option := AssertOption{
PollInterval: 5 * time.Second,
WaitTimeout: CustomForeverTestTimeout,
WaitTimeout: DefaultTestTimeout,
}
for _, fn := range fns {
fn(&option)
}
return func(t *testing.T) {
key := types.NamespacedName{Name: name, Namespace: namespace}
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, option.WaitTimeout, true, func(ctx context.Context) (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5+time.Second, option.WaitTimeout, true, func(ctx context.Context) (done bool, err error) {
pod := &appsv1.StatefulSet{}
err := f.K8sClient.Get(context.Background(), key, pod)
err = f.K8sClient.Get(context.Background(), key, pod)
return err == nil && pod.Status.ReadyReplicas == *pod.Spec.Replicas, nil
}); err != nil {
t.Fatal(err)
Expand All @@ -121,7 +121,7 @@ func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...Option
}

func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, obj client.Object) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
key := types.NamespacedName{Name: name, Namespace: namespace}

if err := f.K8sClient.Get(context.Background(), key, obj); errors.IsNotFound(err) {
Expand Down Expand Up @@ -226,7 +226,7 @@ func (f *Framework) GetOperatorMetrics(t *testing.T) []byte {

stopChan := make(chan struct{})
defer close(stopChan)
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
err := f.StartPortForward(pod.Name, pod.Namespace, "8080", stopChan)
return err == nil, nil
}); err != nil {
Expand Down Expand Up @@ -266,7 +266,7 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
}
var lastErr error

err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
lastErr = nil
err := f.K8sClient.Get(context.Background(), key, &ms)
if err != nil {
Expand All @@ -292,7 +292,7 @@ func (f *Framework) AssertAlertmanagerAbsent(t *testing.T, name, namespace strin
Name: name,
Namespace: namespace,
}
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
err := f.K8sClient.Get(context.Background(), key, &am)
if errors.IsNotFound(err) {
return true, nil
Expand Down
20 changes: 8 additions & 12 deletions test/e2e/monitoring_stack_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func assertCRDExists(t *testing.T, crds ...string) {

func TestMonitoringStackController(t *testing.T) {
err := stack.AddToScheme(scheme.Scheme)
if err != nil {
return
}
assert.NilError(t, err, "adding stack to scheme failed")
assertCRDExists(t,
"prometheuses.monitoring.rhobs",
"alertmanagers.monitoring.rhobs",
Expand Down Expand Up @@ -151,7 +149,7 @@ func nilResrouceSelectorPropagatesToPrometheus(t *testing.T) {
assert.NilError(t, err, "failed to patch monitoring stack with nil resource selector")

prometheus := monv1.Prometheus{}
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
if err := f.K8sClient.Get(context.Background(), types.NamespacedName{Name: updatedMS.Name, Namespace: updatedMS.Namespace}, &prometheus); errors.IsNotFound(err) {
return false, nil
}
Expand Down Expand Up @@ -299,7 +297,7 @@ func reconcileRevertsManualChanges(t *testing.T) {
err = f.K8sClient.Update(context.Background(), modified)
assert.NilError(t, err, "failed to update a prometheus")

err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
reconciled := monv1.Prometheus{}
key := types.NamespacedName{Name: ms.Name, Namespace: ms.Namespace}

Expand Down Expand Up @@ -586,7 +584,7 @@ func prometheusScaleDown(t *testing.T) {
ms.Spec.PrometheusConfig.Replicas = &numOfRep
err = f.K8sClient.Update(context.Background(), ms)
assert.NilError(t, err, "failed to update a monitoring stack")
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
if err := f.K8sClient.Get(context.Background(), key, &prom); errors.IsNotFound(err) {
return false, nil
}
Expand Down Expand Up @@ -743,8 +741,6 @@ func getAlertmanagerAlerts() ([]alert, error) {
}

func newAlerts(t *testing.T) *monv1.PrometheusRule {
durationTenSec := monv1.Duration("10s")
durationOneSec := monv1.Duration("1s")
rule := &monv1.PrometheusRule{
TypeMeta: metav1.TypeMeta{
APIVersion: monv1.SchemeGroupVersion.String(),
Expand All @@ -758,17 +754,17 @@ func newAlerts(t *testing.T) *monv1.PrometheusRule {
Groups: []monv1.RuleGroup{
{
Name: "Test",
Interval: &durationTenSec,
Interval: ptr.To(monv1.Duration("10s")),
Rules: []monv1.Rule{
{
Alert: "AlwaysOn",
Expr: intstr.FromString("vector(1)"),
For: &durationOneSec,
For: ptr.To(monv1.Duration("1s")),
},
{
Alert: "NeverOn",
Expr: intstr.FromString("vector(1) == 0"),
For: &durationOneSec,
For: ptr.To(monv1.Duration("1s")),
},
},
},
Expand Down Expand Up @@ -821,7 +817,7 @@ func newMonitoringStack(t *testing.T, name string, mods ...stackModifier) *stack
}

func waitForStackDeletion(name string) error {
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
key := types.NamespacedName{Name: name, Namespace: e2eTestNamespace}
var ms stack.MonitoringStack
err := f.K8sClient.Get(context.Background(), key, &ms)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/po_admission_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

func TestPrometheusRuleWebhook(t *testing.T) {
Expand Down Expand Up @@ -42,7 +43,6 @@ func invalidPrometheusRuleIsRejected(t *testing.T) {
}

func newSinglePrometheusRule(t *testing.T, name, expr string) *monv1.PrometheusRule {
durationFifteenMinutes := monv1.Duration("15m")
rule := &monv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -54,7 +54,7 @@ func newSinglePrometheusRule(t *testing.T, name, expr string) *monv1.PrometheusR
Rules: []monv1.Rule{{
Alert: "alert name",
Expr: intstr.FromString(expr),
For: &durationFifteenMinutes,
For: ptr.To(monv1.Duration("15m")),
}},
}},
},
Expand Down
26 changes: 0 additions & 26 deletions test/e2e/thanos_querier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ func singleStackWithSidecar(t *testing.T) {
// Assert prometheus instance can be queried
stopChan := make(chan struct{})
defer close(stopChan)
<<<<<<< Updated upstream
//nolint
if err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
=======
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
>>>>>>> Stashed changes
err = f.StartServicePortForward(name, e2eTestNamespace, "9090", stopChan)
return err == nil, nil
}); wait.Interrupted(err) {
Expand All @@ -91,12 +86,7 @@ func singleStackWithSidecar(t *testing.T) {
expectedResults := map[string]int{
"prometheus_build_info": 2, // must return from both prometheus pods
}
<<<<<<< Updated upstream
//nolint
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
=======
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
>>>>>>> Stashed changes
correct := 0
for query, value := range expectedResults {
result, err := promClient.Query(query)
Expand Down Expand Up @@ -147,27 +137,16 @@ func newThanosQuerier(t *testing.T, name string, selector map[string]string) *ms
}

func waitForThanosQuerierDeletion(tq *msov1.ThanosQuerier) error {
<<<<<<< Updated upstream
//nolint
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
err := f.K8sClient.Get(context.Background(),
=======
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
err = f.K8sClient.Get(context.Background(),
>>>>>>> Stashed changes
types.NamespacedName{Name: tq.Name, Namespace: tq.Namespace},
tq)
return errors.IsNotFound(err), nil
})
}

func waitForDeploymentDeletion(name string) error {
<<<<<<< Updated upstream
//nolint
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
=======
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
>>>>>>> Stashed changes
var dep appsv1.Deployment
err = f.K8sClient.Get(context.Background(),
types.NamespacedName{Name: name, Namespace: e2eTestNamespace},
Expand All @@ -177,12 +156,7 @@ func waitForDeploymentDeletion(name string) error {
}

func waitForServiceDeletion(name string) error {
<<<<<<< Updated upstream
//nolint
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
=======
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
>>>>>>> Stashed changes
var svc corev1.Service
err = f.K8sClient.Get(context.Background(),
types.NamespacedName{Name: name, Namespace: e2eTestNamespace},
Expand Down

0 comments on commit 05c1dbc

Please sign in to comment.