github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/database/apply_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  	"testing"
    19  
    20  	databaseBuilder "github.com/sacloud/libsacloud/v2/helper/builder/database"
    21  	"github.com/sacloud/libsacloud/v2/sacloud"
    22  	"github.com/sacloud/libsacloud/v2/sacloud/testutil"
    23  
    24  	"github.com/sacloud/libsacloud/v2/sacloud/types"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestCreateRequest_Validate(t *testing.T) {
    29  	cases := []struct {
    30  		in       *ApplyRequest
    31  		hasError bool
    32  	}{
    33  		{
    34  			in:       &ApplyRequest{},
    35  			hasError: true,
    36  		},
    37  		{
    38  			// mininum
    39  			in: &ApplyRequest{
    40  				Zone:           "tk1a",
    41  				Name:           "test",
    42  				PlanID:         types.DatabasePlans.DB10GB,
    43  				SwitchID:       1,
    44  				IPAddresses:    []string{"192.168.0.11"},
    45  				NetworkMaskLen: 16,
    46  				DefaultRoute:   "192.168.0.1",
    47  				DatabaseType:   "mariadb",
    48  				Username:       "hoge",
    49  				Password:       "pass",
    50  			},
    51  			hasError: false,
    52  		},
    53  		{
    54  			// invalid ip
    55  			in: &ApplyRequest{
    56  				Zone:           "tk1a",
    57  				Name:           "test",
    58  				PlanID:         types.DatabasePlans.DB10GB,
    59  				SwitchID:       1,
    60  				IPAddresses:    []string{"192.168.0.999"},
    61  				NetworkMaskLen: 16,
    62  				DefaultRoute:   "192.168.0.1",
    63  				DatabaseType:   "mariadb",
    64  				Username:       "hoge",
    65  				Password:       "pass",
    66  			},
    67  			hasError: true,
    68  		},
    69  		{
    70  			// invalid ip (out of length)
    71  			in: &ApplyRequest{
    72  				Zone:           "tk1a",
    73  				Name:           "test",
    74  				PlanID:         types.DatabasePlans.DB10GB,
    75  				SwitchID:       1,
    76  				IPAddresses:    []string{"192.168.0.11", "192.168.0.12", "192.168.0.13"},
    77  				NetworkMaskLen: 16,
    78  				DefaultRoute:   "192.168.0.1",
    79  				DatabaseType:   "mariadb",
    80  				Username:       "hoge",
    81  				Password:       "pass",
    82  			},
    83  			hasError: true,
    84  		},
    85  		{
    86  			// invalid source range
    87  			in: &ApplyRequest{
    88  				Zone:           "tk1a",
    89  				Name:           "test",
    90  				PlanID:         types.DatabasePlans.DB10GB,
    91  				SwitchID:       1,
    92  				IPAddresses:    []string{"192.168.0.11"},
    93  				NetworkMaskLen: 16,
    94  				DefaultRoute:   "192.168.0.1",
    95  				SourceNetwork:  []string{"192.168.0.1"}, // require cidr
    96  				DatabaseType:   "mariadb",
    97  				Username:       "hoge",
    98  				Password:       "pass",
    99  			},
   100  			hasError: true,
   101  		},
   102  		{
   103  			// replica user password missing
   104  			in: &ApplyRequest{
   105  				Zone:              "tk1a",
   106  				Name:              "test",
   107  				PlanID:            types.DatabasePlans.DB10GB,
   108  				SwitchID:          1,
   109  				IPAddresses:       []string{"192.168.0.11"},
   110  				NetworkMaskLen:    16,
   111  				DefaultRoute:      "192.168.0.1",
   112  				DatabaseType:      "mariadb",
   113  				Username:          "hoge",
   114  				Password:          "pass",
   115  				EnableReplication: true,
   116  			},
   117  			hasError: true,
   118  		},
   119  		{
   120  			// empty plan
   121  			in: &ApplyRequest{
   122  				Zone:           "tk1a",
   123  				Name:           "test",
   124  				PlanID:         0, // plan is required
   125  				SwitchID:       1,
   126  				IPAddresses:    []string{"192.168.0.11"},
   127  				NetworkMaskLen: 16,
   128  				DefaultRoute:   "192.168.0.1",
   129  				DatabaseType:   "mariadb",
   130  				Username:       "hoge",
   131  				Password:       "pass",
   132  			},
   133  			hasError: true,
   134  		},
   135  		{
   136  			in: &ApplyRequest{
   137  				Zone:                  "tk1a",
   138  				Name:                  "test",
   139  				Description:           "desc",
   140  				Tags:                  types.Tags{"tag1"},
   141  				IconID:                1,
   142  				PlanID:                types.DatabasePlans.DB10GB,
   143  				SwitchID:              1,
   144  				IPAddresses:           []string{"192.168.0.11"},
   145  				NetworkMaskLen:        16,
   146  				DefaultRoute:          "192.168.0.1",
   147  				Port:                  5432,
   148  				SourceNetwork:         []string{"192.168.0.0/24", "192.168.1.0/24"},
   149  				DatabaseType:          "mariadb",
   150  				Username:              "hoge",
   151  				Password:              "pass",
   152  				EnableReplication:     true,
   153  				ReplicaUserPassword:   "pass2",
   154  				EnableWebUI:           true,
   155  				EnableBackup:          true,
   156  				BackupWeekdays:        []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday},
   157  				BackupStartTimeHour:   10,
   158  				BackupStartTimeMinute: 15,
   159  			},
   160  			hasError: false,
   161  		},
   162  	}
   163  	for _, tc := range cases {
   164  		err := tc.in.Validate()
   165  		require.Equal(t, tc.hasError, err != nil, "with: %#v error: %s", tc.in, err)
   166  	}
   167  }
   168  
   169  func TestDatabaseService_convertToActualBuilder(t *testing.T) {
   170  	caller := testutil.SingletonAPICaller()
   171  	cases := []struct {
   172  		in     *Builder
   173  		expect *databaseBuilder.Builder
   174  	}{
   175  		{
   176  			in: &Builder{
   177  				Caller:       caller,
   178  				DatabaseType: types.RDBMSTypesPostgreSQL.String(),
   179  			},
   180  			expect: &databaseBuilder.Builder{
   181  				CommonSetting: &sacloud.DatabaseSettingCommon{
   182  					WebUI:           types.ToWebUI(false),
   183  					ServicePort:     0,
   184  					SourceNetwork:   nil,
   185  					DefaultUser:     "",
   186  					UserPassword:    "",
   187  					ReplicaUser:     "",
   188  					ReplicaPassword: "",
   189  				},
   190  				Conf: &sacloud.DatabaseRemarkDBConfCommon{
   191  					DatabaseName:     types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Name,
   192  					DatabaseVersion:  types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Version,
   193  					DatabaseRevision: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Revision,
   194  				},
   195  				Client: databaseBuilder.NewAPIClient(caller),
   196  			},
   197  		},
   198  		{
   199  			in: &Builder{
   200  				ID:                    0,
   201  				Zone:                  "is1a",
   202  				Name:                  "name",
   203  				Description:           "description",
   204  				Tags:                  types.Tags{"tag1", "tag2"},
   205  				IconID:                types.ID(1),
   206  				PlanID:                types.DatabasePlans.DB30GB,
   207  				SwitchID:              types.ID(1),
   208  				IPAddresses:           []string{"192.168.0.101"},
   209  				NetworkMaskLen:        24,
   210  				DefaultRoute:          "192.168.0.1",
   211  				Port:                  5432,
   212  				SourceNetwork:         []string{"192.168.0.0/24"},
   213  				DatabaseType:          types.RDBMSTypesPostgreSQL.String(),
   214  				Username:              "default",
   215  				Password:              "password1",
   216  				EnableReplication:     true,
   217  				ReplicaUserPassword:   "password2",
   218  				EnableWebUI:           true,
   219  				EnableBackup:          true,
   220  				BackupWeekdays:        []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday},
   221  				BackupStartTimeHour:   10,
   222  				BackupStartTimeMinute: 0,
   223  				NoWait:                true,
   224  				Caller:                caller,
   225  			},
   226  			expect: &databaseBuilder.Builder{
   227  				PlanID:         types.DatabasePlans.DB30GB,
   228  				SwitchID:       types.ID(1),
   229  				IPAddresses:    []string{"192.168.0.101"},
   230  				NetworkMaskLen: 24,
   231  				DefaultRoute:   "192.168.0.1",
   232  				Conf: &sacloud.DatabaseRemarkDBConfCommon{
   233  					DatabaseName:     types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Name,
   234  					DatabaseVersion:  types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Version,
   235  					DatabaseRevision: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Revision,
   236  				},
   237  				SourceID: 0,
   238  				CommonSetting: &sacloud.DatabaseSettingCommon{
   239  					WebUI:           types.ToWebUI(true),
   240  					ServicePort:     5432,
   241  					SourceNetwork:   []string{"192.168.0.0/24"},
   242  					DefaultUser:     "default",
   243  					UserPassword:    "password1",
   244  					ReplicaUser:     "replica",
   245  					ReplicaPassword: "password2",
   246  				},
   247  				BackupSetting: &sacloud.DatabaseSettingBackup{
   248  					Rotate:    0,
   249  					Time:      "10:00",
   250  					DayOfWeek: []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday},
   251  				},
   252  				ReplicationSetting: &sacloud.DatabaseReplicationSetting{
   253  					Model: types.DatabaseReplicationModels.MasterSlave,
   254  				},
   255  				Name:         "name",
   256  				Description:  "description",
   257  				Tags:         types.Tags{"tag1", "tag2"},
   258  				IconID:       types.ID(1),
   259  				SettingsHash: "",
   260  				NoWait:       true,
   261  				SetupOptions: nil,
   262  				Client:       databaseBuilder.NewAPIClient(caller),
   263  			},
   264  		},
   265  		{
   266  			in: &Builder{
   267  				ID:                    0,
   268  				Zone:                  "is1a",
   269  				Name:                  "name",
   270  				Description:           "description",
   271  				Tags:                  types.Tags{"tag1", "tag2"},
   272  				IconID:                types.ID(1),
   273  				PlanID:                types.DatabasePlans.DB30GB,
   274  				SwitchID:              types.ID(1),
   275  				IPAddresses:           []string{"192.168.0.101"},
   276  				NetworkMaskLen:        24,
   277  				DefaultRoute:          "192.168.0.1",
   278  				Port:                  5432,
   279  				SourceNetwork:         []string{"192.168.0.0/24"},
   280  				DatabaseType:          types.RDBMSTypesPostgreSQL.String(),
   281  				Username:              "default",
   282  				Password:              "password1",
   283  				EnableReplication:     false, // UPDATE
   284  				ReplicaUserPassword:   "password2",
   285  				EnableWebUI:           true,
   286  				EnableBackup:          false, // UPDATE
   287  				BackupWeekdays:        []types.EBackupSpanWeekday{types.BackupSpanWeekdays.Monday},
   288  				BackupStartTimeHour:   10,
   289  				BackupStartTimeMinute: 0,
   290  				NoWait:                true,
   291  				Caller:                caller,
   292  			},
   293  			expect: &databaseBuilder.Builder{
   294  				PlanID:         types.DatabasePlans.DB30GB,
   295  				SwitchID:       types.ID(1),
   296  				IPAddresses:    []string{"192.168.0.101"},
   297  				NetworkMaskLen: 24,
   298  				DefaultRoute:   "192.168.0.1",
   299  				Conf: &sacloud.DatabaseRemarkDBConfCommon{
   300  					DatabaseName:     types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Name,
   301  					DatabaseVersion:  types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Version,
   302  					DatabaseRevision: types.RDBMSVersions[types.RDBMSTypesPostgreSQL].Revision,
   303  				},
   304  				SourceID: 0,
   305  				CommonSetting: &sacloud.DatabaseSettingCommon{
   306  					WebUI:           types.ToWebUI(true),
   307  					ServicePort:     5432,
   308  					SourceNetwork:   []string{"192.168.0.0/24"},
   309  					DefaultUser:     "default",
   310  					UserPassword:    "password1",
   311  					ReplicaUser:     "",
   312  					ReplicaPassword: "",
   313  				},
   314  				BackupSetting:      nil,
   315  				ReplicationSetting: nil,
   316  				Name:               "name",
   317  				Description:        "description",
   318  				Tags:               types.Tags{"tag1", "tag2"},
   319  				IconID:             types.ID(1),
   320  				SettingsHash:       "",
   321  				NoWait:             true,
   322  				SetupOptions:       nil,
   323  				Client:             databaseBuilder.NewAPIClient(caller),
   324  			},
   325  		},
   326  	}
   327  
   328  	for _, tc := range cases {
   329  		require.EqualValues(t, tc.expect, tc.in.actualBuilder())
   330  	}
   331  }