Skip to content

Commit ae5aa55

Browse files
authored
test(iam): add ssh file import test (scaleway#5064)
1 parent 21df90e commit ae5aa55

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

internal/namespaces/iam/v1alpha1/custom_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
iam "github.com/scaleway/scaleway-cli/v2/internal/namespaces/iam/v1alpha1"
1010
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
1111
iamsdk "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
)
1315

1416
func Test_initWithSSHKeyCommand(t *testing.T) {
@@ -43,11 +45,11 @@ func Test_initWithSSHKeyCommand(t *testing.T) {
4345
}
4446

4547
func Test_SSHKeyCreateCommand(t *testing.T) {
46-
key := `ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBieay3nO9wViPkuvFVgGGaA1IRlkFrr946yqvg9LxZIRhsnZ61yLCPmIOhvUAZ/gTxZGmhgtMDxkenSUTsG3F0= foobar@foobar`
48+
key1 := `ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBieay3nO9wViPkuvFVgGGaA1IRlkFrr946yqvg9LxZIRhsnZ61yLCPmIOhvUAZ/gTxZGmhgtMDxkenSUTsG3F0= foobar@foobar`
4749
t.Run("simple", core.Test(&core.TestConfig{
4850
Commands: iam.GetCommands(),
4951
Args: []string{
50-
"scw", "iam", "ssh-key", "create", "name=foobar", "public-key=" + key,
52+
"scw", "iam", "ssh-key", "create", "name=foobar", "public-key=" + key1,
5153
},
5254
Check: core.TestCheckCombine(
5355
core.TestCheckGolden(),
@@ -62,6 +64,44 @@ func Test_SSHKeyCreateCommand(t *testing.T) {
6264
})
6365
},
6466
}))
67+
68+
////
69+
// File import will test the @/path/file import functionality
70+
////
71+
key2 := "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo= foobar@foobar2"
72+
t.Run("file_import", func(t *testing.T) {
73+
f, err := os.CreateTemp(t.TempDir(), "ssh.pub")
74+
require.NoError(t, err)
75+
assert.NotNil(t, f)
76+
defer os.Remove(f.Name()) // clean up
77+
78+
_, err = f.WriteString(key2)
79+
require.NoError(t, err)
80+
t.Logf("public key written on %s (will be deleted at the end of the test)", f.Name())
81+
82+
err = f.Close()
83+
require.NoError(t, err)
84+
85+
testConfig := &core.TestConfig{
86+
Commands: iam.GetCommands(),
87+
Args: []string{
88+
"scw", "iam", "ssh-key", "create", "name=foobar2", "public-key=@" + f.Name(),
89+
},
90+
Check: core.TestCheckCombine(
91+
core.TestCheckGolden(),
92+
core.TestCheckExitCode(0),
93+
),
94+
AfterFunc: func(ctx *core.AfterFuncCtx) error {
95+
api := iamsdk.NewAPI(ctx.Client)
96+
key := testhelpers.Value[*iamsdk.SSHKey](t, ctx.CmdResult)
97+
98+
return api.DeleteSSHKey(&iamsdk.DeleteSSHKeyRequest{
99+
SSHKeyID: key.ID,
100+
})
101+
},
102+
}
103+
core.Test(testConfig)(t)
104+
})
65105
}
66106

