Skip to content

Commit 2bbbfa5

Browse files
authored
Merge pull request #3 from varshaprasad96/brett-fix-unit-tests
✨ fix unit tests
2 parents f864421 + 174ef09 commit 2bbbfa5

5 files changed

+179
-109
lines changed

internal/.DS_Store

6 KB
Binary file not shown.

internal/controllers/clusterextension_controller.go

+39-36
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (r *ClusterExtensionReconciler) resolve(ctx context.Context, ext ocv1alpha1
412412
channelName := ext.Spec.Channel
413413
versionRange := ext.Spec.Version
414414

415-
installedBundle, err := r.installedBundle(ctx, allBundles, &ext)
415+
installedBundle, err := GetInstalledbundle(ctx, r.ActionClientGetter, allBundles, &ext)
416416
if err != nil {
417417
return nil, err
418418
}
@@ -450,11 +450,14 @@ func (r *ClusterExtensionReconciler) resolve(ctx context.Context, ext ocv1alpha1
450450
if err != nil {
451451
return nil, err
452452
}
453+
fmt.Println("upgrade error!!!!")
453454
upgradeErrorPrefix = fmt.Sprintf("error upgrading from currently installed version %q: ", installedBundleVersion.String())
454455
}
455456
if len(resultSet) == 0 {
457+
fmt.Println("empty resilt set!!")
456458
switch {
457459
case versionRange != "" && channelName != "":
460+
fmt.Println("here!!!")
458461
return nil, fmt.Errorf("%sno package %q matching version %q in channel %q found", upgradeErrorPrefix, packageName, versionRange, channelName)
459462
case versionRange != "":
460463
return nil, fmt.Errorf("%sno package %q matching version %q found", upgradeErrorPrefix, packageName, versionRange)
@@ -688,8 +691,41 @@ func clusterExtensionRequestsForCatalog(c client.Reader, logger logr.Logger) crh
688691
}
689692
}
690693

691-
func (r *ClusterExtensionReconciler) installedBundle(ctx context.Context, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
692-
cl, err := r.ActionClientGetter.ActionClientFor(ctx, ext)
694+
type releaseState string
695+
696+
const (
697+
stateNeedsInstall releaseState = "NeedsInstall"
698+
stateNeedsUpgrade releaseState = "NeedsUpgrade"
699+
stateUnchanged releaseState = "Unchanged"
700+
stateError releaseState = "Error"
701+
)
702+
703+
func (r *ClusterExtensionReconciler) getReleaseState(cl helmclient.ActionInterface, obj metav1.Object, chrt *chart.Chart, values chartutil.Values, post *postrenderer) (*release.Release, releaseState, error) {
704+
currentRelease, err := cl.Get(obj.GetName())
705+
if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) {
706+
return nil, stateError, err
707+
}
708+
if errors.Is(err, driver.ErrReleaseNotFound) {
709+
return nil, stateNeedsInstall, nil
710+
}
711+
712+
desiredRelease, err := cl.Upgrade(obj.GetName(), r.ReleaseNamespace, chrt, values, func(upgrade *action.Upgrade) error {
713+
upgrade.DryRun = true
714+
return nil
715+
}, helmclient.AppendUpgradePostRenderer(post))
716+
if err != nil {
717+
return currentRelease, stateError, err
718+
}
719+
if desiredRelease.Manifest != currentRelease.Manifest ||
720+
currentRelease.Info.Status == release.StatusFailed ||
721+
currentRelease.Info.Status == release.StatusSuperseded {
722+
return currentRelease, stateNeedsUpgrade, nil
723+
}
724+
return currentRelease, stateUnchanged, nil
725+
}
726+
727+
var GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
728+
cl, err := acg.ActionClientFor(ctx, ext)
693729
if err != nil {
694730
return nil, err
695731
}
@@ -725,39 +761,6 @@ func (r *ClusterExtensionReconciler) installedBundle(ctx context.Context, allBun
725761
return resultSet[0], nil
726762
}
727763

728-
type releaseState string
729-
730-
const (
731-
stateNeedsInstall releaseState = "NeedsInstall"
732-
stateNeedsUpgrade releaseState = "NeedsUpgrade"
733-
stateUnchanged releaseState = "Unchanged"
734-
stateError releaseState = "Error"
735-
)
736-
737-
func (r *ClusterExtensionReconciler) getReleaseState(cl helmclient.ActionInterface, obj metav1.Object, chrt *chart.Chart, values chartutil.Values, post *postrenderer) (*release.Release, releaseState, error) {
738-
currentRelease, err := cl.Get(obj.GetName())
739-
if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) {
740-
return nil, stateError, err
741-
}
742-
if errors.Is(err, driver.ErrReleaseNotFound) {
743-
return nil, stateNeedsInstall, nil
744-
}
745-
746-
desiredRelease, err := cl.Upgrade(obj.GetName(), r.ReleaseNamespace, chrt, values, func(upgrade *action.Upgrade) error {
747-
upgrade.DryRun = true
748-
return nil
749-
}, helmclient.AppendUpgradePostRenderer(post))
750-
if err != nil {
751-
return currentRelease, stateError, err
752-
}
753-
if desiredRelease.Manifest != currentRelease.Manifest ||
754-
currentRelease.Info.Status == release.StatusFailed ||
755-
currentRelease.Info.Status == release.StatusSuperseded {
756-
return currentRelease, stateNeedsUpgrade, nil
757-
}
758-
return currentRelease, stateUnchanged, nil
759-
}
760-
761764
type errRequiredResourceNotFound struct {
762765
error
763766
}

