Skip to content

Commit 553c571

Browse files
committed
fix tests
1 parent fe54899 commit 553c571

File tree

4 files changed

+27
-44
lines changed

4 files changed

+27
-44
lines changed

registry/coder/modules/archive/main.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ const installScriptInContainer = async (
2525
"mkdir -p /tmp/coder-script-data/bin /tmp/coder-script-data",
2626
]);
2727

28-
await execContainer(id, ["sh", "-c", "apk add --no-cache tar gzip zstd"]);
28+
await execContainer(id, [
29+
"sh",
30+
"-c",
31+
"apk add --no-cache bash tar gzip zstd",
32+
]);
2933

3034
const resp = await execContainer(
3135
id,
32-
["sh", "-c", instance.script],
36+
["bash", "-c", instance.script],
3337
[
3438
"--env",
3539
"CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin",
@@ -40,10 +44,12 @@ const installScriptInContainer = async (
4044
);
4145

4246
const stdout = resp.stdout.trim().split("\n");
47+
const stderr = resp.stderr.trim().split("\n");
4348
return {
4449
id,
4550
exitCode: resp.exitCode,
4651
stdout,
52+
stderr,
4753
};
4854
};
4955

@@ -66,9 +72,11 @@ describe("archive", async () => {
6672
);
6773
const result = await installScriptInContainer(state, "alpine");
6874
expect(result.exitCode).toBe(0);
69-
expect(result.stdout[result.stdout.length - 1]).toEqual(
70-
"Installed extract script to: /tmp/coder-script-data/bin/coder-archive-create",
71-
);
75+
expect(result.stdout).toEqual([
76+
"Installed archive library to: /tmp/coder-script-data/archive-lib.sh",
77+
"Installed create script to: /tmp/coder-script-data/bin/coder-archive-create",
78+
"Installed extract script to: /tmp/coder-script-data/bin/coder-archive-extract",
79+
]);
7280

7381
// Verify the script is executable
7482
const execCheck = await execContainer(result.id, [
@@ -79,33 +87,26 @@ describe("archive", async () => {
7987
expect(execCheck.stdout.trim()).toEqual("ok");
8088
});
8189

82-
return;
83-
8490
it("creates a gzip archive and prints the archive path", async () => {
8591
const state = await runTerraformApply(import.meta.dir, {
8692
agent_id: "agent-123",
93+
directory: "$HOME",
8794
paths: `["/etc/hostname"]`,
8895
compression: "gzip",
8996
});
9097

91-
expect(state.outputs.archive_path.value).toEqual("");
9298
const result = await installScriptInContainer(state, "alpine");
9399

94-
// Install bash to run the script
95-
await execContainer(result.id, ["sh", "-c", "apk add --no-cache bash"]);
96-
97100
// Run the archiver with explicit name and output dir
98101
const run = await execContainer(result.id, [
99102
"sh",
100103
"-c",
101-
"/bin/bash /tmp/coder-script-data/bin/coder-archive-create -n test-archive -o /tmp/backup /etc/hosts",
104+
"/tmp/coder-script-data/bin/coder-archive-create -f /tmp/backup/test-archive.tar.gz /etc/hosts",
102105
]);
103106

104-
const out = run.stdout.trim().split("\n").filter(Boolean);
105107
expect(run.exitCode).toBe(0);
106-
// Only the archive path should be printed to stdout
107-
expect(out.length).toBe(1);
108-
expect(out[0]).toEqual("/tmp/backup/test-archive.tar.gz");
108+
// Only the archive path should be printed to stdout.
109+
expect(run.stdout.trim()).toEqual("/tmp/backup/test-archive.tar.gz");
109110

110111
// Validate file exists and is a valid gzip tar
111112
const exists = await execContainer(result.id, [
@@ -123,6 +124,8 @@ describe("archive", async () => {
123124
expect(tarList.exitCode).toBe(0);
124125
}, 20000);
125126

127+
return;
128+
126129
it("creates a zstd-compressed archive when requested", async () => {
127130
const state = await runTerraformApply(import.meta.dir, {
128131
agent_id: "agent-123",

registry/coder/modules/archive/main.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@ variable "paths" {
2020
default = ["."]
2121
}
2222

23-
variable "include_patterns" {
24-
description = "Include patterns for the archive."
25-
type = list(string)
26-
default = []
27-
}
28-
2923
variable "exclude_patterns" {
30-
description = "Exclude patterns for the archive, these take precedence over include patterns."
24+
description = "Exclude patterns for the archive."
3125
type = list(string)
3226
default = []
3327
}
@@ -85,7 +79,6 @@ locals {
8579
# Ensure ~ is expanded because it cannot be expanded inside quotes in a
8680
# templated shell script.
8781
paths = [for v in var.paths : replace(v, "^~", "$HOME")]
88-
include_patterns = var.include_patterns == null ? [] : [for v in var.include_patterns : replace(v, "^~", "$HOME")]
8982
exclude_patterns = var.exclude_patterns == null ? [] : [for v in var.exclude_patterns : replace(v, "^~", "$HOME")]
9083
directory = replace(var.directory, "^~", "$HOME")
9184
output_dir = replace(replace(var.output_dir, "^~", "$HOME"), "/+$", "")
@@ -111,7 +104,6 @@ resource "coder_script" "archive_start_script" {
111104
script = templatefile("${path.module}/run.sh", {
112105
TF_LIB_B64 = base64encode(file("${path.module}/scripts/archive-lib.sh")),
113106
TF_PATHS = join(" ", formatlist("%q", local.paths)),
114-
TF_INCLUDE_PATTERNS = join(" ", formatlist("%q", local.include_patterns)),
115107
TF_EXCLUDE_PATTERNS = join(" ", formatlist("%q", local.exclude_patterns)),
116108
TF_COMPRESSION = var.compression,
117109
TF_ARCHIVE_PATH = local.archive_path,

registry/coder/modules/archive/run.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ EXTRACT_WAIT_TIMEOUT="${TF_EXTRACT_WAIT_TIMEOUT}"
77

88
# Set script defaults from Terraform.
99
DEFAULT_PATHS=(${TF_PATHS})
10-
DEFAULT_INCLUDE_PATTERNS=(${TF_INCLUDE_PATTERNS})
1110
DEFAULT_EXCLUDE_PATTERNS=(${TF_EXCLUDE_PATTERNS})
1211
DEFAULT_COMPRESSION="${TF_COMPRESSION}"
1312
DEFAULT_ARCHIVE_PATH="${TF_ARCHIVE_PATH}"
@@ -40,7 +39,6 @@ set -euo pipefail
4039
$(
4140
declare -p \
4241
DEFAULT_PATHS \
43-
DEFAULT_INCLUDE_PATTERNS \
4442
DEFAULT_EXCLUDE_PATTERNS \
4543
DEFAULT_COMPRESSION \
4644
DEFAULT_ARCHIVE_PATH \
@@ -53,8 +51,8 @@ EOF
5351
mv "$tmp" "$2"
5452
}
5553

56-
CREATE_WRAPPER_PATH="$CODER_SCRIPT_BIN_DIR/coder-create-archive"
57-
EXTRACT_WRAPPER_PATH="$CODER_SCRIPT_BIN_DIR/coder-extract-archive"
54+
CREATE_WRAPPER_PATH="$CODER_SCRIPT_BIN_DIR/coder-archive-create"
55+
EXTRACT_WRAPPER_PATH="$CODER_SCRIPT_BIN_DIR/coder-archive-extract"
5856
create_wrapper create_archive "$CREATE_WRAPPER_PATH"
5957
create_wrapper extract_archive "$EXTRACT_WRAPPER_PATH"
6058

registry/coder/modules/archive/scripts/archive-lib.sh

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ error() {
1414

1515
load_defaults() {
1616
DEFAULT_PATHS=("${DEFAULT_PATHS[@]:-$HOME}")
17-
DEFAULT_INCLUDE_PATTERNS=("${DEFAULT_INCLUDE_PATTERNS[@]:-}")
1817
DEFAULT_EXCLUDE_PATTERNS=("${DEFAULT_EXCLUDE_PATTERNS[@]:-}")
1918
DEFAULT_COMPRESSION="${DEFAULT_COMPRESSION:-gzip}"
2019
DEFAULT_ARCHIVE_PATH="${DEFAULT_ARCHIVE_PATH:-/tmp/coder-archive.tar.gz}"
@@ -112,8 +111,8 @@ create_archive() {
112111
log "Creating archive:"
113112
log " Compression: $compression"
114113
log " Directory: $directory"
115-
log " Path: $file"
116-
log " Include: ${DEFAULT_PATHS[*]}"
114+
log " Archive: $file"
115+
log " Paths: ${DEFAULT_PATHS[*]}"
117116
log " Exclude: ${DEFAULT_EXCLUDE_PATTERNS[*]}"
118117

119118
local -a tar_opts=(-c -f "$file" -C "$directory")
@@ -130,18 +129,14 @@ create_archive() {
130129
;;
131130
esac
132131

133-
for path in "${DEFAULT_INCLUDE_PATTERNS[@]}"; do
134-
tar_opts+=(--include="$path")
135-
done
136-
137132
for path in "${DEFAULT_EXCLUDE_PATTERNS[@]}"; do
138-
tar_opts+=(--exclude="$path")
133+
tar_opts+=(--exclude "$path")
139134
done
140135

141136
umask 077
142137
tar "${tar_opts[@]}" "${DEFAULT_PATHS[@]}"
143138

144-
printf '%s\n' "$name"
139+
printf '%s\n' "$file"
145140
}
146141

147142
usage_extract_archive() {
@@ -216,7 +211,6 @@ extract_archive() {
216211
log " Compression: $compression"
217212
log " Directory: $directory"
218213
log " Path: $file"
219-
log " Include: ${DEFAULT_PATHS[*]}"
220214
log " Exclude: ${DEFAULT_EXCLUDE_PATTERNS[*]}"
221215

222216
# Create archive with appropriate compression, passing paths as separate args to handle spaces safely
@@ -236,12 +230,8 @@ extract_archive() {
236230
;;
237231
esac
238232

239-
for path in "${DEFAULT_INCLUDE_PATTERNS[@]}"; do
240-
tar_opts+=(--include="$path")
241-
done
242-
243233
for path in "${DEFAULT_EXCLUDE_PATTERNS[@]}"; do
244-
tar_opts+=(--exclude="$path")
234+
tar_opts+=(--exclude "$path")
245235
done
246236

247237
umask 077

0 commit comments

Comments
 (0)