vdev 0.3.1

CLI utilities for Vector (vector.dev) development and CI workflows
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::{
    path::{Path, PathBuf},
    process::Command,
};

use anyhow::{Context, Result, bail};

use super::{
    config::{
        ComposeConfig, ComposeTestConfig, E2E_TESTS_DIR, INTEGRATION_TESTS_DIR, RustToolchainConfig,
    },
    runner::{ContainerTestRunner as _, IntegrationTestRunner, TestRunner as _},
};
use crate::{
    app::CommandExt as _,
    testing::{
        build::ALL_INTEGRATIONS_FEATURE_FLAG,
        docker::{CONTAINER_TOOL, DOCKER_SOCKET},
    },
    utils::environment::{Environment, extract_present, rename_environment_keys},
};

const NETWORK_ENV_VAR: &str = "VECTOR_NETWORK";
const E2E_FEATURE_FLAG: &str = "all-e2e-tests";

/// Check if a Docker image exists locally
fn docker_image_exists(image_name: &str) -> Result<bool> {
    use crate::testing::docker::docker_command;
    let output =
        docker_command(["images", "--format", "{{.Repository}}:{{.Tag}}"]).check_output()?;
    Ok(output.lines().any(|line| line == image_name))
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ComposeTestKind {
    E2E,
    Integration,
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct ComposeTestLocalConfig {
    pub(crate) kind: ComposeTestKind,
    pub(crate) directory: &'static str,
    pub(crate) feature_flag: &'static str,
}

impl ComposeTestLocalConfig {
    /// Integration tests are located in the `tests/integration` dir,
    /// and are the full feature flag is `all-integration-tests`.
    pub(crate) fn integration() -> Self {
        Self {
            kind: ComposeTestKind::Integration,
            directory: INTEGRATION_TESTS_DIR,
            feature_flag: ALL_INTEGRATIONS_FEATURE_FLAG,
        }
    }

    /// E2E tests are located in the `tests/e2e` dir,
    /// and the full feature flag is `all-e2e-tests`.
    pub(crate) fn e2e() -> Self {
        Self {
            kind: ComposeTestKind::E2E,
            directory: E2E_TESTS_DIR,
            feature_flag: E2E_FEATURE_FLAG,
        }
    }
}

#[derive(Debug)]
pub(crate) struct ComposeTest {
    local_config: ComposeTestLocalConfig,
    test_name: String,
    environment: String,
    config: ComposeTestConfig,
    runner: IntegrationTestRunner,
    compose: Option<Compose>,
    env_config: Environment,
    retries: u8,
}

impl ComposeTest {
    pub(crate) fn generate(
        local_config: ComposeTestLocalConfig,
        test_name: impl Into<String>,
        environment: impl Into<String>,
        retries: u8,
    ) -> Result<ComposeTest> {
        let test_name: String = test_name.into();
        let environment = environment.into();
        let (test_dir, config) = ComposeTestConfig::load(local_config.directory, &test_name)?;
        let Some(mut env_config) = config.environments().get(&environment).cloned() else {
            bail!("Could not find environment named {environment:?}");
        };

        let network_name = format!("vector-integration-tests-{test_name}");
        let compose = Compose::new(test_dir, env_config.clone(), network_name.clone())?;

        // Auto-detect: If shared image exists, use it. Otherwise use per-test image.
        // Shared image: vector-test-runner-1.90:latest (compiled with all-integration-tests)
        // Per-test image: vector-test-runner-clickhouse-1.90:latest (compiled with specific features)
        let shared_image_name = format!(
            "vector-test-runner-{}:latest",
            RustToolchainConfig::rust_version()
        );
        let runner_name = if docker_image_exists(&shared_image_name).unwrap_or(false) {
            info!("Using shared runner image: {shared_image_name}");
            None
        } else {
            info!("Shared runner image not found, will build image for: {test_name}");
            Some(test_name.clone())
        };

        let runner = IntegrationTestRunner::new(
            runner_name,
            &config.runner,
            compose.is_some().then_some(network_name),
        )?;

        env_config.insert("VECTOR_IMAGE".to_string(), Some(runner.image_name()));

        let compose_test = ComposeTest {
            local_config,
            test_name,
            environment,
            config,
            runner,
            compose,
            env_config: rename_environment_keys(&env_config),
            retries,
        };
        trace!("Generated {compose_test:#?}");
        Ok(compose_test)
    }

    fn project_name(&self) -> String {
        // Docker Compose project names must consist only of lowercase alphanumeric characters,
        // hyphens, and underscores. Replace any dots with hyphens.
        let sanitized_env = self.environment.replace('.', "-");
        format!(
            "vector-{}-{}-{}",
            self.local_config.directory, self.test_name, sanitized_env
        )
    }

    fn is_running(&self) -> Result<bool> {
        let Some(compose) = &self.compose else {
            return Ok(false);
        };

        let output = Command::new(CONTAINER_TOOL.clone())
            .args([
                "compose",
                "--project-name",
                &self.project_name(),
                "ps",
                "--format",
                "json",
                "--status",
                "running",
            ])
            .current_dir(&compose.test_dir)
            .envs(
                compose
                    .env
                    .iter()
                    .filter_map(|(k, v)| v.as_ref().map(|val| (k, val))),
            )
            .output()
            .with_context(|| "Failed to check if compose environment is running")?;

        // If stdout is empty or "[]", no containers are running
        Ok(!output.stdout.is_empty() && output.stdout != b"[]\n" && output.stdout != b"[]")
    }

    pub(crate) fn test(&self, extra_args: Vec<String>) -> Result<()> {
        let was_running = self.is_running()?;
        self.config.check_required()?;

        if !was_running {
            self.start()?;
        }

        let mut env_vars = self.config.env.clone();
        // Make sure the test runner has the same config environment vars as the services do.
        for (key, value) in self.env_config.clone() {
            env_vars.insert(key, value);
        }

        env_vars.insert("VECTOR_LOG".to_string(), Some("info".into()));
        let mut args = self.config.args.clone().unwrap_or_default();

        args.push("--features".to_string());

        // If using shared runner: use 'all-integration-tests' or 'all-e2e-tests'
        // If using per-test runner: use test-specific features from test.yaml
        args.push(if self.runner.is_shared_runner() {
            format!("{},vendored", self.local_config.feature_flag)
        } else {
            format!("{},vendored", self.config.features.join(","))
        });

        // If the test field is not present then use the --lib flag
        match self.config.test {
            Some(ref test_arg) => {
                args.push("--test".to_string());
                args.push(test_arg.clone());
            }
            None => args.push("--lib".to_string()),
        }

        // Ensure the test_filter args are passed as well
        if let Some(ref filter) = self.config.test_filter {
            args.push(filter.clone());
        }
        args.extend(extra_args);

        // Some arguments are not compatible with the --no-capture arg
        if !args.contains(&"--test-threads".to_string()) {
            args.push("--no-capture".to_string());
        }

        if self.retries > 0 {
            args.push("--retries".to_string());
            args.push(self.retries.to_string());
        }

        self.runner.test(
            &env_vars,
            &self.config.runner.env,
            Some(&self.config.features),
            &args,
            self.local_config.kind == ComposeTestKind::E2E,
        )?;

        Ok(())
    }

    pub(crate) fn start(&self) -> Result<()> {
        // For end-to-end tests, we want to run vector as a service, leveraging the
        // image for the runner. So we must build that image before starting the
        // compose so that it is available.
        if self.local_config.kind == ComposeTestKind::E2E {
            self.runner.build(
                Some(&self.config.features),
                &self.env_config,
                true, // E2E tests build Vector in the image
            )?;
        }

        self.config.check_required()?;
        if let Some(compose) = &self.compose {
            self.runner.ensure_network()?;
            self.runner.ensure_external_volumes()?;

            if self.is_running()? {
                bail!("environment is already up");
            }

            let project_name = self.project_name();
            compose.start(&self.env_config, &project_name)?;
        }
        Ok(())
    }

    pub(crate) fn stop(&self) -> Result<()> {
        if let Some(compose) = &self.compose {
            if !self.is_running()? {
                bail!("No environment for {} is up.", self.test_name);
            }

            let project_name = self.project_name();
            compose.stop(&self.env_config, &project_name)?;
        }

        self.runner.remove()?;

        Ok(())
    }
}

#[derive(Debug)]
struct Compose {
    yaml_path: PathBuf,
    test_dir: PathBuf,
    env: Environment,
    #[cfg_attr(target_family = "windows", allow(dead_code))]
    config: ComposeConfig,
    network: String,
}

impl Compose {
    fn new(test_dir: PathBuf, env: Environment, network: String) -> Result<Option<Self>> {
        let yaml_path: PathBuf = [&test_dir, Path::new("compose.yaml")].iter().collect();

        match yaml_path.try_exists() {
            Err(error) => {
                Err(error).with_context(|| format!("Could not lookup {}", yaml_path.display()))
            }
            Ok(false) => Ok(None),
            Ok(true) => {
                // Parse config only for unix volume permission checking
                let config = ComposeConfig::parse(&yaml_path)?;

                Ok(Some(Self {
                    yaml_path,
                    test_dir,
                    env,
                    config,
                    network,
                }))
            }
        }
    }

    fn start(&self, environment: &Environment, project_name: &str) -> Result<()> {
        #[cfg(unix)]
        unix::prepare_compose_volumes(&self.config, &self.test_dir, environment)?;

        self.run(
            "Starting",
            &["up", "--detach"],
            Some(environment),
            project_name,
        )
    }

    fn stop(&self, environment: &Environment, project_name: &str) -> Result<()> {
        self.run(
            "Stopping",
            &["down", "--timeout", "0", "--volumes", "--remove-orphans"],
            Some(environment),
            project_name,
        )
    }

    fn run(
        &self,
        action: &str,
        args: &[&'static str],
        environment: Option<&Environment>,
        project_name: &str,
    ) -> Result<()> {
        let mut command = Command::new(CONTAINER_TOOL.clone());
        command.arg("compose");
        command.arg("--project-name");
        command.arg(project_name);
        command.arg("--file");
        command.arg(&self.yaml_path);

        command.args(args);

        command.current_dir(&self.test_dir);

        command.env("DOCKER_SOCKET", &*DOCKER_SOCKET);
        command.env(NETWORK_ENV_VAR, &self.network);

        // some services require this in order to build Vector
        command.env("RUST_VERSION", RustToolchainConfig::rust_version());

        for (key, value) in &self.env {
            if let Some(value) = value {
                command.env(key, value);
            }
        }
        if let Some(environment) = environment {
            command.envs(extract_present(environment));
        }

        waiting!("{action} service environment");
        command.check_run()
    }
}

#[cfg(unix)]
mod unix {
    use std::{
        fs::{self, Metadata, Permissions},
        os::unix::fs::PermissionsExt as _,
        path::{Path, PathBuf},
    };

    use anyhow::{Context, Result};

    use super::super::config::ComposeConfig;
    use crate::{
        testing::config::VolumeMount,
        utils::environment::{Environment, resolve_placeholders},
    };

    /// Unix permissions mask to allow everybody to read a file
    const ALL_READ: u32 = 0o444;
    /// Unix permissions mask to allow everybody to read a directory
    const ALL_READ_DIR: u32 = 0o555;

    /// Fix up potential issues before starting a compose container
    pub fn prepare_compose_volumes(
        config: &ComposeConfig,
        test_dir: &Path,
        environment: &Environment,
    ) -> Result<()> {
        for service in config.services.values() {
            if let Some(volumes) = &service.volumes {
                for volume in volumes {
                    let source = match volume {
                        VolumeMount::Short(s) => {
                            s.split_once(':').map(|(s, _)| s).ok_or_else(|| {
                                anyhow::anyhow!("Invalid short volume mount format: {s}")
                            })?
                        }
                        VolumeMount::Long { source, .. } => source,
                    };
                    let source = resolve_placeholders(source, environment);
                    if !config.volumes.contains_key(&source)
                        && !source.starts_with('/')
                        && !source.starts_with('$')
                    {
                        let path: PathBuf = [test_dir, Path::new(&source)].iter().collect();
                        add_read_permission(&path)?;
                    }
                }
            }
        }
        Ok(())
    }

    /// Recursively add read permissions to the
    fn add_read_permission(path: &Path) -> Result<()> {
        let metadata = path
            .metadata()
            .with_context(|| format!("Could not get permissions on {}", path.display()))?;

        if metadata.is_file() {
            add_permission(path, &metadata, ALL_READ)
        } else {
            if metadata.is_dir() {
                add_permission(path, &metadata, ALL_READ_DIR)?;
                for entry in fs::read_dir(path)
                    .with_context(|| format!("Could not read directory {}", path.display()))?
                {
                    let entry = entry.with_context(|| {
                        format!("Could not read directory entry in {}", path.display())
                    })?;
                    add_read_permission(&entry.path())?;
                }
            }
            Ok(())
        }
    }

    fn add_permission(path: &Path, metadata: &Metadata, bits: u32) -> Result<()> {
        let perms = metadata.permissions();
        let new_perms = Permissions::from_mode(perms.mode() | bits);
        if new_perms != perms {
            fs::set_permissions(path, new_perms)
                .with_context(|| format!("Could not set permissions on {}", path.display()))?;
        }
        Ok(())
    }
}