Skip to content

Commit

Permalink
Added unit tests for sveltos status transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
wahabmk committed Nov 7, 2024
1 parent a38f3da commit 4e9f82b
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 55 deletions.
55 changes: 0 additions & 55 deletions internal/sveltos/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ package sveltos

import (
"context"
"errors"
"fmt"
"math"
"unsafe"

sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -227,56 +225,3 @@ func priorityToTier(priority int32) (int32, error) {

return 0, fmt.Errorf("invalid value %d, priority has to be between %d and %d", priority, mini, maxi)
}

// SetStatusConditions transforms status from Sveltos ClusterSummary
// object and sets it into the provided list of conditions.
func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary, conditions *[]metav1.Condition) error {
if summary == nil {
return errors.New("nil summary provided")
}

for _, x := range summary.Status.FeatureSummaries {
msg := ""
status := metav1.ConditionTrue
if x.FailureMessage != nil && *x.FailureMessage != "" {
msg = *x.FailureMessage
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
Message: msg,
Reason: string(x.Status),
Status: status,
Type: string(x.FeatureID),
})
}

for _, x := range summary.Status.HelmReleaseSummaries {
msg := "Release " + x.ReleaseNamespace + "/" + x.ReleaseName
status := metav1.ConditionTrue
if x.ConflictMessage != "" {
msg += ": " + x.ConflictMessage
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
Message: msg,
Reason: string(x.Status),
Status: status,
Type: HelmReleaseReadyConditionType(x.ReleaseNamespace, x.ReleaseName),
})
}

return nil
}

// HelmReleaseReadyConditionType returns a SveltosHelmReleaseReady
// type per service to be used in status conditions.
func HelmReleaseReadyConditionType(releaseNamespace, releaseName string) string {
return fmt.Sprintf(
"%s.%s/%s",
releaseNamespace,
releaseName,
hmc.SveltosHelmReleaseReadyCondition,
)
}
86 changes: 86 additions & 0 deletions internal/sveltos/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2024
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sveltos

import (
"errors"
"fmt"

sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
)

// SetStatusConditions transforms status from Sveltos ClusterSummary
// object and sets it into the provided list of conditions.
func SetStatusConditions(summary *sveltosv1beta1.ClusterSummary, conditions *[]metav1.Condition) error {
if summary == nil {
return errors.New("nil summary provided")
}

for _, x := range summary.Status.FeatureSummaries {
msg := ""
status := metav1.ConditionTrue
if x.FailureMessage != nil && *x.FailureMessage != "" {
msg = *x.FailureMessage
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
Message: msg,
Reason: string(x.Status),
Status: status,
Type: string(x.FeatureID),
})
}

for _, x := range summary.Status.HelmReleaseSummaries {
status := metav1.ConditionTrue
if x.ConflictMessage != "" {
status = metav1.ConditionFalse
}

apimeta.SetStatusCondition(conditions, metav1.Condition{
Message: helmReleaseConditionMessage(x.ReleaseNamespace, x.ReleaseName, x.ConflictMessage),
Reason: string(x.Status),
Status: status,
Type: HelmReleaseReadyConditionType(x.ReleaseNamespace, x.ReleaseName),
})
}

return nil
}

// HelmReleaseReadyConditionType returns a SveltosHelmReleaseReady
// type per service to be used in status conditions.
func HelmReleaseReadyConditionType(releaseNamespace, releaseName string) string {
return fmt.Sprintf(
"%s.%s/%s",
releaseNamespace,
releaseName,
hmc.SveltosHelmReleaseReadyCondition,
)
}

func helmReleaseConditionMessage(releaseNamespace, releaseName, conflictMsg string) string {
msg := "Release " + releaseNamespace + "/" + releaseName
if conflictMsg != "" {
msg += ": " + conflictMsg
}

return msg
}
146 changes: 146 additions & 0 deletions internal/sveltos/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2024
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sveltos

