From 2a9e9b018614cd12c3ea8a26ec9694c23a456ad7 Mon Sep 17 00:00:00 2001 From: Jeremy THERIN Date: Tue, 21 Oct 2025 12:03:36 +0200 Subject: [PATCH] feat(iam): add ssh file import test --- .../namespaces/iam/v1alpha1/custom_test.go | 44 ++++++++++- ...y-create-command-file-import.cassette.yaml | 77 +++++++++++++++++++ ...-ssh-key-create-command-file-import.golden | 23 ++++++ 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.cassette.yaml create mode 100644 internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.golden diff --git a/internal/namespaces/iam/v1alpha1/custom_test.go b/internal/namespaces/iam/v1alpha1/custom_test.go index 6dd9e11c7f..4baf96f63c 100644 --- a/internal/namespaces/iam/v1alpha1/custom_test.go +++ b/internal/namespaces/iam/v1alpha1/custom_test.go @@ -9,6 +9,8 @@ import ( iam "github.com/scaleway/scaleway-cli/v2/internal/namespaces/iam/v1alpha1" "github.com/scaleway/scaleway-cli/v2/internal/testhelpers" iamsdk "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func Test_initWithSSHKeyCommand(t *testing.T) { @@ -43,11 +45,11 @@ func Test_initWithSSHKeyCommand(t *testing.T) { } func Test_SSHKeyCreateCommand(t *testing.T) { - key := `ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBieay3nO9wViPkuvFVgGGaA1IRlkFrr946yqvg9LxZIRhsnZ61yLCPmIOhvUAZ/gTxZGmhgtMDxkenSUTsG3F0= foobar@foobar` + key1 := `ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBieay3nO9wViPkuvFVgGGaA1IRlkFrr946yqvg9LxZIRhsnZ61yLCPmIOhvUAZ/gTxZGmhgtMDxkenSUTsG3F0= foobar@foobar` t.Run("simple", core.Test(&core.TestConfig{ Commands: iam.GetCommands(), Args: []string{ - "scw", "iam", "ssh-key", "create", "name=foobar", "public-key=" + key, + "scw", "iam", "ssh-key", "create", "name=foobar", "public-key=" + key1, }, Check: core.TestCheckCombine( core.TestCheckGolden(), @@ -62,6 +64,44 @@ func Test_SSHKeyCreateCommand(t *testing.T) { }) }, })) + + //// + // File import will test the @/path/file import functionality + //// + key2 := "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo= foobar@foobar2" + t.Run("file_import", func(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "ssh.pub") + require.NoError(t, err) + assert.NotNil(t, f) + defer os.Remove(f.Name()) // clean up + + _, err = f.WriteString(key2) + require.NoError(t, err) + t.Logf("public key written on %s (will be deleted at the end of the test)", f.Name()) + + err = f.Close() + require.NoError(t, err) + + testConfig := &core.TestConfig{ + Commands: iam.GetCommands(), + Args: []string{ + "scw", "iam", "ssh-key", "create", "name=foobar2", "public-key=@" + f.Name(), + }, + Check: core.TestCheckCombine( + core.TestCheckGolden(), + core.TestCheckExitCode(0), + ), + AfterFunc: func(ctx *core.AfterFuncCtx) error { + api := iamsdk.NewAPI(ctx.Client) + key := testhelpers.Value[*iamsdk.SSHKey](t, ctx.CmdResult) + + return api.DeleteSSHKey(&iamsdk.DeleteSSHKeyRequest{ + SSHKeyID: key.ID, + }) + }, + } + core.Test(testConfig)(t) + }) } func Test_SSHKeyRemoveCommand(t *testing.T) { diff --git a/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.cassette.yaml b/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.cassette.yaml new file mode 100644 index 0000000000..46540bdc73 --- /dev/null +++ b/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.cassette.yaml @@ -0,0 +1,77 @@ +--- +version: 1 +interactions: +- request: + body: '{"id":"35d4a543-ba4f-46b8-94a1-3e7d1caeccb9", "name":"foobar2", "public_key":"ecdsa-sha2-nistp256 + AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=", + "fingerprint":"256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)", + "created_at":"2025-10-21T10:06:35.771313Z", "updated_at":"2025-10-21T10:06:35.771313Z", + "organization_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", "project_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", + "disabled":false}' + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.0; linux; amd64) cli-e2e-test + url: https://api.scaleway.com/iam/v1alpha1/ssh-keys + method: POST + response: + body: '{"id":"35d4a543-ba4f-46b8-94a1-3e7d1caeccb9", "name":"foobar2", "public_key":"ecdsa-sha2-nistp256 + AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=", + "fingerprint":"256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)", + "created_at":"2025-10-21T10:06:35.771313Z", "updated_at":"2025-10-21T10:06:35.771313Z", + "organization_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", "project_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", + "disabled":false}' + headers: + Content-Length: + - "552" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Tue, 21 Oct 2025 10:06:35 GMT + Server: + - Scaleway API Gateway (fr-par-1;edge02) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 686dc9c2-3fe0-42ca-92fa-9a3ff0b60e1e + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.0; linux; amd64) cli-e2e-test + url: https://api.scaleway.com/iam/v1alpha1/ssh-keys/35d4a543-ba4f-46b8-94a1-3e7d1caeccb9 + method: DELETE + response: + body: "" + headers: + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Tue, 21 Oct 2025 10:06:35 GMT + Server: + - Scaleway API Gateway (fr-par-1;edge02) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - bc788029-3879-4eff-a462-68b6adbddcb7 + status: 204 No Content + code: 204 + duration: "" diff --git a/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.golden b/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.golden new file mode 100644 index 0000000000..d7ff8ce8fa --- /dev/null +++ b/internal/namespaces/iam/v1alpha1/testdata/test-ssh-key-create-command-file-import.golden @@ -0,0 +1,23 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +🟩🟩🟩 STDOUT️ 🟩🟩🟩️ +ID 35d4a543-ba4f-46b8-94a1-3e7d1caeccb9 +Name foobar2 +PublicKey ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo= +Fingerprint 256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256) +CreatedAt few seconds ago +UpdatedAt few seconds ago +OrganizationID 9e9389ca-503a-44c3-bbd5-7a3812303bb0 +ProjectID 9e9389ca-503a-44c3-bbd5-7a3812303bb0 +Disabled false +🟩🟩🟩 JSON STDOUT 🟩🟩🟩 +{ + "id": "35d4a543-ba4f-46b8-94a1-3e7d1caeccb9", + "name": "foobar2", + "public_key": "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=", + "fingerprint": "256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)", + "created_at": "1970-01-01T00:00:00.0Z", + "updated_at": "1970-01-01T00:00:00.0Z", + "organization_id": "9e9389ca-503a-44c3-bbd5-7a3812303bb0", + "project_id": "9e9389ca-503a-44c3-bbd5-7a3812303bb0", + "disabled": false +}