Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mobiledeveloperconsole
- import (
- "context"
- "fmt"
- "testing"
- "github.com/integr8ly/integreatly-operator/pkg/apis/integreatly/v1alpha1"
- "github.com/integr8ly/integreatly-operator/pkg/controller/installation/marketplace"
- "github.com/integr8ly/integreatly-operator/pkg/controller/installation/products/config"
- "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
- "github.com/pkg/errors"
- "k8s.io/apimachinery/pkg/runtime"
- "sigs.k8s.io/controller-runtime/pkg/client"
- integreatlyv1alpha1 "github.com/integr8ly/integreatly-operator/pkg/apis/integreatly/v1alpha1"
- mdc "github.com/integr8ly/integreatly-operator/pkg/apis/mdc/v1alpha1"
- moqclient "github.com/integr8ly/integreatly-operator/pkg/client"
- v1Route "github.com/openshift/api/route/v1"
- coreosv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
- operatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
- marketplacev1 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
- corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- pkgclient "sigs.k8s.io/controller-runtime/pkg/client"
- fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
- )
- func basicConfigMock() *config.ConfigReadWriterMock {
- return &config.ConfigReadWriterMock{
- ReadMobileDeveloperConsoleFunc: func() (ready *config.MobileDeveloperConsole, e error) {
- return config.NewMobileDeveloperConsole(config.ProductConfig{}), nil
- },
- WriteConfigFunc: func(config config.ConfigReadable) error {
- return nil
- },
- }
- }
- func basicInstallation() *v1alpha1.Installation {
- return &v1alpha1.Installation{
- ObjectMeta: metav1.ObjectMeta{
- Name: "installation",
- Namespace: defaultInstallationNamespace,
- },
- TypeMeta: metav1.TypeMeta{
- Kind: "installation",
- APIVersion: v1alpha1.SchemeGroupVersion.String(),
- },
- }
- }
- func getBuildScheme() (*runtime.Scheme, error) {
- scheme := runtime.NewScheme()
- err := integreatlyv1alpha1.SchemeBuilder.AddToScheme(scheme)
- err = operatorsv1alpha1.AddToScheme(scheme)
- err = marketplacev1.SchemeBuilder.AddToScheme(scheme)
- err = corev1.SchemeBuilder.AddToScheme(scheme)
- err = coreosv1.SchemeBuilder.AddToScheme(scheme)
- err = mdc.SchemeBuilder.AddToScheme(scheme)
- err = v1Route.AddToScheme(scheme)
- return scheme, err
- }
- func TestReconciler_config(t *testing.T) {
- cases := []struct {
- Name string
- ExpectError bool
- ExpectedError string
- ExpectedStatus v1alpha1.StatusPhase
- FakeConfig *config.ConfigReadWriterMock
- FakeClient pkgclient.Client
- FakeMPM *marketplace.MarketplaceInterfaceMock
- Installation *v1alpha1.Installation
- }{
- {
- Name: "test error on failed config",
- ExpectError: true,
- ExpectedError: "could not read mobile developer console: could not read mdc config",
- ExpectedStatus: v1alpha1.PhaseFailed,
- Installation: &v1alpha1.Installation{},
- FakeClient: fakeclient.NewFakeClient(),
- FakeConfig: &config.ConfigReadWriterMock{
- ReadMobileDeveloperConsoleFunc: func() (ready *config.MobileDeveloperConsole, e error) {
- return nil, errors.New("could not read mdc config")
- },
- },
- },
- {
- Name: "test namespace is set without fail",
- Installation: &v1alpha1.Installation{},
- FakeClient: fakeclient.NewFakeClient(),
- FakeConfig: &config.ConfigReadWriterMock{
- ReadMobileDeveloperConsoleFunc: func() (ready *config.MobileDeveloperConsole, e error) {
- return config.NewMobileDeveloperConsole(config.ProductConfig{
- "NAMESPACE": "",
- }), nil
- },
- },
- },
- {
- Name: "test subscription phase with error from mpm",
- ExpectedStatus: v1alpha1.PhaseFailed,
- ExpectError: true,
- Installation: &v1alpha1.Installation{},
- FakeMPM: &marketplace.MarketplaceInterfaceMock{
- InstallOperatorFunc: func(ctx context.Context, serverClient pkgclient.Client, owner ownerutil.Owner, os marketplacev1.OperatorSource, t marketplace.Target, operatorGroupNamespaces []string, approvalStrategy operatorsv1alpha1.Approval) error {
- return errors.New("dummy error")
- },
- },
- FakeClient: fakeclient.NewFakeClient(),
- FakeConfig: basicConfigMock(),
- },
- }
- for _, tc := range cases {
- t.Run(tc.Name, func(t *testing.T) {
- _, err := NewReconciler(tc.FakeConfig, tc.Installation, tc.FakeMPM)
- if err != nil && err.Error() != tc.ExpectedError {
- t.Fatalf("unexpected error : '%v', expected: '%v'", err, tc.ExpectedError)
- }
- if err == nil && tc.ExpectedError != "" {
- t.Fatalf("expected error '%v' and got nil", tc.ExpectedError)
- }
- })
- }
- }
- func TestReconciler_reconcileCustomResource(t *testing.T) {
- scheme, err := getBuildScheme()
- if err != nil {
- t.Fatal(err)
- }
- cases := []struct {
- Name string
- FakeClient pkgclient.Client
- FakeConfig *config.ConfigReadWriterMock
- Installation *v1alpha1.Installation
- ExpectError bool
- ExpectedStatus v1alpha1.StatusPhase
- FakeMPM *marketplace.MarketplaceInterfaceMock
- }{
- {
- Name: "Test reconcile custom resource returns in progress when successful created",
- FakeClient: fakeclient.NewFakeClientWithScheme(scheme),
- FakeConfig: basicConfigMock(),
- Installation: &v1alpha1.Installation{},
- ExpectedStatus: v1alpha1.PhaseCompleted,
- },
- {
- Name: "Test reconcile custom resource returns failed when cr status is failed",
- FakeClient: &moqclient.SigsClientInterfaceMock{
- CreateFunc: func(ctx context.Context, obj runtime.Object) error {
- return errors.New("dummy create error")
- },
- },
- FakeConfig: basicConfigMock(),
- Installation: &v1alpha1.Installation{},
- ExpectedStatus: v1alpha1.PhaseFailed,
- ExpectError: true,
- },
- }
- for _, tc := range cases {
- t.Run(tc.Name, func(t *testing.T) {
- reconciler, err := NewReconciler(tc.FakeConfig, tc.Installation, tc.FakeMPM)
- if err != nil {
- t.Fatal("unexpected err ", err)
- }
- phase, err := reconciler.reconcileCustomResource(context.TODO(), tc.FakeClient)
- if tc.ExpectError && err == nil {
- t.Fatal("expected an error but got none")
- }
- if !tc.ExpectError && err != nil {
- t.Fatal("expected no error but got one ", err)
- }
- if tc.ExpectedStatus != phase {
- t.Fatal("expected phase ", tc.ExpectedStatus, " but got ", phase)
- }
- })
- }
- }
- func TestReconciler_handleProgress(t *testing.T) {
- scheme, err := getBuildScheme()
- if err != nil {
- t.Fatal(err)
- }
- unreadyPods := []runtime.Object{}
- for i := 0; i < 2; i++ {
- unreadyPods = append(unreadyPods, &corev1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: fmt.Sprintf("%s-%d", resourceName, i),
- Namespace: defaultInstallationNamespace,
- },
- Status: corev1.PodStatus{
- Conditions: []corev1.PodCondition{
- corev1.PodCondition{
- Type: corev1.ContainersReady,
- Status: corev1.ConditionUnknown,
- },
- },
- },
- })
- }
- readyPods := []runtime.Object{}
- for i := 0; i < 2; i++ {
- readyPods = append(readyPods, &corev1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: fmt.Sprintf("%s-%d", resourceName, i),
- Namespace: defaultInstallationNamespace,
- },
- Status: corev1.PodStatus{
- Conditions: []corev1.PodCondition{
- corev1.PodCondition{
- Type: corev1.ContainersReady,
- Status: corev1.ConditionTrue,
- },
- },
- },
- })
- }
- cases := []struct {
- Name string
- ExpectError bool
- ExpectedStatus v1alpha1.StatusPhase
- ExpectedError string
- FakeConfig *config.ConfigReadWriterMock
- FakeClient client.Client
- FakeMPM *marketplace.MarketplaceInterfaceMock
- Installation *v1alpha1.Installation
- }{
- {
- Name: "test failure to list pods",
- ExpectedStatus: v1alpha1.PhaseFailed,
- ExpectedError: "failed to check mdc installation",
- ExpectError: true,
- FakeClient: &moqclient.SigsClientInterfaceMock{
- ListFunc: func(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
- return errors.New("dummy create error")
- },
- },
- FakeConfig: basicConfigMock(),
- Installation: &v1alpha1.Installation{},
- },
- {
- Name: "test unready pods returns phase in progress",
- ExpectedStatus: v1alpha1.PhaseInProgress,
- FakeClient: moqclient.NewSigsClientMoqWithScheme(scheme, unreadyPods...),
- FakeConfig: basicConfigMock(),
- Installation: &v1alpha1.Installation{},
- },
- {
- Name: "test ready pods returns phase complete",
- ExpectedStatus: v1alpha1.PhaseCompleted,
- FakeClient: moqclient.NewSigsClientMoqWithScheme(scheme, readyPods...),
- FakeConfig: basicConfigMock(),
- Installation: &v1alpha1.Installation{},
- },
- }
- for _, tc := range cases {
- t.Run(tc.Name, func(t *testing.T) {
- reconciler, err := NewReconciler(tc.FakeConfig, tc.Installation, tc.FakeMPM)
- if err != nil && err.Error() != tc.ExpectedError {
- t.Fatalf("unexpected error : '%v', expected: '%v'", err, tc.ExpectedError)
- }
- status, err := reconciler.handleProgress(context.TODO(), tc.FakeClient)
- if err != nil && !tc.ExpectError {
- t.Fatalf("expected error but got one: %v", err)
- }
- if err == nil && tc.ExpectError {
- t.Fatal("expected error but got none")
- }
- if status != tc.ExpectedStatus {
- t.Fatalf("Expected status: '%v', got: '%v'", tc.ExpectedStatus, status)
- }
- })
- }
- }
- func TestReconciler_fullReconcile(t *testing.T) {
- scheme, err := getBuildScheme()
- if err != nil {
- t.Fatal(err)
- }
- // initialise runtime objects
- objs := []runtime.Object{}
- objs = append(objs, &corev1.Namespace{
- ObjectMeta: metav1.ObjectMeta{
- Name: defaultInstallationNamespace,
- OwnerReferences: []metav1.OwnerReference{
- {
- Name: "installation",
- APIVersion: v1alpha1.SchemeGroupVersion.String(),
- },
- },
- },
- Status: corev1.NamespaceStatus{
- Phase: corev1.NamespaceActive,
- },
- }, &v1Route.Route{
- TypeMeta: metav1.TypeMeta{
- Kind: "Route",
- APIVersion: "v1",
- },
- ObjectMeta: metav1.ObjectMeta{
- Namespace: defaultInstallationNamespace,
- Name: mdcRouteName,
- },
- })
- for i := 0; i < 2; i++ {
- objs = append(objs, &corev1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: fmt.Sprintf("%s-%d", resourceName, i),
- Namespace: defaultInstallationNamespace,
- },
- Status: corev1.PodStatus{
- Conditions: []corev1.PodCondition{
- corev1.PodCondition{
- Type: corev1.ContainersReady,
- Status: corev1.ConditionTrue,
- },
- },
- },
- })
- }
- cases := []struct {
- Name string
- ExpectError bool
- ExpectedStatus v1alpha1.StatusPhase
- ExpectedError string
- FakeConfig *config.ConfigReadWriterMock
- FakeClient client.Client
- FakeMPM *marketplace.MarketplaceInterfaceMock
- Installation *v1alpha1.Installation
- Product *v1alpha1.InstallationProductStatus
- }{
- {
- Name: "test successful reconcile",
- ExpectedStatus: v1alpha1.PhaseCompleted,
- FakeClient: moqclient.NewSigsClientMoqWithScheme(scheme, objs...),
- FakeConfig: basicConfigMock(),
- FakeMPM: &marketplace.MarketplaceInterfaceMock{
- InstallOperatorFunc: func(ctx context.Context, serverClient pkgclient.Client, owner ownerutil.Owner, os marketplacev1.OperatorSource, t marketplace.Target, operatorGroupNamespaces []string, approvalStrategy operatorsv1alpha1.Approval) error {
- return nil
- },
- GetSubscriptionInstallPlanFunc: func(ctx context.Context, serverClient client.Client, subName string, ns string) (plan *operatorsv1alpha1.InstallPlan, subscription *operatorsv1alpha1.Subscription, e error) {
- return &operatorsv1alpha1.InstallPlan{
- ObjectMeta: metav1.ObjectMeta{
- Name: "mdc-install-plan",
- },
- Status: operatorsv1alpha1.InstallPlanStatus{
- Phase: operatorsv1alpha1.InstallPlanPhaseComplete,
- },
- }, &operatorsv1alpha1.Subscription{
- Status: operatorsv1alpha1.SubscriptionStatus{
- Install: &operatorsv1alpha1.InstallPlanReference{
- Name: "mdc-install-plan",
- },
- },
- }, nil
- },
- },
- Installation: basicInstallation(),
- Product: &v1alpha1.InstallationProductStatus{},
- },
- }
- for _, tc := range cases {
- t.Run(tc.Name, func(t *testing.T) {
- reconciler, err := NewReconciler(tc.FakeConfig, tc.Installation, tc.FakeMPM)
- if err != nil && err.Error() != tc.ExpectedError {
- t.Fatalf("unexpected error : '%v', expected: '%v'", err, tc.ExpectedError)
- }
- status, err := reconciler.Reconcile(context.TODO(), tc.Installation, tc.Product, tc.FakeClient)
- if err != nil && !tc.ExpectError {
- t.Fatalf("expected error but got one: %v", err)
- }
- if err == nil && tc.ExpectError {
- t.Fatal("expected error but got none")
- }
- if status != tc.ExpectedStatus {
- t.Fatalf("Expected status: '%v', got: '%v'", tc.ExpectedStatus, status)
- }
- })
- }
- }
- func TestReconciler_testPhases(t *testing.T) {
- scheme, err := getBuildScheme()
- if err != nil {
- t.Fatal(err)
- }
- cases := []struct {
- Name string
- ExpectedStatus v1alpha1.StatusPhase
- FakeConfig *config.ConfigReadWriterMock
- FakeClient pkgclient.Client
- FakeMPM *marketplace.MarketplaceInterfaceMock
- Installation *v1alpha1.Installation
- Product *v1alpha1.InstallationProductStatus
- }{
- {
- Name: "test namespace terminating returns phase in progress",
- ExpectedStatus: v1alpha1.PhaseInProgress,
- Installation: basicInstallation(),
- FakeClient: moqclient.NewSigsClientMoqWithScheme(scheme, &corev1.Namespace{
- ObjectMeta: metav1.ObjectMeta{
- Name: defaultInstallationNamespace,
- OwnerReferences: []metav1.OwnerReference{
- {
- Name: "installation",
- APIVersion: v1alpha1.SchemeGroupVersion.String(),
- },
- },
- },
- Status: corev1.NamespaceStatus{
- Phase: corev1.NamespaceTerminating,
- },
- }),
- FakeConfig: basicConfigMock(),
- FakeMPM: &marketplace.MarketplaceInterfaceMock{
- InstallOperatorFunc: func(ctx context.Context, serverClient pkgclient.Client, owner ownerutil.Owner, os marketplacev1.OperatorSource, t marketplace.Target, operatorGroupNamespaces []string, approvalStrategy operatorsv1alpha1.Approval) error {
- return nil
- },
- GetSubscriptionInstallPlanFunc: func(ctx context.Context, serverClient client.Client, subName string, ns string) (plan *operatorsv1alpha1.InstallPlan, subscription *operatorsv1alpha1.Subscription, e error) {
- return &operatorsv1alpha1.InstallPlan{}, &operatorsv1alpha1.Subscription{}, nil
- },
- },
- Product: &v1alpha1.InstallationProductStatus{},
- },
- {
- Name: "test subscription creating returns phase in progress",
- ExpectedStatus: v1alpha1.PhaseInProgress,
- Installation: basicInstallation(),
- FakeClient: fakeclient.NewFakeClientWithScheme(scheme),
- FakeConfig: basicConfigMock(),
- FakeMPM: &marketplace.MarketplaceInterfaceMock{
- InstallOperatorFunc: func(ctx context.Context, serverClient pkgclient.Client, owner ownerutil.Owner, os marketplacev1.OperatorSource, t marketplace.Target, operatorGroupNamespaces []string, approvalStrategy operatorsv1alpha1.Approval) error {
- return nil
- },
- GetSubscriptionInstallPlanFunc: func(ctx context.Context, serverClient client.Client, subName string, ns string) (plan *operatorsv1alpha1.InstallPlan, subscription *operatorsv1alpha1.Subscription, e error) {
- return &operatorsv1alpha1.InstallPlan{}, &operatorsv1alpha1.Subscription{}, nil
- },
- },
- Product: &v1alpha1.InstallationProductStatus{},
- },
- {
- Name: "test components creating returns phase in progress",
- ExpectedStatus: v1alpha1.PhaseInProgress,
- Installation: basicInstallation(),
- FakeClient: fakeclient.NewFakeClientWithScheme(scheme),
- FakeConfig: basicConfigMock(),
- FakeMPM: &marketplace.MarketplaceInterfaceMock{
- InstallOperatorFunc: func(ctx context.Context, serverClient pkgclient.Client, owner ownerutil.Owner, os marketplacev1.OperatorSource, t marketplace.Target, operatorGroupNamespaces []string, approvalStrategy operatorsv1alpha1.Approval) error {
- return nil
- },
- GetSubscriptionInstallPlanFunc: func(ctx context.Context, serverClient client.Client, subName string, ns string) (plan *operatorsv1alpha1.InstallPlan, subscription *operatorsv1alpha1.Subscription, e error) {
- return &operatorsv1alpha1.InstallPlan{
- ObjectMeta: metav1.ObjectMeta{
- Name: "mdc-install-plan",
- Namespace: defaultInstallationNamespace,
- },
- Status: operatorsv1alpha1.InstallPlanStatus{
- Phase: operatorsv1alpha1.InstallPlanPhaseComplete,
- },
- }, &operatorsv1alpha1.Subscription{
- Status: operatorsv1alpha1.SubscriptionStatus{
- Install: &operatorsv1alpha1.InstallPlanReference{
- Name: "mdc-install-plan",
- },
- },
- }, nil
- },
- },
- Product: &v1alpha1.InstallationProductStatus{},
- },
- }
- for _, tc := range cases {
- t.Run(tc.Name, func(t *testing.T) {
- reconciler, err := NewReconciler(tc.FakeConfig, tc.Installation, tc.FakeMPM)
- if err != nil {
- t.Fatalf("unexpected error : '%v'", err)
- }
- status, err := reconciler.Reconcile(context.TODO(), tc.Installation, tc.Product, tc.FakeClient)
- if err != nil {
- t.Fatalf("expected no error but got one: %v", err)
- }
- if status != tc.ExpectedStatus {
- t.Fatalf("Expected status: '%v', got: '%v'", tc.ExpectedStatus, status)
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement