Skip to content

Commit 7724fb1

Browse files
test: Add unit and integration tests for edit command
Signed-off-by: Imranullah Khan <imranullahkhann2004@gmail.com>
1 parent 1c8ceeb commit 7724fb1

2 files changed

Lines changed: 246 additions & 0 deletions

File tree

internal/archive_modifiers_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package internal
2+
3+
import (
4+
"archive/tar"
5+
"bytes"
6+
"encoding/json"
7+
"testing"
8+
)
9+
10+
func TestRemapEnvSlice(t *testing.T) {
11+
env := []any{"A=1", "PORT=5000", 123}
12+
13+
got := remapEnvSlice(env, "5000", "9999")
14+
expected := []any{"A=1", "PORT=9999", 123}
15+
16+
if len(got) != len(expected) {
17+
t.Errorf("Expected env length %d, got %d", len(expected), len(got))
18+
}
19+
for i := range expected {
20+
if got[i] != expected[i] {
21+
t.Errorf("Expected env[%d]=%v, got %v", i, expected[i], got[i])
22+
}
23+
}
24+
}
25+
26+
func TestRemapEnvRecursive(t *testing.T) {
27+
obj := map[string]any{
28+
"env": []any{"PORT=5000", "FOO=bar"},
29+
"process": map[string]any{
30+
"env": []any{"A=1", "PORT=5000"},
31+
},
32+
}
33+
34+
remapEnvRecursive(obj, "5000", "9999")
35+
36+
rootEnv := obj["env"].([]any)
37+
if rootEnv[0] != "PORT=9999" {
38+
t.Errorf("Expected root env PORT to be remapped, got %v", rootEnv[0])
39+
}
40+
41+
nestedEnv := obj["process"].(map[string]any)["env"].([]any)
42+
if nestedEnv[1] != "PORT=9999" {
43+
t.Errorf("Expected nested env PORT to be remapped, got %v", nestedEnv[1])
44+
}
45+
}
46+
47+
func TestRemapPortMappings(t *testing.T) {
48+
obj := map[string]any{
49+
"newPortMappings": []any{
50+
map[string]any{"container_port": float64(5000)},
51+
map[string]any{"container_port": float64(6000)},
52+
},
53+
"nested": map[string]any{
54+
"portMappings": []any{
55+
map[string]any{"container_port": float64(5000)},
56+
},
57+
},
58+
}
59+
60+
remapPortMappings(obj, "5000", "9999")
61+
62+
top := obj["newPortMappings"].([]any)
63+
if top[0].(map[string]any)["container_port"].(float64) != 9999 {
64+
t.Errorf("Expected top-level container_port to be 9999")
65+
}
66+
if top[1].(map[string]any)["container_port"].(float64) != 6000 {
67+
t.Errorf("Expected non-target top-level container_port to stay 6000")
68+
}
69+
70+
nested := obj["nested"].(map[string]any)["portMappings"].([]any)
71+
if nested[0].(map[string]any)["container_port"].(float64) != 9999 {
72+
t.Errorf("Expected nested container_port to be 9999")
73+
}
74+
}
75+
76+
func TestRemapConfigDump(t *testing.T) {
77+
input := []byte(`{
78+
"newPortMappings":[{"container_port":5000},{"container_port":7000}],
79+
"env":["FOO=bar","PORT=5000"],
80+
"nested":{"env":["PORT=5000","X=1"]}
81+
}`)
82+
hdr := &tar.Header{Name: "config.dump", Size: int64(len(input))}
83+
84+
newHdr, out, err := remapConfigDump(hdr, bytes.NewReader(input), "5000", "9999")
85+
if err != nil {
86+
t.Fatalf("Unexpected error: %v", err)
87+
}
88+
89+
if newHdr.Size != int64(len(out)) {
90+
t.Errorf("Expected header size %d, got %d", len(out), newHdr.Size)
91+
}
92+
93+
var got map[string]any
94+
if err := json.Unmarshal(out, &got); err != nil {
95+
t.Fatalf("Failed to unmarshal output JSON: %v", err)
96+
}
97+
98+
ports := got["newPortMappings"].([]any)
99+
if ports[0].(map[string]any)["container_port"].(float64) != 9999 {
100+
t.Errorf("Expected first mapped port to be 9999")
101+
}
102+
if ports[1].(map[string]any)["container_port"].(float64) != 7000 {
103+
t.Errorf("Expected non-target mapped port to remain 7000")
104+
}
105+
106+
env := got["env"].([]any)
107+
if env[1] != "PORT=9999" {
108+
t.Errorf("Expected root PORT env to be remapped, got %v", env[1])
109+
}
110+
111+
nestedEnv := got["nested"].(map[string]any)["env"].([]any)
112+
if nestedEnv[0] != "PORT=9999" {
113+
t.Errorf("Expected nested PORT env to be remapped, got %v", nestedEnv[0])
114+
}
115+
}
116+
117+
func TestRemapSpecDump(t *testing.T) {
118+
input := []byte(`{"process":{"env":["PORT=5000","FOO=bar"]}}`)
119+
hdr := &tar.Header{Name: "spec.dump", Size: int64(len(input))}
120+
121+
newHdr, out, err := remapSpecDump(hdr, bytes.NewReader(input), "5000", "9999")
122+
if err != nil {
123+
t.Fatalf("Unexpected error: %v", err)
124+
}
125+
126+
if newHdr.Size != int64(len(out)) {
127+
t.Errorf("Expected header size %d, got %d", len(out), newHdr.Size)
128+
}
129+
130+
var got map[string]any
131+
if err := json.Unmarshal(out, &got); err != nil {
132+
t.Fatalf("Failed to unmarshal output JSON: %v", err)
133+
}
134+
135+
env := got["process"].(map[string]any)["env"].([]any)
136+
if env[0] != "PORT=9999" {
137+
t.Errorf("Expected PORT env to be remapped, got %v", env[0])
138+
}
139+
}