67107
func Test_SSHKeyRemoveCommand(t *testing.T) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"id":"35d4a543-ba4f-46b8-94a1-3e7d1caeccb9", "name":"foobar2", "public_key":"ecdsa-sha2-nistp256
6+
AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=",
7+
"fingerprint":"256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)",
8+
"created_at":"2025-10-21T10:06:35.771313Z", "updated_at":"2025-10-21T10:06:35.771313Z",
9+
"organization_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", "project_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0",
10+
"disabled":false}'
11+
form: {}
12+
headers:
13+
Content-Type:
14+
- application/json
15+
User-Agent:
16+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.0; linux; amd64) cli-e2e-test
17+
url: https://api.scaleway.com/iam/v1alpha1/ssh-keys
18+
method: POST
19+
response:
20+
body: '{"id":"35d4a543-ba4f-46b8-94a1-3e7d1caeccb9", "name":"foobar2", "public_key":"ecdsa-sha2-nistp256
21+
AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=",
22+
"fingerprint":"256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)",
23+
"created_at":"2025-10-21T10:06:35.771313Z", "updated_at":"2025-10-21T10:06:35.771313Z",
24+
"organization_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0", "project_id":"9e9389ca-503a-44c3-bbd5-7a3812303bb0",
25+
"disabled":false}'
26+
headers:
27+
Content-Length:
28+
- "552"
29+
Content-Security-Policy:
30+
- default-src 'none'; frame-ancestors 'none'
31+
Content-Type:
32+
- application/json
33+
Date:
34+
- Tue, 21 Oct 2025 10:06:35 GMT
35+
Server:
36+
- Scaleway API Gateway (fr-par-1;edge02)
37+
Strict-Transport-Security:
38+
- max-age=63072000
39+
X-Content-Type-Options:
40+
- nosniff
41+
X-Frame-Options:
42+
- DENY
43+
X-Request-Id:
44+
- 686dc9c2-3fe0-42ca-92fa-9a3ff0b60e1e
45+
status: 200 OK
46+
code: 200
47+
duration: ""
48+
- request:
49+
body: ""
50+
form: {}
51+
headers:
52+
User-Agent:
53+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.25.0; linux; amd64) cli-e2e-test
54+
url: https://api.scaleway.com/iam/v1alpha1/ssh-keys/35d4a543-ba4f-46b8-94a1-3e7d1caeccb9
55+
method: DELETE
56+
response:
57+
body: ""
58+
headers:
59+
Content-Security-Policy:
60+
- default-src 'none'; frame-ancestors 'none'
61+
Content-Type:
62+
- application/json
63+
Date:
64+
- Tue, 21 Oct 2025 10:06:35 GMT
65+
Server:
66+
- Scaleway API Gateway (fr-par-1;edge02)
67+
Strict-Transport-Security:
68+
- max-age=63072000
69+
X-Content-Type-Options:
70+
- nosniff
71+
X-Frame-Options:
72+
- DENY
73+
X-Request-Id:
74+
- bc788029-3879-4eff-a462-68b6adbddcb7
75+
status: 204 No Content
76+
code: 204
77+
duration: ""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
ID 35d4a543-ba4f-46b8-94a1-3e7d1caeccb9
4+
Name foobar2
5+
PublicKey ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=
6+
Fingerprint 256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)
7+
CreatedAt few seconds ago
8+
UpdatedAt few seconds ago
9+
OrganizationID 9e9389ca-503a-44c3-bbd5-7a3812303bb0
10+
ProjectID 9e9389ca-503a-44c3-bbd5-7a3812303bb0
11+
Disabled false
12+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
13+
{
14+
"id": "35d4a543-ba4f-46b8-94a1-3e7d1caeccb9",
15+
"name": "foobar2",
16+
"public_key": "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIHoWVcE8ItB7WPbmanQY/GhMWqU2XKQthfoAv51IE4OAgnnMWo7RHf1grLdiRZ4MxIwW2SaDBGIDminJCw1OMo=",
17+
"fingerprint": "256 MD5:a5:b1:5a:ff:d2:71:ed:53:d7:8c:fb:b0:ec:12:10:de (ecdsa-sha2-nistp256)",
18+
"created_at": "1970-01-01T00:00:00.0Z",
19+
"updated_at": "1970-01-01T00:00:00.0Z",
20+
"organization_id": "9e9389ca-503a-44c3-bbd5-7a3812303bb0",
21+
"project_id": "9e9389ca-503a-44c3-bbd5-7a3812303bb0",
22+
"disabled": false
23+
}

0 commit comments

Comments
 (0)