Skip to content

Commit

Permalink
feat: check data consistent by external tool. (#1205)
Browse files Browse the repository at this point in the history
* feat: check data consistent by external tool

- Added a new command-line tool `data-assert` for managing Redis data, including commands to generate data, check data consistency, and create resource YAML files.
- Introduced `go.mod` and `go.sum` for dependency management, incorporating Redis and Cobra libraries.
- Created a template for generating Kubernetes resources, including a ConfigMap and Pod specifications.
- Updated e2e tests to utilize the new data-assert functionality for data generation and validation.

This enhancement improves the management and verification of data in Redis clusters.

Signed-off-by: drivebyer <[email protected]>

* generate

Signed-off-by: drivebyer <[email protected]>

* ignore

Signed-off-by: drivebyer <[email protected]>

---------

Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer authored Jan 13, 2025
1 parent b00f1df commit 5850e81
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ rules:
allow-non-breakable-words: true
allow-non-breakable-inline-mappings: true
comments-indentation: disable

ignore:
- resources.yaml
File renamed without changes.
File renamed without changes.
25 changes: 19 additions & 6 deletions cmd/data-assert/main.go → tests/data-assert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ func genRedisDataCmd(cmd *cobra.Command, args []string) {
ctx := context.Background()
var rdb redis.UniversalClient

// Split host string by comma
hosts := strings.Split(host, ",")
for i := range hosts {
hosts[i] = strings.TrimSpace(hosts[i])
}

switch mode {
case "cluster":
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{host},
Addrs: hosts,
Password: pass,
})
case "sentinel":
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{host},
SentinelAddrs: hosts,
Password: pass,
})
default:
Expand All @@ -95,7 +101,7 @@ func genRedisDataCmd(cmd *cobra.Command, args []string) {
return
}
}
fmt.Printf("successfully generated %d keys\n", totalKey)
fmt.Printf("[OK] successfully generated %d keys\n", totalKey)
}

// DataError represents data consistency check errors
Expand All @@ -118,23 +124,29 @@ func chkRedisDataCmd(cmd *cobra.Command, args []string) {
fmt.Printf("Error occurred during check: %v\n", err)
os.Exit(1)
}
fmt.Printf("Data consistency check passed! All %d keys exist\n", totalKey)
fmt.Printf("[OK] Data consistency check passed! All %d keys exist\n", totalKey)
}

func checkRedisData() error {
ctx := context.Background()
var rdb redis.UniversalClient

// Split host string by comma
hosts := strings.Split(host, ",")
for i := range hosts {
hosts[i] = strings.TrimSpace(hosts[i])
}

switch mode {
case "cluster":
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{host},
Addrs: hosts,
Password: pass,
})
case "sentinel":
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{host},
SentinelAddrs: hosts,
Password: pass,
})
default:
Expand Down Expand Up @@ -198,4 +210,5 @@ func genResourceYamlCmd(cmd *cobra.Command, args []string) {
if err != nil {
panic(err)
}
fmt.Println("✅resources.yaml generated")
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,22 @@ data:
ctx := context.Background()
var rdb redis.UniversalClient
// Split host string by comma
hosts := strings.Split(host, ",")
for i := range hosts {
hosts[i] = strings.TrimSpace(hosts[i])
}
switch mode {
case "cluster":
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{host},
Addrs: hosts,
Password: pass,
})
case "sentinel":
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{host},
SentinelAddrs: hosts,
Password: pass,
})
default:
Expand All @@ -102,7 +108,7 @@ data:
return
}
}
fmt.Printf("successfully generated %d keys\n", totalKey)
fmt.Printf("[OK] successfully generated %d keys\n", totalKey)
}
// DataError represents data consistency check errors
Expand All @@ -125,23 +131,29 @@ data:
fmt.Printf("Error occurred during check: %v\n", err)
os.Exit(1)
}
fmt.Printf("Data consistency check passed! All %d keys exist\n", totalKey)
fmt.Printf("[OK] Data consistency check passed! All %d keys exist\n", totalKey)
}
func checkRedisData() error {
ctx := context.Background()
var rdb redis.UniversalClient
// Split host string by comma
hosts := strings.Split(host, ",")
for i := range hosts {
hosts[i] = strings.TrimSpace(hosts[i])
}
switch mode {
case "cluster":
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{host},
Addrs: hosts,
Password: pass,
})
case "sentinel":
rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: "mymaster",
SentinelAddrs: []string{host},
SentinelAddrs: hosts,
Password: pass,
})
default:
Expand Down Expand Up @@ -205,6 +217,7 @@ data:
if err != nil {
panic(err)
}
fmt.Println("✅resources.yaml generated")
}
go.mod: |
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ spec:
- try:
- apply:
file: cluster.yaml
- apply:
file: ../../../data-assert/resources.yaml
- assert:
file: ready-cluster.yaml

- name: Try saving a key With Password
- name: Put data
try:
- script:
timeout: 30s
content: >
kubectl exec --namespace ${NAMESPACE} --container redis-cluster-v1beta2-leader redis-cluster-v1beta2-leader-0 --
redis-cli -c set foo-0 bar-0
kubectl exec --namespace ${NAMESPACE} --container data-assert data-assert --
bash -c "cd /go/src/data-assert && go run main.go gen-redis-data --host redis-cluster-v1beta2-leader.${NAMESPACE}.svc.cluster.local:6379 --mode cluster"
check:
(contains($stdout, 'OK')): true

Expand All @@ -39,12 +41,12 @@ spec:
- assert:
file: ready-cluster.yaml

- name: Get key
- name: Assert data
try:
- script:
timeout: 30s
content: >
kubectl exec --namespace ${NAMESPACE} --container redis-cluster-v1beta2-leader redis-cluster-v1beta2-leader-0 --
redis-cli -c get foo-0
kubectl exec --namespace ${NAMESPACE} --container data-assert data-assert --
bash -c "cd /go/src/data-assert && go run main.go chk-redis-data --host redis-cluster-v1beta2-leader.${NAMESPACE}.svc.cluster.local:6379 --mode cluster"
check:
(contains($stdout, 'bar-0')): true
(contains($stdout, 'OK')): true

0 comments on commit 5850e81

Please sign in to comment.