...

Source file src/github.com/redhat-developer/odo/tests/helper/helper_devfile.go

Documentation: github.com/redhat-developer/odo/tests/helper

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     7  	"github.com/devfile/api/v2/pkg/attributes"
     8  	"github.com/devfile/library/v2/pkg/devfile/parser"
     9  	parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
    10  	. "github.com/onsi/gomega"
    11  	apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    12  	"k8s.io/utils/pointer"
    13  
    14  	"github.com/redhat-developer/odo/pkg/devfile"
    15  )
    16  
    17  // DevfileUpdater is a helper type that can mutate a Devfile object.
    18  // It is intended to be used in conjunction with the UpdateDevfileContent function.
    19  type DevfileUpdater func(*parser.DevfileObj) error
    20  
    21  // DevfileMetadataNameSetter sets the 'metadata.name' field into the given Devfile
    22  var DevfileMetadataNameSetter = func(name string) DevfileUpdater {
    23  	return func(d *parser.DevfileObj) error {
    24  		return d.SetMetadataName(name)
    25  	}
    26  }
    27  
    28  // DevfileMetadataNameRemover removes the 'metadata.name' field from the given Devfile
    29  var DevfileMetadataNameRemover = DevfileMetadataNameSetter("")
    30  
    31  // DevfileCommandGroupUpdater updates the group definition of the specified command.
    32  // It returns an error if the command was not found in the Devfile, or if there are multiple commands with the same name and type.
    33  var DevfileCommandGroupUpdater = func(cmdName string, cmdType v1alpha2.CommandType, group *v1alpha2.CommandGroup) DevfileUpdater {
    34  	return func(d *parser.DevfileObj) error {
    35  		cmds, err := d.Data.GetCommands(parsercommon.DevfileOptions{
    36  			CommandOptions: parsercommon.CommandOptions{
    37  				CommandType: cmdType,
    38  			},
    39  			FilterByName: cmdName,
    40  		})
    41  		if err != nil {
    42  			return err
    43  		}
    44  		if len(cmds) != 1 {
    45  			return fmt.Errorf("found %v command(s) with name %q", len(cmds), cmdName)
    46  		}
    47  		cmd := cmds[0]
    48  		switch cmdType {
    49  		case v1alpha2.ApplyCommandType:
    50  			cmd.Apply.Group = group
    51  		case v1alpha2.CompositeCommandType:
    52  			cmd.Composite.Group = group
    53  		case v1alpha2.CustomCommandType:
    54  			cmd.Custom.Group = group
    55  		case v1alpha2.ExecCommandType:
    56  			cmd.Exec.Group = group
    57  		default:
    58  			return fmt.Errorf("command type not handled: %q", cmdType)
    59  		}
    60  		return nil
    61  	}
    62  }
    63  
    64  // UpdateDevfileContent parses the Devfile at the given path, then updates its content using the given handlers, and writes the updated Devfile to the given path.
    65  //
    66  // The handlers are invoked in the order they are provided.
    67  //
    68  // No operation is performed if no handler function is specified.
    69  //
    70  // See DevfileMetadataNameRemover for an example of handler function that can operate on the Devfile content.
    71  func UpdateDevfileContent(path string, handlers []DevfileUpdater) {
    72  	if len(handlers) == 0 {
    73  		//Nothing to do => skip
    74  		return
    75  	}
    76  
    77  	d, err := parser.ParseDevfile(parser.ParserArgs{
    78  		Path:               path,
    79  		FlattenedDevfile:   pointer.Bool(false),
    80  		SetBooleanDefaults: pointer.Bool(false),
    81  	})
    82  	Expect(err).NotTo(HaveOccurred())
    83  	for _, h := range handlers {
    84  		err = h(&d)
    85  		Expect(err).NotTo(HaveOccurred())
    86  	}
    87  	err = d.WriteYamlDevfile()
    88  	Expect(err).NotTo(HaveOccurred())
    89  }
    90  
    91  // SetFsGroup is a DevfileUpdater which sets an attribute to a container
    92  // to set a specific fsGroup for the container's pod
    93  func SetFsGroup(containerName string, fsGroup int) DevfileUpdater {
    94  	return func(d *parser.DevfileObj) error {
    95  		containers, err := d.Data.GetComponents(parsercommon.DevfileOptions{
    96  			FilterByName: containerName,
    97  		})
    98  		Expect(err).NotTo(HaveOccurred())
    99  		Expect(len(containers)).To(Equal(1))
   100  		containers[0].Attributes = attributes.Attributes{
   101  			"pod-overrides": apiext.JSON{
   102  				Raw: []byte(fmt.Sprintf(`{"spec": {"securityContext": {"fsGroup": %d}}}`, fsGroup)),
   103  			},
   104  		}
   105  		err = d.Data.UpdateComponent(containers[0])
   106  		Expect(err).NotTo(HaveOccurred())
   107  		return nil
   108  	}
   109  }
   110  
   111  // ReadRawDevfile parses and validates the Devfile specified and returns its raw content.
   112  func ReadRawDevfile(devfilePath string) parser.DevfileObj {
   113  	d, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
   114  	Expect(err).ToNot(HaveOccurred())
   115  	return d
   116  }
   117  

View as plain text