Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check latest version from index instead of using hardcoded value #6789

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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