diff --git a/tests/integration/README.md b/tests/integration/README.md index f517d1fc9..9060632a8 100644 --- a/tests/integration/README.md +++ b/tests/integration/README.md @@ -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: diff --git a/tests/integration/cmd/devfileregistry_test.go b/tests/integration/cmd/devfileregistry_test.go index ad22e9eaf..1cb83907e 100644 --- a/tests/integration/cmd/devfileregistry_test.go +++ b/tests/integration/cmd/devfileregistry_test.go @@ -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. @@ -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" @@ -38,6 +39,8 @@ 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 == "" { @@ -45,9 +48,34 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte { } 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) + } + } + + // 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) {}) diff --git a/tests/integration/pkg/tests/indexserver_tests.go b/tests/integration/pkg/tests/indexserver_tests.go index c2b05f14b..0f5799757 100644 --- a/tests/integration/pkg/tests/indexserver_tests.go +++ b/tests/integration/pkg/tests/indexserver_tests.go @@ -73,7 +73,8 @@ var _ = ginkgo.Describe("[Verify index server is working properly]", func() { gomega.Expect(body).To(gomega.ContainSubstring("")) // 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() { diff --git a/tests/integration/pkg/util/util.go b/tests/integration/pkg/util/util.go index 42501f2f1..04a327711 100644 --- a/tests/integration/pkg/util/util.go +++ b/tests/integration/pkg/util/util.go @@ -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. @@ -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" ) @@ -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)