Skip to content

Commit

Permalink
Check latest version from index instead of using hardcoded value (#6789)
Browse files Browse the repository at this point in the history
* Check latest version from index instead of using harcoded value

* Use checkVersion

* Fix when DEVFILE_REGISTRY ends with /
  • Loading branch information
feloy authored May 5, 2023
1 parent 6dd357d commit 32e2df0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
52 changes: 52 additions & 0 deletions tests/helper/helper_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package helper

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/redhat-developer/odo/pkg/api"
)

type Registry struct {
url string
}

func NewRegistry(url string) Registry {
return Registry{
url: url,
}
}

func (o Registry) GetIndex() ([]api.DevfileStack, error) {
url, err := url.JoinPath(o.url, "v2index")
if err != nil {
return nil, err
}
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var target []api.DevfileStack
err = json.NewDecoder(resp.Body).Decode(&target)
if err != nil {
return nil, err
}
return target, nil
}

func (o Registry) GetStack(name string) (api.DevfileStack, error) {
index, err := o.GetIndex()
if err != nil {
return api.DevfileStack{}, err
}
for _, stack := range index {
if stack.Name == name {
return stack, nil
}
}
return api.DevfileStack{}, fmt.Errorf("stack %q not found", name)
}
33 changes: 22 additions & 11 deletions tests/integration/cmd_devfile_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"

"github.com/redhat-developer/odo/pkg/config"
Expand Down Expand Up @@ -157,24 +158,34 @@ var _ = Describe("odo devfile init command tests", func() {
})
})

const (
devfileName = "go"
)
for _, ctx := range []struct {
title, devfileVersion, requiredVersion string
checkVersion func(metadataVersion string)
}{
{
title: "to download the latest version",
devfileVersion: "latest",
requiredVersion: "2.0.0",
title: "to download the latest version",
devfileVersion: "latest",
checkVersion: func(metadataVersion string) {
reg := helper.NewRegistry(helper.GetDevfileRegistryURL())
stack, err := reg.GetStack(devfileName)
Expect(err).ToNot(HaveOccurred())
Expect(len(stack.Versions)).ToNot(BeZero())
lastVersion := stack.Versions[0]
Expect(metadataVersion).To(BeEquivalentTo(lastVersion.Version))
},
},
{
title: "to download a specific version",
devfileVersion: "1.0.2",
requiredVersion: "1.0.2",
title: "to download a specific version",
devfileVersion: "1.0.2",
checkVersion: func(metadataVersion string) {
Expect(metadataVersion).To(BeEquivalentTo("1.0.2"))
},
},
} {
ctx := ctx
const (
devfileName = "go"
)
When(fmt.Sprintf("using --devfile-version flag %s", ctx.title), func() {
BeforeEach(func() {
helper.Cmd("odo", "init", "--name", "aname", "--devfile", devfileName, "--devfile-version", ctx.devfileVersion).ShouldPass()
Expand All @@ -184,7 +195,7 @@ var _ = Describe("odo devfile init command tests", func() {
files := helper.ListFilesInDir(commonVar.Context)
Expect(files).To(ContainElements("devfile.yaml"))
metadata := helper.GetMetadataFromDevfile(filepath.Join(commonVar.Context, "devfile.yaml"))
Expect(metadata.Version).To(BeEquivalentTo(ctx.requiredVersion))
ctx.checkVersion(metadata.Version)
})
})

Expand All @@ -197,7 +208,7 @@ var _ = Describe("odo devfile init command tests", func() {
It("should show the requested devfile version", func() {
stdout := res.Out()
Expect(helper.IsJSON(stdout)).To(BeTrue())
helper.JsonPathContentIs(stdout, "devfileData.devfile.metadata.version", ctx.requiredVersion)
ctx.checkVersion(gjson.Get(stdout, "devfileData.devfile.metadata.version").String())
})
})
}
Expand Down

0 comments on commit 32e2df0

Please sign in to comment.