Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add resources e2e #329

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tests/e2e/e2evalidator/e2evalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@ import (
"strings"

"github.com/go-playground/validator/v10"
corev1 "k8s.io/api/core/v1"
)

type Ruler interface {
Cmp(val any) error
}

type Rule string

func StructWithRules(actual any, rulesMapping map[string]Rule) (errMessages []string) {
func (r Rule) Cmp(val any) error {
return validator.New().Var(val, string(r))
}

type Resource corev1.ResourceRequirements
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the definition could be more abstract rather than specific to a certain type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


func (r Resource) Cmp(val any) error {
if !reflect.DeepEqual(val, corev1.ResourceRequirements(r)) {
return fmt.Errorf("resource not equal, expected %v, got %v", r, val)
}

return nil
}

func StructWithRules(actual any, rulesMapping map[string]Ruler) (errMessages []string) {
for field, rule := range rulesMapping {
actualVal := reflect.ValueOf(actual)

Expand All @@ -32,7 +51,8 @@ func StructWithRules(actual any, rulesMapping map[string]Rule) (errMessages []st
actualVal = actualVal.Elem()
}
val := actualVal.Interface()
if err := validator.New().Var(val, string(rule)); err != nil {

if err := rule.Cmp(val); err != nil {
errMessages = append(errMessages, fmt.Sprintf("field %q(%v) does not match %q\n", field, val, rule))
}
}
Expand Down
65 changes: 62 additions & 3 deletions tests/e2e/envfuncsext/nebulacluster-ready-func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package envfuncsext
import (
"context"
stderrors "errors"
"reflect"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/e2e-framework/pkg/envconf"

Expand All @@ -46,7 +49,7 @@ func DefaultNebulaClusterReadyFunc(ctx context.Context, cfg *envconf.Config, nc
return true, nil
}

func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping map[string]e2evalidator.Rule) NebulaClusterReadyFunc {
func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping map[string]e2evalidator.Ruler) NebulaClusterReadyFunc {
return func(_ context.Context, _ *envconf.Config, nc *appsv1alpha1.NebulaCluster) (isReady bool, err error) {
if errMessages := e2evalidator.StructWithRules(nc, rulesMapping); len(errMessages) > 0 {
if ignoreValidationErrors {
Expand All @@ -61,7 +64,7 @@ func NebulaClusterReadyFuncForFields(ignoreValidationErrors bool, rulesMapping m
}
}

func defaultNebulaClusterReadyFuncForStatus(_ context.Context, _ *envconf.Config, nc *appsv1alpha1.NebulaCluster) (bool, error) {
func defaultNebulaClusterReadyFuncForStatus(ctx context.Context, cfg *envconf.Config, nc *appsv1alpha1.NebulaCluster) (bool, error) {
isReady := nc.IsReady()
if isReady {
// TODO: Add more checks
Expand Down Expand Up @@ -110,6 +113,24 @@ func defaultNebulaClusterReadyFuncForStatus(_ context.Context, _ *envconf.Config
)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This func is used to cmp nc status, maybe put those in defaultNebulaClusterReadyFuncForGraphd/Metad/Storaged is more suitable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

{ // Metad Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.MetadComponent()) {
isReady = false
}
}

{ // Storaged Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.StoragedComponent()) {
isReady = false
}
}

{ // Graphd Resource checks
if !isComponentResourceExpected(ctx, cfg, nc.GraphdComponent()) {
isReady = false
}
}
}

return isReady, nil
Expand Down Expand Up @@ -152,7 +173,7 @@ func isComponentStatusExpected(
) bool {
if errMessages := e2evalidator.StructWithRules(
componentStatus,
map[string]e2evalidator.Rule{
map[string]e2evalidator.Ruler{
"Version": e2evalidator.Eq(componentSpec.Version),
"Phase": e2evalidator.Eq(appsv1alpha1.RunningPhase),
"Workload.ReadyReplicas": e2evalidator.Eq(*componentSpec.Replicas),
Expand All @@ -173,3 +194,41 @@ func isComponentStatusExpected(

return true
}

func isComponentResourceExpected(ctx context.Context, cfg *envconf.Config, component appsv1alpha1.NebulaClusterComponent) bool {
sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: component.GetName(),
Namespace: component.GetNamespace(),
},
}

if err := cfg.Client().Resources().Get(ctx, sts.Name, sts.Namespace, sts); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have already obtained it, why not check more fields, such as version, replicas , etc.

klog.InfoS("Check Component Resource but statefulset not found",
"namespace", sts.Namespace,
"name", sts.Name,
)
return false
}

for _, c := range sts.Spec.Template.Spec.Containers {
if c.Name != component.ComponentType().String() {
continue
}
resource := component.ComponentSpec().Resources()
if reflect.DeepEqual(c.Resources.DeepCopy(), resource) {
return true
} else {
klog.InfoS("Check Component Resource but not expected",
"namespace", sts.Namespace,
"name", sts.Name,
"requests", c.Resources.Requests,
"requestsExpected", resource.Requests,
"limits", c.Resources.Limits,
"limitsExpected", resource.Limits,
)
}
}

return false
}
8 changes: 4 additions & 4 deletions tests/e2e/envfuncsext/nebulacluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func InstallNebulaCluster(opts ...NebulaClusterOption) env.Func {
o := (&NebulaClusterOptions{}).
WithOptions(
WithNebulaClusterHelmOptions(WithHelmOptions(
// default values
// default values
)),
).WithOptions(opts...)

Expand All @@ -110,7 +110,7 @@ func UpgradeNebulaCluster(opts ...NebulaClusterOption) env.Func {
o := (&NebulaClusterOptions{}).
WithOptions(
WithNebulaClusterHelmOptions(WithHelmOptions(
// default values
// default values
)),
).WithOptions(opts...)

Expand All @@ -133,7 +133,7 @@ func WaitNebulaClusterReady(opts ...NebulaClusterOption) env.Func {
o := (&NebulaClusterOptions{}).
WithOptions(
WithNebulaClusterHelmOptions(WithHelmOptions(
// default values
// default values
)),
).WithOptions(opts...)

Expand Down Expand Up @@ -192,7 +192,7 @@ func UninstallNebulaCluster(opts ...NebulaClusterOption) env.Func {
o := (&NebulaClusterOptions{}).
WithOptions(
WithNebulaClusterHelmOptions(WithHelmOptions(
// default values
// default values
)),
).WithOptions(opts...)

Expand Down
Loading