Skip to content

Commit

Permalink
Check latest version from index instead of using harcoded value
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed May 4, 2023
1 parent d0ba624 commit 3f42724
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
47 changes: 47 additions & 0 deletions tests/helper/helper_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package helper

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

"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) {
resp, err := http.Get(o.url + "/v2index")
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)
}
31 changes: 26 additions & 5 deletions tests/integration/cmd_devfile_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ var _ = Describe("odo devfile init command tests", func() {

for _, ctx := range []struct {
title, devfileVersion, requiredVersion string
gotLatest bool
}{
{
title: "to download the latest version",
devfileVersion: "latest",
requiredVersion: "2.0.0",
title: "to download the latest version",
devfileVersion: "latest",
gotLatest: true,
},
{
title: "to download a specific version",
Expand All @@ -184,7 +185,17 @@ 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))
if ctx.requiredVersion != "" {
Expect(metadata.Version).To(BeEquivalentTo(ctx.requiredVersion))
}
if ctx.gotLatest {
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(metadata.Version).To(BeEquivalentTo(lastVersion.Version))
}
})
})

Expand All @@ -197,7 +208,17 @@ 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)
if ctx.requiredVersion != "" {
helper.JsonPathContentIs(stdout, "devfileData.devfile.metadata.version", ctx.requiredVersion)
}
if ctx.gotLatest {
reg := helper.NewRegistry(helper.GetDevfileRegistryURL())
stack, err := reg.GetStack(devfileName)
Expect(err).ToNot(HaveOccurred())
Expect(len(stack.Versions)).ToNot(BeZero())
lastVersion := stack.Versions[0]
helper.JsonPathContentIs(stdout, "devfileData.devfile.metadata.version", lastVersion.Version)
}
})
})
}
Expand Down

0 comments on commit 3f42724

Please sign in to comment.