Skip to content

Commit

Permalink
Add Validation of package (elastic#97)
Browse files Browse the repository at this point in the history
To make sure the registry only serves valid packages, packages should be validated during build time. For this a `Package.Validate() error` method was added. For not it is simple and only checks for title and description but more checks should be added in the future.

The packages which were not valid were updated.
  • Loading branch information
ruflin authored Sep 10, 2019
1 parent c4ef27a commit 1489213
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dev/package-generated/kafka-1.0.1/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: kafka
description: Kafka Logs and metrics

title: Kafka
version: 1.0.1
categories: ["logs", "metrics"]
release: beta
Expand Down
2 changes: 1 addition & 1 deletion dev/package-generated/kibana-1.3.2/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: kibana
description: Kibana Logs and metrics

title: Kibana
version: 1.3.2
categories: ["logs", "metrics"]
release: beta
Expand Down
2 changes: 1 addition & 1 deletion dev/package-generated/mysql-1.0.1/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mysql
description: Mysql Logs and metrics

title: MySQL
version: 1.0.1
categories: ["logs", "metrics"]
release: beta
Expand Down
2 changes: 1 addition & 1 deletion dev/package-generated/system-2.0.1/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: system
description: System SQL Logs and metrics

title: System
version: 2.0.1
categories: ["logs", "metrics"]
release: beta
Expand Down
2 changes: 1 addition & 1 deletion dev/package-generated/traefik-1.0.2/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: traefik
description: Traefik SQL Logs and metrics

title: Traefik
version: 1.0.2
categories: ["logs", "metrics"]
release: beta
Expand Down
6 changes: 6 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ func buildPackage(packagesBasePath, path string) error {
return err
}

// Checks if the package is valid
err = p.Validate()
if err != nil {
return fmt.Errorf("Invalid package %s-%s: %s", p.Name, p.Version, err)
}

err = p.LoadAssets(path)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions util/package.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -185,3 +186,16 @@ func (p *Package) LoadAssets(packagePath string) (err error) {
}
return nil
}

func (p *Package) Validate() error {

if p.Title == nil || *p.Title == "" {
return fmt.Errorf("no title set")
}

if p.Description == "" {
return fmt.Errorf("no description set")
}

return nil
}
51 changes: 51 additions & 0 deletions util/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

var (
title = "foo"
)
var packageTests = []struct {
p Package
valid bool
description string
}{
{
Package{},
false,
"empty",
},
{
Package{
Title: &title,
},
false,
"missing description",
},
{
Package{
Title: &title,
Description: "my description",
},
true,
"complete",
},
}

func TestValidate(t *testing.T) {
for _, tt := range packageTests {
t.Run(tt.description, func(t *testing.T) {
err := tt.p.Validate()

if err != nil {
assert.False(t, tt.valid)
} else {
assert.True(t, tt.valid)
}
})
}
}

0 comments on commit 1489213

Please sign in to comment.