...

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

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

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     9  	devfilepkg "github.com/devfile/api/v2/pkg/devfile"
    10  	"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
    11  
    12  	"github.com/redhat-developer/odo/pkg/devfile"
    13  
    14  	. "github.com/onsi/ginkgo/v2"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  // GetPreferenceValue a global config value of given key or
    19  // returns an empty string if value is not set
    20  func GetPreferenceValue(key string) string {
    21  	stdOut := Cmd("odo", "preference", "view").ShouldPass().Out()
    22  	re := regexp.MustCompile(" " + key + `.+`)
    23  	odoConfigKeyValue := re.FindString(stdOut)
    24  	if odoConfigKeyValue == "" {
    25  		return fmt.Sprintf("%s not found", key)
    26  	}
    27  	trimKeyValue := strings.TrimSpace(odoConfigKeyValue)
    28  	if strings.Compare(key, trimKeyValue) != 0 {
    29  		return strings.TrimSpace(strings.SplitN(trimKeyValue, " ", 2)[1])
    30  	}
    31  	return ""
    32  }
    33  
    34  // CreateRandProject create new project with random name (10 letters)
    35  // without writing to the config file (without switching project)
    36  func CreateRandProject() string {
    37  	projectName := GenerateProjectName()
    38  	fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
    39  	session := Cmd("odo", "create", "project", projectName, "-w", "-v4").ShouldPass().Out()
    40  	Expect(session).To(ContainSubstring("New project created"))
    41  	Expect(session).To(ContainSubstring(projectName))
    42  	return projectName
    43  }
    44  
    45  // DeleteProject deletes a specified project
    46  func DeleteProject(projectName string) {
    47  	fmt.Fprintf(GinkgoWriter, "Deleting project: %s\n", projectName)
    48  	session := Cmd("odo", "delete", "project", projectName, "-f").ShouldPass().Out()
    49  	Expect(session).To(ContainSubstring(fmt.Sprintf("Project %q will be deleted asynchronously", projectName)))
    50  }
    51  
    52  // GetMetadataFromDevfile retrieves the metadata from devfile
    53  func GetMetadataFromDevfile(devfilePath string) devfilepkg.DevfileMetadata {
    54  	devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
    55  	Expect(err).ToNot(HaveOccurred())
    56  	return devObj.Data.GetMetadata()
    57  }
    58  
    59  func GetDevfileComponents(devfilePath, componentName string) []v1alpha2.Component {
    60  	devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
    61  	Expect(err).ToNot(HaveOccurred())
    62  	components, err := devObj.Data.GetComponents(common.DevfileOptions{
    63  		FilterByName: componentName,
    64  	})
    65  	Expect(err).ToNot(HaveOccurred())
    66  	return components
    67  }
    68  
    69  type OdoV2Watch struct {
    70  	CmpName               string
    71  	StringsToBeMatched    []string
    72  	StringsNotToBeMatched []string
    73  	FolderToCheck         string
    74  	SrcType               string
    75  }
    76  
    77  // VerifyContainerSyncEnv verifies the sync env in the container
    78  func VerifyContainerSyncEnv(podName, containerName, namespace, projectSourceValue, projectsRootValue string, cliRunner CliRunner) {
    79  	envProjectsRoot, envProjectSource := "PROJECTS_ROOT", "PROJECT_SOURCE"
    80  	projectSourceMatched, projectsRootMatched := false, false
    81  
    82  	envNamesAndValues := cliRunner.GetContainerEnv(podName, "runtime", namespace)
    83  	envNamesAndValuesArr := strings.Fields(envNamesAndValues)
    84  
    85  	for _, envNamesAndValues := range envNamesAndValuesArr {
    86  		envNameAndValueArr := strings.Split(envNamesAndValues, ":")
    87  
    88  		if envNameAndValueArr[0] == envProjectSource && strings.Contains(envNameAndValueArr[1], projectSourceValue) {
    89  			projectSourceMatched = true
    90  		}
    91  
    92  		if envNameAndValueArr[0] == envProjectsRoot && strings.Contains(envNameAndValueArr[1], projectsRootValue) {
    93  			projectsRootMatched = true
    94  		}
    95  	}
    96  
    97  	Expect(projectSourceMatched).To(Equal(true))
    98  	Expect(projectsRootMatched).To(Equal(true))
    99  }
   100  

View as plain text