Skip to content

Commit

Permalink
NO-JIRA: hypershift:unitests: adjust controller unit-tests (#1105)
Browse files Browse the repository at this point in the history
* hypershift:component: remove receiver from function

We want to reuse this functions in the unit tests
so we remove the receiver and export it.

Signed-off-by: Talor Itzhak <[email protected]>

* error: change to pointer in error message

Report the accurate type in the returned error.
We are expeting pointer of Type and and not Type itself.

Signed-off-by: Talor Itzhak <[email protected]>

* unittests:controller: adjust for hypershift

We want same unit tests to be running for hypershift,
so we convert the tests into table and added one entry for OCP and one for HCP.

As for now most of the tests are not supported/qualified to be running on HCP and
are skipped for now.

But it is better and safer to have a low coverage than nothing.
We'll work to support additional tests in future PRs

Signed-off-by: Talor Itzhak <[email protected]>

---------

Signed-off-by: Talor Itzhak <[email protected]>
  • Loading branch information
Tal-or authored Jul 9, 2024
1 parent 86bde6b commit 35bfa29
Show file tree
Hide file tree
Showing 5 changed files with 992 additions and 873 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewHandler(cli client.Client, scheme *runtime.Scheme) components.Handler {
func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.EventRecorder, opts *components.Options) error {
profile, ok := obj.(*performancev2.PerformanceProfile)
if !ok {
return fmt.Errorf("wrong type conversion; want=PerformanceProfile got=%T", obj)
return fmt.Errorf("wrong type conversion; want=*PerformanceProfile got=%T", obj)
}

if profileutil.IsPaused(profile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h *handler) Exists(ctx context.Context, profileName string) bool {
func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.EventRecorder, options *components.Options) error {
instance, ok := obj.(*corev1.ConfigMap)
if !ok {
return fmt.Errorf("wrong type conversion; want=ConfigMap got=%T", obj)
return fmt.Errorf("wrong type conversion; want=*ConfigMap got=%T", obj)
}

s, ok := instance.Data[hypershift.TuningKey]
Expand Down Expand Up @@ -147,7 +147,7 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
}

if mcMutated != nil {
cm, err := h.encapsulateObjInConfigMap(instance, mfs.MachineConfig, profile.Name, mcoConfigMapConfigKey, ntoGeneratedMachineConfigLabel)
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.MachineConfig, profile.Name, mcoConfigMapConfigKey, ntoGeneratedMachineConfigLabel)
if err != nil {
return err
}
Expand All @@ -158,7 +158,7 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
}

if kcMutated != nil {
cm, err := h.encapsulateObjInConfigMap(instance, mfs.KubeletConfig, profile.Name, mcoConfigMapConfigKey, ntoGeneratedMachineConfigLabel)
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.KubeletConfig, profile.Name, mcoConfigMapConfigKey, ntoGeneratedMachineConfigLabel)
if err != nil {
return err
}
Expand All @@ -169,7 +169,7 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
}

if performanceTunedMutated != nil {
cm, err := h.encapsulateObjInConfigMap(instance, mfs.Tuned, profile.Name, tunedConfigMapConfigKey, tunedConfigMapLabel)
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.Tuned, profile.Name, tunedConfigMapConfigKey, tunedConfigMapLabel)
if err != nil {
return err
}
Expand All @@ -196,8 +196,8 @@ func (h *handler) getContainerRuntimeName(ctx context.Context, profile *performa
return mcov1.ContainerRuntimeDefaultRuntimeRunc, nil
}

func (h *handler) encapsulateObjInConfigMap(instance *corev1.ConfigMap, object client.Object, profileName, dataKey, objectLabel string) (*corev1.ConfigMap, error) {
encodedObj, err := hypershift.EncodeManifest(object, h.scheme)
func EncapsulateObjInConfigMap(scheme *runtime.Scheme, instance *corev1.ConfigMap, object client.Object, profileName, dataKey, objectLabel string) (*corev1.ConfigMap, error) {
encodedObj, err := hypershift.EncodeManifest(object, scheme)
if err != nil {
return nil, err
}
Expand All @@ -208,7 +208,7 @@ func (h *handler) encapsulateObjInConfigMap(instance *corev1.ConfigMap, object c

name := fmt.Sprintf("%s-%s", strings.ToLower(object.GetObjectKind().GroupVersionKind().Kind), instance.Name)
cm := configMapMeta(name, profileName, instance.GetNamespace(), nodePoolNamespacedName)
err = controllerutil.SetControllerReference(instance, cm, h.scheme)
err = controllerutil.SetControllerReference(instance, cm, scheme)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func NewWriter(c client.Client) Writer {
func (w *writer) Update(ctx context.Context, object client.Object, conditions []conditionsv1.Condition) error {
profile, ok := object.(*performancev2.PerformanceProfile)
if !ok {
return fmt.Errorf("wrong type conversion; want=PerformanceProfile got=%T", object)
return fmt.Errorf("wrong type conversion; want=*PerformanceProfile got=%T", object)
}
return w.update(ctx, profile, conditions)
}

func (w *writer) UpdateOwnedConditions(ctx context.Context, object client.Object) error {
profile, ok := object.(*performancev2.PerformanceProfile)
if !ok {
return fmt.Errorf("wrong type conversion; want=PerformanceProfile got=%T", object)
return fmt.Errorf("wrong type conversion; want=*PerformanceProfile got=%T", object)
}

// get kubelet false condition
Expand Down
Loading

0 comments on commit 35bfa29

Please sign in to comment.