internal/controllers/clusterextension_controller_test.go

+95-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
ctrl "sigs.k8s.io/controller-runtime"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
2121

22+
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
2223
"github.com/operator-framework/operator-registry/alpha/declcfg"
2324
"github.com/operator-framework/operator-registry/alpha/property"
2425
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
@@ -416,11 +417,16 @@ func verifyConditionsInvariants(t *testing.T, ext *ocv1alpha1.ClusterExtension)
416417
func TestClusterExtensionUpgrade(t *testing.T) {
417418
cl, reconciler := newClientAndReconciler(t)
418419
mockUnpacker := unpacker.(*MockUnpacker)
419-
// Set up the Unpack method to return a result with StateUnpacked
420+
// Set up the Unpack method to return a result with StateUnpackPending
420421
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
421422
State: source.StatePending,
422423
}, nil)
423424
ctx := context.Background()
425+
defer func() {
426+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
427+
return nil, nil
428+
}
429+
}()
424430

425431
t.Run("semver upgrade constraints enforcement of upgrades within major version", func(t *testing.T) {
426432
defer featuregatetesting.SetFeatureGateDuringTest(t, features.OperatorControllerFeatureGate, features.ForceSemverUpgradeConstraints, true)()
@@ -470,6 +476,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
470476
err = cl.Update(ctx, clusterExtension)
471477
require.NoError(t, err)
472478

479+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
480+
return &catalogmetadata.Bundle{
481+
Bundle: declcfg.Bundle{
482+
Name: "operatorhub/prometheus/beta/1.0.0",
483+
Package: "prometheus",
484+
Image: "quay.io/operatorhubio/[email protected]",
485+
Properties: []property.Property{
486+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
487+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
488+
},
489+
},
490+
CatalogName: "fake-catalog",
491+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
492+
}, nil
493+
}
494+
473495
// Run reconcile again
474496
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
475497
require.Error(t, err)
@@ -488,7 +510,7 @@ func TestClusterExtensionUpgrade(t *testing.T) {
488510
require.NotNil(t, cond)
489511
assert.Equal(t, metav1.ConditionFalse, cond.Status)
490512
assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason)
491-
assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"2.0.0\" found in channel \"beta\"", cond.Message)
513+
assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"2.0.0\" in channel \"beta\" found", cond.Message)
492514

493515
// Valid update skipping one version
494516
clusterExtension.Spec.Version = "1.2.0"
@@ -563,6 +585,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
563585
err = cl.Update(ctx, clusterExtension)
564586
require.NoError(t, err)
565587

588+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
589+
return &catalogmetadata.Bundle{
590+
Bundle: declcfg.Bundle{
591+
Name: "operatorhub/prometheus/beta/1.0.0",
592+
Package: "prometheus",
593+
Image: "quay.io/operatorhubio/[email protected]",
594+
Properties: []property.Property{
595+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
596+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
597+
},
598+
},
599+
CatalogName: "fake-catalog",
600+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
601+
}, nil
602+
}
603+
566604
// Run reconcile again
567605
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
568606
require.Error(t, err)
@@ -581,7 +619,7 @@ func TestClusterExtensionUpgrade(t *testing.T) {
581619
require.NotNil(t, cond)
582620
assert.Equal(t, metav1.ConditionFalse, cond.Status)
583621
assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason)
584-
assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"1.2.0\" found in channel \"beta\"", cond.Message)
622+
assert.Equal(t, "error upgrading from currently installed version \"1.0.0\": no package \"prometheus\" matching version \"1.2.0\" in channel \"beta\" found", cond.Message)
585623

