Skip to content

Commit

Permalink
MakeVersionMap function moved to util package & unit test added for M…
Browse files Browse the repository at this point in the history
…akeVersionMap function.

Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron committed Jun 15, 2022
1 parent 3f65417 commit 33639be
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 35 deletions.
34 changes: 2 additions & 32 deletions index/server/pkg/server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
indexSchema "github.com/devfile/registry-support/index/generator/schema"
"github.com/devfile/registry-support/index/server/pkg/util"
"github.com/gin-gonic/gin"
versionpkg "github.com/hashicorp/go-version"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/segmentio/analytics-go.v3"
)
Expand Down Expand Up @@ -134,7 +133,7 @@ func serveDevfileStarterProjectWithVersion(c *gin.Context) {
devfileBytes, devfileIndex := fetchDevfile(c, devfileName, version)

if len(devfileIndex.Versions) > 1 {
versionMap, err := makeVersionMap(devfileIndex)
versionMap, err := util.MakeVersionMap(devfileIndex)
if err != nil {
log.Print(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down Expand Up @@ -393,35 +392,6 @@ func buildIndexAPIResponse(c *gin.Context, wantV1Index bool) {
}
}

// makeVersionMap creates a map of versions for a given devfile index schema.
func makeVersionMap(devfileIndex indexSchema.Schema) (map[string]indexSchema.Version, error) {
versionMap := make(map[string]indexSchema.Version)
var latestVersion string
for _, versionElement := range devfileIndex.Versions {
versionMap[versionElement.Version] = versionElement
if versionElement.Default {
versionMap["default"] = versionElement
}
if latestVersion != "" {
latest, err := versionpkg.NewVersion(latestVersion)
if err != nil {
return map[string]indexSchema.Version{}, err
}
current, err := versionpkg.NewVersion(versionElement.Version)
if err != nil {
return map[string]indexSchema.Version{}, err
}
if current.GreaterThan(latest) {
latestVersion = versionElement.Version
}
} else {
latestVersion = versionElement.Version
}
}
versionMap["latest"] = versionMap[latestVersion]
return versionMap, nil
}

// fetchDevfile retrieves a specified devfile by fetching stacks from the OCI
// registry and samples from the `samplesPath` given by server. Also retrieves index
// schema from `indexPath` given by server.
Expand Down Expand Up @@ -489,7 +459,7 @@ func fetchDevfile(c *gin.Context, name string, version string) ([]byte, indexSch
sampleDevfilePath = path.Join(samplesPath, devfileIndex.Name, devfileName)
}
} else {
versionMap, err := makeVersionMap(devfileIndex)
versionMap, err := util.MakeVersionMap(devfileIndex)
if err != nil {
log.Print(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down
31 changes: 31 additions & 0 deletions index/server/pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"strings"

versionpkg "github.com/hashicorp/go-version"

indexLibrary "github.com/devfile/registry-support/index/generator/library"
indexSchema "github.com/devfile/registry-support/index/generator/schema"
)
Expand Down Expand Up @@ -182,3 +184,32 @@ func IsTelemetryEnabled() bool {
}
return false
}

// MakeVersionMap creates a map of versions for a given devfile index schema.
func MakeVersionMap(devfileIndex indexSchema.Schema) (map[string]indexSchema.Version, error) {
versionMap := make(map[string]indexSchema.Version)
var latestVersion string
for _, versionElement := range devfileIndex.Versions {
versionMap[versionElement.Version] = versionElement
if versionElement.Default {
versionMap["default"] = versionElement
}
if latestVersion != "" {
latest, err := versionpkg.NewVersion(latestVersion)
if err != nil {
return map[string]indexSchema.Version{}, err
}
current, err := versionpkg.NewVersion(versionElement.Version)
if err != nil {
return map[string]indexSchema.Version{}, err
}
if current.GreaterThan(latest) {
latestVersion = versionElement.Version
}
} else {
latestVersion = versionElement.Version
}
}
versionMap["latest"] = versionMap[latestVersion]
return versionMap, nil
}
69 changes: 66 additions & 3 deletions index/server/pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package util

import (
"encoding/json"
"github.com/devfile/registry-support/index/generator/schema"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"

indexSchema "github.com/devfile/registry-support/index/generator/schema"
)

func TestIsHtmlRequested(t *testing.T) {
Expand Down Expand Up @@ -206,12 +208,12 @@ func TestConvertToOldIndexFormat(t *testing.T) {
if err != nil {
t.Errorf("Failed to oldIndexStruct.json: %v", err)
}
var inputIndex []schema.Schema
var inputIndex []indexSchema.Schema
err = json.Unmarshal(bytes, &inputIndex)
if err != nil {
t.Errorf("Failed to unmarshal inputIndex json")
}
var wantIndex []schema.Schema
var wantIndex []indexSchema.Schema
err = json.Unmarshal(expected, &wantIndex)
if err != nil {
t.Errorf("Failed to unmarshal wantIndex json")
Expand All @@ -225,3 +227,64 @@ func TestConvertToOldIndexFormat(t *testing.T) {
}
})
}

func TestMakeVersionMap(t *testing.T) {
devfileIndex := indexSchema.Schema{
Name: "Test Devfile",
Version: "2.2.0",
Attributes: nil,
DisplayName: "",
Description: "",
Type: "",
Tags: []string{},
Architectures: []string{},
Icon: "",
GlobalMemoryLimit: "",
ProjectType: "",
Language: "",
Links: map[string]string{},
Resources: []string{},
StarterProjects: []string{},
Git: &indexSchema.Git{},
Provider: "",
SupportUrl: "",
Versions: []indexSchema.Version{
{
Version: "1.1.0",
Default: true,
},
{Version: "1.2.0"},
},
}
tests := []struct {
key string
wantVal string
}{
{
key: "default",
wantVal: "1.1.0",
},
{
key: "latest",
wantVal: "1.2.0",
},
{
key: "1.1.0",
wantVal: "1.1.0",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("Test generate version map with key %s", test.key), func(t *testing.T) {
versionMap, err := MakeVersionMap(devfileIndex)
if err != nil {
t.Errorf("Was not expecting error with MakeVersionMap: %v", err)
}

if !reflect.DeepEqual(test.wantVal, versionMap[test.key].Version) {
t.Errorf("Was expecting '%s' to map to '%s' not '%s'",
test.key, test.wantVal, versionMap[test.key].Version)
}
})
}
}

0 comments on commit 33639be

Please sign in to comment.