Skip to content

Commit

Permalink
test: misc ut
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 1, 2024
1 parent a27c47b commit d350d55
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/config/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func LoadTenantOperatorFromPath(path string) (TenantOperator, error) {
if err != nil {
return nil, err
}
if err := Init(*cfg.Config, cfg.Spec.APIVersion); err != nil {
if err = Init(*cfg.Config, cfg.Spec.APIVersion); err != nil {
return nil, err
}

Expand Down
145 changes: 145 additions & 0 deletions pkg/config/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package config

import (
"fmt"
"os"
"testing"
)
Expand Down Expand Up @@ -187,3 +188,147 @@ logging:
assert.Error(t, err)
assert.Nil(t, cfg2)
}

func TestLoadTenantOperator(t *testing.T) {
root, config := bootstrapPath(t)
defer func() {
_ = os.Remove(root)
_ = os.Remove(config)
}()
bootOptions, err := LoadBootOptions(root)
assert.NoError(t, err)
_, err = LoadTenantOperator(bootOptions)
assert.NoError(t, err)
}

func TestLoadTenantOperatorFromPath(t *testing.T) {
_, err := LoadTenantOperatorFromPath("")
assert.Error(t, err)
root, config := bootstrapPath(t)
defer func() {
_ = os.Remove(root)
_ = os.Remove(config)
}()
_, err = LoadTenantOperatorFromPath(root)
assert.NoError(t, err)
}

func bootstrapPath(t *testing.T) (string, string) {
create := func() string {
tmp, err := os.CreateTemp("", "arana-fake.*.yaml")
assert.NoError(t, err)
return tmp.Name()
}

config := `kind: ConfigMap
apiVersion: "1.0"
metadata:
name: arana-config
data:
tenants:
- name: arana
sys_db:
host: arana-mysql
port: 3306
username: root
password: "123456"
database: __arana_sys
weight: r10w10
parameters:
users:
- username: arana
password: "123456"
clusters:
- name: employees
type: mysql
sql_max_limit: -1
tenant: arana
parameters:
slow_threshold: 1s
max_allowed_packet: 256M
groups:
- name: employees_0000
nodes:
- node0
sharding_rule:
tables:
- name: employees.student
sequence:
type: snowflake
option:
db_rules:
- columns:
- name: uid
expr: parseInt($0 % 32 / 8)
tbl_rules:
- columns:
- name: uid
expr: $0 % 32
topology:
db_pattern: employees_${0000..0003}
tbl_pattern: student_${0000..0031}
attributes:
allow_full_scan: true
sqlMaxLimit: -1
- name: employees.friendship
sequence:
type: snowflake
option:
db_rules:
- columns:
- name: uid
- name: friend_id
expr: parseInt(($0*31+$1) % 32 / 8)
tbl_rules:
- columns:
- name: uid
- name: friend_id
expr: ($0*31+$1) % 32
topology:
db_pattern: employees_${0000..0003}
tbl_pattern: friendship_${0000..0031}
attributes:
allow_full_scan: true
sqlMaxLimit: -1
nodes:
node0:
name: node0
host: arana-mysql
port: 3306
username: root
password: "123456"
database: employees_0000
weight: r10w10
parameters:`
configPath := create()
assert.NoError(t, os.WriteFile(configPath, []byte(config), 0644))

bootstrap := `listeners:
- protocol_type: "http"
server_version: "1.0"
config:
name: file
options:
path: %s
registry:
enable: true
name: "registryName"
root_path: "/root/path"
trace:
type: "jaeger"
address: "http://localhost:14268/api/traces"
supervisor:
username: "admin"
password: "password"
logging:
level: INFO
path: /Users/baerwang/project/arana-db/arana
max_size: 128m
max_backups: 3
max_age: 7
compress: true
console: true`
rootPath := create()
assert.NoError(t, os.WriteFile(rootPath, []byte(fmt.Sprintf(bootstrap, configPath)), 0644))
return rootPath, configPath
}

0 comments on commit d350d55

Please sign in to comment.