加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fixed_database_test.go 15.19 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
package rediscloud_api
import (
"context"
"net/http/httptest"
"testing"
"time"
"github.com/RedisLabs/rediscloud-go-api/redis"
"github.com/RedisLabs/rediscloud-go-api/service/databases"
fixedDatabases "github.com/RedisLabs/rediscloud-go-api/service/fixed/databases"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFixedDatabase_Create(t *testing.T) {
server := httptest.NewServer(
testServer(
"apiKey",
"secret",
postRequest(
t,
"/fixed/subscriptions/111728/databases",
`{
"name": "my-test-fixed-database",
"protocol": "memcached",
"respVersion": "resp2",
"dataPersistence": "none",
"dataEvictionPolicy": "noeviction",
"replication": false,
"alerts": []
}`,
`{
"taskId": "784299af-17ea-4ed6-b08f-dd643238c8dd",
"commandType": "fixedDatabaseCreateRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-05-10T14:14:14.736763484Z",
"links": [
{
"rel": "task",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/784299af-17ea-4ed6-b08f-dd643238c8dd"
}
]
}`,
),
getRequest(
t,
"/tasks/784299af-17ea-4ed6-b08f-dd643238c8dd",
`{
"taskId": "784299af-17ea-4ed6-b08f-dd643238c8dd",
"commandType": "fixedDatabaseCreateRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-05-10T14:14:34.153537279Z",
"response": {
"resourceId": 51055029,
"additionalResourceId": 111728
},
"links": [
{
"rel": "resource",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/111728/databases/51055029"
},
{
"rel": "self",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/784299af-17ea-4ed6-b08f-dd643238c8dd"
}
]
}`,
),
),
)
subject, err := clientFromTestServer(server, "apiKey", "secret")
require.NoError(t, err)
actual, err := subject.FixedDatabases.Create(
context.TODO(),
111728,
fixedDatabases.CreateFixedDatabase{
Name: redis.String("my-test-fixed-database"),
Protocol: redis.String("memcached"),
RespVersion: redis.String("resp2"),
DataPersistence: redis.String("none"),
DataEvictionPolicy: redis.String("noeviction"),
Replication: redis.Bool(false),
Alerts: &[]*databases.Alert{},
},
)
require.NoError(t, err)
assert.Equal(t, 51055029, actual)
}
func TestFixedDatabase_List(t *testing.T) {
server := httptest.NewServer(
testServer(
"apiKey",
"secret",
getRequestWithQuery(
t,
"/fixed/subscriptions/111930/databases",
map[string][]string{
"limit": {
"100",
},
"offset": {
"0",
},
},
`{
"accountId": 69369,
"subscription": {
"subscriptionId": 111930,
"numberOfDatabases": 1,
"databases": [
{
"databaseId": 51055698,
"name": "my-second-test-fixed-database",
"protocol": "memcached",
"provider": "AWS",
"region": "us-west-1",
"respVersion": "resp2",
"status": "draft",
"planMemoryLimit": 1,
"memoryLimitMeasurementUnit": "GB",
"memoryUsedInMb": 7,
"memoryStorage": "ram",
"supportOSSClusterApi": false,
"useExternalEndpointForOSSClusterApi": false,
"dataPersistence": "none",
"replication": false,
"dataEvictionPolicy": "noeviction",
"activatedOn": "2024-05-14T09:27:48Z",
"replicaOf": null,
"replica": null,
"clustering": {
"enabled": true,
"regexRules": [
{
"ordinal": 0,
"pattern": ".*\\{(?<tag>.*)\\}.*"
},
{
"ordinal": 1,
"pattern": "(?<tag>.*)"
}
],
"hashingPolicy": "standard"
},
"security": {
"defaultUserEnabled": true,
"sslClientAuthentication": false,
"tlsClientAuthentication": false,
"enableTls": false,
"sourceIps": [
"0.0.0.0/0"
]
},
"modules": [],
"alerts": [],
"backup": {
"remoteBackupEnabled": false,
"status": "idle"
},
"links": []
}
],
"links": []
},
"links": [
{
"rel": "self",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/111930/databases?offset=0&limit=100"
}
]
}`,
),
getRequestWithQuery(
t,
"/fixed/subscriptions/111930/databases",
map[string][]string{
"limit": {
"100",
},
"offset": {
"100",
},
},
`{
"accountId" : 69369,
"subscription" : {
"subscriptionId" : 111930,
"numberOfDatabases" : 0,
"databases" : [ ],
"links" : [ ]
},
"links" : [ {
"rel" : "self",
"type" : "GET",
"href" : "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/112330/databases?limit=100&offset=100"
} ]
}`,
),
),
)
subject, err := clientFromTestServer(server, "apiKey", "secret")
require.NoError(t, err)
list := subject.FixedDatabases.List(context.TODO(), 111930)
var actual []*fixedDatabases.FixedDatabase
for list.Next() {
actual = append(actual, list.Value())
}
require.NoError(t, list.Err())
assert.Equal(t, []*fixedDatabases.FixedDatabase{
{
DatabaseId: redis.Int(51055698),
Name: redis.String("my-second-test-fixed-database"),
Protocol: redis.String("memcached"),
Provider: redis.String("AWS"),
Region: redis.String("us-west-1"),
RespVersion: redis.String("resp2"),
Status: redis.String("draft"),
PlanMemoryLimit: redis.Float64(1),
MemoryLimitMeasurementUnit: redis.String("GB"),
MemoryUsedInMb: redis.Float64(7),
MemoryStorage: redis.String("ram"),
SupportOSSClusterAPI: redis.Bool(false),
UseExternalEndpointForOSSClusterAPI: redis.Bool(false),
DataPersistence: redis.String("none"),
Replication: redis.Bool(false),
DataEvictionPolicy: redis.String("noeviction"),
ActivatedOn: redis.Time(time.Date(2024, 5, 14, 9, 27, 48, 0, time.UTC)),
Clustering: &fixedDatabases.Clustering{
Enabled: redis.Bool(true),
RegexRules: []*databases.RegexRule{
{
Ordinal: 0,
Pattern: ".*\\{(?<tag>.*)\\}.*",
},
{
Ordinal: 1,
Pattern: "(?<tag>.*)",
},
},
HashingPolicy: redis.String("standard"),
},
Security: &fixedDatabases.Security{
EnableDefaultUser: redis.Bool(true),
SSLClientAuthentication: redis.Bool(false),
TLSClientAuthentication: redis.Bool(false),
EnableTls: redis.Bool(false),
SourceIPs: redis.StringSlice("0.0.0.0/0"),
},
Modules: &[]*databases.Module{},
Alerts: &[]*databases.Alert{},
Backup: &fixedDatabases.Backup{
Enabled: redis.Bool(false),
Status: redis.String("idle"),
},
},
}, actual)
}
func TestFixedDatabase_Get(t *testing.T) {
server := httptest.NewServer(
testServer(
"apiKey",
"secret",
getRequest(
t,
"/fixed/subscriptions/111728/databases/51055029",
`{
"databaseId": 51055029,
"name": "my-test-fixed-database",
"protocol": "memcached",
"provider": "AWS",
"region": "us-west-1",
"respVersion": "resp2",
"status": "draft",
"planMemoryLimit": 1,
"memoryLimitMeasurementUnit": "GB",
"memoryUsedInMb": 7,
"memoryStorage": "ram",
"supportOSSClusterApi": false,
"useExternalEndpointForOSSClusterApi": false,
"dataPersistence": "none",
"replication": false,
"dataEvictionPolicy": "noeviction",
"activatedOn": "2024-05-10T14:14:33Z",
"replicaOf": null,
"replica": null,
"clustering": {
"enabled": true,
"regexRules": [
{
"ordinal": 0,
"pattern": ".*\\{(?<tag>.*)\\}.*"
},
{
"ordinal": 1,
"pattern": "(?<tag>.*)"
}
],
"hashingPolicy": "standard"
},
"security": {
"defaultUserEnabled": true,
"password": "s1j5CSgTliqM0EHzMU68GbflSVGuCIcF",
"sslClientAuthentication": false,
"tlsClientAuthentication": false,
"enableTls": false,
"sourceIps": [
"0.0.0.0/0"
]
},
"modules": [],
"alerts": [],
"backup": {
"remoteBackupEnabled": false,
"status": "idle"
},
"links": [
{
"rel": "self",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/111728/databases/51055029"
}
]
}`,
),
),
)
subject, err := clientFromTestServer(server, "apiKey", "secret")
require.NoError(t, err)
actual, err := subject.FixedDatabases.Get(context.TODO(), 111728, 51055029)
require.NoError(t, err)
assert.Equal(t, &fixedDatabases.FixedDatabase{
DatabaseId: redis.Int(51055029),
Name: redis.String("my-test-fixed-database"),
Protocol: redis.String("memcached"),
Provider: redis.String("AWS"),
Region: redis.String("us-west-1"),
RespVersion: redis.String("resp2"),
Status: redis.String("draft"),
PlanMemoryLimit: redis.Float64(1),
MemoryLimitMeasurementUnit: redis.String("GB"),
MemoryUsedInMb: redis.Float64(7),
MemoryStorage: redis.String("ram"),
SupportOSSClusterAPI: redis.Bool(false),
UseExternalEndpointForOSSClusterAPI: redis.Bool(false),
DataPersistence: redis.String("none"),
Replication: redis.Bool(false),
DataEvictionPolicy: redis.String("noeviction"),
ActivatedOn: redis.Time(time.Date(2024, 5, 10, 14, 14, 33, 0, time.UTC)),
Clustering: &fixedDatabases.Clustering{
Enabled: redis.Bool(true),
RegexRules: []*databases.RegexRule{
{
Ordinal: 0,
Pattern: ".*\\{(?<tag>.*)\\}.*",
},
{
Ordinal: 1,
Pattern: "(?<tag>.*)",
},
},
HashingPolicy: redis.String("standard"),
},
Security: &fixedDatabases.Security{
EnableDefaultUser: redis.Bool(true),
Password: redis.String("s1j5CSgTliqM0EHzMU68GbflSVGuCIcF"),
SSLClientAuthentication: redis.Bool(false),
TLSClientAuthentication: redis.Bool(false),
EnableTls: redis.Bool(false),
SourceIPs: redis.StringSlice("0.0.0.0/0"),
},
Modules: &[]*databases.Module{},
Alerts: &[]*databases.Alert{},
Backup: &fixedDatabases.Backup{
Enabled: redis.Bool(false),
Status: redis.String("idle"),
},
}, actual)
}
func TestFixedDatabase_Update(t *testing.T) {
server := httptest.NewServer(
testServer(
"apiKey",
"secret",
putRequest(
t,
"/fixed/subscriptions/112119/databases/51056892",
`{
"name": "my-test-fixed-database",
"respVersion": "resp2",
"dataPersistence": "none",
"dataEvictionPolicy": "volatile-lru",
"replication": false,
"enableDefaultUser": true,
"alerts": [
{
"name": "datasets-size",
"value": 80
}
]
}`,
`{
"taskId": "4a9dc555-7c7d-4657-a8c9-5d868d7acdd2",
"commandType": "fixedDatabaseUpdateRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-05-15T14:47:08.975576016Z",
"links": [
{
"rel": "task",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/4a9dc555-7c7d-4657-a8c9-5d868d7acdd2"
}
]
}`,
),
getRequest(
t,
"/tasks/4a9dc555-7c7d-4657-a8c9-5d868d7acdd2",
`{
"taskId": "4a9dc555-7c7d-4657-a8c9-5d868d7acdd2",
"commandType": "fixedDatabaseUpdateRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-05-15T14:47:21.687621578Z",
"response": {
"resourceId": 51056892,
"additionalResourceId": 112119
},
"links": [
{
"rel": "resource",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/112119/databases/51056892"
},
{
"rel": "self",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/4a9dc555-7c7d-4657-a8c9-5d868d7acdd2"
}
]
}`,
),
),
)
subject, err := clientFromTestServer(server, "apiKey", "secret")
require.NoError(t, err)
err = subject.FixedDatabases.Update(
context.TODO(),
112119,
51056892,
fixedDatabases.UpdateFixedDatabase{
Name: redis.String("my-test-fixed-database"),
RespVersion: redis.String("resp2"),
DataPersistence: redis.String("none"),
DataEvictionPolicy: redis.String("volatile-lru"),
Replication: redis.Bool(false),
EnableDefaultUser: redis.Bool(true),
Alerts: &[]*databases.Alert{
{
Name: redis.String("datasets-size"),
Value: redis.Int(80),
},
},
},
)
require.NoError(t, err)
}
func TestFixedDatabase_Delete(t *testing.T) {
server := httptest.NewServer(
testServer(
"apiKey",
"secret",
deleteRequest(
t,
"/fixed/subscriptions/112119/databases/51056892",
`{
"taskId": "3e26ac3a-231c-486d-b4cf-8519f520a5f4",
"commandType": "fixedDatabaseDeleteRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-05-15T14:55:04.008723915Z",
"links": [
{
"rel": "task",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/3e26ac3a-231c-486d-b4cf-8519f520a5f4"
}
]
}`,
),
getRequest(
t,
"/tasks/3e26ac3a-231c-486d-b4cf-8519f520a5f4",
`{
"taskId": "3e26ac3a-231c-486d-b4cf-8519f520a5f4",
"commandType": "fixedDatabaseDeleteRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-05-15T14:55:06.538386979Z",
"response": {
"resourceId": 51056892,
"additionalResourceId": 112119
},
"links": [
{
"rel": "self",
"type": "GET",
"href": "https://api-staging.qa.redislabs.com/v1/tasks/3e26ac3a-231c-486d-b4cf-8519f520a5f4"
}
]
}`,
),
),
)
subject, err := clientFromTestServer(server, "apiKey", "secret")
require.NoError(t, err)
err = subject.FixedDatabases.Delete(context.TODO(), 112119, 51056892)
require.NoError(t, err)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化