...

Source file src/github.com/redhat-developer/odo/pkg/binding/remove_binding_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  	devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil"
    11  )
    12  
    13  func TestBindingClient_ValidateRemoveBinding(t *testing.T) {
    14  	type args struct {
    15  		flags map[string]string
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantErr bool
    21  	}{
    22  		{
    23  			name:    "--name flag is passed",
    24  			args:    args{flags: map[string]string{"name": "redis-my-node-app"}},
    25  			wantErr: false,
    26  		},
    27  		{
    28  			name:    "--name flag is not passed",
    29  			args:    args{flags: map[string]string{}},
    30  			wantErr: true,
    31  		},
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			o := &BindingClient{}
    36  			if err := o.ValidateRemoveBinding(tt.args.flags); (err != nil) != tt.wantErr {
    37  				t.Errorf("ValidateRemoveBinding() error = %v, wantErr %v", err, tt.wantErr)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func TestBindingClient_RemoveBinding(t *testing.T) {
    44  	type args struct {
    45  		servicebindingName string
    46  		obj                parser.DevfileObj
    47  	}
    48  	tests := []struct {
    49  		name    string
    50  		args    args
    51  		want    parser.DevfileObj
    52  		wantErr bool
    53  	}{
    54  		// TODO: Add test cases.
    55  		{
    56  			name: "removed the k8s binding successfully when bound as files",
    57  			args: args{
    58  				servicebindingName: "my-nodejs-app-cluster-sample-k8s", // name is hard coded from the devfile-with-service-binding-files.yaml
    59  				obj:                odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml"),
    60  			},
    61  			want: func() parser.DevfileObj {
    62  				obj := odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml")
    63  				_ = obj.Data.DeleteComponent("my-nodejs-app-cluster-sample-k8s")
    64  				return obj
    65  			}(),
    66  			wantErr: false,
    67  		},
    68  		{
    69  			name: "removed the ocp binding successfully when bound as files",
    70  			args: args{
    71  				servicebindingName: "my-nodejs-app-cluster-sample-ocp", // name is hard coded from the devfile-with-service-binding-files.yaml
    72  				obj:                odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml"),
    73  			},
    74  			want: func() parser.DevfileObj {
    75  				obj := odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml")
    76  				_ = obj.Data.DeleteComponent("my-nodejs-app-cluster-sample-ocp")
    77  				return obj
    78  			}(),
    79  			wantErr: false,
    80  		},
    81  		{
    82  			name: "removed the k8s binding successfully when not bound as files",
    83  			args: args{
    84  				servicebindingName: "my-nodejs-app-cluster-sample-k8s",
    85  				obj:                odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-envvars.yaml"),
    86  			},
    87  			want: func() parser.DevfileObj {
    88  				obj := odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-envvars.yaml")
    89  				_ = obj.Data.DeleteComponent("my-nodejs-app-cluster-sample-k8s")
    90  				return obj
    91  			}(),
    92  			wantErr: false,
    93  		},
    94  		{
    95  			name: "removed the ocp binding successfully when not bound as files",
    96  			args: args{
    97  				servicebindingName: "my-nodejs-app-cluster-sample-ocp",
    98  				obj:                odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-envvars.yaml"),
    99  			},
   100  			want: func() parser.DevfileObj {
   101  				obj := odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-envvars.yaml")
   102  				_ = obj.Data.DeleteComponent("my-nodejs-app-cluster-sample-ocp")
   103  				return obj
   104  			}(),
   105  			wantErr: false,
   106  		},
   107  		{
   108  			name: "failed to remove non-existent binding",
   109  			args: args{
   110  				servicebindingName: "something",
   111  				obj:                odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml"),
   112  			},
   113  			want:    odoTestingUtil.GetTestDevfileObjFromFile("devfile-with-service-binding-files.yaml"),
   114  			wantErr: true,
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			o := &BindingClient{}
   120  			got, err := o.RemoveBinding(tt.args.servicebindingName, tt.args.obj)
   121  			if (err != nil) != tt.wantErr {
   122  				t.Errorf("RemoveBinding() error = %v, wantErr %v", err, tt.wantErr)
   123  				return
   124  			}
   125  			if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(devfileCtx.DevfileCtx{})); diff != "" {
   126  				t.Errorf("BindingClient.RemoveBinding() mismatch (-want +got):\n%s", diff)
   127  			}
   128  		})
   129  	}
   130  }
   131  

View as plain text