Solved multiple Buildah-related issues in the Buildah SAL + fixed test suite for Buildah
This commit is contained in:
		| @@ -22,7 +22,8 @@ fn assert_eq(actual, expected, message) { | ||||
| // Helper function to check if buildah is available | ||||
| fn is_buildah_available() { | ||||
|     try { | ||||
|         let result = run("which buildah"); | ||||
|         let command = run("which buildah"); | ||||
|         let result = command.execute(); | ||||
|         return result.success; | ||||
|     } catch(err) { | ||||
|         return false; | ||||
| @@ -121,7 +122,7 @@ try { | ||||
|     // Test committing to an image | ||||
|     print("Testing commit()..."); | ||||
|     let image_name = "rhai_test_image:latest"; | ||||
|     builder.commit(image_name); | ||||
|     builder.commit(image_name, []); | ||||
|     print("✓ commit(): Container committed to image successfully"); | ||||
|      | ||||
|     // Test removing the container | ||||
| @@ -154,19 +155,21 @@ try { | ||||
|     // Clean up in case of error | ||||
|     try { | ||||
|         // Remove test container if it exists | ||||
|         run("buildah rm rhai_test_container"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rm rhai_test_container"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|      | ||||
|     try { | ||||
|         // Remove test image if it exists | ||||
|         run("buildah rmi rhai_test_image:latest"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rmi alpine"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|      | ||||
|     try { | ||||
|         // Remove test files if they exist | ||||
|         delete("test_add_file.txt"); | ||||
|         delete("test_copy_file.txt"); | ||||
|     } catch(_) {} | ||||
|     } catch(err) {} | ||||
|      | ||||
|     throw err; | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,8 @@ fn assert_eq(actual, expected, message) { | ||||
| // Helper function to check if buildah is available | ||||
| fn is_buildah_available() { | ||||
|     try { | ||||
|         let result = run("which buildah"); | ||||
|         let command = run("which buildah"); | ||||
|         let result = command.execute(); | ||||
|         return result.success; | ||||
|     } catch(err) { | ||||
|         return false; | ||||
| @@ -32,8 +33,18 @@ fn is_buildah_available() { | ||||
| // Helper function to check if an image exists | ||||
| fn image_exists(image_name) { | ||||
|     try { | ||||
|         let result = run(`buildah images -q ${image_name}`); | ||||
|         return result.success && result.stdout.trim() != ""; | ||||
|         // First, check for the exact image name | ||||
|         let command = run(`buildah images -q ${image_name}`); | ||||
|         let result = command.execute(); | ||||
|         if result.success && result.stdout.trim() != "" { | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         // If not found, check for the localhost-prefixed version | ||||
|         let prefixed_image_name = `localhost/${image_name}`; | ||||
|         let command = run(`buildah images -q ${prefixed_image_name}`); | ||||
|         let result_prefixed = command.execute(); | ||||
|         return result_prefixed.success && result_prefixed.stdout.trim() != ""; | ||||
|     } catch(err) { | ||||
|         return false; | ||||
|     } | ||||
| @@ -82,8 +93,10 @@ try { | ||||
|      | ||||
|     // Find our tagged image | ||||
|     let found_tag = false; | ||||
|     let expected_tag = "rhai_test_tag:latest"; | ||||
|     for image in images { | ||||
|         if image.names.contains("rhai_test_tag:latest") { | ||||
|         // The tag might be prefixed with 'localhost/' if no registry is specified. | ||||
|         if image.names.contains(expected_tag) || image.names.contains("localhost/" + expected_tag) { | ||||
|             found_tag = true; | ||||
|             break; | ||||
|         } | ||||
| @@ -95,10 +108,11 @@ try { | ||||
|     print("Testing build()..."); | ||||
|      | ||||
|     // Create a simple Dockerfile | ||||
|     let dockerfile_content = `FROM alpine:latest | ||||
| RUN echo "Hello from Dockerfile" > /hello.txt | ||||
| CMD ["cat", "/hello.txt"] | ||||
| `; | ||||
|     let dockerfile_content = ` | ||||
|         FROM alpine:latest | ||||
|         RUN echo "Hello from Dockerfile" > /hello.txt | ||||
|         CMD ["cat", "/hello.txt"] | ||||
|     `; | ||||
|     file_write(`${test_dir}/Dockerfile`, dockerfile_content); | ||||
|      | ||||
|     // Build the image | ||||
| @@ -133,18 +147,23 @@ CMD ["cat", "/hello.txt"] | ||||
|     // Clean up in case of error | ||||
|     try { | ||||
|         // Remove test container if it exists | ||||
|         run("buildah rm rhai_test_container"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rm rhai_test_container"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|      | ||||
|     try { | ||||
|         // Remove test images if they exist | ||||
|         run("buildah rmi rhai_test_tag:latest"); | ||||
|         run("buildah rmi rhai_test_build:latest"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rmi rhai_test_tag:latest"); | ||||
|         command.execute(); | ||||
|         let command = run("buildah rmi rhai_test_build:latest"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|  | ||||
|     try { | ||||
|         // Remove test directory if it exists | ||||
|         delete(test_dir); | ||||
|         print("✓ Cleanup: Test directory removed"); | ||||
|     } catch (err) {} | ||||
|      | ||||
|     throw err; | ||||
| } finally { | ||||
|     // Clean up test directory | ||||
|     delete(test_dir); | ||||
|     print("✓ Cleanup: Test directory removed"); | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,8 @@ fn assert_eq(actual, expected, message) { | ||||
| // Helper function to check if buildah is available | ||||
| fn is_buildah_available() { | ||||
|     try { | ||||
|         let result = run("which buildah"); | ||||
|         let command = run("which buildah"); | ||||
|         let result = command.execute(); | ||||
|         return result.success; | ||||
|     } catch(err) { | ||||
|         return false; | ||||
| @@ -59,10 +60,12 @@ try { | ||||
|      | ||||
|     // Test config | ||||
|     print("Testing config()..."); | ||||
|     let config_options = #{ | ||||
|         "LABEL": "rhai_test=true", | ||||
|         "ENV": "TEST_VAR=test_value" | ||||
|     }; | ||||
|     let config_options = [ | ||||
|         ["label", "rhai_test_true"], | ||||
|         ["env", "TEST_VAR=test_value"], | ||||
|         ["env", "ANOTHER_VAR=another_value"], | ||||
|         ["author", "Rhai Test With Spaces"] | ||||
|     ]; | ||||
|     builder.config(config_options); | ||||
|     print("✓ config(): Container configured successfully"); | ||||
|      | ||||
| @@ -77,9 +80,10 @@ try { | ||||
|     print("Testing content operations..."); | ||||
|      | ||||
|     // Write content to a file | ||||
|     let script_content = `#!/bin/sh | ||||
| echo "Hello from script" | ||||
| `; | ||||
|     let script_content = ` | ||||
|         #!/bin/sh | ||||
|         echo "Hello from script" | ||||
|     `; | ||||
|     builder.write_content(script_content, "/script.sh"); | ||||
|      | ||||
|     // Make the script executable | ||||
| @@ -91,14 +95,10 @@ echo "Hello from script" | ||||
|     assert_true(script_result.stdout.contains("Hello from script"), "Script output should contain expected text"); | ||||
|     print("✓ Content operations: Script created and executed successfully"); | ||||
|      | ||||
|     // Test commit with config | ||||
|     print("Testing commit with config..."); | ||||
|     let commit_options = #{ | ||||
|         "author": "Rhai Test", | ||||
|         "message": "Test commit" | ||||
|     }; | ||||
|     builder.commit("rhai_test_commit:latest", commit_options); | ||||
|     print("✓ commit(): Container committed with config successfully"); | ||||
|     // Test commit | ||||
|     print("Testing commit..."); | ||||
|     builder.commit("rhai_test_commit:latest", [["q", ""]]); | ||||
|     print("✓ commit(): Container committed successfully"); | ||||
|      | ||||
|     // Clean up | ||||
|     builder.remove(); | ||||
| @@ -115,13 +115,15 @@ echo "Hello from script" | ||||
|     // Clean up in case of error | ||||
|     try { | ||||
|         // Remove test container if it exists | ||||
|         run("buildah rm rhai_test_container"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rm rhai_test_container"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|      | ||||
|     try { | ||||
|         // Remove test image if it exists | ||||
|         run("buildah rmi rhai_test_commit:latest"); | ||||
|     } catch(_) {} | ||||
|         let command = run("buildah rmi rhai_test_commit:latest"); | ||||
|         command.execute(); | ||||
|     } catch(err) {} | ||||
|      | ||||
|     throw err; | ||||
| } | ||||
|   | ||||
| @@ -1,155 +0,0 @@ | ||||
| // run_all_tests.rhai | ||||
| // Runs all Buildah module tests | ||||
|  | ||||
| print("=== Running Buildah Module Tests ==="); | ||||
|  | ||||
| // Custom assert function | ||||
| fn assert_true(condition, message) { | ||||
|     if !condition { | ||||
|         print(`ASSERTION FAILED: ${message}`); | ||||
|         throw message; | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Helper function to check if buildah is available | ||||
| fn is_buildah_available() { | ||||
|     try { | ||||
|         let result = run("which buildah"); | ||||
|         return result.success; | ||||
|     } catch(e) { | ||||
|         return false; | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Run each test directly | ||||
| let passed = 0; | ||||
| let failed = 0; | ||||
| let skipped = 0; | ||||
| let total = 0; | ||||
|  | ||||
| // Check if buildah is available | ||||
| let buildah_available = is_buildah_available(); | ||||
| if !buildah_available { | ||||
|     print("Buildah is not available. Skipping all Buildah tests."); | ||||
|     skipped = 3; // Skip all three tests | ||||
|     total = 3; | ||||
| } else { | ||||
|     // Test 1: Builder Pattern | ||||
|     print("\n--- Running Builder Pattern Tests ---"); | ||||
|     try { | ||||
|         // Create a builder | ||||
|         let builder = bah_new("rhai_test_container", "alpine:latest"); | ||||
|          | ||||
|         // Test basic properties | ||||
|         assert_true(builder.container_id != "", "Container ID should not be empty"); | ||||
|         assert_true(builder.name == "rhai_test_container", "Container name should match"); | ||||
|          | ||||
|         // Run a simple command | ||||
|         let result = builder.run("echo 'Hello from container'"); | ||||
|         assert_true(result.success, "Command should succeed"); | ||||
|          | ||||
|         // Clean up | ||||
|         builder.remove(); | ||||
|          | ||||
|         print("--- Builder Pattern Tests completed successfully ---"); | ||||
|         passed += 1; | ||||
|     } catch(err) { | ||||
|         print(`!!! Error in Builder Pattern Tests: ${err}`); | ||||
|         failed += 1; | ||||
|          | ||||
|         // Clean up in case of error | ||||
|         try { | ||||
|             run("buildah rm rhai_test_container"); | ||||
|         } catch(e) { | ||||
|             // Ignore errors during cleanup | ||||
|         } | ||||
|     } | ||||
|     total += 1; | ||||
|      | ||||
|     // Test 2: Image Operations | ||||
|     print("\n--- Running Image Operations Tests ---"); | ||||
|     try { | ||||
|         // Create a temporary directory for testing | ||||
|         let test_dir = "rhai_test_buildah"; | ||||
|         mkdir(test_dir); | ||||
|          | ||||
|         // Create a builder | ||||
|         let builder = bah_new("rhai_test_container", "alpine:latest"); | ||||
|          | ||||
|         // List images | ||||
|         let images = builder.images(); | ||||
|         assert_true(images.len() > 0, "There should be at least one image"); | ||||
|          | ||||
|         // Clean up | ||||
|         builder.remove(); | ||||
|         delete(test_dir); | ||||
|          | ||||
|         print("--- Image Operations Tests completed successfully ---"); | ||||
|         passed += 1; | ||||
|     } catch(err) { | ||||
|         print(`!!! Error in Image Operations Tests: ${err}`); | ||||
|         failed += 1; | ||||
|          | ||||
|         // Clean up in case of error | ||||
|         try { | ||||
|             run("buildah rm rhai_test_container"); | ||||
|             delete("rhai_test_buildah"); | ||||
|         } catch(e) { | ||||
|             // Ignore errors during cleanup | ||||
|         } | ||||
|     } | ||||
|     total += 1; | ||||
|      | ||||
|     // Test 3: Container Operations | ||||
|     print("\n--- Running Container Operations Tests ---"); | ||||
|     try { | ||||
|         // Create a builder | ||||
|         let builder = bah_new("rhai_test_container", "alpine:latest"); | ||||
|          | ||||
|         // Test reset | ||||
|         builder.reset(); | ||||
|          | ||||
|         // Create a new container | ||||
|         builder = bah_new("rhai_test_container", "alpine:latest"); | ||||
|          | ||||
|         // Run a command | ||||
|         let result = builder.run("echo 'Hello from container'"); | ||||
|         assert_true(result.success, "Command should succeed"); | ||||
|          | ||||
|         // Clean up | ||||
|         builder.remove(); | ||||
|          | ||||
|         print("--- Container Operations Tests completed successfully ---"); | ||||
|         passed += 1; | ||||
|     } catch(err) { | ||||
|         print(`!!! Error in Container Operations Tests: ${err}`); | ||||
|         failed += 1; | ||||
|          | ||||
|         // Clean up in case of error | ||||
|         try { | ||||
|             run("buildah rm rhai_test_container"); | ||||
|         } catch(e) { | ||||
|             // Ignore errors during cleanup | ||||
|         } | ||||
|     } | ||||
|     total += 1; | ||||
| } | ||||
|  | ||||
| print("\n=== Test Summary ==="); | ||||
| print(`Passed: ${passed}`); | ||||
| print(`Failed: ${failed}`); | ||||
| print(`Skipped: ${skipped}`); | ||||
| print(`Total: ${total}`); | ||||
|  | ||||
| if failed == 0 { | ||||
|     if skipped > 0 { | ||||
|         print("\n⚠️ All tests skipped or passed!"); | ||||
|     } else { | ||||
|         print("\n✅ All tests passed!"); | ||||
|     } | ||||
| } else { | ||||
|     print("\n❌ Some tests failed!"); | ||||
| } | ||||
|  | ||||
| // Return the number of failed tests (0 means success) | ||||
| failed; | ||||
		Reference in New Issue
	
	Block a user