test/checkpointctl.bats

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,3 +1328,110 @@ EOF
13281328

13291329
PATH="$ORIG_PATH"
13301330
}
1331+
1332+
@test "Run checkpointctl edit with no flags" {
1333+
checkpointctl edit /does-not-exist
1334+
[ "$status" -eq 1 ]
1335+
[[ "${output}" == "Error: no edit operation specified; use --tcp-listen-remap" ]]
1336+
}
1337+
1338+
@test "Run checkpointctl edit with --tcp-listen-remap and invalid port format" {
1339+
checkpointctl edit --tcp-listen-remap abc /does-not-exist
1340+
[ "$status" -eq 1 ]
1341+
[[ "${output}" == "Error: invalid parameters: expected oldport:newport" ]]
1342+
1343+
checkpointctl edit --tcp-listen-remap 8080:ab /does-not-exist
1344+
[ "$status" -eq 1 ]
1345+
[[ "${output}" == "Error: invalid parameters: expected oldport:newport" ]]
1346+
}
1347+
1348+
@test "Run checkpointctl edit with --tcp-listen-remap and invalid port number" {
1349+
checkpointctl edit --tcp-listen-remap -1:8080 /does-not-exist
1350+
[ "$status" -eq 1 ]
1351+
[[ "$output" == "Error: invalid parameters: expected oldport:newport" ]]
1352+
1353+
checkpointctl edit --tcp-listen-remap 8080:65536 /does-not-exist
1354+
[ "$status" -eq 1 ]
1355+
[[ "$output" == "Error: new port 65536 is out of valid range (1-65535)" ]]
1356+
}
1357+
1358+
@test "Run checkpointctl edit with --tcp-listen-remap and non existing archive" {
1359+
checkpointctl edit --tcp-listen-remap 80:8080 /does-not-exist
1360+
[ "$status" -eq 1 ]
1361+
[[ "$output" == "Error: open /does-not-exist: no such file or directory" ]]
1362+
}
1363+
1364+
@test "Run checkpointctl edit with --tcp-listen-remap and empty archive" {
1365+
touch "$TEST_TMP_DIR1"/empty.tar
1366+
checkpointctl edit --tcp-listen-remap 80:8080 "$TEST_TMP_DIR1"/empty.tar
1367+
[ "$status" -eq 1 ]
1368+
[[ ${lines[0]} == *"checkpoint directory is missing in the archive file"* ]]
1369+
}
1370+
1371+
@test "Run checkpointctl edit with --tcp-listen-remap on uncompressed tar" {
1372+
cp data/config.dump \
1373+
data/spec.dump "$TEST_TMP_DIR1"
1374+
mkdir "$TEST_TMP_DIR1"/checkpoint
1375+
cp test-imgs/pstree.img \
1376+
test-imgs/core-*.img \
1377+
test-imgs/files.img \
1378+
test-imgs/ids-*.img \
1379+
test-imgs/fdinfo-*.img "$TEST_TMP_DIR1"/checkpoint
1380+
1381+
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )
1382+
checkpointctl edit --tcp-listen-remap 5000:80 "$TEST_TMP_DIR2"/test.tar
1383+
[ "$status" -eq 0 ]
1384+
[[ ${lines[0]} == *"Successfully remapped port 5000 -> 80"* ]]
1385+
1386+
checkpointctl inspect "$TEST_TMP_DIR2"/test.tar --sockets
1387+
[[ "$status" -eq 0 ]]
1388+
found=0
1389+
for line in "${lines[@]}"; do
1390+
if [[ "$line" == *"[TCP (LISTEN)]"* && "$line" == *"0.0.0.0:80"* ]]; then
1391+
found=1
1392+
break
1393+
fi
1394+
done
1395+
1396+
[ "$found" -eq 1 ]
1397+
}
1398+
1399+
@test "Run checkpointctl edit with --tcp-listen-remap on compressed tar" {
1400+
cp data/config.dump \
1401+
data/spec.dump "$TEST_TMP_DIR1"
1402+
mkdir "$TEST_TMP_DIR1"/checkpoint
1403+
cp test-imgs/pstree.img \
1404+
test-imgs/core-*.img \
1405+
test-imgs/files.img \
1406+
test-imgs/ids-*.img \
1407+
test-imgs/fdinfo-*.img "$TEST_TMP_DIR1"/checkpoint
1408+
1409+
( cd "$TEST_TMP_DIR1" && tar czf "$TEST_TMP_DIR2"/test.tar.gz . )
1410+
checkpointctl edit --tcp-listen-remap 5000:80 "$TEST_TMP_DIR2"/test.tar.gz
1411+
[ "$status" -eq 0 ]
1412+
[[ ${lines[0]} == *"Successfully remapped port 5000 -> 80"* ]]
1413+
1414+
checkpointctl inspect "$TEST_TMP_DIR2"/test.tar.gz --sockets
1415+
[[ "$status" -eq 0 ]]
1416+
found=0
1417+
for line in "${lines[@]}"; do
1418+
if [[ "$line" == *"[TCP (LISTEN)]"* && "$line" == *"0.0.0.0:80"* ]]; then
1419+
found=1
1420+
break
1421+
fi
1422+
done
1423+
1424+
[ "$found" -eq 1 ]
1425+
}
1426+
1427+
@test "Run checkpointctl edit with --tcp-listen-remap with non matching port" {
1428+
cp data/config.dump \
1429+
data/spec.dump "$TEST_TMP_DIR1"
1430+
mkdir "$TEST_TMP_DIR1"/checkpoint
1431+
cp test-imgs/files.img "$TEST_TMP_DIR1"/checkpoint
1432+
1433+
( cd "$TEST_TMP_DIR1" && tar czf "$TEST_TMP_DIR2"/test.tar.gz . )
1434+
checkpointctl edit --tcp-listen-remap 8080:80 "$TEST_TMP_DIR2"/test.tar.gz
1435+
[ "$status" -eq 1 ]
1436+
[[ ${lines[0]} == *"no TCP listen sockets found with source port 8080"* ]]
1437+
}

0 commit comments

Comments
 (0)