Skip to content

Commit

Permalink
Add tests for bundles with local modules
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Mar 2, 2024
1 parent 10d8938 commit 4f7e3fd
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
37 changes: 36 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ permissions:
contents: read

jobs:
kubernetes:
k8s-modules:
runs-on: ubuntu-latest
services:
registry:
Expand Down Expand Up @@ -62,3 +62,38 @@ jobs:
- name: Uninstall module
run: |
timoni -n test delete nginx --wait
k8s-bundles:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: 1.22.x
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Setup Kubernetes
uses: helm/kind-action@99576bfa6ddf9a8e612d83b513da5a75875caced # v1.9.0
with:
version: v0.22.0
cluster_name: kind
- name: Install
run: make install
- name: Vet bundle with local module
run: |
timoni bundle vet -f hack/tests/bundle_local.cue
- name: Build bundle with local module
run: |
timoni bundle build -f hack/tests/bundle_local.cue
- name: Apply bundle with local module
run: |
timoni bundle apply -f hack/tests/bundle_local.cue
- name: Inspect bundle
run: |
timoni bundle status local-test
- name: Delete bundle
run: |
timoni bundle delete local-test
52 changes: 52 additions & 0 deletions cmd/timoni/bundle_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
ssautil "github.com/fluxcd/pkg/ssa/utils"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/stefanprodan/timoni/internal/engine"
)

func Test_BundleBuild(t *testing.T) {
Expand Down Expand Up @@ -140,6 +142,56 @@ bundle:
})
}

func Test_BundleBuild_LocalModule(t *testing.T) {
g := NewWithT(t)

modPath := "testdata/module"
namespace := rnd("my-namespace", 5)

bundleCue := fmt.Sprintf(`
bundle: {
apiVersion: "v1alpha1"
name: "my-bundle"
instances: {
backend: {
module: {
url: "file://%[1]s"
}
namespace: "%[2]s"
values: client: enabled: true
}
}
}
`, modPath, namespace)

wd := t.TempDir()
cuePath := filepath.Join(wd, "bundle.cue")
g.Expect(os.WriteFile(cuePath, []byte(bundleCue), 0644)).ToNot(HaveOccurred())

err := engine.CopyModule(modPath, filepath.Join(wd, modPath))
g.Expect(err).ToNot(HaveOccurred())

output, err := executeCommand(fmt.Sprintf(
"bundle build -f %s -p main",
cuePath,
))
g.Expect(err).ToNot(HaveOccurred())

objects, err := ssautil.ReadObjects(strings.NewReader(output))
g.Expect(err).ToNot(HaveOccurred())

backendClientCm, err := getObjectByName(objects, "backend-server")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(backendClientCm.GetKind()).To(BeEquivalentTo("ConfigMap"))
g.Expect(backendClientCm.GetNamespace()).To(ContainSubstring(namespace))

host, found, err := unstructured.NestedString(backendClientCm.Object, "data", "hostname")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(found).To(BeTrue())
g.Expect(host).To(ContainSubstring("example.internal"))

}

func Test_BundleBuild_Runtime(t *testing.T) {
g := NewWithT(t)

Expand Down
18 changes: 18 additions & 0 deletions hack/tests/bundle_local.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
bundle: {
apiVersion: "v1alpha1"
name: "local-test"
instances: {
ngnix: {
module: {
url: "file://../../blueprints/starter"
}
namespace: "nginx"
values: {
resources: limits: {
cpu: "100m"
memory: "128Mi"
}
}
}
}
}
3 changes: 2 additions & 1 deletion internal/engine/bundle_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func (b *BundleBuilder) getInstanceUrl(v cue.Value) string {
source = origin
}
url = filepath.Clean(filepath.Join(filepath.Dir(source), path))
url = "file://" + path // re-add prefix
url = "file://" + url // re-add prefix

}
return url
}
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Options struct {
DefaultLocal bool
}

// NewFetcher is a factory function that creates a new Fetcher based on the provided options.
// New is a factory function that creates a new Fetcher based on the provided options.
// If you know the type of fetcher you want to create, prefer using the specific factory function.
func New(ctx context.Context, opts Options) (Fetcher, error) {
switch {
Expand Down
2 changes: 2 additions & 0 deletions internal/engine/fetcher/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
Expand All @@ -34,6 +35,7 @@ type Local struct {

// NewLocal creates a local Fetcher for the given module.
func NewLocal(src, dst string) *Local {
src = strings.TrimPrefix(src, apiv1.LocalPrefix)
requiredFiles := []string{
path.Join(src, "cue.mod", "module.cue"),
path.Join(src, "timoni.cue"),
Expand Down

0 comments on commit 4f7e3fd

Please sign in to comment.