Skip to content

Commit

Permalink
Merge pull request #15 from zilliztech/pr
Browse files Browse the repository at this point in the history
enhance: add more test and doc
  • Loading branch information
samhuang-z committed May 8, 2024
2 parents f3608e5 + 5376de0 commit 50808f8
Show file tree
Hide file tree
Showing 60 changed files with 1,565 additions and 470 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.21.9
12 changes: 12 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ default: testacc
.PHONY: testacc
testacc:
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m

unit-test:
go test ./... -v

install:
sudo go install -v ./...

doc:
go generate ./...

lint:
golangci-lint run
18 changes: 17 additions & 1 deletion client/cluster.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package client

type Plan string

var (
FreePlan Plan = "Free"
ServerlessPlan Plan = "Serverless"
StandardPlan Plan = "Standard"
EnterprisePlan Plan = "Enterprise"
)

type ModifyClusterParams struct {
CuSize int `json:"cuSize"`
}
Expand Down Expand Up @@ -41,6 +50,7 @@ type Cluster struct {
Description string `json:"description"`
RegionId string `json:"regionId"`
ClusterType string `json:"clusterType"`
Plan Plan `json:"plan"`
CuSize int64 `json:"cuSize"`
Status string `json:"status"`
ConnectAddress string `json:"connectAddress"`
Expand All @@ -61,16 +71,19 @@ func (c *Client) DescribeCluster(clusterId string) (Cluster, error) {
}

type CreateClusterParams struct {
Plan string `json:"plan"`
Plan Plan `json:"plan"`
ClusterName string `json:"clusterName"`
CUSize int `json:"cuSize"`
CUType string `json:"cuType"`
ProjectId string `json:"projectId"`
RegionId string
}

type CreateServerlessClusterParams struct {
ClusterName string `json:"clusterName"`
ProjectId string `json:"projectId"`
Plan Plan `json:"plan,omitempty"`
RegionId string
}

type CreateClusterResponse struct {
Expand All @@ -81,6 +94,9 @@ type CreateClusterResponse struct {
}

func (c *Client) CreateCluster(params CreateClusterParams) (*CreateClusterResponse, error) {
if params.RegionId == "" && c.RegionId == "" {
return nil, errRegionIdRequired
}
var clusterResponse zillizResponse[CreateClusterResponse]
err := c.do("POST", "clusters/create", params, &clusterResponse)
return &clusterResponse.Data, err
Expand Down
295 changes: 295 additions & 0 deletions client/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,296 @@
package client

import (
"context"
"errors"
"testing"
"time"
)

func TestClient_Cluster(t *testing.T) {
var clusterId string
var projectID string
if update {
pollInterval = 60
}

checkPlan := func(plan string) func(resp *Cluster) bool {
return func(resp *Cluster) bool {
return resp.Plan == Plan(plan)
}
}

checkCUSize := func(cuSize int64) func(resp *Cluster) bool {
return func(resp *Cluster) bool {
return resp.CuSize == cuSize
}
}

checkClusterType := func(clusterType string) func(resp *Cluster) bool {
return func(resp *Cluster) bool {
return resp.ClusterType == clusterType
}
}

c, teardown := zillizClient[Clusters](t)
defer teardown()

getProject := func() string {

projects, err := c.ListProjects()
if err != nil {
t.Fatalf("failed to list projects: %v", err)
}

var want string = "Default Project"

if len(projects) == 0 || projects[0].ProjectName != want {
t.Errorf("want = %s, got = %v", want, projects)
}

return projects[0].ProjectId
}

projectID = getProject()
params := CreateClusterParams{
ProjectId: projectID,
Plan: "Standard",
ClusterName: "a-standard-type-cluster",
CUSize: 1,
CUType: "Performance-optimized",
}

t.Run("CreateCluster", func(t *testing.T) {
c, teardown := zillizClient[Clusters](t)
defer teardown()

resp, err := c.CreateCluster(params)
if err != nil {
t.Fatalf("failed to create cluster: %v", err)
}

if resp.ClusterId == "" {
t.Fatalf("failed to create cluster: %v", resp)
}

clusterId = resp.ClusterId
})

t.Run("duplicately create cluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()
_, err := c.CreateCluster(params)

var e = Error{
Code: 80010,
}

if !errors.Is(err, e) {
t.Fatalf("want = %v, but got = %v", e, err)
}

})

t.Run("DescribeCluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()
// checkfn:=make([]func(resp *Cluster) bool,0)
checkfn := []func(resp *Cluster) bool{
checkPlan("Standard"),
checkCUSize(1),
checkClusterType("Performance-optimized"),
}

ctx, cancelfn := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelfn()
got := pollClusterStatus(ctx, t, c, clusterId, "RUNNING", pollInterval)

for _, fn := range checkfn {
if !fn(got) {
t.Errorf("check failed")
}
}

})

t.Run("scale up cluster", func(t *testing.T) {
c, teardown := zillizClient[Clusters](t)
defer teardown()
checkfn := []func(resp *Cluster) bool{
checkCUSize(2),
}
_, err := c.ModifyCluster(clusterId, &ModifyClusterParams{

CuSize: 2,
})
if err != nil {
t.Fatalf("failed to describe cluster: %v", err)
}

ctx, cancelfn := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelfn()
got := pollClusterStatus(ctx, t, c, clusterId, "RUNNING", pollInterval)
for _, fn := range checkfn {
if !fn(got) {
t.Errorf("check failed")
}
}

})

t.Run("DeleteCluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()
got, err := c.DropCluster(clusterId)
if err != nil {
t.Fatalf("failed to delete cluster: %v", err)
}

if got == nil || *got != clusterId {
t.Fatalf("want = %s, got = %v", clusterId, got)
}
})
}

