-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathmain_test.go
More file actions
111 lines (92 loc) · 2.62 KB
/
main_test.go
File metadata and controls
111 lines (92 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package e2e
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
"testing"
"github.com/shellhub-io/shellhub/tests/environment"
log "github.com/sirupsen/logrus"
)
// keygen generates private and public keys required to startup a ShellHub instance.
func keygen() error {
const PrivateKeyPermission uint = 0o600
const PublicKeyPermission uint = 0o644
const APIPrivatKeyPath string = "../api_private_key"
const APIPublicKeyPath string = "../api_public_key"
const SSHPrivateKey string = "../ssh_private_key"
if _, err := os.Stat(SSHPrivateKey); os.IsNotExist(err) {
sshPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
bytesSSHPrivateKey, err := x509.MarshalPKCS8PrivateKey(sshPrivateKey)
if err != nil {
return err
}
if err := os.WriteFile(SSHPrivateKey, pem.EncodeToMemory(
&pem.Block{
Type: "PRIVATE KEY",
Bytes: bytesSSHPrivateKey,
},
), os.FileMode(PrivateKeyPermission)); err != nil {
return err
}
}
if _, err := os.Stat(APIPrivatKeyPath); os.IsNotExist(err) {
apiPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
bytesAPIPrivateKey, err := x509.MarshalPKCS8PrivateKey(apiPrivateKey)
if err != nil {
return err
}
if err := os.WriteFile(APIPrivatKeyPath, pem.EncodeToMemory(
&pem.Block{
Type: "PRIVATE KEY",
Bytes: bytesAPIPrivateKey,
},
), os.FileMode(PrivateKeyPermission)); err != nil {
return err
}
bytesAPIPublicKey, err := x509.MarshalPKIXPublicKey(&apiPrivateKey.PublicKey)
if err != nil {
return err
}
if err := os.WriteFile(APIPublicKeyPath, pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: bytesAPIPublicKey,
},
), os.FileMode(PublicKeyPermission)); err != nil {
return err
}
}
return nil
}
func TestMain(m *testing.M) {
// INFO: Due to issue related on testcontainers-go, we are disabling Ryuk as a temporary solution.
// We implement our own cleanup mechanism in environment/cleanup.go to handle resource cleanup.
//
// https://github.com/testcontainers/testcontainers-go/issues/2445
if environment.ShouldDisableRyuk() {
os.Setenv("TESTCONTAINERS_RYUK_DISABLED", "true")
}
// Initialize cleanup system
environment.InitCleanup()
if err := keygen(); err != nil {
log.WithError(err).Error("failed to generate the ShellHub keys")
os.Exit(1)
}
// Run tests
exitCode := m.Run()
// Cleanup any orphaned containers
ctx := context.Background()
if err := environment.CleanupOrphanedContainers(ctx); err != nil {
log.WithError(err).Warn("failed to cleanup orphaned containers")
}
os.Exit(exitCode)
}