Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions integrationtests/cli/assets/bundle/bundle-all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: Bundle
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: match-all-clusters
namespace: fleet-local
spec:
resources:
- content: |
kind: ConfigMap
apiVersion: v1
metadata:
name: root
data:
value: root
name: cm.yaml
targets:
- clusterSelector: {}
32 changes: 32 additions & 0 deletions integrationtests/cli/assets/bundle/bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
kind: Bundle
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: external-secret
namespace: fleet-local
spec:
resources:
- content: |
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
name: nginx.yaml
targets:
- clusterName: local
32 changes: 32 additions & 0 deletions integrationtests/cli/assets/bundledeployment/bd-only.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
apiVersion: fleet.cattle.io/v1alpha1
kind: BundleDeployment
metadata:
creationTimestamp: null
labels:
fleet.cattle.io/bundle-name: testbundle-simple-chart
fleet.cattle.io/bundle-namespace: fleet-local
fleet.cattle.io/cluster: local
fleet.cattle.io/cluster-namespace: fleet-local
fleet.cattle.io/commit: e40edabfeada51874ac9caf5770655b720177380
fleet.cattle.io/managed: "true"
name: testbundle-simple-chart
namespace: cluster-fleet-local-local-1a3d67d0a899
spec:
deploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5
options:
helm:
chart: config-chart
values:
name: example-value
ignore: {}
stagedDeploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5
stagedOptions:
helm:
chart: config-chart
values:
name: example-value
ignore: {}
status:
display: {}

41 changes: 41 additions & 0 deletions integrationtests/cli/assets/bundledeployment/bd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
apiVersion: fleet.cattle.io/v1alpha1
content: H4sIAAAAAAAA/4SRMW/bMBCF9/6KA2eLtlygA7fCa7t6KTucpZNNhKII8iTEEPTfA5KRESdOsgjQ3Xv33gfOIlAcxtBQFOrfLBz2JJRoBteZc9VcMPD2kL7yir0Vm7RhciyUQG+OFKIZnIJpr12yKnjr1K6l2ATjOYt+A1NkyCvgCzJMFExnKILh+OrUjq+eFKD31jSYnNpNa85O1nKnHXp/i9ailvUvudNCO7FsHiMw9d4iU9yWcY/+G6BauyfjWgWHbPiLXrueGFtkVNoBFNxEVEXTe0slqloxbsIkSTUntCPV+5+p6GqfZ5DHNI8yDWBZvoCYivBD73KqpQ5Hy1X6uz/SWaIH73ch2+eC+fr7lwMocVmx1qVnzKR5dR/yWbkxWAVnO5zQylKksWNkCn/wRLZgp0v/lx8vAQAA//+su6S1jAIAAA==
kind: Content
metadata:
creationTimestamp: null
name: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e
sha256sum: ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36eebc

---
apiVersion: fleet.cattle.io/v1alpha1
kind: BundleDeployment
metadata:
creationTimestamp: null
labels:
fleet.cattle.io/bundle-name: testbundle-simple-chart
fleet.cattle.io/bundle-namespace: fleet-local
fleet.cattle.io/cluster: local
fleet.cattle.io/cluster-namespace: fleet-local
fleet.cattle.io/commit: e40edabfeada51874ac9caf5770655b720177380
fleet.cattle.io/managed: "true"
name: testbundle-simple-chart
namespace: cluster-fleet-local-local-1a3d67d0a899
spec:
deploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5
options:
helm:
chart: config-chart
values:
name: example-value
ignore: {}
stagedDeploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5
stagedOptions:
helm:
chart: config-chart
values:
name: example-value
ignore: {}
status:
display: {}

8 changes: 8 additions & 0 deletions integrationtests/cli/assets/bundledeployment/content.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: fleet.cattle.io/v1alpha1
content: H4sIAAAAAAAA/4SRMW/bMBCF9/6KA2eLtlygA7fCa7t6KTucpZNNhKII8iTEEPTfA5KRESdOsgjQ3Xv33gfOIlAcxtBQFOrfLBz2JJRoBteZc9VcMPD2kL7yir0Vm7RhciyUQG+OFKIZnIJpr12yKnjr1K6l2ATjOYt+A1NkyCvgCzJMFExnKILh+OrUjq+eFKD31jSYnNpNa85O1nKnHXp/i9ailvUvudNCO7FsHiMw9d4iU9yWcY/+G6BauyfjWgWHbPiLXrueGFtkVNoBFNxEVEXTe0slqloxbsIkSTUntCPV+5+p6GqfZ5DHNI8yDWBZvoCYivBD73KqpQ5Hy1X6uz/SWaIH73ch2+eC+fr7lwMocVmx1qVnzKR5dR/yWbkxWAVnO5zQylKksWNkCn/wRLZgp0v/lx8vAQAA//+su6S1jAIAAA==
kind: Content
metadata:
creationTimestamp: null
name: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e
sha256sum: ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36eebc
169 changes: 169 additions & 0 deletions integrationtests/cli/deploy/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package deploy_test

