comparison mrjunejune/test/create_snapshots.c @ 67:6626ec933933

[Seobeo] Separated out Client Server logic. Created test tools.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Dec 2025 09:15:55 -0800
parents
children 092afa595764
comparison
equal deleted inserted replaced
66:a0f0ad5e42eb 67:6626ec933933
1 #include "seobeo/seobeo.h"
2 #include "seobeo/snapshot_creator.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/wait.h>
7 #include <signal.h>
8
9 #define TEST_PORT "6969"
10 #define TEST_HOST "127.0.0.1"
11 #define SNAPSHOT_DIR "mrjunejune/test/snapshots"
12
13 // Start the server process
14 static pid_t start_server(const char *server_binary)
15 {
16 pid_t server_pid = fork();
17
18 if (server_pid < 0)
19 {
20 perror("fork");
21 return -1;
22 }
23
24 if (server_pid == 0)
25 {
26 // Child process - run server
27 printf("Starting server on port %s...\n", TEST_PORT);
28 execl(server_binary, server_binary, NULL);
29 perror("execl failed");
30 exit(1);
31 }
32
33 // Parent - verify server started
34 printf("Server started (PID: %d)\n", server_pid);
35
36 usleep(100000);
37 int status;
38 pid_t result = waitpid(server_pid, &status, WNOHANG);
39 if (result != 0)
40 {
41 if (WIFEXITED(status))
42 {
43 fprintf(stderr, "Server exited with code: %d\n", WEXITSTATUS(status));
44 }
45 else if (WIFSIGNALED(status))
46 {
47 fprintf(stderr, "Server killed by signal: %d\n", WTERMSIG(status));
48 }
49 return -1;
50 }
51
52 sleep(2);
53 printf("Server ready\n\n");
54
55 return server_pid;
56 }
57
58 // Stop the server process
59 static void stop_server_test(pid_t server_pid)
60 {
61 if (server_pid > 0)
62 {
63 printf("\nStopping server (PID: %d)...\n", server_pid);
64 kill(server_pid, SIGTERM);
65 waitpid(server_pid, NULL, 0);
66 printf("Server stopped\n");
67 }
68 }
69
70 int main(int argc, char *argv[])
71 {
72 printf("=== Seobeo Snapshot Creator ===\n\n");
73
74 // Get workspace directory (where the source files are)
75 const char *workspace_dir = getenv("BUILD_WORKSPACE_DIRECTORY");
76 if (!workspace_dir)
77 {
78 fprintf(stderr, "Error: BUILD_WORKSPACE_DIRECTORY not set\n");
79 fprintf(stderr, "This binary must be run with 'bazel run'\n");
80 return 1;
81 }
82
83 // Construct full path to snapshot directory
84 char snapshot_path[1024];
85 snprintf(snapshot_path, sizeof(snapshot_path), "%s/%s", workspace_dir, SNAPSHOT_DIR);
86
87 // Get server binary path
88 const char *server_binary = "./mrjunejune_server";
89 if (argc > 1)
90 {
91 server_binary = argv[1];
92 }
93
94 printf("Workspace: %s\n", workspace_dir);
95 printf("Server binary: %s\n", server_binary);
96 printf("Snapshot directory: %s\n\n", snapshot_path);
97
98 // Start server
99 pid_t server_pid = start_server(server_binary);
100 if (server_pid < 0)
101 {
102 fprintf(stderr, "Failed to start server\n");
103 return 1;
104 }
105
106 // Define snapshots to create - paths that should succeed (200 OK)
107 SnapshotConfig success_snapshots[] = {
108 {"/", 200, snapshot_path, TEST_HOST, TEST_PORT},
109 {"/index.html", 200, snapshot_path, TEST_HOST, TEST_PORT},
110 };
111 int num_success = sizeof(success_snapshots) / sizeof(success_snapshots[0]);
112
113 // Define snapshots for error paths (404)
114 SnapshotConfig error_snapshots[] = {
115 {"/nonexistent", 404, snapshot_path, TEST_HOST, TEST_PORT},
116 {"/does/not/exist", 404, snapshot_path, TEST_HOST, TEST_PORT},
117 {"/missing.html", 404, snapshot_path, TEST_HOST, TEST_PORT},
118 };
119 int num_errors = sizeof(error_snapshots) / sizeof(error_snapshots[0]);
120
121 int total_failed = 0;
122 int total_passed = 0;
123
124 // Create success snapshots
125 printf("Creating snapshots for successful paths:\n\n");
126 for (int i = 0; i < num_success; i++)
127 {
128 if (Seobeo_Snapshot_Create(&success_snapshots[i]) == 0)
129 {
130 total_passed++;
131 }
132 else
133 {
134 total_failed++;
135 }
136 }
137
138 // Create error snapshots
139 printf("Creating snapshots for error paths:\n\n");
140 for (int i = 0; i < num_errors; i++)
141 {
142 if (Seobeo_Snapshot_Create(&error_snapshots[i]) == 0)
143 {
144 total_passed++;
145 }
146 else
147 {
148 total_failed++;
149 }
150 }
151
152 // Stop server
153 stop_server_test(server_pid);
154
155 // Print summary
156 printf("\n=== Summary ===\n");
157 printf("Snapshots created: %d\n", total_passed);
158 printf("Failed: %d\n", total_failed);
159
160 if (total_failed == 0)
161 {
162 printf("\n✓ All snapshots created successfully!\n");
163 printf("\nSnapshots saved to: %s/\n", snapshot_path);
164 printf("\nRun tests to verify:\n");
165 printf(" bazel test //mrjunejune:integration_test\n");
166 return 0;
167 }
168 else
169 {
170 printf("\n✗ Some snapshots failed\n");
171 return 1;
172 }
173 }