mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-08-23 22:26:49 +00:00
98 lines
2.9 KiB
Go
98 lines
2.9 KiB
Go
package spoke
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/onsi/ginkgo/v2"
|
|
"github.com/onsi/gomega"
|
|
"github.com/openshift/library-go/pkg/controller/controllercmd"
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
"k8s.io/client-go/rest"
|
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
|
|
|
ocmfeature "open-cluster-management.io/api/feature"
|
|
workapiv1 "open-cluster-management.io/api/work/v1"
|
|
|
|
commonoptions "open-cluster-management.io/ocm/pkg/common/options"
|
|
"open-cluster-management.io/ocm/pkg/features"
|
|
"open-cluster-management.io/ocm/test/integration/util"
|
|
)
|
|
|
|
var testEnv *envtest.Environment
|
|
var sourceConfigFileName string
|
|
var cfg *rest.Config
|
|
|
|
var CRDPaths = []string{
|
|
// hub
|
|
"../../../vendor/open-cluster-management.io/api/work/v1/0000_00_work.open-cluster-management.io_manifestworks.crd.yaml",
|
|
"../../../vendor/open-cluster-management.io/api/work/v1/0000_01_work.open-cluster-management.io_appliedmanifestworks.crd.yaml",
|
|
}
|
|
|
|
func TestWorkAgent(t *testing.T) {
|
|
gomega.RegisterFailHandler(ginkgo.Fail)
|
|
ginkgo.RunSpecs(t, "Work Manager Suite")
|
|
}
|
|
|
|
var _ = ginkgo.BeforeSuite(func() {
|
|
logf.SetLogger(zap.New(zap.WriteTo(ginkgo.GinkgoWriter), zap.UseDevMode(true)))
|
|
ginkgo.By("bootstrapping test environment")
|
|
var err error
|
|
|
|
// start a kube-apiserver
|
|
testEnv = &envtest.Environment{
|
|
ErrorIfCRDPathMissing: true,
|
|
CRDDirectoryPaths: CRDPaths,
|
|
}
|
|
cfg, err = testEnv.Start()
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
gomega.Expect(cfg).ToNot(gomega.BeNil())
|
|
|
|
err = workapiv1.Install(scheme.Scheme)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
// create kubeconfig file for hub in a tmp dir
|
|
tempDir, err := os.MkdirTemp("", "test")
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
gomega.Expect(tempDir).ToNot(gomega.BeEmpty())
|
|
|
|
sourceConfigFileName = path.Join(tempDir, "kubeconfig")
|
|
err = util.CreateKubeconfigFile(cfg, sourceConfigFileName)
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
|
|
err = features.SpokeMutableFeatureGate.Add(ocmfeature.DefaultSpokeWorkFeatureGates)
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
})
|
|
|
|
var _ = ginkgo.AfterSuite(func() {
|
|
ginkgo.By("tearing down the test environment")
|
|
|
|
err := testEnv.Stop()
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
})
|
|
|
|
var _ = ginkgo.Describe("start work agent", func() {
|
|
ginkgo.It("start work agent", func() {
|
|
ctx, stopAgent := context.WithCancel(context.Background())
|
|
commonOpts := commonoptions.NewAgentOptions()
|
|
commonOpts.SpokeClusterName = "cluster1"
|
|
opts := NewWorkloadAgentOptions()
|
|
opts.WorkloadSourceConfig = sourceConfigFileName
|
|
agentConfig := NewWorkAgentConfig(commonOpts, opts)
|
|
|
|
// start hub controller
|
|
go func() {
|
|
err := agentConfig.RunWorkloadAgent(ctx, &controllercmd.ControllerContext{
|
|
KubeConfig: cfg,
|
|
EventRecorder: util.NewIntegrationTestEventRecorder("work-agent"),
|
|
})
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
}()
|
|
stopAgent()
|
|
})
|
|
})
|