github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/database/update_test.go (about) 1 // Copyright 2016-2022 The Libsacloud Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package database 16 17 import ( 18 "context" 19 "testing" 20 21 databaseBuilder "github.com/sacloud/libsacloud/v2/helper/builder/database" 22 "github.com/sacloud/libsacloud/v2/helper/wait" 23 "github.com/sacloud/libsacloud/v2/sacloud" 24 "github.com/sacloud/libsacloud/v2/sacloud/pointer" 25 "github.com/sacloud/libsacloud/v2/sacloud/testutil" 26 "github.com/sacloud/libsacloud/v2/sacloud/types" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestDatabaseService_convertUpdateRequest(t *testing.T) { 31 if testutil.IsAccTest() { 32 t.Skip("This test runs only without TESTACC=1") 33 } 34 ctx := context.Background() 35 zone := testutil.TestZone() 36 name := testutil.ResourceName("database-service-update") 37 caller := testutil.SingletonAPICaller() 38 39 sw, err := sacloud.NewSwitchOp(caller).Create(ctx, zone, &sacloud.SwitchCreateRequest{Name: name}) 40 if err != nil { 41 t.Fatal(err) 42 } 43 builder := &databaseBuilder.Builder{ 44 PlanID: types.DatabasePlans.DB10GB, 45 SwitchID: sw.ID, 46 IPAddresses: []string{"192.168.0.101"}, 47 NetworkMaskLen: 24, 48 DefaultRoute: "192.168.0.1", 49 Conf: &sacloud.DatabaseRemarkDBConfCommon{ 50 DatabaseName: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Name, 51 DatabaseVersion: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Version, 52 DatabaseRevision: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Revision, 53 }, 54 SourceID: 0, 55 CommonSetting: &sacloud.DatabaseSettingCommon{ 56 WebUI: types.ToWebUI(true), 57 ServicePort: 5432, 58 SourceNetwork: []string{"192.168.0.0/24"}, 59 DefaultUser: "default", 60 UserPassword: "password1", 61 ReplicaUser: "replica", 62 ReplicaPassword: "password2", 63 }, 64 BackupSetting: &sacloud.DatabaseSettingBackup{ 65 Rotate: 0, 66 Time: "10:00", 67 DayOfWeek: []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday}, 68 }, 69 ReplicationSetting: &sacloud.DatabaseReplicationSetting{ 70 Model: types.DatabaseReplicationModels.MasterSlave, 71 }, 72 Name: name, 73 Description: "description", 74 Tags: types.Tags{"tag1", "tag2"}, 75 SettingsHash: "", 76 NoWait: false, 77 Parameters: map[string]interface{}{ 78 "max_connections": float64(100), 79 }, 80 Client: databaseBuilder.NewAPIClient(caller), 81 } 82 db, err := builder.Build(ctx, zone) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 defer func() { 88 dbOp := sacloud.NewDatabaseOp(caller) 89 if err := dbOp.Shutdown(ctx, zone, db.ID, &sacloud.ShutdownOption{Force: true}); err != nil { 90 return 91 } 92 if _, err := wait.UntilDatabaseIsDown(ctx, dbOp, zone, db.ID); err != nil { 93 return 94 } 95 if err := dbOp.Delete(ctx, zone, db.ID); err != nil { 96 return 97 } 98 sacloud.NewSwitchOp(caller).Delete(ctx, zone, sw.ID) // nolint 99 }() 100 101 cases := []struct { 102 in *UpdateRequest 103 expect *ApplyRequest 104 }{ 105 { 106 in: &UpdateRequest{ 107 Zone: zone, 108 ID: db.ID, 109 Name: pointer.NewString(db.Name + "-upd"), 110 NoWait: true, 111 }, 112 expect: &ApplyRequest{ 113 Zone: zone, 114 ID: db.ID, 115 Name: db.Name + "-upd", 116 Description: "description", 117 Tags: types.Tags{"tag1", "tag2"}, 118 PlanID: types.DatabasePlans.DB10GB, 119 SwitchID: sw.ID, 120 IPAddresses: []string{"192.168.0.101"}, 121 NetworkMaskLen: 24, 122 DefaultRoute: "192.168.0.1", 123 Port: 5432, 124 SourceNetwork: []string{"192.168.0.0/24"}, 125 DatabaseType: types.RDBMSTypesPostgreSQL.String(), 126 Username: "default", 127 Password: "password1", 128 EnableReplication: true, 129 ReplicaUserPassword: "password2", 130 EnableWebUI: true, 131 EnableBackup: true, 132 BackupWeekdays: []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday}, 133 BackupStartTimeHour: 10, 134 BackupStartTimeMinute: 0, 135 Parameters: map[string]interface{}{ 136 "max_connections": float64(100), 137 }, 138 NoWait: true, 139 }, 140 }, 141 { 142 in: &UpdateRequest{ 143 Zone: zone, 144 ID: db.ID, 145 EnableReplication: pointer.NewBool(false), 146 EnableBackup: pointer.NewBool(false), 147 Parameters: &map[string]interface{}{ 148 "work_mem": float64(4096), 149 }, 150 NoWait: true, 151 }, 152 expect: &ApplyRequest{ 153 Zone: zone, 154 ID: db.ID, 155 Name: db.Name, 156 Description: "description", 157 Tags: types.Tags{"tag1", "tag2"}, 158 PlanID: types.DatabasePlans.DB10GB, 159 SwitchID: sw.ID, 160 IPAddresses: []string{"192.168.0.101"}, 161 NetworkMaskLen: 24, 162 DefaultRoute: "192.168.0.1", 163 Port: 5432, 164 SourceNetwork: []string{"192.168.0.0/24"}, 165 DatabaseType: types.RDBMSTypesPostgreSQL.String(), 166 Username: "default", 167 Password: "password1", 168 EnableReplication: false, 169 ReplicaUserPassword: "password2", 170 EnableWebUI: true, 171 EnableBackup: false, 172 BackupWeekdays: []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday}, 173 BackupStartTimeHour: 10, 174 BackupStartTimeMinute: 0, 175 Parameters: map[string]interface{}{ 176 "max_connections": float64(100), 177 "work_mem": float64(4096), 178 }, 179 NoWait: true, 180 }, 181 }, 182 } 183 184 for _, tc := range cases { 185 got, err := tc.in.ApplyRequest(ctx, caller) 186 require.NoError(t, err) 187 require.EqualValues(t, tc.expect, got) 188 } 189 }