Skip to content

Commit

Permalink
Add conditions as future replacement of requirements
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 committed Jun 30, 2020
1 parent 5628ef3 commit ad1bca1
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -25,6 +25,9 @@
"versions": "\u003e=7.0.0"
}
},
"conditions": {
"kibana.version": "~7.x.x"
},
"screenshots": [
{
"src": "/package/example/1.0.0/img/kibana-iptables.png",
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 @@ -23,6 +23,9 @@
"versions": "\u003e7.9.0"
}
},
"conditions": {
"kibana.version": "\u003e7.9.0"
},
"assets": [
"/package/base/0.2.0/manifest.yml",
"/package/base/0.2.0/docs/README.md",
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 @@ -24,6 +24,9 @@
"versions": "\u003e=6.0.0"
}
},
"conditions": {
"kibana.version": "\u003e=6.0.0"
},
"assets": [
"/package/example/0.0.2/manifest.yml",
"/package/example/0.0.2/docs/README.md",
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 @@ -25,6 +25,9 @@
"versions": "\u003e=7.0.0"
}
},
"conditions": {
"kibana.version": "~7.x.x"
},
"screenshots": [
{
"src": "/package/example/1.0.0/img/kibana-iptables.png",
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 @@ -24,6 +24,9 @@
"versions": "\u003e=7.0.0"
}
},
"conditions": {
"kibana.version": "\u003e=7.0.0"
},
"assets": [
"/package/foo/1.0.0/manifest.yml",
"/package/foo/1.0.0/docs/README.md"
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 @@ -31,6 +31,9 @@
"versions": "\u003e6.7.0"
}
},
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"assets": [
"/package/longdocs/1.0.4/manifest.yml",
"/package/longdocs/1.0.4/docs/README.md",
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 @@ -31,6 +31,9 @@
"versions": "\u003e6.7.0"
}
},
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"assets": [
"/package/multiversion/1.0.3/changelog.yml",
"/package/multiversion/1.0.3/manifest.yml",
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 @@ -31,6 +31,9 @@
"versions": "\u003e6.7.0"
}
},
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"assets": [
"/package/multiversion/1.0.4/changelog.yml",
"/package/multiversion/1.0.4/manifest.yml",
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 @@ -31,6 +31,9 @@
"versions": "\u003e6.7.0"
}
},
"conditions": {
"kibana.version": "\u003e6.7.0"
},
"assets": [
"/package/multiversion/1.1.0/changelog.yml",
"/package/multiversion/1.1.0/manifest.yml",
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 @@ -32,6 +32,9 @@
"versions": "\u003e6.7.0 \u003c7.6.0"
}
},
"conditions": {
"kibana.version": "\u003e6.7.0 \u003c7.6.0"
},
"assets": [
"/package/reference/1.0.0/changelog.yml",
"/package/reference/1.0.0/manifest.yml",
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 @@ -61,6 +61,7 @@ type Package struct {
Categories []string `config:"categories" json:"categories"`
Release string `config:"release,omitempty" json:"release,omitempty"`
Requirement Requirement `config:"requirement" json:"requirement"`
Conditions *Conditions `config:"conditions,omitempty" json:"conditions,omitempty" yaml:"conditions,omitempty"`
Screenshots []Image `config:"screenshots,omitempty" json:"screenshots,omitempty" yaml:"screenshots,omitempty"`
Assets []string `config:"assets,omitempty" json:"assets,omitempty" yaml:"assets,omitempty"`
Datasources []Datasource `config:"datasources,omitempty" json:"config_templates,omitempty" yaml:"datasources,omitempty"`
Expand Down Expand Up @@ -97,6 +98,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"`
kibanaVersion *semver.Constraints
}

type ProductRequirement struct {
Versions string `config:"versions,omitempty" json:"versions,omitempty" yaml:"versions,omitempty"`
semVerRange *semver.Constraints
Expand Down Expand Up @@ -150,7 +156,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 @@ -185,8 +191,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.kibanaVersion, err = semver.NewConstraint(p.Conditions.KibanaVersion)
if err != nil {
return nil, errors.Wrapf(err, "invalid Kibana versions range: %s", p.Requirement.Kibana.Versions)
}
// TODO: remove legacy part
} else if p.Requirement.Kibana.Versions != "" {
p.Conditions = &Conditions{
KibanaVersion: p.Requirement.Kibana.Versions,
}
p.Conditions.kibanaVersion, 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 @@ -253,16 +269,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.kibanaVersion.Check(version)
}

func (p *Package) IsNewerOrEqual(pp Package) bool {
Expand Down
70 changes: 70 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,74 @@ 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,
},
}

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{
kibanaVersion: 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 ad1bca1

Please sign in to comment.