mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-11 03:47:59 +00:00
* Feat: Refactoring the API Server module for better layering Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: code style Signed-off-by: barnettZQG <barnett.zqg@gmail.com> * Fix: fix the lint errors Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
/*
|
|
Copyright 2021 The KubeVela Authors.
|
|
|
|
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 api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/utils/pointer"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/clients"
|
|
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore"
|
|
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore/kubeapi"
|
|
"github.com/oam-dev/kubevela/pkg/apiserver/infrastructure/datastore/mongodb"
|
|
"github.com/oam-dev/kubevela/pkg/utils/common"
|
|
)
|
|
|
|
var cfg *rest.Config
|
|
var k8sClient client.Client
|
|
var testEnv *envtest.Environment
|
|
|
|
func TestAPIInterface(t *testing.T) {
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "APIInterface Suite")
|
|
}
|
|
|
|
var _ = BeforeSuite(func(done Done) {
|
|
rand.Seed(time.Now().UnixNano())
|
|
By("bootstrapping Sync test environment")
|
|
|
|
testEnv = &envtest.Environment{
|
|
ControlPlaneStartTimeout: time.Minute * 3,
|
|
ControlPlaneStopTimeout: time.Minute,
|
|
UseExistingCluster: pointer.BoolPtr(false),
|
|
CRDDirectoryPaths: []string{"../../../charts/vela-core/crds"},
|
|
}
|
|
|
|
By("start kube test env")
|
|
var err error
|
|
cfg, err = testEnv.Start()
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
Expect(cfg).ToNot(BeNil())
|
|
|
|
By("new kube client")
|
|
cfg.Timeout = time.Minute * 2
|
|
k8sClient, err = client.New(cfg, client.Options{Scheme: common.Scheme})
|
|
Expect(err).Should(BeNil())
|
|
Expect(k8sClient).ToNot(BeNil())
|
|
clients.SetKubeClient(k8sClient)
|
|
By("new kube client success")
|
|
clients.SetKubeClient(k8sClient)
|
|
Expect(err).Should(BeNil())
|
|
close(done)
|
|
}, 240)
|
|
|
|
var _ = AfterSuite(func() {
|
|
By("tearing down the test environment")
|
|
err := testEnv.Stop()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
})
|
|
|
|
func NewDatastore(cfg datastore.Config) (ds datastore.DataStore, err error) {
|
|
switch cfg.Type {
|
|
case "mongodb":
|
|
ds, err = mongodb.New(context.Background(), cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create mongodb datastore instance failure %w", err)
|
|
}
|
|
case "kubeapi":
|
|
ds, err = kubeapi.New(context.Background(), cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create mongodb datastore instance failure %w", err)
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf("not support datastore type %s", cfg.Type)
|
|
}
|
|
return ds, nil
|
|
}
|