Skip to content

Commit

Permalink
Add conditions as future replacement of requirements (#519)
Browse files Browse the repository at this point in the history
This change adds `conditions` as part of the package manifest to align with other parts in the stack. Currently it adds the future of conditions and does not remove requirements yet. Requirements will be removed as soon as packages are updated.
  • Loading branch information
ruflin authored Jun 30, 2020
1 parent 71f9dff commit 33c9f9d
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add list of downloads to /search endpoint. [#512](https://github.com/elastic/package-registry/pull/512)
* Apply rule: first package found served. [#546](https://github.com/elastic/package-registry/pull/546)
* Implement package watcher. [#553](https://github.com/elastic/package-registry/pull/553)
* Add conditions as future replacement of requirements. [#519](https://github.com/elastic/package-registry/pull/519)

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"azure"
],
"release": "ga",
"conditions": {
"kibana.version": "~7.x.x"
},
"requirement": {
"kibana": {
"versions": "\u003e=7.0.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/base/0.2.0/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"license": "basic",
"categories": [],
"release": "ga",
"conditions": {
"kibana.version": "\u003e7.9.0"
},
"requirement": {
"kibana": {
"versions": "\u003e7.9.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/example/0.0.2/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"web"
],
"release": "beta",
"conditions": {
"kibana.version": "\u003e=6.0.0"
},
"requirement": {
"kibana": {
"versions": "\u003e=6.0.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/example/1.0.0/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"azure"
],
"release": "ga",
"conditions": {
"kibana.version": "~7.x.x"
},
"requirement": {
"kibana": {
"versions": "\u003e=7.0.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/foo/1.0.0/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"custom"
],
"release": "beta",
"conditions": {
"kibana.version": "\u003e=7.0.0"
},
"requirement": {
"kibana": {
"versions": "\u003e=7.0.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/longdocs/1.0.4/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"web"
],
"release": "ga",
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"requirement": {
"kibana": {
"versions": "\u003e6.7.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/multiversion/1.0.3/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"web"
],
"release": "ga",
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"requirement": {
"kibana": {
"versions": "\u003e6.7.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/multiversion/1.0.4/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"web"
],
"release": "ga",
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"requirement": {
"kibana": {
"versions": "\u003e6.7.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/multiversion/1.1.0/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"web"
],
"release": "ga",
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"requirement": {
"kibana": {
"versions": "\u003e6.7.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/generated/package/reference/1.0.0/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"web"
],
"release": "ga",
"conditions": {
"kibana.version": "\u003e6.7.0 \u003c7.6.0"
},
"requirement": {
"kibana": {
"versions": "\u003e6.7.0 \u003c7.6.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/package/example/1.0.0/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ release: ga

owner.github: "ruflin"

conditions:
kibana.version: "~7.x.x"

requirement:
kibana:
versions: ">=7.0.0"
Expand Down
3 changes: 3 additions & 0 deletions testdata/package/reference/1.0.0/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type: integration
owner:
github: "ruflin"

conditions:
kibana.version: ">6.7.0 <7.6.0"

requirement:
kibana:
versions: ">6.7.0 <7.6.0"
Expand Down
31 changes: 21 additions & 10 deletions util/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Package struct {
versionSemVer *semver.Version
Categories []string `config:"categories" json:"categories"`
Release string `config:"release,omitempty" json:"release,omitempty"`
Conditions *Conditions `config:"conditions,omitempty" json:"conditions,omitempty" yaml:"conditions,omitempty"`
Requirement Requirement `config:"requirement" json:"requirement"`
Screenshots []Image `config:"screenshots,omitempty" json:"screenshots,omitempty" yaml:"screenshots,omitempty"`
Assets []string `config:"assets,omitempty" json:"assets,omitempty" yaml:"assets,omitempty"`
Expand Down Expand Up @@ -96,6 +97,11 @@ type Requirement struct {
Kibana ProductRequirement `config:"kibana" json:"kibana,omitempty" yaml:"kibana"`
}

type Conditions struct {
KibanaVersion string `config:"kibana.version,omitempty" json:"kibana.version,omitempty" yaml:"kibana.version,omitempty"`
kibanaConstraint *semver.Constraints
}

type ProductRequirement struct {
Versions string `config:"versions,omitempty" json:"versions,omitempty" yaml:"versions,omitempty"`
semVerRange *semver.Constraints
Expand Down Expand Up @@ -149,7 +155,7 @@ func NewPackage(basePath string) (*Package, error) {
var p = &Package{
BasePath: basePath,
}
err = manifest.Unpack(p)
err = manifest.Unpack(p, ucfg.PathSep("."))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,8 +190,18 @@ func NewPackage(basePath string) (*Package, error) {

p.Downloads = []Download{NewDownload(*p, "tar")}

if p.Requirement.Kibana.Versions != "" {
p.Requirement.Kibana.semVerRange, err = semver.NewConstraint(p.Requirement.Kibana.Versions)
// If the new conditions are used, select them over the requirements
if p.Conditions != nil && p.Conditions.KibanaVersion != "" {
p.Conditions.kibanaConstraint, err = semver.NewConstraint(p.Conditions.KibanaVersion)
if err != nil {
return nil, errors.Wrapf(err, "invalid Kibana versions range: %s", p.Conditions.KibanaVersion)
}
// TODO: remove legacy part
} else if p.Requirement.Kibana.Versions != "" {
p.Conditions = &Conditions{
KibanaVersion: p.Requirement.Kibana.Versions,
}
p.Conditions.kibanaConstraint, err = semver.NewConstraint(p.Requirement.Kibana.Versions)
if err != nil {
return nil, errors.Wrapf(err, "invalid Kibana versions range: %s", p.Requirement.Kibana.Versions)
}
Expand Down Expand Up @@ -252,16 +268,11 @@ func (p *Package) HasCategory(category string) bool {
func (p *Package) HasKibanaVersion(version *semver.Version) bool {

// If the version is not specified, it is for all versions
if p.Requirement.Kibana.Versions == "" {
if p.Conditions == nil || version == nil {
return true
}

if version != nil {
if !p.Requirement.Kibana.semVerRange.Check(version) {
return false
}
}
return true
return p.Conditions.kibanaConstraint.Check(version)
}

func (p *Package) IsNewerOrEqual(pp Package) bool {
Expand Down
82 changes: 82 additions & 0 deletions util/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package util

import (
"log"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -127,6 +129,86 @@ func TestValidate(t *testing.T) {
}
}

var kibanaVersionPackageTests = []struct {
description string
constraint string
kibanaVersion string
check bool
}{
{
"last major",
">= 7.0.0",
"6.7.0",
false,
},
{
"next minor",
">= 7.0.0",
"7.1.0",
true,
},
{
"next minor tilde",
"~7",
"7.1.0",
true,
},
{
"next minor tilde, x",
"~7.x.x",
"7.1.0",
true,
},
{
"next minor tilde, not matching",
"~7.0.0",
"7.1.0",
false,
},
{
"next minor tilde, matching",
"~7.0.x",
"7.0.2",
true,
},
{
"inside major, not",
"^7.6.0",
"7.0.2",
false,
},
{
"inside major",
"^7.6.0",
"7.12.2",
true,
},
}

func TestHasKibanaVersion(t *testing.T) {
for _, tt := range kibanaVersionPackageTests {
t.Run(tt.description, func(t *testing.T) {

constraint, err := semver.NewConstraint(tt.constraint)
assert.NoError(t, err)

p := Package{
Conditions: &Conditions{
kibanaConstraint: constraint,
},
}

kibanaVersion, err := semver.NewVersion(tt.kibanaVersion)
assert.NoError(t, err)

check := p.HasKibanaVersion(kibanaVersion)
log.Println(check)
assert.Equal(t, tt.check, check)

})
}
}

func BenchmarkNewPackage(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := NewPackage("../testdata/package/reference/1.0.0")
Expand Down

0 comments on commit 33c9f9d

Please sign in to comment.