import (
"testing"

sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestSetStatusConditions(t *testing.T) {
releaseNamespace := "testnamespace"
releaseName := "testname"
conflictMsg := "some conflict message"
failureMesg := "some failure message"

for _, tc := range []struct {
err error
expectCondition metav1.Condition
name string
summary sveltosv1beta1.ClusterSummary
}{
{
name: "sveltos featuresummary provisioning",
summary: sveltosv1beta1.ClusterSummary{
Status: sveltosv1beta1.ClusterSummaryStatus{
FeatureSummaries: []sveltosv1beta1.FeatureSummary{
{
FeatureID: sveltosv1beta1.FeatureHelm,
Status: sveltosv1beta1.FeatureStatusProvisioning,
},
},
},
},
expectCondition: metav1.Condition{
Type: string(sveltosv1beta1.FeatureHelm),
Status: metav1.ConditionTrue,
Reason: string(sveltosv1beta1.FeatureStatusProvisioning),
},
},
{
name: "sveltos featuresummary provisioned",
summary: sveltosv1beta1.ClusterSummary{
Status: sveltosv1beta1.ClusterSummaryStatus{
FeatureSummaries: []sveltosv1beta1.FeatureSummary{
{
FeatureID: sveltosv1beta1.FeatureHelm,
Status: sveltosv1beta1.FeatureStatusProvisioned,
},
},
},
},
expectCondition: metav1.Condition{
Type: string(sveltosv1beta1.FeatureHelm),
Status: metav1.ConditionTrue,
Reason: string(sveltosv1beta1.FeatureStatusProvisioned),
},
},
{
name: "sveltos featuresummary failed",
summary: sveltosv1beta1.ClusterSummary{
Status: sveltosv1beta1.ClusterSummaryStatus{
FeatureSummaries: []sveltosv1beta1.FeatureSummary{
{
FeatureID: sveltosv1beta1.FeatureHelm,
Status: sveltosv1beta1.FeatureStatusFailed,
FailureMessage: &failureMesg,
},
},
},
},
expectCondition: metav1.Condition{
Type: string(sveltosv1beta1.FeatureHelm),
Status: metav1.ConditionFalse,
Reason: string(sveltosv1beta1.FeatureStatusFailed),
Message: failureMesg,
},
},
{
name: "sveltos helmreleasesummary managing",
summary: sveltosv1beta1.ClusterSummary{
Status: sveltosv1beta1.ClusterSummaryStatus{
HelmReleaseSummaries: []sveltosv1beta1.HelmChartSummary{
{
ReleaseNamespace: releaseNamespace,
ReleaseName: releaseName,
Status: sveltosv1beta1.HelmChartStatusManaging,
},
},
},
},
expectCondition: metav1.Condition{
Type: HelmReleaseReadyConditionType(releaseNamespace, releaseName),
Status: metav1.ConditionTrue,
Reason: string(sveltosv1beta1.HelmChartStatusManaging),
Message: helmReleaseConditionMessage(releaseNamespace, releaseName, ""),
},
},
{
name: "sveltos helmreleasesummary conflict",
summary: sveltosv1beta1.ClusterSummary{
Status: sveltosv1beta1.ClusterSummaryStatus{
HelmReleaseSummaries: []sveltosv1beta1.HelmChartSummary{
{
ReleaseNamespace: releaseNamespace,
ReleaseName: releaseName,
Status: sveltosv1beta1.HelmChartStatusConflict,
ConflictMessage: conflictMsg,
},
},
},
},
expectCondition: metav1.Condition{
Type: HelmReleaseReadyConditionType(releaseNamespace, releaseName),
Status: metav1.ConditionFalse,
Reason: string(sveltosv1beta1.HelmChartStatusConflict),
Message: helmReleaseConditionMessage(releaseNamespace, releaseName, conflictMsg),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
conditions := []metav1.Condition{}
require.NoError(t, SetStatusConditions(&tc.summary, &conditions))
assert.Len(t, conditions, 1)
assert.Equal(t, tc.expectCondition.Type, conditions[0].Type)
assert.Equal(t, tc.expectCondition.Status, conditions[0].Status)
assert.Equal(t, tc.expectCondition.Reason, conditions[0].Reason)
assert.Equal(t, tc.expectCondition.Message, conditions[0].Message)
})
}
}

0 comments on commit 4e9f82b

Please sign in to comment.