Skip to content

Commit

Permalink
Start HTTP server to replace Devfile Registry server in the tests (#7154
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rm3l authored Dec 1, 2023
1 parent 172c57d commit 15f663f
Show file tree
Hide file tree
Showing 104 changed files with 6,721 additions and 54 deletions.
8 changes: 5 additions & 3 deletions .ibm/pipelines/windows-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ TEST_NAME="Windows Tests (OCP)"

export REPO=${REPO:-"https://github.com/redhat-developer/odo"}
#copy test script inside /tmp/
sshpass -p $WINDOWS_PASSWORD scp -o StrictHostKeyChecking=no ./.ibm/pipelines/windows-test-script.ps1 Administrator@$WINDOWS_IP:/tmp/windows-test-script.ps1
sshpass -p $WINDOWS_PASSWORD scp -o StrictHostKeyChecking=no powershell "New-Item -ItemType Directory -Force -Path C:\tmp -ErrorAction SilentlyContinue"

sshpass -p $WINDOWS_PASSWORD scp -o StrictHostKeyChecking=no ./.ibm/pipelines/windows-test-script.ps1 Administrator@$WINDOWS_IP:C:\tmp\windows-test-script.ps1

#execute test from the test script
export TEST_EXEC_NODES=${TEST_EXEC_NODES:-"16"}
sshpass -p $WINDOWS_PASSWORD ssh Administrator@$WINDOWS_IP -o StrictHostKeyChecking=no powershell /tmp/windows-test-script.ps1 "${GIT_PR_NUMBER}" "${BUILD_NUMBER}" "${API_KEY_QE}" "${IBM_OPENSHIFT_ENDPOINT}" "${LOGFILE}" "${REPO}" "${CLUSTER_ID}" "${TEST_EXEC_NODES}" "${SKIP_SERVICE_BINDING_TESTS}"
sshpass -p $WINDOWS_PASSWORD ssh Administrator@$WINDOWS_IP -o StrictHostKeyChecking=no powershell C:\tmp\windows-test-script.ps1 "${GIT_PR_NUMBER}" "${BUILD_NUMBER}" "${API_KEY_QE}" "${IBM_OPENSHIFT_ENDPOINT}" "${LOGFILE}" "${REPO}" "${CLUSTER_ID}" "${TEST_EXEC_NODES}" "${SKIP_SERVICE_BINDING_TESTS}"
RESULT=$?
echo "RESULT: $RESULT"

Expand All @@ -36,6 +38,6 @@ save_results "/tmp/test-integration.xml" "${LOGFILE}" "${TEST_NAME}" "${BUILD_NU
save_results "/tmp/test-e2e.xml" "${LOGFILE}" "${TEST_NAME}" "${BUILD_NUMBER}"

# cleanup
sshpass -p $WINDOWS_PASSWORD ssh Administrator@$WINDOWS_IP -o StrictHostKeyChecking=no rm -rf /tmp/windows-test-script.ps1
sshpass -p $WINDOWS_PASSWORD ssh Administrator@$WINDOWS_IP -o StrictHostKeyChecking=no rm -rf C:\tmp\windows-test-script.ps1

exit ${RESULT}
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,11 @@ generate-api: generate-apiserver generate-apifront ## Generate code based on odo
.PHONY: copy-swagger-ui
copy-swagger-ui:
./scripts/copy-swagger-ui.sh

generate-test-registry-build: ## Rebuild the local registry artifacts. Only for testing
mkdir -p "${PWD}"/tests/helper/registry_server/testdata/registry-build
rm -rf "${PWD}"/tests/helper/registry_server/testdata/registry-build/*
podman container run --rm \
-v "${PWD}"/tests/helper/registry_server/testdata:/code \
-t docker.io/golang:1.19 \
bash /code/build-registry.sh /code/registry/ /code/registry-build/
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
```console
$ odo init --devfile-path https://registry.devfile.io/devfiles/nodejs-angular --name my-nodejs-app --starter nodejs-angular-starter
$ odo init --devfile-path https://registry.stage.devfile.io/devfiles/nodejs-angular --name my-nodejs-app --starter nodejs-angular-starter
__
/ \__ Initializing a new component
\__/ \
/ \__/ odo version: v3.15.0
\__/

✓ Downloading devfile from "https://registry.devfile.io/devfiles/nodejs-angular" [1s]
✓ Downloading devfile from "https://registry.stage.devfile.io/devfiles/nodejs-angular" [1s]
✓ Downloading starter project "nodejs-angular-starter" [958ms]

Your new component 'my-nodejs-app' is ready in the current directory.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```console
$ odo registry --devfile nodejs-react
NAME REGISTRY DESCRIPTION ARCHITECTURES VERSIONS
nodejs-react StagingRegistry React is a free and open-source front-en... 2.0.2, 2.1.0, 2.2.0
nodejs-react DefaultDevfileRegistry React is a free and open-source front-en... 2.0.2, 2.1.0, 2.2.0
nodejs-react StagingRegistry React is a free and open-source front-en... 2.0.2, 2.2.0
nodejs-react DefaultDevfileRegistry React is a free and open-source front-en... 2.0.2, 2.2.0
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ $ odo preference view
Devfile registries:
NAME URL SECURE
StagingRegistry https://registry.stage.devfile.io No
DefaultDevfileRegistry https://registry.devfile.io No
DefaultDevfileRegistry https://registry.stage.devfile.io No
```
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
```console
$ odo init --devfile go --name my-go-app --devfile-version 2.0.0
$ odo init --devfile go --name my-go-app --devfile-version 2.2.0
__
/ \__ Initializing a new component
\__/ \
/ \__/ odo version: v3.15.0
\__/

✓ Downloading devfile "go:2.0.0" [3s]
✓ Downloading devfile "go:2.2.0" [3s]

Your new component 'my-go-app' is ready in the current directory.
To start editing your component, use 'odo dev' and open this folder in your favorite IDE.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ Supported architectures: all
Language: Go
Project type: Go
Application ports: 8080
The devfile "go:1.0.2" from the registry "DefaultDevfileRegistry" will be downloaded.
The devfile "go:1.2.0" from the registry "DefaultDevfileRegistry" will be downloaded.
? Is this correct? Yes
✓ Downloading devfile "go:1.0.2" from registry "DefaultDevfileRegistry" [3s]
✓ Downloading devfile "go:1.2.0" from registry "DefaultDevfileRegistry" [3s]

↪ Container Configuration "runtime":
OPEN PORTS:
- 8080
- 5858
ENVIRONMENT VARIABLES:
- DEBUG_PORT = 5858

? Select container for which you want to change configuration? NONE - configuration is correct
? Enter component name: my-go-app

You can automate this command by executing:
odo init --name my-go-app --devfile go --devfile-registry DefaultDevfileRegistry --devfile-version 1.0.2
odo init --name my-go-app --devfile go --devfile-registry DefaultDevfileRegistry --devfile-version 1.2.0

Your new component 'my-go-app' is ready in the current directory.
To start editing your component, use 'odo dev' and open this folder in your favorite IDE.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/go-openapi/spec v0.20.8
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/jedib0t/go-pretty/v6 v6.4.7
github.com/kubernetes-sigs/service-catalog v0.3.1
Expand Down Expand Up @@ -110,6 +111,7 @@ require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
Context("Non Interactive Mode", func() {

It("Fetch Devfile of a specific version", func() {
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "2.0.0"}
args := []string{"init", "--devfile", "go", "--name", "my-go-app", "--devfile-version", "2.2.0"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
Expand All @@ -126,10 +126,11 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
})

It("Fetch Devfile from a URL", func() {
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
args := []string{"init", "--devfile-path", fmt.Sprintf("%s/devfiles/nodejs-angular", commonVar.GetDevfileRegistryURL()), "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
got = helper.ReplaceRegistryUrl(commonVar, got)
file := "devfile_from_url_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
Expand All @@ -139,21 +140,19 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
Context("fetching devfile from a registry", func() {
When("setting up the registry", func() {
const (
defaultReg = "DefaultDevfileRegistry"
defaultRegURL = "https://registry.devfile.io"
stagingReg = "StagingRegistry"
stagingRegURL = "https://registry.stage.devfile.io"
defaultReg = "DefaultDevfileRegistry"
stagingReg = "StagingRegistry"
)
BeforeEach(func() {
helper.Cmd("odo", "preference", "remove", "registry", defaultReg, "-f").ShouldPass()
helper.Cmd("odo", "preference", "add", "registry", defaultReg, defaultRegURL).ShouldPass()

helper.Cmd("odo", "preference", "add", "registry", stagingReg, stagingRegURL).ShouldPass()
devfileRegistryURL := commonVar.GetDevfileRegistryURL()
helper.Cmd("odo", "preference", "add", "registry", defaultReg, devfileRegistryURL).ShouldPass()
helper.Cmd("odo", "preference", "add", "registry", stagingReg, devfileRegistryURL).ShouldPass()
})

AfterEach(func() {
helper.Cmd("odo", "preference", "remove", "registry", stagingReg, "-f").ShouldPass()
helper.SetDefaultDevfileRegistryAsStaging()
helper.SetDefaultDevfileRegistry(&commonVar)
})

removePreferenceKeys := func(docString string) string {
Expand All @@ -165,10 +164,26 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
got := helper.StripAnsi(out)
got = removePreferenceKeys(got)
got = fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(got))
got = helper.ReplaceRegistryUrl(commonVar, got)
file := "registry_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty(), file)
want = helper.ReplaceRegistryUrl(commonVar, want)
wantLines, err := helper.ExtractLines(want)
Expect(err).ShouldNot(HaveOccurred())
gotLines, err := helper.ExtractLines(got)
Expect(err).ShouldNot(HaveOccurred())
Expect(gotLines).ShouldNot(BeEmpty())
Expect(gotLines).Should(HaveLen(len(wantLines)),
fmt.Sprintf("%s: unexpected number of lines:\n==want:\n%s\n\n==got:\n%s", file, want, got))
for i, line := range wantLines {
if strings.Contains(line, "SECURE") {
continue
}
wantFields := strings.Fields(line)
gotFields := strings.Fields(gotLines[i])
Expect(gotFields).Should(HaveExactElements(wantFields),
fmt.Sprintf("%s: mismatch at line %d:\n==want line:\n%s\n\n==got line:\n%s", file, i, line, gotLines[i]))
}
}

It("Fetch Devfile from a specific registry of the list", func() {
Expand Down Expand Up @@ -219,10 +234,11 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
})

It("Fetch Devfile from a URL", func() {
args := []string{"init", "--devfile-path", "https://registry.devfile.io/devfiles/nodejs-angular", "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
args := []string{"init", "--devfile-path", fmt.Sprintf("%s/devfiles/nodejs-angular", commonVar.GetDevfileRegistryURL()), "--name", "my-nodejs-app", "--starter", "nodejs-angular-starter"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
got = helper.ReplaceRegistryUrl(commonVar, got)
file := "devfile_from_url_output.mdx"
want := helper.GetMDXContent(filepath.Join(commonPath, file))
diff := cmp.Diff(want, got)
Expand Down
5 changes: 5 additions & 0 deletions tests/helper/helper_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,8 @@ func ReplaceAllForwardedPorts(docString string, cmdEndpointsMap map[string]strin
}
return
}

// ReplaceRegistryUrl replaces the registry URL used for testing by a more static one
func ReplaceRegistryUrl(commonVar CommonVar, docString string) string {
return strings.ReplaceAll(docString, commonVar.GetDevfileRegistryURL(), "https://registry.stage.devfile.io")
}
37 changes: 26 additions & 11 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/redhat-developer/odo/pkg/segment"
"github.com/redhat-developer/odo/tests/helper/registry_server"

dfutil "github.com/devfile/library/v2/pkg/util"

Expand Down Expand Up @@ -179,6 +180,8 @@ type CommonVar struct {
// original values to get restored after the test is done
OriginalWorkingDirectory string
OriginalKubeconfig string
registryServer RegistryServer
registryUrl string
// Ginkgo test realted
testFileName string
testCase string
Expand Down Expand Up @@ -256,7 +259,7 @@ func CommonBeforeEach() CommonVar {
// Use ephemeral volumes (emptyDir) in tests to make test faster
err = cfg.SetConfiguration(preference.EphemeralSetting, "true")
Expect(err).To(BeNil())
SetDefaultDevfileRegistryAsStaging()
SetDefaultDevfileRegistry(&commonVar)
return commonVar
}

Expand Down Expand Up @@ -299,6 +302,15 @@ func CommonAfterEach(commonVar CommonVar) {
}
}

if commonVar.registryServer != nil {
err = commonVar.registryServer.Stop()
if err != nil {
fmt.Fprintf(GinkgoWriter, "[warn] failed to stop mock registry server at %q: %v\n", commonVar.registryServer.GetUrl(), err)
}
commonVar.registryServer = nil
commonVar.registryUrl = ""
}

if commonVar.Project != "" && commonVar.CliRunner.HasNamespaceProject(commonVar.Project) {
// delete the random project/namespace created in CommonBeforeEach
commonVar.CliRunner.DeleteNamespaceProject(commonVar.Project, false)
Expand Down Expand Up @@ -394,20 +406,23 @@ type ResourceInfo struct {
Namespace string
}

func SetDefaultDevfileRegistryAsStaging() {
func SetDefaultDevfileRegistry(commonVar *CommonVar) {
commonVar.registryUrl = os.Getenv("DEVFILE_REGISTRY")
if commonVar.registryUrl == "" {
commonVar.registryServer = registry_server.NewMockRegistryServer()
var err error
commonVar.registryUrl, err = commonVar.registryServer.Start()
Expect(err).ShouldNot(HaveOccurred())
}
fmt.Printf("Using Devfile Registry URL at: %q\n", commonVar.registryUrl)

const registryName string = "DefaultDevfileRegistry"
Cmd("odo", "preference", "remove", "registry", registryName, "-f").ShouldPass()
Cmd("odo", "preference", "add", "registry", registryName, GetDevfileRegistryURL()).ShouldPass()
Cmd("odo", "preference", "add", "registry", registryName, commonVar.registryUrl).ShouldPass()
}

func GetDevfileRegistryURL() string {
registryURL := "https://registry.stage.devfile.io"
customReg := os.Getenv("DEVFILE_REGISTRY")
if customReg != "" {
registryURL = customReg
}
fmt.Printf("Using Devfile Registry URL at: %q\n", registryURL)
return registryURL
func (c CommonVar) GetDevfileRegistryURL() string {
return c.registryUrl
}

func GetOdoVersion() (version string, gitCommit string) {
Expand Down
10 changes: 9 additions & 1 deletion tests/helper/helper_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"

. "github.com/onsi/ginkgo/v2"

Expand All @@ -26,7 +27,7 @@ func NewRegistry(url string) Registry {
}

func (o Registry) GetIndex() ([]api.DevfileStack, error) {
url, err := url.JoinPath(o.url, "v2index")
url, err := url.JoinPath(strings.TrimSuffix(o.url, "/"), "/v2index")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -82,3 +83,10 @@ func GetVersions(registryName string, stackName string) []string {
func HasAtLeastTwoVersions(registryName string, stackName string) bool {
return len(GetVersions(registryName, stackName)) >= 2
}

type RegistryServer interface {
Start() (url string, err error)
Stop() error
IsStarted() bool
GetUrl() string
}
Loading

0 comments on commit 15f663f

Please sign in to comment.