Skip to content

Commit

Permalink
Add bundle linting tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Apr 2, 2023
1 parent 8504a9e commit 4d50e02
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 5 deletions.
7 changes: 5 additions & 2 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import (
"time"

"cuelang.org/go/cue"
"cuelang.org/go/cue/load"

"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"github.com/fluxcd/pkg/ssa"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -122,6 +121,10 @@ func runBundleApplyCmd(cmd *cobra.Command, args []string) error {
return v.Err()
}

if err := v.Validate(cue.Concrete(true)); err != nil {
return err
}

apiVersion := v.LookupPath(cue.ParsePath(apiv1.BundleAPIVersionSelector.String()))
if apiVersion.Err() != nil {
return fmt.Errorf("lookup %s failed, error: %w", apiv1.BundleAPIVersionSelector.String(), apiVersion.Err())
Expand Down
7 changes: 4 additions & 3 deletions cmd/timoni/bundle_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package main
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/gomega"
)

Expand Down
4 changes: 4 additions & 0 deletions cmd/timoni/bundle_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func runBundleLintCmd(cmd *cobra.Command, args []string) error {
return v.Err()
}

if err := v.Validate(cue.Concrete(true)); err != nil {
return err
}

apiVersion := v.LookupPath(cue.ParsePath(apiv1.BundleAPIVersionSelector.String()))
if apiVersion.Err() != nil {
return fmt.Errorf("lookup %s failed, error: %w", apiv1.BundleAPIVersionSelector.String(), apiVersion.Err())
Expand Down
156 changes: 156 additions & 0 deletions cmd/timoni/bundle_lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
Copyright 2023 Stefan Prodan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
)

func Test_BundleLint(t *testing.T) {

tests := []struct {
name string
bundle string
matchErr string
}{
{
name: "fails for invalid API Version",
matchErr: "bundle.apiVersion",
bundle: `
bundle: {
apiVersion: "v1alpha2"
instances: {
test: {
module: {
url: "oci://docker.io/test"
version: "latest"
}
namespace: "default"
values: {}
}
}
}
`,
},
{
name: "fails for invalid module URL",
matchErr: "bundle.instances.test.module.url",
bundle: `
bundle: {
apiVersion: "v1alpha1"
instances: {
test: {
module: {
url: "docker.io/test"
version: "latest"
}
namespace: "default"
values: {}
}
}
}
`,
},
{
name: "fails for missing module version",
matchErr: "bundle.instances.test.module.version",
bundle: `
bundle: {
apiVersion: "v1alpha1"
instances: {
test: {
module: {
url: "oci://docker.io/test"
}
namespace: "default"
values: {}
}
}
}
`,
},
{
name: "fails for invalid module prop",
matchErr: "url2",
bundle: `
bundle: {
apiVersion: "v1alpha1"
instances: {
test: {
module: {
url2: "oci://docker.io/test"
version: "latest"
}
namespace: "default"
values: {}
}
}
}
`,
},
{
name: "fails for missing namespace",
matchErr: "bundle.instances.test.namespace",
bundle: `
bundle: {
apiVersion: "v1alpha1"
instances: {
test: {
module: {
url: "oci://docker.io/test"
version: "latest"
}
}
}
}
`,
},
{
name: "fails for missing instances",
matchErr: "no instances",
bundle: `
bundle: {
apiVersion: "v1alpha1"
instances: {}
}
`,
},
}

tmpDir := t.TempDir()
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
bundlePath := filepath.Join(tmpDir, fmt.Sprintf("bundle-%v.cue", i))
err := os.WriteFile(bundlePath, []byte(tt.bundle), 0644)
g.Expect(err).ToNot(HaveOccurred())

_, err = executeCommand(fmt.Sprintf(
"bundle lint -f %s",
bundlePath,
))

g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(MatchRegexp(tt.matchErr))
})
}
}
2 changes: 2 additions & 0 deletions cmd/timoni/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func resetCmdArgs() {
listArgs = listFlags{}
pullModArgs = pullModFlags{}
pushModArgs = pushModFlags{}
bundleApplyArgs = bundleApplyFlags{}
bundleLintArgs = bundleLintFlags{}
}

func rnd(prefix string, n int) string {
Expand Down

0 comments on commit 4d50e02

Please sign in to comment.