github.com/sacloud/iaas-api-go@v1.12.0/test/ssh_key_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  )
    23  
    24  func TestSSHKeyOpCRUD(t *testing.T) {
    25  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    26  		Parallel:           true,
    27  		IgnoreStartupWait:  true,
    28  		SetupAPICallerFunc: singletonAPICaller,
    29  		Create: &testutil.CRUDTestFunc{
    30  			Func: testSSHKeyCreate,
    31  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    32  				ExpectValue:  createSSHKeyExpected,
    33  				IgnoreFields: ignoreSSHKeyFields,
    34  			}),
    35  		},
    36  		Read: &testutil.CRUDTestFunc{
    37  			Func: testSSHKeyRead,
    38  			CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    39  				ExpectValue:  createSSHKeyExpected,
    40  				IgnoreFields: ignoreSSHKeyFields,
    41  			}),
    42  		},
    43  		Updates: []*testutil.CRUDTestFunc{
    44  			{
    45  				Func: testSSHKeyUpdate,
    46  				CheckFunc: testutil.AssertEqualWithExpected(&testutil.CRUDTestExpect{
    47  					ExpectValue:  updateSSHKeyExpected,
    48  					IgnoreFields: ignoreSSHKeyFields,
    49  				}),
    50  			},
    51  		},
    52  		Delete: &testutil.CRUDTestDeleteFunc{
    53  			Func: testSSHKeyDelete,
    54  		},
    55  	})
    56  }
    57  
    58  func TestSSHKeyOp_Generate(t *testing.T) {
    59  	testutil.RunCRUD(t, &testutil.CRUDTestCase{
    60  		Parallel:           true,
    61  		IgnoreStartupWait:  true,
    62  		SetupAPICallerFunc: singletonAPICaller,
    63  		Create: &testutil.CRUDTestFunc{
    64  			Func: func(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
    65  				client := iaas.NewSSHKeyOp(caller)
    66  				return client.Generate(ctx, &iaas.SSHKeyGenerateRequest{
    67  					Name:        testutil.ResourceName("sshkey-generate"),
    68  					Description: "libsacloud-sshKey-generate",
    69  					PassPhrase:  "libsacloud-sshKey-passphrase",
    70  				})
    71  			},
    72  			CheckFunc: func(t testutil.TestT, ctx *testutil.CRUDTestContext, v interface{}) error {
    73  				sshKey := v.(*iaas.SSHKeyGenerated)
    74  				return testutil.DoAsserts(
    75  					testutil.AssertNotNilFunc(t, sshKey, "SSHKeyGenerated"),
    76  					testutil.AssertNotEmptyFunc(t, sshKey.PublicKey, "SSHKeyGenerated.PublicKey"),
    77  					testutil.AssertNotEmptyFunc(t, sshKey.PrivateKey, "SSHKeyGenerated.PrivateKey"),
    78  					testutil.AssertNotEmptyFunc(t, sshKey.Fingerprint, "SSHKeyGenerated.Fingerprint"),
    79  				)
    80  			},
    81  		},
    82  		Read: &testutil.CRUDTestFunc{
    83  			Func: testSSHKeyRead,
    84  		},
    85  		Delete: &testutil.CRUDTestDeleteFunc{
    86  			Func: testSSHKeyDelete,
    87  		},
    88  	})
    89  }
    90  
    91  var (
    92  	fakePublicKey   = `ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAs7YFtxjGrI49MCBnSFbUPxqz0e5HSGQPnLlPJ0u/9w4WLpoOZYmoQDTMfuFA61qv+0dp5mpMZPj3f5YEGlwUFKPy3Cmrp0ub1nYDb7n62s+Xf68TNvbVgQMLF0xdOaWxdRsQwmH8lOWan1Ubc8iwfOa3TNGwOzGLMjdW3PiJ7hcE7nFqnmbQUabHWow8G6JYDHKyjAdpz+edK8u+LY0iEP8M8VAjRJKJVg4p1/oDjHFKI0qjfjitKzoLm5FGaFv8afH2WQSpu/2To7d/RaLhfoMZsUReLSxeDnQkKGERXrAywTHnFu60cOaT3EvaAhP1H3BPj2LESm8M4ja9FaARnQ== `
    93  	fakeFingerprint = "79:d7:ac:b8:cf:cf:01:44:b2:19:ba:d4:82:fd:c4:2d"
    94  
    95  	ignoreSSHKeyFields = []string{
    96  		"ID",
    97  		"CreatedAt",
    98  	}
    99  	createSSHKeyParam = &iaas.SSHKeyCreateRequest{
   100  		Name:        testutil.ResourceName("sshkey"),
   101  		Description: "libsacloud-sshKey",
   102  		PublicKey:   fakePublicKey,
   103  	}
   104  	createSSHKeyExpected = &iaas.SSHKey{
   105  		Name:        createSSHKeyParam.Name,
   106  		Description: createSSHKeyParam.Description,
   107  		PublicKey:   fakePublicKey,
   108  		Fingerprint: fakeFingerprint,
   109  	}
   110  	updateSSHKeyParam = &iaas.SSHKeyUpdateRequest{
   111  		Name:        testutil.ResourceName("sshkey-upd"),
   112  		Description: "libsacloud-sshKey-upd",
   113  	}
   114  	updateSSHKeyExpected = &iaas.SSHKey{
   115  		Name:        updateSSHKeyParam.Name,
   116  		Description: updateSSHKeyParam.Description,
   117  		PublicKey:   fakePublicKey,
   118  		Fingerprint: fakeFingerprint,
   119  	}
   120  )
   121  
   122  func testSSHKeyCreate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   123  	client := iaas.NewSSHKeyOp(caller)
   124  	return client.Create(ctx, createSSHKeyParam)
   125  }
   126  
   127  func testSSHKeyRead(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   128  	client := iaas.NewSSHKeyOp(caller)
   129  	return client.Read(ctx, ctx.ID)
   130  }
   131  
   132  func testSSHKeyUpdate(ctx *testutil.CRUDTestContext, caller iaas.APICaller) (interface{}, error) {
   133  	client := iaas.NewSSHKeyOp(caller)
   134  	return client.Update(ctx, ctx.ID, updateSSHKeyParam)
   135  }
   136  
   137  func testSSHKeyDelete(ctx *testutil.CRUDTestContext, caller iaas.APICaller) error {
   138  	client := iaas.NewSSHKeyOp(caller)
   139  	return client.Delete(ctx, ctx.ID)
   140  }