Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Jun 28, 2023
1 parent 70633c4 commit 9beb6e0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 13 deletions.
9 changes: 4 additions & 5 deletions internal/cli/cmd/infrastructure/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ const (
)

const (
DefaultK8sClusterName = "cluster.local"
DefaultK8sDNSDomain = "cluster.local"
DefaultAPIDNSDomain = "lb.kubeblocks.local"
DefaultK8sProxyMode = "ipvs"
DefaultAPIServerPort = 6443
DefaultK8sDNSDomain = "cluster.local"
DefaultAPIDNSDomain = "lb.kubeblocks.local"
DefaultK8sProxyMode = "ipvs"
DefaultAPIServerPort = 6443

DefaultNetworkPlugin = "cilium"
)
66 changes: 63 additions & 3 deletions internal/cli/cmd/infrastructure/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,81 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package infrastructure

import (
"os"
"path/filepath"

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

"k8s.io/cli-runtime/pkg/genericclioptions"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"

"github.com/apecloud/kubeblocks/internal/cli/testing"
"github.com/apecloud/kubeblocks/test/testdata"
)

var _ = Describe("reconfigure test", func() {
var _ = Describe("infra create test", func() {

var (
tf *cmdtesting.TestFactory
streams genericclioptions.IOStreams
)

BeforeEach(func() {
streams, _, _, _ = genericclioptions.NewTestIOStreams()
tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace)
})

AfterEach(func() {
tf.Cleanup()
})

It("check params for creating infra", func() {
mockPrivateKeyFile := func(tmpDir string) string {
privateKeyFile := filepath.Join(tmpDir, "id_rsa.pem")
Expect(os.WriteFile(privateKeyFile, []byte("private key"), os.ModePerm)).Should(Succeed())
return privateKeyFile
}

It("test create k8s cluster with config file", func() {
tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-")
defer os.RemoveAll(tmpDir)

By("Create cluster with config file")
o := &createOptions{
clusterOptions: clusterOptions{
IOStreams: streams,
}}
o.checkAndSetDefaultVersion()
o.clusterConfig = testdata.SubTestDataPath("infrastructure/infra-cluster.yaml")
Expect(o.Complete()).To(Succeed())
o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir)
Expect(o.Validate()).To(Succeed())
})

It("test create k8s cluster with params", func() {
tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-")
defer os.RemoveAll(tmpDir)

By("Create cluster with config file")
o := &createOptions{
clusterOptions: clusterOptions{
IOStreams: streams,
}}
o.checkAndSetDefaultVersion()

o.nodes = []string{
"node0:1.1.1.1:10.128.0.1",
"node1:1.1.1.2:10.128.0.2",
"node2:1.1.1.3:10.128.0.3",
}
o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir)
Expect(o.Complete()).Should(Succeed())
Expect(o.Validate()).ShouldNot(Succeed())

By("Create cluster with args")
o.Cluster.RoleGroup.Master = []string{"node0"}
o.Cluster.RoleGroup.ETCD = []string{"node0"}
o.Cluster.RoleGroup.Worker = []string{"node1", "node2"}
Expect(o.Validate()).Should(Succeed())
})

})
12 changes: 7 additions & 5 deletions internal/cli/cmd/infrastructure/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type NodeOptions struct {

type Kubernetes struct {
// ClusterName string `json:"clusterName"`
DNSDomain string `json:"dnsDomain"`
// DNSDomain string `json:"dnsDomain"`
ProxyMode string `json:"proxyMode"`

Networking Networking `json:"networking"`
Expand All @@ -107,7 +107,9 @@ type Kubernetes struct {

type Networking struct {
// using network plugin, default is calico
Plugin string `json:"plugin"`
Plugin string `json:"plugin"`

// apis/kubeadm/types.Networking
ServiceSubnet string `json:"serviceSubnet"`
PodSubnet string `json:"podSubnet"`
DNSDomain string `json:"dnsDomain"`
Expand Down Expand Up @@ -158,9 +160,9 @@ func (k *Kubernetes) AutoDefaultFill() {
// if k.ClusterName == "" {
// k.ClusterName = constant.DefaultK8sClusterName
// }
if k.DNSDomain == "" {
k.DNSDomain = constant.DefaultK8sDNSDomain
}
// if k.DNSDomain == "" {
// k.DNSDomain = constant.DefaultK8sDNSDomain
// }
if k.ProxyMode == "" {
k.ProxyMode = constant.DefaultK8sProxyMode
}
Expand Down
56 changes: 56 additions & 0 deletions test/testdata/infrastructure/infra-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
metadata:
name: kb-k8s-test-cluster
user:
name: user1
privateKeyPath: ~/.ssh/test.pem
nodes:
- name: kb-infra-node-0
address: 1.1.1.1
internalAddress: 10.128.0.19
- name: kb-infra-node-1
address: 1.1.1.2
internalAddress: 10.128.0.20
- name: kb-infra-node-2
address: 1.1.1.3
internalAddress: 10.128.0.21
options:
hugePageFeature:
hugePageSize: 10GB
roleGroup:
etcd:
- kb-infra-node-0
- kb-infra-node-1
- kb-infra-node-2
master:
- kb-infra-node-0
worker:
- kb-infra-node-1
- kb-infra-node-2

kubernetes:
containerManager: containerd
# apis/kubeadm/types.Networking
networking:
plugin: cilium
dnsDomain: cluster.local
podSubnet: 10.233.64.0/18
serviceSubnet: 10.233.0.0/18
controlPlaneEndpoint:
domain: lb.kubeblocks.local
port: 6443
cri:
containerRuntimeType: "containerd"
containerRuntimeEndpoint: "unix:///run/containerd/containerd.sock"
sandBoxImage: "k8s.gcr.io/pause:3.8"
addons:
- name: openebs
namespace: kube-blocks
sources:
chart:
name: openebs
version: 3.7.0
repo: https://openebs.github.io/charts
options:
values:
- "localprovisioner.basePath=/mnt/disks"
- "localprovisioner.hostpathClass.isDefaultClass=true"

0 comments on commit 9beb6e0

Please sign in to comment.