Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ To build the test binary locally, run: `./build.sh`

## Custom Tests

### Specific Registries

Some tests like using the arch filter are registry specific. For example, the community registry may not have devfiles with archs mentioned but the test registry in this repo does. As such, run these specific tests by setting the env `IS_TEST_REGISTRY=true`

### Registry Probing

Optionally, a health check probe can be run to require the target registry to be at a ready status. This can be enabled by setting the env `PROBE_TIMEOUT`, to have the probe timeout in 30 seconds set the env to `PROBE_TIMEOUT=30`.

## Run in a Container

The recommended way to run the tests is in a container, simply run the following after building the image in the previous step:
Expand Down
36 changes: 33 additions & 3 deletions tests/integration/cmd/devfileregistry_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021-2022 Red Hat, Inc.
/* Copyright 2021-2023 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@ import (
"testing"

"github.com/devfile/registry-support/tests/integration/pkg/config"
"github.com/devfile/registry-support/tests/integration/pkg/util"

_ "github.com/devfile/registry-support/tests/integration/pkg/tests"
"github.com/onsi/ginkgo"
Expand All @@ -38,16 +39,45 @@ const (

//SynchronizedBeforeSuite blocks is executed before run all test suites
var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
var err error

fmt.Println("Starting to setup objects before run ginkgo suite")
registry := os.Getenv("REGISTRY")
if registry == "" {
registry = "https://registry.devfile.io"
}
config.Registry = registry
config.RegistryList = registry + "," + "https://registry.stage.devfile.io"
os.Setenv("REGISTRY_LIST", config.RegistryList)
err = os.Setenv("REGISTRY_LIST", config.RegistryList)
if err != nil {
fmt.Println(err)
panic(err)
}

// If the registry is the test one, set IsTestRegistry variable to run extra test cases
if isTestRegistry, isSet := os.LookupEnv("IS_TEST_REGISTRY"); isSet {
config.IsTestRegistry, err = strconv.ParseBool(isTestRegistry)
if err != nil {
fmt.Println(err)
panic(err)
}
} else {
config.IsTestRegistry = false
}

// If timeout duration until readiness probe runs out is set, run readiness probe
if timeout, isSet := os.LookupEnv("PROBE_TIMEOUT"); isSet {
probeTimeout, err := strconv.Atoi(timeout)
if err != nil {
fmt.Println(err)
panic(err)
}

config.IsTestRegistry, _ = strconv.ParseBool(os.Getenv("IS_TEST_REGISTRY"))
if err = util.ProbeRegistry(config.Registry, probeTimeout); err != nil {
fmt.Println(err)
panic(err)
}
}

return nil
}, func(data []byte) {})
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/pkg/tests/indexserver_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ var _ = ginkgo.Describe("[Verify index server is working properly]", func() {
gomega.Expect(body).To(gomega.ContainSubstring("<!DOCTYPE html>"))

// Verify that the registry viewer's page has been properly generated
gomega.Expect(body).To(gomega.ContainSubstring("A simple Hello World Node.js application"))
gomega.Expect(body).To(gomega.ContainSubstring("Devfile Registry"))
gomega.Expect(body).To(gomega.ContainSubstring("Devfile, OpenShift, Kubernetes"))
})

ginkgo.It("/index endpoint should return list of stacks", func() {
Expand Down
38 changes: 37 additions & 1 deletion tests/integration/pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Red Hat, Inc.
// Copyright 2022-2023 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,12 @@ package util

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"time"

indexSchema "github.com/devfile/registry-support/index/generator/schema"
"github.com/onsi/gomega"
)
Expand All @@ -28,6 +31,39 @@ type OCICatalog struct {
Repositories []string `json:"repositories,omitempty"`
}

// Probes a devfile registry to check if ready
func ProbeRegistry(registryUrl string, timeout int) error {
endpointUrl := registryUrl + "/health"
timeoutDuration := time.Duration(timeout) * time.Second
probe := &http.Client{}

// Checks if registry endpoint /health is ready
if resp, err := probe.Get(endpointUrl); resp != nil && resp.StatusCode != http.StatusOK {
start := time.Now()

// Set initial request timeout to the timeout duration of probing
probe.Timeout = timeoutDuration
// Loop until exited or timeout is reached
for time.Since(start) < timeoutDuration {
// Reduce request timeout by the time elapsed probing
probe.Timeout -= time.Since(start)
resp, err = probe.Get(endpointUrl)
// If request errors return error
// Else if response is OK status then health check passes and registry is ready
if err != nil {
return err
} else if resp != nil && resp.StatusCode == http.StatusOK {
return nil
}
time.Sleep(time.Second)
}
err = fmt.Errorf("probe timeout: '%s' was not ready in time", endpointUrl)
return err
} else {
return err
}
}

// GetRegistryIndex downloads the registry index.json at the specified URL and returns it
func GetRegistryIndex(url string) []indexSchema.Schema {
resp, err := http.Get(url)
Expand Down