Skip to content

Commit

Permalink
Merge pull request #139 from jstrachan/changes2
Browse files Browse the repository at this point in the history
fix: add fix and tests for validation
  • Loading branch information
jenkins-x-bot-test authored May 11, 2021
2 parents 64cb833 + 6798dcc commit 70f0c19
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 384 deletions.
2 changes: 1 addition & 1 deletion docs/apidocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4145,5 +4145,5 @@ string
<hr/>
<p><em>
Generated with <code>gen-crd-api-reference-docs</code>
on git commit <code>c3ce029</code>.
on git commit <code>1b57b62</code>.
</em></p>
13 changes: 12 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ string
<p>Schedule cron of auto updates</p>
</td>
</tr>
<tr>
<td>
<code>autoMerge</code></br>
<em>
bool
</em>
</td>
<td>
<p>AutoMerge if enabled lets auto merge any generated update PullRequests on the dev cluster git repository</p>
</td>
</tr>
</tbody>
</table>
<h3 id="core.jenkins-x.io/v4beta1.AzureConfig">AzureConfig
Expand Down Expand Up @@ -2101,5 +2112,5 @@ If not specified the &lsquo;kubernetes&rsquo; is used.</p>
<hr/>
<p><em>
Generated with <code>gen-crd-api-reference-docs</code>
on git commit <code>c3ce029</code>.
on git commit <code>1b57b62</code>.
</em></p>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/jenkins-x/jx-logging/v3 v3.0.3
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a
github.com/pkg/errors v0.9.1
github.com/rawlingsj/jsonschema v0.0.0-20201130104235-44c4fb269f83 // use a fork until https://github.com/alecthomas/jsonschema/issues/65 is fixed
github.com/rawlingsj/jsonschema v0.0.0-20210511111326-a8f2a263bf66 // use a fork until https://github.com/alecthomas/jsonschema/issues/65 is fixed
github.com/stretchr/testify v1.6.1
github.com/vrischmann/envconfig v1.2.0
github.com/xeipuuv/gojsonschema v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rawlingsj/jsonschema v0.0.0-20201130104235-44c4fb269f83 h1:+2o6r9BUAIJJIlhJROCIV4DES6xJb88vMYcfeUg6WxE=
github.com/rawlingsj/jsonschema v0.0.0-20201130104235-44c4fb269f83/go.mod h1:8LFgdjjkhuo3+T0/kprWPWGqh2+v8QC4hLyjNK6j15s=
github.com/rawlingsj/jsonschema v0.0.0-20210511111326-a8f2a263bf66 h1:Y8axTXO1bABk6UfkKNbv6Qq/GHDf12ff3/4MPMtQSf8=
github.com/rawlingsj/jsonschema v0.0.0-20210511111326-a8f2a263bf66/go.mod h1:8LFgdjjkhuo3+T0/kprWPWGqh2+v8QC4hLyjNK6j15s=
github.com/rickar/props v0.0.0-20170718221555-0b06aeb2f037 h1:HFsTO5S+nnw/Xs9lRYF+UUJvH8wMSRMRal321W0hfdY=
github.com/rickar/props v0.0.0-20170718221555-0b06aeb2f037/go.mod h1:F1p8BNM4IXv2UcptwSp8HJOapKurodd/PYu1D6Gtn9Y=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
32 changes: 32 additions & 0 deletions pkg/util/test_data/bad_env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Source: jxboot-helmfile-resources/templates/environments.yaml
apiVersion: jenkins.io/v1
kind: Environment
metadata:
labels:
env: "dev"
team: jx
name: "bad"
spec:
invalidPropertyName: cheese
source:
ref: "master"
url: https://github.com/myorg/environment-mycluster-dev.git
kind: Development
label: Development
namespace: jx
promotionStrategy: "Never"
webHookEngine: "Lighthouse"
teamSettings:
appsRepository: http://chartmuseum.jenkins-x.io
defaultScheduler:
apiVersion: jenkins.io/v1
kind: Scheduler
name: jx-meta-pipeline
dockerRegistryOrg: ""
envOrganisation: myorg
gitServer: https://github.com
gitPublic: true
kubeProvider: "kubernetes"
pipelineUsername: "jenkins-x-labs-bot"
pipelineUserEmail: "jenkins-x@googlegroups.com"
prowConfig: Scheduler
31 changes: 31 additions & 0 deletions pkg/util/test_data/good_env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Source: jxboot-helmfile-resources/templates/environments.yaml
apiVersion: jenkins.io/v1
kind: Environment
metadata:
labels:
env: "dev"
team: jx
name: "dev"
spec:
source:
ref: "master"
url: https://github.com/myorg/environment-mycluster-dev.git
kind: Development
label: Development
namespace: jx
promotionStrategy: "Never"
webHookEngine: "Lighthouse"
teamSettings:
appsRepository: http://chartmuseum.jenkins-x.io
defaultScheduler:
apiVersion: jenkins.io/v1
kind: Scheduler
name: jx-meta-pipeline
dockerRegistryOrg: ""
envOrganisation: myorg
gitServer: https://github.com
gitPublic: true
kubeProvider: "kubernetes"
pipelineUsername: "jenkins-x-labs-bot"
pipelineUserEmail: "jenkins-x@googlegroups.com"
prowConfig: Scheduler
2 changes: 2 additions & 0 deletions pkg/util/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func GenerateSchema(target interface{}) *schemagen.Schema {
IgnoredTypes: []interface{}{
corev1.Container{},
},

//ExpandedStruct: true,
RequiredFromJSONSchemaTags: true,
}
return reflector.Reflect(target)
Expand Down
49 changes: 49 additions & 0 deletions pkg/util/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package util_test

import (
"github.com/ghodss/yaml"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/v4/pkg/util"
"github.com/stretchr/testify/require"
"io/ioutil"
"path/filepath"
"testing"
)

func TestValidation(t *testing.T) {
t.Parallel()

path := filepath.Join("test_data", "good_env.yaml")
data, err := ioutil.ReadFile(path)
require.NoError(t, err, "failed to load %s", path)

deploy := &v1.Environment{}
err = yaml.Unmarshal(data, deploy)
require.NoError(t, err, "failed to unmarshal %s", path)

results, err := util.ValidateYaml(deploy, data)
t.Logf("got results %#v\n", results)

require.NoError(t, err, "should not have failed to validate yaml file %s", path)

require.Empty(t, results, "should not have validation errors for file %s", path)
}

func TestValidationFails(t *testing.T) {
t.Parallel()

path := filepath.Join("test_data", "bad_env.yaml")
data, err := ioutil.ReadFile(path)
require.NoError(t, err, "failed to load %s", path)

deploy := &v1.Environment{}
err = yaml.Unmarshal(data, deploy)
require.NoError(t, err, "failed to unmarshal %s", path)

results, err := util.ValidateYaml(deploy, data)
t.Logf("got results %#v\n", results)

require.NoError(t, err, "should not have failed to validate yaml file %s", path)

require.NotEmpty(t, results, "should have validation errors for file %s", path)
}
Loading

0 comments on commit 70f0c19

Please sign in to comment.