func TestClient_ServerlessCluster(t *testing.T) {
var clusterId string
var projectID string
if update {
pollInterval = 60
}

checkClusterId := func(clusterId string) func(resp *Cluster) bool {
return func(resp *Cluster) bool {
return resp.ClusterId == clusterId
}
}

c, teardown := zillizClient[Clusters](t)
defer teardown()

getProject := func() string {

projects, err := c.ListProjects()
if err != nil {
t.Fatalf("failed to list projects: %v", err)
}

var want string = "Default Project"

if len(projects) == 0 || projects[0].ProjectName != want {
t.Errorf("want = %s, got = %v", want, projects)
}

return projects[0].ProjectId
}

projectID = getProject()
params := CreateServerlessClusterParams{
ProjectId: projectID,
ClusterName: "a-starter-type-cluster",
}

t.Run("create serverless cluster", func(t *testing.T) {
c, teardown := zillizClient[Clusters](t)
defer teardown()

resp, err := c.CreateServerlessCluster(params)
if err != nil {
t.Fatalf("failed to create cluster: %v", err)
}

if resp.ClusterId == "" {
t.Fatalf("failed to create cluster: %v", resp)
}

clusterId = resp.ClusterId
})

t.Run("duplicately create cluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()

_, err := c.CreateServerlessCluster(params)
var e = Error{
Code: 80010,
}

if !errors.Is(err, e) {
t.Fatalf("want = %v, but got = %v", e, err)
}

})

t.Run("DescribeCluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()
// checkfn:=make([]func(resp *Cluster) bool,0)
checkfn := []func(resp *Cluster) bool{
checkClusterId(clusterId),
}

ctx, cancelfn := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelfn()
got := pollClusterStatus(ctx, t, c, clusterId, "RUNNING", pollInterval)

for _, fn := range checkfn {
if !fn(got) {
t.Errorf("check failed")
}
}

})

t.Run("DeleteCluster", func(t *testing.T) {

c, teardown := zillizClient[Clusters](t)
defer teardown()
got, err := c.DropCluster(clusterId)
if err != nil {
t.Fatalf("failed to delete cluster: %v", err)
}

if got == nil || *got != clusterId {
t.Fatalf("want = %s, got = %v", clusterId, got)
}
})
}

func pollClusterStatus(ctx context.Context, t *testing.T, c *Client, clusterId string, status string, pollingInterval int) *Cluster {

var (
got Cluster
err error
)
interval := time.Duration(pollingInterval) * time.Second
// ctx, _ := context.WithTimeout(ctx, 5*time.Minute)

for {
select {
case <-ctx.Done():
t.Fatalf("timeout")
return nil

case <-time.After(interval):
t.Logf("[%s] polling cluster status...", time.Now().Format("2006-01-02 15:04:05"))

got, err = c.DescribeCluster(clusterId)
if err != nil {
t.Fatalf("failed to describe cluster: %v", err)
}

switch got.Status {
case status:
return &got
default:
continue
}
}

}
}
8 changes: 8 additions & 0 deletions client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ type Error struct {
func (err Error) Error() string {
return fmt.Sprintf("code:%d,Message:%s", err.Code, err.Message)
}

func (err Error) Is(target error) bool {
t, ok := target.(Error)
if !ok {
return false
}
return t.Code == err.Code
}
Loading

0 comments on commit 50808f8

Please sign in to comment.