Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rm3l committed Nov 30, 2023
1 parent 172c57d commit 0335059
Show file tree
Hide file tree
Showing 94 changed files with 6,254 additions and 46 deletions.
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
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
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,8 +164,10 @@ 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))
want = helper.ReplaceRegistryUrl(commonVar, want)
diff := cmp.Diff(want, got)
Expect(diff).To(BeEmpty(), file)
}
Expand All @@ -177,7 +178,7 @@ var _ = Describe("doc command reference odo init", Label(helper.LabelNoCluster),
})

By("checking for the init output", func() {
args := []string{"init", "--name", "my-spring-app", "--devfile", "java-springboot", "--devfile-registry", "DefaultDevfileRegistry", "--starter", "springbootproject"}
args := []string{"init", "--name", "my-spring-app", "--devfile", "java-springboot", "--devfile-registry", stagingReg, "--starter", "springbootproject"}
out := helper.Cmd("odo", args...).ShouldPass().Out()
got := fmt.Sprintf(outputStringFormat, strings.Join(args, " "), helper.StripSpinner(out))
got = helper.StripGitCommitFromVersion(got)
Expand Down Expand Up @@ -219,10 +220,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
}
2 changes: 1 addition & 1 deletion tests/helper/odo_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func DeleteProject(projectName string) {

// GetMetadataFromDevfile retrieves the metadata from devfile
func GetMetadataFromDevfile(devfilePath string) devfilepkg.DevfileMetadata {
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", true)
devObj, err := devfile.ParseAndValidateFromFile(devfilePath, "", false)
Expect(err).ToNot(HaveOccurred())
return devObj.Data.GetMetadata()
}
Expand Down
Loading

0 comments on commit 0335059

Please sign in to comment.