Skip to content

Commit 4d46112

Browse files
committed
Integration testing with testcontainers
1 parent f7a03c4 commit 4d46112

File tree

11 files changed

+2286
-0
lines changed

11 files changed

+2286
-0
lines changed

testcontainers/atmoz.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package testcontainers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
"github.com/testcontainers/testcontainers-go"
10+
"github.com/testcontainers/testcontainers-go/wait"
11+
"golang.org/x/crypto/ssh"
12+
13+
"github.com/c2fo/vfs/v7/backend"
14+
"github.com/c2fo/vfs/v7/backend/sftp"
15+
)
16+
17+
const (
18+
atmozPort = "22/tcp"
19+
atmozUsername = "dummy"
20+
atmozPassword = "dummy"
21+
)
22+
23+
func registerAtmoz(t *testing.T) string {
24+
ctx := context.Background()
25+
is := require.New(t)
26+
27+
req := testcontainers.GenericContainerRequest{
28+
ContainerRequest: testcontainers.ContainerRequest{
29+
Name: "vfs-atmoz-sftp",
30+
Image: "atmoz/sftp:alpine",
31+
Env: map[string]string{"SFTP_USERS": fmt.Sprintf("%s:%s:::upload", atmozUsername, atmozPassword)},
32+
WaitingFor: wait.ForListeningPort(atmozPort),
33+
},
34+
Started: true,
35+
}
36+
ctr, err := testcontainers.GenericContainer(ctx, req)
37+
testcontainers.CleanupContainer(t, ctr)
38+
is.NoError(err)
39+
40+
host, err := ctr.Host(ctx)
41+
is.NoError(err)
42+
43+
port, err := ctr.MappedPort(ctx, atmozPort)
44+
is.NoError(err)
45+
46+
authority := fmt.Sprintf("sftp://%s@%s:%s/upload/", atmozUsername, host, port.Port())
47+
backend.Register(authority, sftp.NewFileSystem(sftp.WithOptions(sftp.Options{
48+
Password: vsftpdPassword,
49+
KnownHostsCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec
50+
})))
51+
return authority
52+
}

testcontainers/azurite.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package testcontainers
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"testing"
7+
8+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
9+
"github.com/stretchr/testify/require"
10+
"github.com/testcontainers/testcontainers-go"
11+
"github.com/testcontainers/testcontainers-go/modules/azure/azurite"
12+
13+
"github.com/c2fo/vfs/v7/backend"
14+
"github.com/c2fo/vfs/v7/backend/azure"
15+
)
16+
17+
func registerAzurite(t *testing.T) string {
18+
ctx := context.Background()
19+
is := require.New(t)
20+
21+
ctr, err := azurite.Run(ctx, "mcr.microsoft.com/azure-storage/azurite:latest", testcontainers.WithName("vfs-azurite"))
22+
testcontainers.CleanupContainer(t, ctr)
23+
is.NoError(err)
24+
25+
ep, err := ctr.BlobServiceURL(ctx)
26+
is.NoError(err)
27+
28+
cred, err := azblob.NewSharedKeyCredential(azurite.AccountName, azurite.AccountKey)
29+
is.NoError(err)
30+
31+
u, err := url.JoinPath(ep, azurite.AccountName)
32+
is.NoError(err)
33+
34+
cli, err := azblob.NewClientWithSharedKeyCredential(u, cred, nil)
35+
is.NoError(err)
36+
37+
_, err = cli.CreateContainer(ctx, "azurite", nil)
38+
is.NoError(err)
39+
40+
c, err := azure.NewClient(&azure.Options{
41+
ServiceURL: u,
42+
AccountName: azurite.AccountName,
43+
AccountKey: azurite.AccountKey,
44+
})
45+
is.NoError(err)
46+
47+
backend.Register("https://azurite/", azure.NewFileSystem(azure.WithClient(c)))
48+
return "https://azurite/"
49+
}

0 commit comments

Comments
 (0)