-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_test.go
108 lines (93 loc) · 4.13 KB
/
api_test.go
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
//nolint:forbidigo
package api
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/keboola/keboola-as-code/internal/pkg/env"
"github.com/keboola/keboola-as-code/internal/pkg/filesystem"
volume "github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/local/volume/model"
"github.com/keboola/keboola-as-code/internal/pkg/utils/etcdhelper"
"github.com/keboola/keboola-as-code/internal/pkg/utils/netutils"
"github.com/keboola/keboola-as-code/internal/pkg/utils/testhelper"
"github.com/keboola/keboola-as-code/internal/pkg/utils/testhelper/runner"
)
// TestStreamApiE2E runs one Stream API functional test per each subdirectory.
func TestStreamApiE2E(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("Skipping API E2E tests on Windows")
}
binaryPath := testhelper.CompileBinary(t, "stream-service", "build-stream-service")
ctx := t.Context()
runner.
NewRunner(t).
ForEachTest(func(test *runner.Test) {
// Get default branch
defaultBranch, err := test.TestProject().DefaultBranch()
require.NoError(t, err)
test.TestProject().Env().Set(`TEST_DEFAULT_BRANCH_ID`, defaultBranch.ID.String())
// Connect to the etcd
etcdCfg := etcdhelper.TmpNamespaceFromEnv(t, "STREAM_ETCD_")
etcdClient := etcdhelper.ClientForTest(t, etcdCfg)
// Init etcd state
etcdStateFile := "initial-etcd-kvs.txt"
if test.TestDirFS().IsFile(ctx, etcdStateFile) {
etcdStateFileContentStr := test.ReadFileFromTestDir(etcdStateFile)
err := etcdhelper.PutAllFromSnapshot(t.Context(), etcdClient, etcdStateFileContentStr)
assert.NoError(test.T(), err)
}
// Simulate an empty volume with fixed volumeID
volumesPath := t.TempDir()
volumePath := filepath.Join(volumesPath, "hdd", "my-volume")
require.NoError(t, os.MkdirAll(volumePath, 0o700))
require.NoError(t, os.WriteFile(filepath.Join(volumePath, volume.IDFile), []byte("my-volume"), 0o600))
// Use aes encryption for tests
secretKey := make([]byte, 32)
_, err = rand.Read(secretKey)
require.NoError(t, err)
addEnvs := env.FromMap(map[string]string{
"STREAM_DATADOG_ENABLED": "false",
"STREAM_NODE_ID": "test-node",
"STREAM_HOSTNAME": "test-node.localhost",
"STREAM_STORAGE_API_HOST": test.TestProject().StorageAPIHost(),
"STREAM_STORAGE_VOLUMES_PATH": volumesPath,
"STREAM_STORAGE_LEVEL_LOCAL_WRITER_NETWORK_LISTEN": fmt.Sprintf("0.0.0.0:%d", netutils.FreePortForTest(t)),
"STREAM_API_PUBLIC_URL": "https://stream.keboola.local",
"STREAM_SOURCE_HTTP_PUBLIC_URL": "https://stream-in.keboola.local",
"STREAM_ETCD_NAMESPACE": etcdCfg.Namespace,
"STREAM_ETCD_ENDPOINT": etcdCfg.Endpoint,
"STREAM_ETCD_USERNAME": etcdCfg.Username,
"STREAM_ETCD_PASSWORD": etcdCfg.Password,
"STREAM_ENCRYPTION_PROVIDER": "aes",
"STREAM_ENCRYPTION_AES_SECRET_KEY": base64.StdEncoding.EncodeToString(secretKey),
})
// Run the test
test.Run(
runner.WithInitProjectState(),
runner.WithRunAPIServerAndRequests(
binaryPath,
[]string{"api", "storage-writer"}, // start api component and storage-writer to detect the volume
addEnvs,
nil,
),
runner.WithAssertProjectState(),
)
// Write current etcd KVs
etcdDump, err := etcdhelper.DumpAllToString(ctx, etcdClient)
assert.NoError(test.T(), err)
assert.NoError(test.T(), test.WorkingDirFS().WriteFile(ctx, filesystem.NewRawFile("actual-etcd-kvs.txt", etcdDump)))
// Assert current etcd state against expected state.
expectedEtcdKVsPath := "expected-etcd-kvs.txt"
if test.TestDirFS().IsFile(ctx, expectedEtcdKVsPath) {
// Compare expected and actual kvs
etcdhelper.AssertKVsString(test.T(), etcdClient, test.ReadFileFromTestDir(expectedEtcdKVsPath))
}
})
}