...

Source file src/github.com/redhat-developer/odo/pkg/testingutil/devfile.go

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

     1  package testingutil
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  
     9  	v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
    10  	devfilepkg "github.com/devfile/api/v2/pkg/devfile"
    11  	"github.com/devfile/library/v2/pkg/devfile/parser"
    12  	devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
    13  	"github.com/devfile/library/v2/pkg/devfile/parser/data"
    14  	devfilefs "github.com/devfile/library/v2/pkg/testingutil/filesystem"
    15  
    16  	"github.com/redhat-developer/odo/pkg/devfile"
    17  )
    18  
    19  // GetFakeContainerComponent returns a fake container component for testing
    20  func GetFakeContainerComponent(name string, ports ...int) v1.Component {
    21  	image := "docker.io/maven:latest"
    22  	memoryLimit := "128Mi"
    23  	volumeName := "myvolume1"
    24  	volumePath := "/my/volume/mount/path1"
    25  	mountSources := true
    26  
    27  	var endpoints []v1.Endpoint
    28  	for _, p := range ports {
    29  		endpoints = append(endpoints, v1.Endpoint{
    30  			Name:       fmt.Sprintf("port-%d", p),
    31  			TargetPort: p,
    32  		})
    33  	}
    34  
    35  	return v1.Component{
    36  		Name: name,
    37  		ComponentUnion: v1.ComponentUnion{
    38  			Container: &v1.ContainerComponent{
    39  				Container: v1.Container{
    40  					Image:       image,
    41  					Env:         []v1.EnvVar{},
    42  					MemoryLimit: memoryLimit,
    43  					VolumeMounts: []v1.VolumeMount{{
    44  						Name: volumeName,
    45  						Path: volumePath,
    46  					}},
    47  					MountSources: &mountSources,
    48  				},
    49  				Endpoints: endpoints,
    50  			}}}
    51  
    52  }
    53  
    54  // GetFakeVolumeComponent returns a fake volume component for testing
    55  func GetFakeVolumeComponent(name, size string) v1.Component {
    56  	return v1.Component{
    57  		Name: name,
    58  		ComponentUnion: v1.ComponentUnion{
    59  			Volume: &v1.VolumeComponent{
    60  				Volume: v1.Volume{
    61  					Size: size,
    62  				}}}}
    63  
    64  }
    65  
    66  // GetTestDevfileObj returns a devfile object for testing
    67  func GetTestDevfileObj(fs devfilefs.Filesystem) parser.DevfileObj {
    68  	devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
    69  	devfileData.SetMetadata(devfilepkg.DevfileMetadata{Name: "my-nodejs-app"})
    70  
    71  	_ = devfileData.AddCommands([]v1.Command{
    72  		{
    73  			Id: "devbuild",
    74  			CommandUnion: v1.CommandUnion{
    75  				Exec: &v1.ExecCommand{
    76  					WorkingDir: "/projects/nodejs-starter",
    77  				},
    78  			},
    79  		},
    80  	})
    81  	_ = devfileData.AddComponents([]v1.Component{
    82  		{
    83  			Name: "runtime",
    84  			ComponentUnion: v1.ComponentUnion{
    85  				Container: &v1.ContainerComponent{
    86  					Container: v1.Container{
    87  						Image: "quay.io/nodejs-12",
    88  					},
    89  					Endpoints: []v1.Endpoint{
    90  						{
    91  							Name:       "port-3030",
    92  							TargetPort: 3000,
    93  						},
    94  					},
    95  				},
    96  			},
    97  		},
    98  		{
    99  			Name: "loadbalancer",
   100  			ComponentUnion: v1.ComponentUnion{
   101  				Container: &v1.ContainerComponent{
   102  					Container: v1.Container{
   103  						Image: "quay.io/nginx",
   104  					},
   105  				},
   106  			},
   107  		},
   108  	})
   109  
   110  	return parser.DevfileObj{
   111  		Ctx:  devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath),
   112  		Data: devfileData,
   113  	}
   114  }
   115  
   116  // GetTestDevfileObjWithPreStopEvents returns a devfile object with preStop event.
   117  // This function can further be extended to accept other type of events.
   118  func GetTestDevfileObjWithPreStopEvents(fs devfilefs.Filesystem, preStopId, preStopCMD string) parser.DevfileObj {
   119  	obj := GetTestDevfileObj(fs)
   120  	_ = obj.Data.AddCommands([]v1.Command{
   121  		{
   122  			Id: preStopId,
   123  			CommandUnion: v1.CommandUnion{
   124  				Exec: &v1.ExecCommand{
   125  					CommandLine: preStopCMD,
   126  					Component:   "runtime",
   127  					WorkingDir:  "/projects/nodejs-starter",
   128  				},
   129  			},
   130  		},
   131  	})
   132  	_ = obj.Data.AddEvents(v1.Events{
   133  		DevWorkspaceEvents: v1.DevWorkspaceEvents{
   134  			PreStop: []string{strings.ToLower(preStopId)},
   135  		}})
   136  	return obj
   137  }
   138  
   139  // GetTestDevfileObjFromFile takes the filename of devfile from tests/examples/source/devfiles/nodejs and returns a parser.DevfileObj
   140  func GetTestDevfileObjFromFile(fileName string) parser.DevfileObj {
   141  	// filename of this file
   142  	_, filename, _, _ := runtime.Caller(0)
   143  	// path to the devfile
   144  	devfilePath := filepath.Join(filepath.Dir(filename), "..", "..", "tests", "examples", filepath.Join("source", "devfiles", "nodejs", fileName))
   145  
   146  	devfileObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
   147  	if err != nil {
   148  		return parser.DevfileObj{}
   149  	}
   150  	return devfileObj
   151  }
   152  

View as plain text