...

Source file src/github.com/redhat-developer/odo/pkg/binding/list_test.go

Documentation: github.com/redhat-developer/odo/pkg/binding

     1  package binding
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/devfile/library/v2/pkg/devfile/parser"
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	"github.com/redhat-developer/odo/pkg/api"
    11  	"github.com/redhat-developer/odo/pkg/kclient"
    12  	bindingApis "github.com/redhat-developer/service-binding-operator/apis"
    13  	"github.com/redhat-developer/service-binding-operator/apis/binding/v1alpha1"
    14  
    15  	corev1 "k8s.io/api/core/v1"
    16  	"k8s.io/apimachinery/pkg/api/errors"
    17  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    18  	"k8s.io/apimachinery/pkg/runtime/schema"
    19  )
    20  
    21  var apiServiceBinding = api.ServiceBinding{
    22  	Name: "my-nodejs-app-cluster-sample",
    23  	Spec: api.ServiceBindingSpec{
    24  		Application: api.ServiceBindingReference{
    25  			Name:       "my-nodejs-app-app",
    26  			APIVersion: deploymentApiVersion,
    27  			Kind:       deploymentKind,
    28  		},
    29  		Services: []api.ServiceBindingReference{
    30  			{
    31  				Name:       "cluster-sample",
    32  				APIVersion: ClusterAPIVersion,
    33  				Kind:       clusterKind,
    34  			},
    35  		},
    36  		BindAsFiles:            true,
    37  		DetectBindingResources: true,
    38  	},
    39  }
    40  
    41  var bindingServiceBinding = kclient.NewServiceBindingObject(
    42  	"my-nodejs-app-cluster-sample",
    43  	true,
    44  	"my-nodejs-app-app",
    45  	"",
    46  	deploymentGVK,
    47  	nil,
    48  	[]v1alpha1.Service{
    49  		{
    50  			NamespacedRef: v1alpha1.NamespacedRef{
    51  				Ref: v1alpha1.Ref{
    52  					Group:   clusterGVK.Group,
    53  					Version: clusterGVK.Version,
    54  					Kind:    clusterKind,
    55  					Name:    "cluster-sample",
    56  				},
    57  			},
    58  		},
    59  	},
    60  	v1alpha1.ServiceBindingStatus{
    61  		Conditions: []metav1.Condition{
    62  			{
    63  				Type:   bindingApis.InjectionReady,
    64  				Status: metav1.ConditionTrue,
    65  			},
    66  		},
    67  		Secret: "asecret",
    68  	},
    69  )
    70  
    71  var sbSecret = corev1.Secret{
    72  	Data: map[string][]byte{
    73  		"akey": []byte("avalue"),
    74  	},
    75  }
    76  
    77  func TestBindingClient_ListAllBindings(t *testing.T) {
    78  	bindingServiceBinding.SetLabels(map[string]string{
    79  		"odo.dev/mode": "Dev",
    80  	})
    81  	type fields struct {
    82  		kubernetesClient func(ctrl *gomock.Controller) kclient.ClientInterface
    83  	}
    84  	type args struct {
    85  		devfileObj *parser.DevfileObj
    86  		context    string
    87  	}
    88  	tests := []struct {
    89  		name          string
    90  		fields        fields
    91  		args          args
    92  		want          []api.ServiceBinding
    93  		wantInDevfile []string
    94  		wantErr       bool
    95  	}{
    96  		{
    97  			name: "a servicebinding defined in Devfile, nothing in cluster",
    98  			fields: fields{
    99  				func(ctrl *gomock.Controller) kclient.ClientInterface {
   100  					client := kclient.NewMockClientInterface(ctrl)
   101  					client.EXPECT().ListServiceBindingsFromAllGroups().Return(nil, nil, nil)
   102  					client.EXPECT().GetBindingServiceBinding(gomock.Any()).Return(
   103  						v1alpha1.ServiceBinding{},
   104  						errors.NewNotFound(
   105  							schema.GroupResource{
   106  								Group:    "dont",
   107  								Resource: "care",
   108  							},
   109  							"my-nodejs-app-cluster-sample",
   110  						),
   111  					)
   112  					return client
   113  				},
   114  			}, args: args{
   115  				devfileObj: getDevfileObjWithServiceBinding("aname", "", true, ""),
   116  				context:    "/apath",
   117  			},
   118  			want:          []api.ServiceBinding{apiServiceBinding},
   119  			wantInDevfile: []string{"my-nodejs-app-cluster-sample"},
   120  		},
   121  		{
   122  			name: "a servicebinding defined in Devfile, also in cluster",
   123  			fields: fields{
   124  				func(ctrl *gomock.Controller) kclient.ClientInterface {
   125  					client := kclient.NewMockClientInterface(ctrl)
   126  					client.EXPECT().ListServiceBindingsFromAllGroups().Return(nil, []v1alpha1.ServiceBinding{
   127  						*bindingServiceBinding,
   128  					}, nil)
   129  					client.EXPECT().GetBindingServiceBinding(gomock.Any()).Return(
   130  						*bindingServiceBinding,
   131  						nil,
   132  					).AnyTimes()
   133  					client.EXPECT().GetCurrentNamespace().Return("anamespace").AnyTimes()
   134  					client.EXPECT().GetSecret("asecret", "anamespace").Return(&sbSecret, nil).AnyTimes()
   135  					return client
   136  				},
   137  			}, args: args{
   138  				devfileObj: getDevfileObjWithServiceBinding("aname", "", true, ""),
   139  				context:    "/apath",
   140  			},
   141  			want: []api.ServiceBinding{
   142  				{
   143  					Name: "my-nodejs-app-cluster-sample",
   144  					Spec: api.ServiceBindingSpec{
   145  						Application: api.ServiceBindingReference{
   146  							Name:       "my-nodejs-app-app",
   147  							APIVersion: deploymentApiVersion,
   148  							Kind:       deploymentKind,
   149  						},
   150  						Services: []api.ServiceBindingReference{
   151  							{
   152  								Name:       "cluster-sample",
   153  								APIVersion: ClusterAPIVersion,
   154  								Kind:       clusterKind,
   155  							},
   156  						},
   157  						BindAsFiles:            true,
   158  						DetectBindingResources: true,
   159  					},
   160  					Status: &api.ServiceBindingStatus{
   161  						BindingFiles: []string{"${SERVICE_BINDING_ROOT}/my-nodejs-app-cluster-sample/akey"},
   162  						RunningIn:    api.RunningModes{"dev": true, "deploy": false},
   163  					},
   164  				},
   165  			},
   166  			wantInDevfile: []string{"my-nodejs-app-cluster-sample"},
   167  		},
   168  		{
   169  			name: "a servicebinding defined in cluster",
   170  			fields: fields{
   171  				func(ctrl *gomock.Controller) kclient.ClientInterface {
   172  					client := kclient.NewMockClientInterface(ctrl)
   173  					client.EXPECT().ListServiceBindingsFromAllGroups().Return(nil, []v1alpha1.ServiceBinding{
   174  						*bindingServiceBinding,
   175  					}, nil)
   176  					client.EXPECT().GetBindingServiceBinding(gomock.Any()).Return(
   177  						*bindingServiceBinding,
   178  						nil,
   179  					).AnyTimes()
   180  					client.EXPECT().GetCurrentNamespace().Return("anamespace").AnyTimes()
   181  					client.EXPECT().GetSecret("asecret", "anamespace").Return(&sbSecret, nil).AnyTimes()
   182  					return client
   183  				},
   184  			}, args: args{},
   185  			want: []api.ServiceBinding{
   186  				{
   187  					Name: "my-nodejs-app-cluster-sample",
   188  					Spec: api.ServiceBindingSpec{
   189  						Application: api.ServiceBindingReference{
   190  							Name:       "my-nodejs-app-app",
   191  							APIVersion: deploymentApiVersion,
   192  							Kind:       deploymentKind,
   193  						},
   194  						Services: []api.ServiceBindingReference{
   195  							{
   196  								Name:       "cluster-sample",
   197  								APIVersion: ClusterAPIVersion,
   198  								Kind:       clusterKind,
   199  							},
   200  						},
   201  						BindAsFiles:            true,
   202  						DetectBindingResources: true,
   203  					},
   204  					Status: &api.ServiceBindingStatus{
   205  						BindingFiles: []string{"${SERVICE_BINDING_ROOT}/my-nodejs-app-cluster-sample/akey"},
   206  						RunningIn:    api.RunningModes{"dev": true, "deploy": false},
   207  					},
   208  				},
   209  			},
   210  			wantInDevfile: nil,
   211  		},
   212  	}
   213  	for _, tt := range tests {
   214  		t.Run(tt.name, func(t *testing.T) {
   215  			ctrl := gomock.NewController(t)
   216  
   217  			o := &BindingClient{
   218  				kubernetesClient: tt.fields.kubernetesClient(ctrl),
   219  			}
   220  			got, gotInDevfile, err := o.ListAllBindings(tt.args.devfileObj, tt.args.context)
   221  			if (err != nil) != tt.wantErr {
   222  				t.Errorf("BindingClient.ListAllBindings() error = %v, wantErr %v", err, tt.wantErr)
   223  				return
   224  			}
   225  			if diff := cmp.Diff(tt.want, got); diff != "" {
   226  				t.Errorf("BindingClient.ListAllBindings() mismatch (-want +got):\n%s", diff)
   227  			}
   228  			if diff := cmp.Diff(tt.wantInDevfile, gotInDevfile); diff != "" {
   229  				t.Errorf("BindingClient.ListAllBindings() wantInDevfile mismatch (-want +got):\n%s", diff)
   230  			}
   231  		})
   232  	}
   233  }
   234  

View as plain text