github.com/sacloud/iaas-api-go@v1.12.0/test/dns_op_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go 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 test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/sacloud/iaas-api-go"
    21  	"github.com/sacloud/iaas-api-go/testutil"
    22  	"github.com/sacloud/iaas-api-go/types"
    23  )
    24  
    25  func TestDNSOp_CRUD(t *testing.T) {
    26  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    27  		Parallel: true,
    28  
    29  		SetupAPICallerFunc: singletonAPICaller,
    30  
    31  		Create: &testutil.CRUDTestFunc{
    32  			Func: testDNSCreate,
    33  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    34  				ExpectValue:  createDNSExpected,
    35  				IgnoreFields: ignoreDNSFields,
    36  			}),
    37  		},
    38  
    39  		Read: &testutil.CRUDTestFunc{
    40  			Func: testDNSRead,
    41  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    42  				ExpectValue:  createDNSExpected,
    43  				IgnoreFields: ignoreDNSFields,
    44  			}),
    45  		},
    46  
    47  		Updates: []*testutil.CRUDTestFunc{
    48  			{
    49  				Func: testDNSUpdateSettings,
    50  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    51  					ExpectValue:  updateDNSSettingsExpected,
    52  					IgnoreFields: ignoreDNSFields,
    53  				}),
    54  			},
    55  			{
    56  				Func: testDNSUpdate,
    57  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    58  					ExpectValue:  updateDNSExpected,
    59  					IgnoreFields: ignoreDNSFields,
    60  				}),
    61  			},
    62  			{
    63  				Func: testDNSUpdateToMin,
    64  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    65  					ExpectValue:  updateDNSToMinExpected,
    66  					IgnoreFields: ignoreDNSFields,
    67  				}),
    68  			},
    69  		},
    70  
    71  		Delete: &testutil.CRUDTestDeleteFunc{
    72  			Func: testDNSDelete,
    73  		},
    74  	})
    75  }
    76  
    77  var (
    78  	ignoreDNSFields = []string{
    79  		"ID",
    80  		"Class",
    81  		"SettingsHash",
    82  		"FQDN",
    83  		"CreatedAt",
    84  		"ModifiedAt",
    85  		"DNSNameServers",
    86  	}
    87  	createDNSParam = &iaas.DNSCreateRequest{
    88  		Name:        testutil.ResourceName("dns.com"),
    89  		Description: "desc",
    90  		Tags:        []string{"tag1", "tag2"},
    91  		Records: []*iaas.DNSRecord{
    92  			{
    93  				Name:  "host1",
    94  				Type:  types.DNSRecordTypes.A,
    95  				RData: "192.0.2.1",
    96  			},
    97  			{
    98  				Name:  "host2",
    99  				Type:  types.DNSRecordTypes.A,
   100  				RData: "192.0.2.2",
   101  			},
   102  		},
   103  	}
   104  	createDNSExpected = &iaas.DNS{
   105  		Name:         createDNSParam.Name,
   106  		Description:  createDNSParam.Description,
   107  		Tags:         createDNSParam.Tags,
   108  		Availability: types.Availabilities.Available,
   109  		DNSZone:      createDNSParam.Name,
   110  		Records:      createDNSParam.Records,
   111  	}
   112  	updateDNSSettingsParam = &iaas.DNSUpdateSettingsRequest{
   113  		Records: []*iaas.DNSRecord{
   114  			{
   115  				Name:  "host1",
   116  				Type:  types.DNSRecordTypes.A,
   117  				RData: "192.0.2.11",
   118  			},
   119  			{
   120  				Name:  "host2",
   121  				Type:  types.DNSRecordTypes.A,
   122  				RData: "192.0.2.12",
   123  			},
   124  			{
   125  				Name:  "host3",
   126  				Type:  types.DNSRecordTypes.A,
   127  				RData: "192.0.2.13",
   128  			},
   129  			{
   130  				Name:  "host4",
   131  				Type:  types.DNSRecordTypes.A,
   132  				RData: "192.0.2.14",
   133  			},
   134  		},
   135  	}
   136  	updateDNSSettingsExpected = &iaas.DNS{
   137  		Name:         createDNSParam.Name,
   138  		Description:  createDNSParam.Description,
   139  		Tags:         createDNSParam.Tags,
   140  		Availability: types.Availabilities.Available,
   141  		DNSZone:      createDNSParam.Name,
   142  		Records:      updateDNSSettingsParam.Records,
   143  	}
   144  	updateDNSParam = &iaas.DNSUpdateRequest{
   145  		Description: "desc-upd",
   146  		Tags:        []string{"tag1-upd", "tag2-upd"},
   147  		IconID:      testIconID,
   148  		Records: []*iaas.DNSRecord{
   149  			{
   150  				Name:  "host1",
   151  				Type:  types.DNSRecordTypes.A,
   152  				RData: "192.0.2.11",
   153  			},
   154  			{
   155  				Name:  "host2",
   156  				Type:  types.DNSRecordTypes.A,
   157  				RData: "192.0.2.12",
   158  			},
   159  			{
   160  				Name:  "host3",
   161  				Type:  types.DNSRecordTypes.A,
   162  				RData: "192.0.2.13",
   163  			},
   164  		},
   165  	}
   166  	updateDNSExpected = &iaas.DNS{
   167  		Name:         createDNSParam.Name,
   168  		Description:  updateDNSParam.Description,
   169  		IconID:       testIconID,
   170  		Tags:         updateDNSParam.Tags,
   171  		Availability: types.Availabilities.Available,
   172  		DNSZone:      createDNSParam.Name,
   173  		Records:      updateDNSParam.Records,
   174  	}
   175  	updateDNSToMinParam    = &iaas.DNSUpdateRequest{}
   176  	updateDNSToMinExpected = &iaas.DNS{
   177  		Name:         createDNSParam.Name,
   178  		Availability: types.Availabilities.Available,
   179  		DNSZone:      createDNSParam.Name,
   180  	}
   181  )
   182  
   183  func testDNSCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   184  	client := iaas.NewDNSOp(caller)
   185  	return client.Create(ctx, createDNSParam)
   186  }
   187  
   188  func testDNSRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   189  	client := iaas.NewDNSOp(caller)
   190  	return client.Read(ctx, ctx.ID)
   191  }
   192  
   193  func testDNSUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   194  	client := iaas.NewDNSOp(caller)
   195  	return client.Update(ctx, ctx.ID, updateDNSParam)
   196  }
   197  
   198  func testDNSUpdateSettings(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   199  	client := iaas.NewDNSOp(caller)
   200  	return client.UpdateSettings(ctx, ctx.ID, updateDNSSettingsParam)
   201  }
   202  
   203  func testDNSUpdateToMin(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   204  	client := iaas.NewDNSOp(caller)
   205  	return client.Update(ctx, ctx.ID, updateDNSToMinParam)
   206  }
   207  
   208  func testDNSDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
   209  	client := iaas.NewDNSOp(caller)
   210  	return client.Delete(ctx, ctx.ID)
   211  }