-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
192 lines (155 loc) · 4.66 KB
/
main_test.go
File metadata and controls
192 lines (155 loc) · 4.66 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//go:build e2e
// +build e2e
package main
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/hotosm/scaleodm/app/db"
"github.com/hotosm/scaleodm/app/meta"
"github.com/hotosm/scaleodm/app/workflows"
"github.com/hotosm/scaleodm/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testDB creates a test database connection for E2E tests
func testDB(t *testing.T) (*db.DB, func()) {
t.Helper()
dbURL := testutil.TestDBURL()
database, err := db.NewDB(dbURL)
if err != nil {
t.Fatalf("Failed to connect to test database: %v", err)
}
// Initialize schema
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := database.InitSchema(ctx); err != nil {
database.Close()
t.Fatalf("Failed to initialize schema: %v", err)
}
// Cleanup function
cleanup := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Clean up test data
_, _ = database.Pool.Exec(ctx, "TRUNCATE TABLE scaleodm_job_metadata CASCADE")
database.Close()
}
return database, cleanup
}
// E2E tests require:
// - Database running (via docker compose)
// - Kubernetes cluster with Argo Workflows installed
// - S3 endpoint available (MinIO via docker compose)
func TestE2E_HealthCheck(t *testing.T) {
db, cleanup := testDB(t)
defer cleanup()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := db.HealthCheck(ctx)
require.NoError(t, err)
}
func TestE2E_CreateAndListJobs(t *testing.T) {
db, cleanup := testDB(t)
defer cleanup()
store := meta.NewStore(db)
ctx := context.Background()
// Set up test S3 bucket
err := testutil.SetupTestS3Bucket(ctx, "test-bucket")
require.NoError(t, err, "Failed to set up test S3 bucket")
// Create multiple jobs
for i := 0; i < 3; i++ {
_, createErr := store.CreateJob(
ctx,
fmt.Sprintf("e2e-workflow-%d", i),
"e2e-project",
"s3://test-bucket/images/",
"s3://test-bucket/output/",
[]string{"--fast-orthophoto"},
"us-east-1",
)
require.NoError(t, createErr)
}
// List jobs
jobs, err := store.ListJobs(ctx, "", "", 0)
require.NoError(t, err)
assert.GreaterOrEqual(t, len(jobs), 3)
}
func TestE2E_WorkflowClient_WithK8s(t *testing.T) {
kubeconfig := os.Getenv("KUBECONFIG_PATH")
namespace := os.Getenv("K8S_NAMESPACE")
if namespace == "" {
namespace = "default"
}
client, err := workflows.NewClient(kubeconfig, namespace)
require.NoError(t, err)
assert.NotNil(t, client)
// List workflows (should work even if empty)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
wfList, err := client.ListWorkflows(ctx, "")
require.NoError(t, err)
assert.NotNil(t, wfList)
}
func TestE2E_JobLifecycle(t *testing.T) {
db, cleanup := testDB(t)
defer cleanup()
store := meta.NewStore(db)
ctx := context.Background()
// Set up test S3 bucket
err := testutil.SetupTestS3Bucket(ctx, "test-bucket")
require.NoError(t, err, "Failed to set up test S3 bucket")
// Create job
workflowName := "e2e-lifecycle-workflow"
job, err := store.CreateJob(
ctx,
workflowName,
"e2e-project",
"s3://test-bucket/images/",
"s3://test-bucket/output/",
[]string{"--fast-orthophoto"},
"us-east-1",
)
require.NoError(t, err)
require.NotNil(t, job, "Job should be created successfully")
// The default status for new jobs in the metadata store is 'queued'
assert.Equal(t, "queued", job.JobStatus)
// Verify job exists before updating - use a small retry in case of timing issues
var retrievedJob *meta.JobMetadata
for i := 0; i < 5; i++ {
retrievedJob, err = store.GetJob(ctx, workflowName)
if err == nil && retrievedJob != nil {
break
}
if i < 4 {
time.Sleep(10 * time.Millisecond)
}
}
require.NoError(t, err)
require.NotNil(t, retrievedJob, "Job should exist before status update")
job = retrievedJob
// Update to running
err = store.UpdateJobStatus(ctx, workflowName, "running", nil)
require.NoError(t, err)
job, err = store.GetJob(ctx, workflowName)
require.NoError(t, err)
require.NotNil(t, job, "Job should exist after status update")
assert.Equal(t, "running", job.JobStatus)
assert.NotNil(t, job.StartedAt)
// Update to completed
err = store.UpdateJobStatus(ctx, workflowName, "completed", nil)
require.NoError(t, err)
job, err = store.GetJob(ctx, workflowName)
require.NoError(t, err)
require.NotNil(t, job, "Job should exist after status update")
assert.Equal(t, "completed", job.JobStatus)
assert.NotNil(t, job.CompletedAt)
// Delete job
err = store.DeleteJob(ctx, workflowName)
require.NoError(t, err)
job, err = store.GetJob(ctx, workflowName)
require.NoError(t, err)
assert.Nil(t, job)
}