mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-05-16 06:08:40 +00:00
79 lines
2.7 KiB
Go
79 lines
2.7 KiB
Go
package testing
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
clienttesting "k8s.io/client-go/testing"
|
|
|
|
opratorapiv1 "github.com/open-cluster-management/api/operator/v1"
|
|
"github.com/open-cluster-management/registration-operator/pkg/helpers"
|
|
)
|
|
|
|
func AssertAction(t *testing.T, actual clienttesting.Action, expected string) {
|
|
if actual.GetVerb() != expected {
|
|
t.Errorf("expected %s action but got: %#v", expected, actual)
|
|
}
|
|
}
|
|
|
|
func AssertGet(t *testing.T, actual clienttesting.Action, group, version, resource string) {
|
|
t.Helper()
|
|
if actual.GetVerb() != "get" {
|
|
t.Error(spew.Sdump(actual))
|
|
}
|
|
if actual.GetResource() != (schema.GroupVersionResource{Group: group, Version: version, Resource: resource}) {
|
|
t.Error(spew.Sdump(actual))
|
|
}
|
|
}
|
|
|
|
func NamedCondition(name, reason string, status metav1.ConditionStatus) opratorapiv1.StatusCondition {
|
|
return opratorapiv1.StatusCondition{Type: name, Status: status, Reason: reason}
|
|
}
|
|
|
|
func AssertOnlyConditions(t *testing.T, actual runtime.Object, expectedConditions ...opratorapiv1.StatusCondition) {
|
|
t.Helper()
|
|
|
|
var actualConditions []opratorapiv1.StatusCondition
|
|
if klusterlet, ok := actual.(*opratorapiv1.Klusterlet); ok {
|
|
actualConditions = klusterlet.Status.Conditions
|
|
} else {
|
|
clustermanager := actual.(*opratorapiv1.ClusterManager)
|
|
actualConditions = clustermanager.Status.Conditions
|
|
}
|
|
if len(actualConditions) != len(expectedConditions) {
|
|
t.Errorf("expected %v condition but got: %v", len(expectedConditions), spew.Sdump(actualConditions))
|
|
}
|
|
|
|
for _, expectedCondition := range expectedConditions {
|
|
actual := helpers.FindOperatorCondition(actualConditions, expectedCondition.Type)
|
|
if actual == nil {
|
|
t.Errorf("missing %v in %v", spew.Sdump(expectedCondition), spew.Sdump(actual))
|
|
}
|
|
if actual.Status != expectedCondition.Status {
|
|
t.Errorf("wrong result for %v in %v", spew.Sdump(expectedCondition), spew.Sdump(actual))
|
|
}
|
|
if actual.Reason != expectedCondition.Reason {
|
|
t.Errorf("wrong result for %v in %v", spew.Sdump(expectedCondition), spew.Sdump(actual))
|
|
}
|
|
}
|
|
}
|
|
|
|
func AssertEqualNumber(t *testing.T, actual, expected int) {
|
|
if actual != expected {
|
|
t.Errorf("expected %d number of actions but got: %d", expected, actual)
|
|
}
|
|
}
|
|
|
|
func AssertEqualNameNamespace(t *testing.T, actualName, actualNamespace, name, namespace string) {
|
|
if actualName != name {
|
|
t.Errorf("Name of the object does not match, expected %s, actual %s", name, actualName)
|
|
}
|
|
|
|
if actualNamespace != namespace {
|
|
t.Errorf("Namespace of the object does not match, expected %s, actual %s", namespace, actualNamespace)
|
|
}
|
|
}
|