Skip to content

⚠️ Deal with panics during unit test #767

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

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions api/v1alpha1/clusterextension_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func init() {
ReasonErrorGettingReleaseState,
ReasonUpgradeFailed,
ReasonCreateDynamicWatchFailed,
ReasonBundleLoadFailed,
ReasonErrorGettingClient,
)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func main() {
acg, err := helmclient.NewActionClientGetter(cfgGetter)
if err != nil {
setupLog.Error(err, "unable to create helm client")
os.Exit(1)
}

if systemNamespace == "" {
Expand All @@ -143,6 +144,7 @@ func main() {
unpacker, err := source.NewDefaultUnpacker(mgr, systemNamespace, unpackImage)
if err != nil {
setupLog.Error(err, "unable to create unpacker")
os.Exit(1)
}

storageURL, err := url.Parse(fmt.Sprintf("%s/bundles/", httpExternalAddr))
Expand Down
3 changes: 3 additions & 0 deletions internal/controllers/clusterextension_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (r *ClusterExtensionReconciler) Reconcile(ctx context.Context, req ctrl.Req

reconciledExt := existingExt.DeepCopy()
res, reconcileErr := r.reconcile(ctx, reconciledExt)
if reconcileErr != nil {
return ctrl.Result{}, reconcileErr
}

// Do checks before any Update()s, as Update() may modify the resource structure!
updateStatus := !equality.Semantic.DeepEqual(existingExt.Status, reconciledExt.Status)
Expand Down
27 changes: 24 additions & 3 deletions internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ import (
"path/filepath"
"testing"

"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
carvelv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/operator-framework/deppy/pkg/deppy/solver"
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"

ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/controllers"
"github.com/operator-framework/operator-controller/internal/rukpak/source"
"github.com/operator-framework/operator-controller/internal/rukpak/util"
testutil "github.com/operator-framework/operator-controller/test/util"
)

Expand All @@ -53,9 +59,11 @@ func newClientAndReconciler(t *testing.T) (client.Client, *controllers.ClusterEx
cl := newClient(t)
fakeCatalogClient := testutil.NewFakeCatalogClient(testBundleList)
reconciler := &controllers.ClusterExtensionReconciler{
Client: cl,
BundleProvider: &fakeCatalogClient,
Scheme: sch,
Client: cl,
BundleProvider: &fakeCatalogClient,
Scheme: sch,
ActionClientGetter: acg,
Unpacker: unp,
}
return cl, reconciler
}
Expand All @@ -73,6 +81,8 @@ func newClientAndExtensionReconciler(t *testing.T) (client.Client, *controllers.
var (
sch *runtime.Scheme
cfg *rest.Config
acg helmclient.ActionClientGetter
unp source.Unpacker
)

func TestMain(m *testing.M) {
Expand All @@ -96,6 +106,17 @@ func TestMain(m *testing.M) {
utilruntime.Must(corev1.AddToScheme(sch))
utilruntime.Must(carvelv1alpha1.AddToScheme(sch))

rm := meta.NewDefaultRESTMapper(nil)
cfgGetter, err := helmclient.NewActionConfigGetter(cfg, rm, logr.Logger{})
utilruntime.Must(err)
acg, err = helmclient.NewActionClientGetter(cfgGetter)
utilruntime.Must(err)

mgr, err := manager.New(cfg, manager.Options{})
utilruntime.Must(err)
unp, err = source.NewDefaultUnpacker(mgr, util.DefaultSystemNamespace, util.DefaultUnpackImage)
utilruntime.Must(err)

code := m.Run()
utilruntime.Must(testEnv.Stop())
os.Exit(code)
Expand Down