Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #22721: Update rudderc documentation #4782

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions policies/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion policies/rudderc/docs/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tool.

Techniques in Rudder are system configuration or audit templates,
which can include extra files and take parameters.
They are instantiated inside of Rudder by directives,
They are instantiated by directives,
which are in turn linked to rules to be applied.
Techniques are based on building blocks called methods,
which control basic items (like the content of a file
Expand Down
4 changes: 2 additions & 2 deletions policies/rudderc/docs/src/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Testing

## Writing technique tests
## Write technique tests

**NOTE 1**: Techniques tests are only supported on Linux for now.

Expand Down Expand Up @@ -39,7 +39,7 @@ The detailed test process is, for each `*.yml` file in the `tests` directory:

The build stops when it encounters a command returning a non-zero return code.

## Running technique tests
## Run technique tests

To run the test `case1` from:

Expand Down
16 changes: 11 additions & 5 deletions policies/rudderc/docs/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ There are currently two possible targets, which are the platforms Rudder has age
* Linux/AIX
* Windows

These platforms use different agent technology but the YAML policies
These platforms use different agent technology, but the YAML policies
unify them.
To be able to check and compile techniques, the `rudderc` program
needs access to the methods library of the target systems.
needs access to the method library of the target systems.

To get access to the base Linux methods set, you can use git
and use the repository as library argument:
Expand All @@ -25,6 +25,10 @@ $ git clone https://github.com/Normation/ncf/
$ rudderc subcommand -l /.../ncf/tree/30_generic_methods/
```

Alternatively, if you `rudderc` on a system with a Rudder agent, like a Rudder server,
which also has a built-in `rudderc` binary, you do not need to specify anything and
the local library (in `/var/rudder/ncf`) will be used automatically.

### Create a technique

To setup the technique structure:
Expand All @@ -41,16 +45,18 @@ This will create the base structure of your new technique:
```text
my_technique/
├── technique.yml
└── resources/
├── resources/
└── tests/
```

The `technique.yml` is the technique content,
and the `resources` directory can be used to include
external files (configuration files, templates, etc.).
The tests directory will contain your technique's tests.
All files produced by `rudderc` will be placed in the `target`
directory.

### Checking a technique
### Check a technique

You can check the current technique syntax with:

Expand Down Expand Up @@ -79,7 +85,7 @@ $ rudderc build -l /path/to/methods/lib
Copied resources
```

### Clean you produced
### Clean files

The `clean` command allows removing all generated files.

Expand Down
26 changes: 10 additions & 16 deletions policies/rudderc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ pub fn run(args: MainArgs) -> Result<()> {
// Actions
pub mod action {
use std::{
env, fs,
fs::{create_dir, create_dir_all, read_to_string, remove_dir_all, File},
fs::{self, create_dir, create_dir_all, read_to_string, remove_dir_all, File},
io::{self, Write},
path::{Path, PathBuf},
process::Command,
Expand All @@ -155,7 +154,7 @@ pub mod action {
ir::Technique,
logs::ok_output,
test::TestCase,
METADATA_FILE, RESOURCES_DIR, TECHNIQUE, TECHNIQUE_SRC,
METADATA_FILE, RESOURCES_DIR, TECHNIQUE, TECHNIQUE_SRC, TESTS_DIR,
};

/// Create a technique skeleton
Expand All @@ -173,6 +172,9 @@ pub mod action {
create_dir(resources_dir.as_path()).with_context(|| {
format!("Failed to create resources dir {}", resources_dir.display())
})?;
let tests_dir = output.join(TESTS_DIR);
create_dir(tests_dir.as_path())
.with_context(|| format!("Failed to create tests dir {}", tests_dir.display()))?;
ok_output("Wrote", tech_path.display());
Ok(())
}
Expand Down Expand Up @@ -265,18 +267,15 @@ pub mod action {

/// Run a test
pub fn test(
technique: &Path,
technique_file: &Path,
test_dir: &Path,
libraries: &[PathBuf],
filter: Option<String>,
) -> Result<()> {
// Run everything relatively to the test directory
let cwd = env::current_dir()?;
let technique_file = cwd.join(technique);
env::set_current_dir(test_dir)?;
// Collect test cases
let mut cases = vec![];
for entry in fs::read_dir(".")? {
for entry in fs::read_dir(test_dir)? {
let e = entry?;
let name = e.file_name().into_string().unwrap();
if e.file_type()?.is_file() && name.ends_with(".yml") {
Expand All @@ -296,7 +295,7 @@ pub mod action {
let yaml = read_to_string(&case_path)?;
let case: TestCase = serde_yaml::from_str(&yaml)?;
// Run test setup
case.setup()?;
case.setup(test_dir)?;
// Run the technique
// TODO: support several lib dirs
ok_output(
Expand All @@ -306,15 +305,10 @@ pub mod action {
case_path.display()
),
);
cf_agent(
technique_file.as_path(),
case_path.as_path(),
libraries[0].as_path(),
)?;
cf_agent(technique_file, case_path.as_path(), libraries[0].as_path())?;
// Run test checks
case.check()?;
case.check(test_dir)?;
}
env::set_current_dir(&cwd)?;
Ok(())
}

Expand Down
14 changes: 9 additions & 5 deletions policies/rudderc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// Test file specifications. Do we want several test cases in one file?

use std::path::Path;
use std::{collections::HashMap, process::Command};

use anyhow::{bail, Result};
Expand All @@ -29,19 +30,22 @@ pub struct Step {
pub struct TestCase {
/// Parameters, we don't actually use them as they're loaded directly by the technique
#[serde(rename = "params")]
#[serde(default)]
parameters: HashMap<String, String>,
/// Test setup steps
#[serde(default)]
setup: Vec<Step>,
/// Check test after
check: Vec<Step>,
}

impl TestCase {
fn run(step: &Step) -> Result<()> {
fn run(step: &Step, dir: &Path) -> Result<()> {
ok_output("Running", format!("'{}'", &step.command));
let output = Command::new("/bin/sh")
.arg("-c")
.arg(&step.command)
.current_dir(dir)
.output()?;
if !output.status.success() {
bail!(
Expand All @@ -54,16 +58,16 @@ impl TestCase {
Ok(())
}

pub fn setup(&self) -> Result<()> {
pub fn setup(&self, dir: &Path) -> Result<()> {
for s in &self.setup {
Self::run(s)?;
Self::run(s, dir)?;
}
Ok(())
}

pub fn check(&self) -> Result<()> {
pub fn check(&self, dir: &Path) -> Result<()> {
for s in &self.check {
Self::run(s)?;
Self::run(s, dir)?;
}
Ok(())
}
Expand Down
13 changes: 13 additions & 0 deletions policies/rudderc/tests/cases/test/zero_param/technique.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
id: one_param
name: Test with no parameter
version: "0.1"
description: "Test the testing process with zero parameter"
items:
- name: "Ensure correct ntp configuration"
id: d86ce2e5-d5b6-45cc-87e8-c11cca71d907
method: file_content
params:
path: "/tmp/plop"
lines: "rudder.cm"
enforce: "true"
10 changes: 10 additions & 0 deletions policies/rudderc/tests/cases/test/zero_param/tests/case1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Parameters for the technique
# none here
# Prepare env
setup:
- sh: "rm -f /tmp/plop"
# Check after technique has run
check:
# Commands to run to check
- sh: "test -f /tmp/plop"
- sh: "diff ./reference.conf /tmp/plop"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rudder.cm
Loading