...

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

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

     1  package helper
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"net/url"
     8  	"regexp"
     9  	"strings"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  
    13  	"github.com/redhat-developer/odo/pkg/api"
    14  )
    15  
    16  // Version pattern has always been in the form of X.X.X
    17  var versionRe = regexp.MustCompile(`(\d.\d.\d)`)
    18  
    19  type Registry struct {
    20  	url string
    21  }
    22  
    23  func NewRegistry(url string) Registry {
    24  	return Registry{
    25  		url: url,
    26  	}
    27  }
    28  
    29  func (o Registry) GetIndex() ([]api.DevfileStack, error) {
    30  	url, err := url.JoinPath(strings.TrimSuffix(o.url, "/"), "/v2index")
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	resp, err := http.Get(url)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	defer func() {
    39  		if cErr := resp.Body.Close(); cErr != nil {
    40  			fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr)
    41  		}
    42  	}()
    43  
    44  	var target []api.DevfileStack
    45  	err = json.NewDecoder(resp.Body).Decode(&target)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return target, nil
    50  }
    51  
    52  func (o Registry) GetStack(name string) (api.DevfileStack, error) {
    53  	index, err := o.GetIndex()
    54  	if err != nil {
    55  		return api.DevfileStack{}, err
    56  	}
    57  	for _, stack := range index {
    58  		if stack.Name == name {
    59  			return stack, nil
    60  		}
    61  	}
    62  	return api.DevfileStack{}, fmt.Errorf("stack %q not found", name)
    63  }
    64  
    65  // GetVersions returns the list of all versions for the given stack name in the given Devfile registry.
    66  // It uses the "odo registry" command to find out this information.
    67  //
    68  // The registry name is optional, and defaults to DefaultDevfileRegistry if not set.
    69  func GetVersions(registryName string, stackName string) []string {
    70  	devfileReg := "DefaultDevfileRegistry"
    71  	if registryName != "" {
    72  		devfileReg = registryName
    73  	}
    74  	out := Cmd("odo", "registry", "--devfile", stackName, "--devfile-registry", devfileReg).ShouldPass().Out()
    75  	return versionRe.FindAllString(out, -1)
    76  }
    77  
    78  // HasAtLeastTwoVersions returns whether the given stack in the given Devfile registry has at least two versions.
    79  // This is useful to determine if the "Select version" prompt will be displayed in the interactive "odo init" tests.
    80  // Otherwise, "odo init" would just skip this "Select version" prompt if the stack selected has no version or only a single one.
    81  //
    82  // Note that the registry name is optional, and defaults to DefaultDevfileRegistry if not set.
    83  func HasAtLeastTwoVersions(registryName string, stackName string) bool {
    84  	return len(GetVersions(registryName, stackName)) >= 2
    85  }
    86  
    87  type RegistryServer interface {
    88  	Start() (url string, err error)
    89  	Stop() error
    90  	IsStarted() bool
    91  	GetUrl() string
    92  }
    93  

View as plain text