...

Source file src/github.com/redhat-developer/odo/pkg/init/backend/applicationports_test.go

Documentation: github.com/redhat-developer/odo/pkg/init/backend

     1  package backend
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     8  	devfilepkg "github.com/devfile/api/v2/pkg/devfile"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser"
    10  	devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
    11  	"github.com/devfile/library/v2/pkg/devfile/parser/data"
    12  	devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/google/go-cmp/cmp/cmpopts"
    15  
    16  	"github.com/redhat-developer/odo/pkg/testingutil"
    17  )
    18  
    19  var fs = devfilefs.NewFakeFs()
    20  
    21  func buildDevfileObjWithComponents(components ...v1.Component) parser.DevfileObj {
    22  	devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion220))
    23  	devfileData.SetMetadata(devfilepkg.DevfileMetadata{Name: "my-nodejs-app"})
    24  	_ = devfileData.AddComponents(components)
    25  	return parser.DevfileObj{
    26  		Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
    27  		Data: devfileData,
    28  	}
    29  }
    30  
    31  func Test_handleApplicationPorts(t *testing.T) {
    32  	type devfileProvider func() parser.DevfileObj
    33  	type args struct {
    34  		devfileObjProvider devfileProvider
    35  		ports              []int
    36  	}
    37  
    38  	tests := []struct {
    39  		name         string
    40  		args         args
    41  		wantErr      bool
    42  		wantProvider devfileProvider
    43  	}{
    44  		{
    45  			name: "no component, no ports to set",
    46  			args: args{
    47  				devfileObjProvider: func() parser.DevfileObj { return buildDevfileObjWithComponents() },
    48  			},
    49  			wantProvider: func() parser.DevfileObj { return buildDevfileObjWithComponents() },
    50  		},
    51  		{
    52  			name: "multiple container components, no ports to set",
    53  			args: args{
    54  				devfileObjProvider: func() parser.DevfileObj {
    55  					return buildDevfileObjWithComponents(
    56  						testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
    57  						testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
    58  					)
    59  				},
    60  			},
    61  			wantProvider: func() parser.DevfileObj {
    62  				return buildDevfileObjWithComponents(
    63  					testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
    64  					testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
    65  				)
    66  			},
    67  		},
    68  		{
    69  			name: "no container components",
    70  			args: args{
    71  				devfileObjProvider: func() parser.DevfileObj {
    72  					return buildDevfileObjWithComponents(testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
    73  				},
    74  				ports: []int{8888, 8889, 8890},
    75  			},
    76  			wantProvider: func() parser.DevfileObj {
    77  				return buildDevfileObjWithComponents(testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
    78  			},
    79  		},
    80  		{
    81  			name: "more than one container components",
    82  			args: args{
    83  				devfileObjProvider: func() parser.DevfileObj {
    84  					return buildDevfileObjWithComponents(
    85  						testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
    86  						testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
    87  						testingutil.GetFakeVolumeComponent("vol1", "1Gi"),
    88  					)
    89  				},
    90  				ports: []int{8888, 8889, 8890},
    91  			},
    92  			wantProvider: func() parser.DevfileObj {
    93  				return buildDevfileObjWithComponents(
    94  					testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
    95  					testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
    96  					testingutil.GetFakeVolumeComponent("vol1", "1Gi"),
    97  				)
    98  			},
    99  		},
   100  		{
   101  			name: "single container component with both application and debug ports",
   102  			args: args{
   103  				devfileObjProvider: func() parser.DevfileObj {
   104  					contWithDebug := testingutil.GetFakeContainerComponent("cont1", 18080, 18081, 18082)
   105  					contWithDebug.ComponentUnion.Container.Endpoints = append(contWithDebug.ComponentUnion.Container.Endpoints,
   106  						v1.Endpoint{Name: "debug", TargetPort: 5005},
   107  						v1.Endpoint{Name: "debug-another", TargetPort: 5858},
   108  					)
   109  					return buildDevfileObjWithComponents(
   110  						contWithDebug,
   111  						testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
   112  				},
   113  				ports: []int{3000, 9000},
   114  			},
   115  			wantProvider: func() parser.DevfileObj {
   116  				newCont := testingutil.GetFakeContainerComponent("cont1")
   117  				newCont.ComponentUnion.Container.Endpoints = append(newCont.ComponentUnion.Container.Endpoints,
   118  					v1.Endpoint{Name: "port-3000-tcp", TargetPort: 3000, Protocol: v1.TCPEndpointProtocol},
   119  					v1.Endpoint{Name: "port-9000-tcp", TargetPort: 9000, Protocol: v1.TCPEndpointProtocol},
   120  					v1.Endpoint{Name: "debug", TargetPort: 5005},
   121  					v1.Endpoint{Name: "debug-another", TargetPort: 5858},
   122  				)
   123  				return buildDevfileObjWithComponents(
   124  					newCont,
   125  					testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
   126  			},
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			var output bytes.Buffer
   132  			got, err := handleApplicationPorts(&output, tt.args.devfileObjProvider(), tt.args.ports)
   133  			if (err != nil) != tt.wantErr {
   134  				t.Errorf("handleApplicationPorts() error = %v, wantErr %v", err, tt.wantErr)
   135  				return
   136  			}
   137  			if diff := cmp.Diff(tt.wantProvider(), got,
   138  				cmp.AllowUnexported(devfileCtx.DevfileCtx{}),
   139  				cmpopts.IgnoreInterfaces(struct{ devfilefs.Filesystem }{})); diff != "" {
   140  				t.Errorf("handleApplicationPorts() mismatch (-want +got):\n%s", diff)
   141  			}
   142  		})
   143  	}
   144  }
   145  

View as plain text