586624
// Valid update skipping one version
587625
clusterExtension.Spec.Version = "1.0.1"
@@ -670,6 +708,22 @@ func TestClusterExtensionUpgrade(t *testing.T) {
670708
err = cl.Update(ctx, clusterExtension)
671709
require.NoError(t, err)
672710

711+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
712+
return &catalogmetadata.Bundle{
713+
Bundle: declcfg.Bundle{
714+
Name: "operatorhub/prometheus/beta/1.0.0",
715+
Package: "prometheus",
716+
Image: "quay.io/operatorhubio/[email protected]",
717+
Properties: []property.Property{
718+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.0"}`)},
719+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
720+
},
721+
},
722+
CatalogName: "fake-catalog",
723+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
724+
}, nil
725+
}
726+
673727
// Run reconcile again
674728
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
675729
require.NoError(t, err)
@@ -701,6 +755,11 @@ func TestClusterExtensionDowngrade(t *testing.T) {
701755
State: source.StatePending,
702756
}, nil)
703757
ctx := context.Background()
758+
defer func() {
759+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
760+
return nil, nil
761+
}
762+
}()
704763

705764
t.Run("enforce upgrade constraints", func(t *testing.T) {
706765
for _, tt := range []struct {
@@ -761,6 +820,22 @@ func TestClusterExtensionDowngrade(t *testing.T) {
761820
err = cl.Update(ctx, clusterExtension)
762821
require.NoError(t, err)
763822

823+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
824+
return &catalogmetadata.Bundle{
825+
Bundle: declcfg.Bundle{
826+
Name: "operatorhub/prometheus/beta/1.0.1",
827+
Package: "prometheus",
828+
Image: "quay.io/operatorhubio/[email protected]",
829+
Properties: []property.Property{
830+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"1.0.1"}`)},
831+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
832+
},
833+
},
834+
CatalogName: "fake-catalog",
835+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
836+
}, nil
837+
}
838+
764839
// Run reconcile again
765840
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
766841
require.Error(t, err)
@@ -779,7 +854,7 @@ func TestClusterExtensionDowngrade(t *testing.T) {
779854
require.NotNil(t, cond)
780855
assert.Equal(t, metav1.ConditionFalse, cond.Status)
781856
assert.Equal(t, ocv1alpha1.ReasonResolutionFailed, cond.Reason)
782-
assert.Equal(t, "error upgrading from currently installed version \"1.0.1\": no package \"prometheus\" matching version \"1.0.0\" found in channel \"beta\"", cond.Message)
857+
assert.Equal(t, "error upgrading from currently installed version \"1.0.1\": no package \"prometheus\" matching version \"1.0.0\" in channel \"beta\" found", cond.Message)
783858
})
784859
}
785860
})
@@ -844,6 +919,22 @@ func TestClusterExtensionDowngrade(t *testing.T) {
844919
err = cl.Update(ctx, clusterExtension)
845920
require.NoError(t, err)
846921

922+
controllers.GetInstalledbundle = func(ctx context.Context, acg helmclient.ActionClientGetter, allBundles []*catalogmetadata.Bundle, ext *ocv1alpha1.ClusterExtension) (*catalogmetadata.Bundle, error) {
923+
return &catalogmetadata.Bundle{
924+
Bundle: declcfg.Bundle{
925+
Name: "operatorhub/prometheus/beta/2.0.0",
926+
Package: "prometheus",
927+
Image: "quay.io/operatorhubio/[email protected]",
928+
Properties: []property.Property{
929+
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"prometheus","version":"2.0.0"}`)},
930+
{Type: property.TypeGVK, Value: json.RawMessage(`[]`)},
931+
},
932+
},
933+
CatalogName: "fake-catalog",
934+
InChannels: []*catalogmetadata.Channel{&prometheusBetaChannel},
935+
}, nil
936+
}
937+
847938
// Run reconcile again
848939
res, err = reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
849940
require.NoError(t, err)

internal/controllers/clusterextension_status.go

-69
This file was deleted.

0 commit comments

Comments
 (0)