Skip to content

Commit

Permalink
add tests for logging and move variables to consts
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoBraveCoding committed May 31, 2024
1 parent 26c9fb5 commit 2e9b4e9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 89 deletions.
28 changes: 14 additions & 14 deletions internal/addon/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,37 @@ func AgentHealthProber() *agent.HealthProber {
ProbeFields: []agent.ProbeField{
{
ResourceIdentifier: workapiv1.ResourceIdentifier{
Group: otelv1alpha1.GroupVersion.Group,
Resource: OpenTelemetryCollectorsResource,
Name: spokeOTELColName,
Namespace: spokeOTELColNamespace,
Group: loggingv1.GroupVersion.Group,
Resource: ClusterLogForwardersResource,
Name: SpokeCLFName,
Namespace: SpokeCLFNamespace,
},
ProbeRules: []workapiv1.FeedbackRule{
{
Type: workapiv1.JSONPathsType,
JsonPaths: []workapiv1.JsonPath{
{
Name: "replicas",
Path: ".spec.replicas",
Name: clfProbeKey,
Path: clfProbePath,
},
},
},
},
},
{
ResourceIdentifier: workapiv1.ResourceIdentifier{
Group: loggingv1.GroupVersion.Group,
Resource: ClusterLogForwardersResource,
Name: spokeCLFName,
Namespace: spokeLoggingNamespace,
Group: otelv1alpha1.GroupVersion.Group,
Resource: OpenTelemetryCollectorsResource,
Name: SpokeOTELColName,
Namespace: SpokeOTELColNamespace,
},
ProbeRules: []workapiv1.FeedbackRule{
{
Type: workapiv1.JSONPathsType,
JsonPaths: []workapiv1.JsonPath{
{
Name: "isReady",
Path: ".status.conditions[?(@.type==\"Ready\")].status",
Name: otelColProbeKey,
Path: otelColProbePath,
},
},
},
Expand All @@ -90,7 +90,7 @@ func AgentHealthProber() *agent.HealthProber {
for _, value := range result.Values {
switch {
case identifier.Resource == ClusterLogForwardersResource:
if value.Name != "isReady" {
if value.Name != clfProbeKey {
continue
}

Expand All @@ -100,7 +100,7 @@ func AgentHealthProber() *agent.HealthProber {

return nil
case identifier.Resource == OpenTelemetryCollectorsResource:
if value.Name != "replicas" {
if value.Name != otelColProbeKey {
continue
}

Expand Down
148 changes: 79 additions & 69 deletions internal/addon/addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,97 @@ import (
"fmt"
"testing"

//"github.com/openshift/cluster-logging-operator/internal/status"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
v1 "open-cluster-management.io/api/work/v1"

otelv1alpha1 "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
loggingapis "github.com/openshift/cluster-logging-operator/apis"
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
loggingv1 "github.com/openshift/cluster-logging-operator/apis/logging/v1"
)

var (
_ = loggingapis.AddToScheme(scheme.Scheme)
_ = operatorsv1.AddToScheme(scheme.Scheme)
_ = operatorsv1alpha1.AddToScheme(scheme.Scheme)
)

func Test_AgentHealthProber_Healthy(t *testing.T) {
otelcol := &otelv1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: spokeOTELColName,
Namespace: spokeOTELColNamespace,
func Test_AgentHealthProber_CLF(t *testing.T) {
unhealthyError := fmt.Errorf("%w: clusterlogforwarder status condition type is %s for %s/%s", errUnavailable, "False", SpokeCLFNamespace, SpokeCLFName)
for _, tc := range []struct {
name string
status string
expectedErr string
}{
{
name: "healthy",
status: "True",
},
Spec: otelv1alpha1.OpenTelemetryCollectorSpec{
Replicas: ptr.To[int32](1),
{
name: "unhealthy",
status: "False",
expectedErr: unhealthyError.Error(),
},
}

healthProber := AgentHealthProber()

err := healthProber.WorkProber.HealthCheck(v1.ResourceIdentifier{
Group: otelcol.APIVersion,
Resource: OpenTelemetryCollectorsResource,
Name: otelcol.Name,
Namespace: otelcol.Namespace,
}, v1.StatusFeedbackResult{
Values: []v1.FeedbackValue{
{
Name: "replicas",
Value: v1.FieldValue{
Type: v1.Integer,
Integer: ptr.To[int64](1),
} {
t.Run(tc.name, func(t *testing.T) {
healthProber := AgentHealthProber()
err := healthProber.WorkProber.HealthCheck(v1.ResourceIdentifier{
Group: loggingv1.GroupVersion.Group,
Resource: ClusterLogForwardersResource,
Name: SpokeCLFName,
Namespace: SpokeCLFNamespace,
}, v1.StatusFeedbackResult{
Values: []v1.FeedbackValue{
{
Name: "isReady",
Value: v1.FieldValue{
Type: v1.String,
String: &tc.status,
},
},
},
},
},
})

require.NoError(t, err)
})
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
return
}
require.NoError(t, err)
})
}
}

func Test_AgentHealthProber_Unhealthy(t *testing.T) {
otelcol := &otelv1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: spokeOTELColName,
Namespace: spokeOTELColNamespace,
func Test_AgentHealthProber_OTELCol(t *testing.T) {
unhealthyError := fmt.Errorf("%w: opentelemetrycollector replicas is %d for %s/%s", errUnavailable, 0, SpokeOTELColNamespace, SpokeOTELColName)
for _, tc := range []struct {
name string
replicas int64
expectedErr string
}{
{
name: "healthy",
replicas: 1,
},
Spec: otelv1alpha1.OpenTelemetryCollectorSpec{
Replicas: ptr.To[int32](0),
{
name: "unhealthy",
replicas: 0,
expectedErr: unhealthyError.Error(),
},
}
healthProber := AgentHealthProber()

err := healthProber.WorkProber.HealthCheck(v1.ResourceIdentifier{
Group: otelcol.APIVersion,
Resource: OpenTelemetryCollectorsResource,
Name: otelcol.Name,
Namespace: otelcol.Namespace,
}, v1.StatusFeedbackResult{
Values: []v1.FeedbackValue{
{
Name: "replicas",
Value: v1.FieldValue{
Type: v1.Integer,
Integer: ptr.To[int64](0),
} {
t.Run(tc.name, func(t *testing.T) {
healthProber := AgentHealthProber()
err := healthProber.WorkProber.HealthCheck(v1.ResourceIdentifier{
Group: otelv1alpha1.GroupVersion.Group,
Resource: OpenTelemetryCollectorsResource,
Name: SpokeOTELColName,
Namespace: SpokeOTELColNamespace,
}, v1.StatusFeedbackResult{
Values: []v1.FeedbackValue{
{
Name: "replicas",
Value: v1.FieldValue{
Type: v1.Integer,
Integer: &tc.replicas,
},
},
},
},
},
})

expectedErr := fmt.Errorf("%w: replicas is %d for %s/%s", errUnavailable, 0, otelcol.Namespace, otelcol.Name)
require.EqualError(t, err, expectedErr.Error())
})
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
return
}
require.NoError(t, err)
})
}
}
12 changes: 8 additions & 4 deletions internal/addon/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ const (
AdcTracingisabledKey = "tracingDisabled"

ClusterLogForwardersResource = "clusterlogforwarders"
spokeCLFName = "instance"
spokeLoggingNamespace = "openshift-logging"
SpokeCLFName = "instance"
SpokeCLFNamespace = "openshift-logging"
clfProbeKey = "isReady"
clfProbePath = ".status.conditions[?(@.type==\"Ready\")].status"

OpenTelemetryCollectorsResource = "opentelemetrycollectors"
spokeOTELColName = "spoke-otelcol"
spokeOTELColNamespace = "spoke-otelcol"
SpokeOTELColName = "spoke-otelcol"
SpokeOTELColNamespace = "spoke-otelcol"
otelColProbeKey = "replicas"
otelColProbePath = ".spec.replicas"
)

//go:embed manifests
Expand Down
4 changes: 4 additions & 0 deletions internal/logging/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func Test_Logging_AllConfigsTogether_AllResources(t *testing.T) {
require.NotNil(t, obj.Spec.Outputs[1].Secret)
require.Equal(t, "static-authentication", obj.Spec.Outputs[0].Secret.Name)
require.Equal(t, "static-authentication", obj.Spec.Outputs[1].Secret.Name)
// Check name and namespace to make usre that if we change the helm
// manifests that we don't break the addon probes
require.Equal(t, addon.SpokeCLFName, obj.Name)
require.Equal(t, addon.SpokeCLFNamespace, obj.Namespace)
case *corev1.Secret:
if obj.Name == "static-authentication" {
require.Equal(t, staticCred.Data, obj.Data)
Expand Down
6 changes: 4 additions & 2 deletions internal/tracing/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ func Test_Tracing_AllConfigsTogether_AllResources(t *testing.T) {
for _, obj := range objects {
switch obj := obj.(type) {
case *otelv1alpha1.OpenTelemetryCollector:
require.Equal(t, "spoke-otelcol", obj.ObjectMeta.Name)
require.Equal(t, "spoke-otelcol", obj.ObjectMeta.Namespace)
// Check name and namespace to make usre that if we change the helm
// manifests that we don't break the addon probes
require.Equal(t, addon.SpokeOTELColName, obj.Name)
require.Equal(t, addon.SpokeOTELColNamespace, obj.Namespace)
require.NotEmpty(t, obj.Spec.Config)
case *corev1.Secret:
if obj.Name == "tracing-otlphttp-auth" {
Expand Down

0 comments on commit 2e9b4e9

Please sign in to comment.