github.com/sacloud/iaas-api-go@v1.12.0/test/esme_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  	"os"
    19  	"testing"
    20  
    21  	"github.com/sacloud/iaas-api-go"
    22  	"github.com/sacloud/iaas-api-go/testutil"
    23  	"github.com/sacloud/iaas-api-go/types"
    24  )
    25  
    26  func TestESMEOpCRUD(t *testing.T) {
    27  	testutil.PreCheckEnvsFunc("SAKURACLOUD_ESME_DESTINATION")(t)
    28  
    29  	destination := os.Getenv("SAKURACLOUD_ESME_DESTINATION")
    30  
    31  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    32  		Parallel: true,
    33  
    34  		SetupAPICallerFunc: singletonAPICaller,
    35  
    36  		Create: &testutil.CRUDTestFunc{
    37  			Func: testESMECreate,
    38  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    39  				ExpectValue:  createESMEExpected,
    40  				IgnoreFields: ignoreESMEFields,
    41  			}),
    42  		},
    43  
    44  		Read: &testutil.CRUDTestFunc{
    45  			Func: testESMERead,
    46  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    47  				ExpectValue:  createESMEExpected,
    48  				IgnoreFields: ignoreESMEFields,
    49  			}),
    50  		},
    51  
    52  		Updates: []*testutil.CRUDTestFunc{
    53  			{
    54  				Func: testESMEUpdate,
    55  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    56  					ExpectValue:  updateESMEExpected,
    57  					IgnoreFields: ignoreESMEFields,
    58  				}),
    59  			},
    60  			{
    61  				Func: testESMEUpdateToMin,
    62  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    63  					ExpectValue:  updateESMEToMinExpected,
    64  					IgnoreFields: ignoreESMEFields,
    65  				}),
    66  			},
    67  			// send SMS
    68  			{
    69  				Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
    70  					client := iaas.NewESMEOp(caller)
    71  					_, err := client.SendMessageWithGeneratedOTP(ctx, ctx.ID, &iaas.ESMESendMessageWithGeneratedOTPRequest{
    72  						Destination: destination,
    73  						Sender:      "libsacloud-test",
    74  						DomainName:  "www.example.com",
    75  					})
    76  					return nil, err
    77  				},
    78  			},
    79  			{
    80  				Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
    81  					client := iaas.NewESMEOp(caller)
    82  					logs, err := client.Logs(ctx, ctx.ID)
    83  					if err != nil {
    84  						return nil, err
    85  					}
    86  					return nil, testutil.DoAsserts(
    87  						testutil.AssertLenFunc(t, logs, 1, "Logs"),
    88  					)
    89  				},
    90  			},
    91  			{
    92  				Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
    93  					client := iaas.NewESMEOp(caller)
    94  					_, err := client.SendMessageWithInputtedOTP(ctx, ctx.ID, &iaas.ESMESendMessageWithInputtedOTPRequest{
    95  						Destination: destination,
    96  						Sender:      "libsacloud-test",
    97  						DomainName:  "www.example.com",
    98  						OTP:         "397397",
    99  					})
   100  					return nil, err
   101  				},
   102  			},
   103  			{
   104  				Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   105  					client := iaas.NewESMEOp(caller)
   106  					logs, err := client.Logs(ctx, ctx.ID)
   107  					if err != nil {
   108  						return nil, err
   109  					}
   110  					return nil, testutil.DoAsserts(
   111  						testutil.AssertLenFunc(t, logs, 2, "Logs"),
   112  					)
   113  				},
   114  			},
   115  		},
   116  
   117  		Delete: &testutil.CRUDTestDeleteFunc{
   118  			Func: testESMEDelete,
   119  		},
   120  	})
   121  }
   122  
   123  var (
   124  	ignoreESMEFields = []string{
   125  		"ID",
   126  		"Class",
   127  		"Settings",
   128  		"SettingsHash",
   129  		"CreatedAt",
   130  		"ModifiedAt",
   131  	}
   132  	createESMEParam = &iaas.ESMECreateRequest{
   133  		Name:        testutil.ResourceName("esme"),
   134  		Description: "desc",
   135  		Tags:        []string{"tag1", "tag2"},
   136  	}
   137  	createESMEExpected = &iaas.ESME{
   138  		Name:         createESMEParam.Name,
   139  		Description:  createESMEParam.Description,
   140  		Tags:         createESMEParam.Tags,
   141  		Availability: types.Availabilities.Available,
   142  	}
   143  	updateESMEParam = &iaas.ESMEUpdateRequest{
   144  		Name:        testutil.ResourceName("esme-upd"),
   145  		Description: "desc-upd",
   146  		Tags:        []string{"tag1-upd", "tag2-upd"},
   147  		IconID:      testIconID,
   148  	}
   149  	updateESMEExpected = &iaas.ESME{
   150  		Name:         updateESMEParam.Name,
   151  		Description:  updateESMEParam.Description,
   152  		Tags:         updateESMEParam.Tags,
   153  		Availability: types.Availabilities.Available,
   154  		IconID:       testIconID,
   155  	}
   156  
   157  	updateESMEToMinParam = &iaas.ESMEUpdateRequest{
   158  		Name: testutil.ResourceName("esme-to-min"),
   159  	}
   160  	updateESMEToMinExpected = &iaas.ESME{
   161  		Name:         updateESMEToMinParam.Name,
   162  		Availability: types.Availabilities.Available,
   163  	}
   164  )
   165  
   166  func testESMECreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   167  	client := iaas.NewESMEOp(caller)
   168  	return client.Create(ctx, createESMEParam)
   169  }
   170  
   171  func testESMERead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   172  	client := iaas.NewESMEOp(caller)
   173  	return client.Read(ctx, ctx.ID)
   174  }
   175  
   176  func testESMEUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   177  	client := iaas.NewESMEOp(caller)
   178  	return client.Update(ctx, ctx.ID, updateESMEParam)
   179  }
   180  
   181  func testESMEUpdateToMin(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   182  	client := iaas.NewESMEOp(caller)
   183  	return client.Update(ctx, ctx.ID, updateESMEToMinParam)
   184  }
   185  
   186  func testESMEDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
   187  	client := iaas.NewESMEOp(caller)
   188  	return client.Delete(ctx, ctx.ID)
   189  }