rash_core 2.17.8

Declarative shell scripting using Rust native bindings
Documentation
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
use crate::cli::modules::run_test_with_env;
use std::sync::atomic::{AtomicU64, Ordering};

// Global counter for unique test file names
static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

// Helper function to get a unique passwd file for this test
fn get_unique_passwd_file() -> String {
    let test_id = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    format!("/tmp/rash_test_passwd_{}", test_id)
}

#[test]
fn test_user_create() {
    let passwd_file = get_unique_passwd_file();

    // Clean up passwd file before test
    let _ = std::fs::remove_file(&passwd_file);

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module create user
  user:
    name: testuser
    state: present
    uid: 1500
    shell: /bin/bash
    comment: Test User
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed', got: {}",
        stdout
    );

    // Validate user was created in passwd file
    let passwd = std::fs::read_to_string(&passwd_file).expect("passwd file should exist");
    assert!(
        passwd.contains("testuser:x:1500:"),
        "passwd should contain testuser with uid 1500"
    );
    assert!(
        passwd.contains(":/bin/bash"),
        "passwd should contain /bin/bash shell"
    );
    assert!(
        passwd.contains(":Test User:"),
        "passwd should contain Test User comment"
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

#[test]
fn test_user_create_system() {
    let passwd_file = get_unique_passwd_file();

    // Clean up passwd file before test
    let _ = std::fs::remove_file(&passwd_file);

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module create system user
  user:
    name: sysuser
    state: present
    system: true
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed', got: {}",
        stdout
    );

    // Validate system user was created
    let passwd = std::fs::read_to_string(&passwd_file).expect("passwd file should exist");
    assert!(
        passwd.contains("sysuser:x:999:999:"),
        "passwd should contain sysuser with uid/gid 999"
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

#[test]
fn test_user_delete() {
    let passwd_file = get_unique_passwd_file();

    // Setup: Create a user first
    let _ = std::fs::remove_file(&passwd_file);
    std::fs::write(
        &passwd_file,
        "olduser:x:1001:1001::/home/olduser:/bin/bash\n",
    )
    .expect("Failed to create test passwd file");

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module delete user
  user:
    name: olduser
    state: absent
    remove: true
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed', got: {}",
        stdout
    );

    // Validate user was removed
    let passwd = std::fs::read_to_string(&passwd_file).expect("passwd file should exist");
    assert!(
        !passwd.contains("olduser"),
        "passwd should not contain olduser after deletion"
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

#[test]
fn test_user_delete_nonexistent() {
    let passwd_file = get_unique_passwd_file();

    // Clean up passwd file before test
    let _ = std::fs::remove_file(&passwd_file);

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module delete nonexistent user
  user:
    name: nonexistent
    state: absent
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    // User doesn't exist, so should be "ok" not "changed"
    assert!(
        stdout.contains("ok"),
        "stdout should contain 'ok', got: {}",
        stdout
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

#[test]
fn test_user_with_groups() {
    let passwd_file = get_unique_passwd_file();

    // Clean up passwd file before test
    let _ = std::fs::remove_file(&passwd_file);

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module with supplementary groups
  user:
    name: testuser
    state: present
    groups:
      - docker
      - wheel
    append: true
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed', got: {}",
        stdout
    );

    // Validate user was created
    let passwd = std::fs::read_to_string(&passwd_file).expect("passwd file should exist");
    assert!(
        passwd.contains("testuser:x:"),
        "passwd should contain testuser"
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

#[test]
fn test_user_modify() {
    let passwd_file = get_unique_passwd_file();

    // Setup: Create a user first
    let _ = std::fs::remove_file(&passwd_file);
    std::fs::write(
        &passwd_file,
        "moduser:x:1002:1002:Old Comment:/home/moduser:/bin/sh\n",
    )
    .expect("Failed to create test passwd file");

    // Set environment variable for this test

    let script_text = r#"
#!/usr/bin/env rash
- name: test user module modify user
  user:
    name: moduser
    state: present
    uid: 1003
    shell: /bin/bash
    comment: New Comment
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[("RASH_TEST_PASSWD_FILE", &passwd_file)],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed', got: {}",
        stdout
    );

    // Validate user was modified
    let passwd = std::fs::read_to_string(&passwd_file).expect("passwd file should exist");
    assert!(
        passwd.contains("moduser:x:1003:"),
        "passwd should contain moduser with updated uid"
    );
    assert!(
        passwd.contains(":/bin/bash"),
        "passwd should contain updated shell"
    );
    assert!(
        passwd.contains(":New Comment:"),
        "passwd should contain updated comment"
    );
    assert!(
        !passwd.contains("Old Comment"),
        "passwd should not contain old comment"
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
}

// Helper function to get a unique group file for this test
fn get_unique_group_file() -> String {
    let test_id = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    format!("/tmp/rash_test_group_{}", test_id)
}

#[test]
fn test_user_append_groups_already_present() {
    let passwd_file = get_unique_passwd_file();
    let group_file = get_unique_group_file();

    // Setup: Create a user in passwd file
    let _ = std::fs::remove_file(&passwd_file);
    let _ = std::fs::remove_file(&group_file);
    std::fs::write(
        &passwd_file,
        "groupuser:x:1010:1010:Group User:/home/groupuser:/bin/bash\n",
    )
    .expect("Failed to create test passwd file");

    // Setup: Create group file where the user is already a member of docker, wheel, and audio
    std::fs::write(
        &group_file,
        "docker:x:100:groupuser,otheruser\nwheel:x:101:groupuser\naudio:x:102:groupuser\nvideo:x:103:someoneelse\n",
    )
    .expect("Failed to create test group file");

    // Try to append docker and wheel groups - user already has these
    let script_text = r#"
#!/usr/bin/env rash
- name: test user module append groups already present
  user:
    name: groupuser
    state: present
    groups:
      - docker
      - wheel
    append: true
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[
            ("RASH_TEST_PASSWD_FILE", &passwd_file),
            ("RASH_TEST_GROUP_FILE", &group_file),
        ],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    // User already has both groups, so should be "ok" not "changed"
    assert!(
        stdout.contains("ok"),
        "stdout should contain 'ok' (no change needed) when groups are already present, got: {}",
        stdout
    );
    assert!(
        !stdout.contains("changed"),
        "stdout should NOT contain 'changed' when groups are already present, got: {}",
        stdout
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
    let _ = std::fs::remove_file(&group_file);
}

#[test]
fn test_user_append_groups_partially_present() {
    let passwd_file = get_unique_passwd_file();
    let group_file = get_unique_group_file();

    // Setup: Create a user in passwd file
    let _ = std::fs::remove_file(&passwd_file);
    let _ = std::fs::remove_file(&group_file);
    std::fs::write(
        &passwd_file,
        "partialuser:x:1011:1011:Partial User:/home/partialuser:/bin/bash\n",
    )
    .expect("Failed to create test passwd file");

    // Setup: Create group file where the user is only a member of docker (not wheel)
    std::fs::write(
        &group_file,
        "docker:x:100:partialuser\nwheel:x:101:otheruser\naudio:x:102:partialuser\n",
    )
    .expect("Failed to create test group file");

    // Try to append docker and wheel groups - user only has docker
    let script_text = r#"
#!/usr/bin/env rash
- name: test user module append groups partially present
  user:
    name: partialuser
    state: present
    groups:
      - docker
      - wheel
    append: true
        "#
    .to_string();

    let args = ["--diff"];
    let (stdout, stderr) = run_test_with_env(
        &script_text,
        &args,
        &[
            ("RASH_TEST_PASSWD_FILE", &passwd_file),
            ("RASH_TEST_GROUP_FILE", &group_file),
        ],
    );

    assert!(stderr.is_empty(), "stderr should be empty, got: {}", stderr);
    // User needs wheel group, so should be "changed"
    assert!(
        stdout.contains("changed"),
        "stdout should contain 'changed' when some groups need to be added, got: {}",
        stdout
    );

    // Cleanup
    let _ = std::fs::remove_file(&passwd_file);
    let _ = std::fs::remove_file(&group_file);
}