Skip to content

Commit

Permalink
feat: add env, labels, annotations e2e test (#349)
Browse files Browse the repository at this point in the history
* feat: add k8s env, annotations, labels e2e test

* fix: lint

* fix: adjust code

* feat: add tolerations nodeSelector affinity

* feat: add initcontainer sidecarcontainer

* feat: add probe

* feat: add readiness

* feat: add volume

* feat: add mount

* fix: probe

* feat: lint
  • Loading branch information
kqzh authored Nov 8, 2023
1 parent 2ff5c6b commit 4e53632
Show file tree
Hide file tree
Showing 4 changed files with 1,038 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tests/e2e/e2ematcher/e2ematcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func structImpl(preFields []string, actual any, matchers map[string]any) (retErr

if m, ok := matcher.(Matcher); ok {
for actualVal.Kind() == reflect.Ptr {
if actualVal.IsNil() {
break
}
actualVal = actualVal.Elem()
}
val := actualVal.Interface()
Expand Down
118 changes: 116 additions & 2 deletions tests/e2e/envfuncsext/nebulacluster-ready-func.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
"regexp"
"strings"

"github.com/google/go-cmp/cmp"
nebulago "github.com/vesoft-inc/nebula-go/v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/klog/v2"
"sigs.k8s.io/e2e-framework/klient/k8s/resources"
"sigs.k8s.io/e2e-framework/pkg/envconf"
Expand Down Expand Up @@ -567,6 +569,94 @@ func isComponentStatefulSetExpected(ctx context.Context, cfg *envconf.Config, co
return false
}

klog.InfoS("Check Component Resource find container in StatefulSet",
"namespace", sts.Namespace,
"name", sts.Name,
"container", component.ComponentType().String(),
"index", componentContainerIdx,
)

env := component.ComponentSpec().PodEnvVars()
if len(env) == 0 {
env = nil
}
nodeSelector := component.ComponentSpec().NodeSelector()
if len(nodeSelector) == 0 {
nodeSelector = nil
}

readinessProbe := component.ComponentSpec().ReadinessProbe()
if readinessProbe == nil {
readinessProbe = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/status",
Port: intstr.FromInt(int(component.GetPort("http"))),
Scheme: "HTTP",
},
},
FailureThreshold: int32(3),
PeriodSeconds: int32(10),
TimeoutSeconds: int32(5),
SuccessThreshold: int32(1),
InitialDelaySeconds: int32(10),
}
}

for _, sVolume := range component.ComponentSpec().Volumes() {
found := false
for _, volume := range sts.Spec.Template.Spec.Volumes {
if volume.Name == sVolume.Name {
if diff := cmp.Diff(volume, sVolume); diff != "" {
klog.ErrorS(nil, "Waiting for NebulaCluster to be ready but StatefulSet volume not expected",
"namespace", sts.Namespace,
"name", sts.Name,
"volume", sVolume.Name,
"diff", fmt.Errorf("DeepEqual failed %s", diff),
)
return false
}
found = true
break
}
}
if !found {
klog.ErrorS(nil, "Waiting for NebulaCluster to be ready but StatefulSet volume not found",
"namespace", sts.Namespace,
"name", sts.Name,
"volume", sVolume.Name,
)
return false
}
}

for _, sVolumeMount := range component.ComponentSpec().VolumeMounts() {
found := false
for _, volumeMount := range sts.Spec.Template.Spec.Containers[componentContainerIdx].VolumeMounts {
if volumeMount.Name == sVolumeMount.Name {
if diff := cmp.Diff(volumeMount, sVolumeMount); diff != "" {
klog.ErrorS(nil, "Waiting for NebulaCluster to be ready but StatefulSet volumeMount not expected",
"namespace", sts.Namespace,
"name", sts.Name,
"volumeMount", sVolumeMount.Name,
"diff", fmt.Errorf("DeepEqual failed %s", diff),
)
return false
}
found = true
break
}
}
if !found {
klog.ErrorS(nil, "Waiting for NebulaCluster to be ready but StatefulSet volumeMount not found",
"namespace", sts.Namespace,
"name", sts.Name,
"volumeMount", sVolumeMount.Name,
)
return false
}
}

if err := e2ematcher.Struct(
sts,
map[string]any{
Expand All @@ -577,13 +667,23 @@ func isComponentStatefulSetExpected(ctx context.Context, cfg *envconf.Config, co
"Spec": map[string]any{
"Replicas": e2ematcher.ValidatorEq(component.ComponentSpec().Replicas()),
"Template": map[string]any{
"ObjectMeta": map[string]any{
"Annotations": e2ematcher.MapContainsMatchers(component.ComponentSpec().PodAnnotations()),
"Labels": e2ematcher.MapContainsMatchers(component.ComponentSpec().PodLabels()),
},
"Spec": map[string]any{
"Containers": map[string]any{
fmt.Sprint(componentContainerIdx): map[string]any{
"Image": e2ematcher.ValidatorEq(component.ComponentSpec().PodImage()),
"Resources": e2ematcher.DeepEqual(*component.ComponentSpec().Resources()),
"Image": e2ematcher.ValidatorEq(component.ComponentSpec().PodImage()),
"Resources": e2ematcher.DeepEqual(*component.ComponentSpec().Resources()),
"Env": e2ematcher.DeepEqual(env),
"ReadinessProbe": e2ematcher.DeepEqual(*readinessProbe),
"LivenessProbe": e2ematcher.DeepEqual(probeOrNil(component.ComponentSpec().LivenessProbe())),
},
},
"NodeSelector": e2ematcher.DeepEqual(nodeSelector),
"Affinity": e2ematcher.DeepEqual(affinityOrNil(component.ComponentSpec().Affinity())),
"Tolerations": e2ematcher.DeepEqual(component.ComponentSpec().Tolerations()),
},
},
},
Expand Down Expand Up @@ -773,3 +873,17 @@ func extractComponentConfig(r io.Reader, paramValuePattern *regexp.Regexp) (map[
}
return componentConfig, nil
}

func affinityOrNil(affinity *corev1.Affinity) any {
if affinity == nil {
return affinity
}
return *affinity
}

func probeOrNil(probe *corev1.Probe) any {
if probe == nil {
return probe
}
return *probe
}
Loading

0 comments on commit 4e53632

Please sign in to comment.