From 14892133f009ac821adddd96fb20e6032dd38f3f Mon Sep 17 00:00:00 2001 From: Nicolas Ruflin Date: Tue, 10 Sep 2019 11:04:08 +0200 Subject: [PATCH] Add Validation of package (#97) 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. --- .../kafka-1.0.1/manifest.yml | 2 +- .../kibana-1.3.2/manifest.yml | 2 +- .../mysql-1.0.1/manifest.yml | 2 +- .../system-2.0.1/manifest.yml | 2 +- .../traefik-1.0.2/manifest.yml | 2 +- magefile.go | 6 +++ util/package.go | 14 +++++ util/package_test.go | 51 +++++++++++++++++++ 8 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 util/package_test.go diff --git a/dev/package-generated/kafka-1.0.1/manifest.yml b/dev/package-generated/kafka-1.0.1/manifest.yml index fc771325d7..1d89a7b15b 100644 --- a/dev/package-generated/kafka-1.0.1/manifest.yml +++ b/dev/package-generated/kafka-1.0.1/manifest.yml @@ -1,6 +1,6 @@ name: kafka description: Kafka Logs and metrics - +title: Kafka version: 1.0.1 categories: ["logs", "metrics"] release: beta diff --git a/dev/package-generated/kibana-1.3.2/manifest.yml b/dev/package-generated/kibana-1.3.2/manifest.yml index 97896ac1f3..197cf41eab 100644 --- a/dev/package-generated/kibana-1.3.2/manifest.yml +++ b/dev/package-generated/kibana-1.3.2/manifest.yml @@ -1,6 +1,6 @@ name: kibana description: Kibana Logs and metrics - +title: Kibana version: 1.3.2 categories: ["logs", "metrics"] release: beta diff --git a/dev/package-generated/mysql-1.0.1/manifest.yml b/dev/package-generated/mysql-1.0.1/manifest.yml index 77c08d469f..f0952fd375 100644 --- a/dev/package-generated/mysql-1.0.1/manifest.yml +++ b/dev/package-generated/mysql-1.0.1/manifest.yml @@ -1,6 +1,6 @@ name: mysql description: Mysql Logs and metrics - +title: MySQL version: 1.0.1 categories: ["logs", "metrics"] release: beta diff --git a/dev/package-generated/system-2.0.1/manifest.yml b/dev/package-generated/system-2.0.1/manifest.yml index ff5a11e065..a3a61c9aa3 100644 --- a/dev/package-generated/system-2.0.1/manifest.yml +++ b/dev/package-generated/system-2.0.1/manifest.yml @@ -1,6 +1,6 @@ name: system description: System SQL Logs and metrics - +title: System version: 2.0.1 categories: ["logs", "metrics"] release: beta diff --git a/dev/package-generated/traefik-1.0.2/manifest.yml b/dev/package-generated/traefik-1.0.2/manifest.yml index 0d63f71ad1..78597906fe 100644 --- a/dev/package-generated/traefik-1.0.2/manifest.yml +++ b/dev/package-generated/traefik-1.0.2/manifest.yml @@ -1,6 +1,6 @@ name: traefik description: Traefik SQL Logs and metrics - +title: Traefik version: 1.0.2 categories: ["logs", "metrics"] release: beta diff --git a/magefile.go b/magefile.go index f1bfcd645f..06c2957d33 100644 --- a/magefile.go +++ b/magefile.go @@ -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 diff --git a/util/package.go b/util/package.go index cc4f7089f4..941841a33e 100644 --- a/util/package.go +++ b/util/package.go @@ -1,6 +1,7 @@ package util import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -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 +} diff --git a/util/package_test.go b/util/package_test.go new file mode 100644 index 0000000000..8c6c774610 --- /dev/null +++ b/util/package_test.go @@ -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) + } + }) + } +}