...

Source file src/github.com/redhat-developer/odo/pkg/portForward/kubeportforward/writer_test.go

Documentation: github.com/redhat-developer/odo/pkg/portForward/kubeportforward

     1  package kubeportforward
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     7  	"github.com/google/go-cmp/cmp"
     8  
     9  	"github.com/redhat-developer/odo/pkg/api"
    10  )
    11  
    12  func Test_getForwardedPort(t *testing.T) {
    13  	type args struct {
    14  		mapping map[string][]v1alpha2.Endpoint
    15  		s       string
    16  		address string
    17  	}
    18  	tests := []struct {
    19  		name    string
    20  		args    args
    21  		want    api.ForwardedPort
    22  		wantErr bool
    23  	}{
    24  		{
    25  			name: "find port in container",
    26  			args: args{
    27  				mapping: map[string][]v1alpha2.Endpoint{
    28  					"container1": {
    29  						v1alpha2.Endpoint{Name: "port-11", TargetPort: 3000},
    30  						v1alpha2.Endpoint{Name: "debug-11", TargetPort: 4200},
    31  					},
    32  					"container2": {
    33  						v1alpha2.Endpoint{Name: "port-21", TargetPort: 80},
    34  						v1alpha2.Endpoint{Name: "port-22", TargetPort: 8080},
    35  					},
    36  				},
    37  				s: "Forwarding from 127.0.0.1:40407 -> 3000",
    38  			},
    39  			want: api.ForwardedPort{
    40  				ContainerName: "container1",
    41  				PortName:      "port-11",
    42  				LocalAddress:  "127.0.0.1",
    43  				IsDebug:       false,
    44  				LocalPort:     40407,
    45  				ContainerPort: 3000,
    46  			},
    47  			wantErr: false,
    48  		},
    49  		{
    50  			name: "find port in container and use custom address",
    51  			args: args{
    52  				mapping: map[string][]v1alpha2.Endpoint{
    53  					"container1": {
    54  						v1alpha2.Endpoint{Name: "port-11", TargetPort: 3000},
    55  						v1alpha2.Endpoint{Name: "debug-11", TargetPort: 4200},
    56  					},
    57  					"container2": {
    58  						v1alpha2.Endpoint{Name: "port-21", TargetPort: 80},
    59  						v1alpha2.Endpoint{Name: "port-22", TargetPort: 8080},
    60  					},
    61  				},
    62  				s:       "Forwarding from 192.168.0.1:40407 -> 3000",
    63  				address: "192.168.0.1",
    64  			},
    65  			want: api.ForwardedPort{
    66  				ContainerName: "container1",
    67  				PortName:      "port-11",
    68  				LocalAddress:  "192.168.0.1",
    69  				IsDebug:       false,
    70  				LocalPort:     40407,
    71  				ContainerPort: 3000,
    72  			},
    73  			wantErr: false,
    74  		},
    75  		{
    76  			name: "string error",
    77  			args: args{
    78  				mapping: map[string][]v1alpha2.Endpoint{
    79  					"container1": {
    80  						v1alpha2.Endpoint{Name: "port-11", TargetPort: 3000},
    81  						v1alpha2.Endpoint{Name: "debug-11", TargetPort: 4200},
    82  					},
    83  					"container2": {
    84  						v1alpha2.Endpoint{Name: "port-21", TargetPort: 80},
    85  						v1alpha2.Endpoint{Name: "port-22", TargetPort: 8080},
    86  					},
    87  				},
    88  				s: "Forwarding from 127.0.0.1:40407 => 3000",
    89  			},
    90  			want:    api.ForwardedPort{},
    91  			wantErr: true,
    92  		},
    93  		{
    94  			name: "find debug port in container",
    95  			args: args{
    96  				mapping: map[string][]v1alpha2.Endpoint{
    97  					"container1": {
    98  						v1alpha2.Endpoint{Name: "port-11", TargetPort: 3000},
    99  						v1alpha2.Endpoint{Name: "debug-11", TargetPort: 4200},
   100  					},
   101  					"container2": {
   102  						v1alpha2.Endpoint{Name: "port-21", TargetPort: 80},
   103  						v1alpha2.Endpoint{Name: "port-22", TargetPort: 8080},
   104  					},
   105  				},
   106  				s: "Forwarding from 127.0.0.1:40407 -> 4200",
   107  			},
   108  			want: api.ForwardedPort{
   109  				ContainerName: "container1",
   110  				PortName:      "debug-11",
   111  				IsDebug:       true,
   112  				LocalAddress:  "127.0.0.1",
   113  				LocalPort:     40407,
   114  				ContainerPort: 4200,
   115  			},
   116  			wantErr: false,
   117  		},
   118  	}
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  			got, err := getForwardedPort(tt.args.mapping, tt.args.s, tt.args.address)
   122  			if (err != nil) != tt.wantErr {
   123  				t.Errorf("getForwardedPort() error = %v, wantErr %v", err, tt.wantErr)
   124  				return
   125  			}
   126  			if diff := cmp.Diff(tt.want, got); diff != "" {
   127  				t.Errorf("getForwardedPort() mismatch (-want +got):\n%s", diff)
   128  			}
   129  		})
   130  	}
   131  }
   132  

View as plain text