import (
"github.com/onsi/gomega/gbytes"

clihelper "github.com/rancher/fleet/integrationtests/cli"
"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/internal/cmd/cli"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Fleet CLI Deploy", func() {
var args []string

act := func(args []string) (*gbytes.Buffer, error) {
cmd := cli.NewDeploy()
args = append([]string{"--kubeconfig", kubeconfigPath}, args...)
cmd.SetArgs(args)

buf := gbytes.NewBuffer()
cmd.SetOutput(buf)
err := cmd.Execute()
return buf, err
}

BeforeEach(func() {
var err error
namespace, err = utils.NewNamespaceName()
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
})).ToNot(HaveOccurred())

DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
})).ToNot(HaveOccurred())
})
})

When("input file parameter is missing", func() {
It("prints the help", func() {
buf, err := act(args)
Expect(err).NotTo(HaveOccurred())
Expect(buf).To(gbytes.Say("Usage:"))
})
})

When("Input file is missing", func() {
BeforeEach(func() {
args = []string{"--input-file", "/tmp/does-not-exist-bundle.yaml"}
})

It("prints an error", func() {
errBuf, err := act(args)
Expect(err).To(HaveOccurred())
Expect(errBuf).To(gbytes.Say("no such file or directory"))
})
})

When("Input file is invalid", func() {
BeforeEach(func() {
args = []string{"--input-file", clihelper.AssetsPath + "helmrepository/config-chart-0.1.0.tgz"}
})

It("prints an error", func() {
errBuf, err := act(args)
Expect(err).To(HaveOccurred())
Expect(errBuf).To(gbytes.Say("yaml: control characters are not allowed"))
})
})

When("Input file does not contain a content resource", func() {
BeforeEach(func() {
args = []string{"--input-file", clihelper.AssetsPath + "bundledeployment/bd-only.yaml"}
})

It("prints an error", func() {
errBuf, err := act(args)
Expect(err).To(HaveOccurred())
Expect(errBuf).To(gbytes.Say("failed to read content resource from file"))
})
})

When("Input file does not contain a bundledeployment resource", func() {
BeforeEach(func() {
args = []string{"--input-file", clihelper.AssetsPath + "bundledeployment/content.yaml"}
})

It("prints an error", func() {
errBuf, err := act(args)
Expect(err).To(HaveOccurred())
Expect(errBuf).To(gbytes.Say("failed to read bundledeployment"))
})
})

When("Deploying to a cluster", func() {
BeforeEach(func() {
args = []string{
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml",
}
})

It("creates resources", func() {
buf, err := act(args)
Expect(err).NotTo(HaveOccurred())
Expect(buf).To(gbytes.Say("defaultNamespace: default"))
Expect(buf).To(gbytes.Say("objects"))
Expect(buf).To(gbytes.Say("- apiVersion: v1"))

cm := &corev1.ConfigMap{}
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "test-simple-chart-config"}, cm)
Expect(err).NotTo(HaveOccurred())
})
})

When("Specifying a namespace", func() {
BeforeEach(func() {
args = []string{
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml",
"--namespace", namespace,
}
})

It("creates resources", func() {
buf, err := act(args)
Expect(err).NotTo(HaveOccurred())
Expect(buf).To(gbytes.Say("defaultNamespace: " + namespace))
Expect(buf).To(gbytes.Say("objects"))
Expect(buf).To(gbytes.Say("- apiVersion: v1"))

cm := &corev1.ConfigMap{}
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "test-simple-chart-config"}, cm)
Expect(err).NotTo(HaveOccurred())
})
})

When("Printing results with --dry-run", func() {
BeforeEach(func() {
args = []string{
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml",
"--dry-run",
}
})

It("prints a manifest and bundledeployment", func() {
buf, err := act(args)
Expect(err).NotTo(HaveOccurred())
Expect(buf).To(gbytes.Say("- apiVersion: v1"))
Expect(buf).To(gbytes.Say(" data:"))
Expect(buf).To(gbytes.Say(" name: example-value"))

cm := &corev1.ConfigMap{}
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "test-simple-chart-config"}, cm)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
})
})
77 changes: 77 additions & 0 deletions integrationtests/cli/deploy/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package deploy_test

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

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

var (
ctx context.Context
cancel context.CancelFunc
cfg *rest.Config
testEnv *envtest.Environment
tmpdir string
kubeconfigPath string

k8sClient client.Client
namespace string

scheme = runtime.NewScheme()
)

func TestFleetDeploy(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Fleet CLI Deploy Suite")
}

var _ = BeforeSuite(func() {
ctx, cancel = context.WithCancel(context.TODO())
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "charts", "fleet-crd", "templates", "crds.yaml")},
ErrorIfCRDPathMissing: true,
}

var err error
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

tmpdir, _ = os.MkdirTemp("", "fleet-")
kubeconfigPath = path.Join(tmpdir, "kubeconfig")
err = utils.WriteKubeConfig(cfg, kubeconfigPath)
Expect(err).NotTo(HaveOccurred())

// scheme for k8sClient
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
})

var _ = AfterSuite(func() {
if os.Getenv("SKIP_CLEANUP") == "true" {
return
}
os.RemoveAll(tmpdir)

cancel()
_ = testEnv.Stop()
})
Loading