Skip to content

Commit

Permalink
test(crit): add integration test
Browse files Browse the repository at this point in the history
This test performs decode and encode on an image file and compares the
result with the original file. The test passes if both files are
equivalent byte for byte.

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
  • Loading branch information
snprajwal committed Aug 10, 2022
1 parent 2d21b63 commit a47967c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ test/test.coverage
test/piggie/piggie
test/phaul/phaul
test/phaul/phaul.coverage
test/crit/loop
test/crit/*.(img|log|json)
test/crit/stats-*
test/loop/loop
test/crit/crit-test
test/crit/test-imgs
image
scripts/*.h
scripts/expected.go
Expand Down
29 changes: 21 additions & 8 deletions test/crit/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
CC ?= gcc
GO ?= go
CRIU ?= criu

all: test clean
all: integration-test e2e-test clean

test: gen-imgs
integration-test: test-imgs crit-test
@echo "Running integration test"
@./crit-test

e2e-test: ../../crit/bin/crit test-imgs crit-test.sh
@echo "Running E2E test"
@./crit-test.sh

gen-imgs: loop
$(eval PID := $(shell ./loop))
criu dump -v4 -o dump.log -D ./ -t $(PID)
test-imgs: ../loop/loop
$(eval PID := $(shell ../loop/loop))
mkdir -p $@
$(CRIU) dump -v4 -o dump.log -D $@ -t $(PID)

../../crit/bin/crit:
$(MAKE) -C ../../crit bin/crit

loop: loop.c
../loop/loop: ../loop/loop.c
$(CC) $^ -o $@

crit-test: main.go
$(GO) build -v -o $@ $^

clean:
@rm -f *.img *.log *.json stats-*
@rm -rf test-imgs

.PHONY: all test gen-imgs clean
.PHONY: all test integration-test e2e-test clean
81 changes: 35 additions & 46 deletions test/crit/crit-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
set -x

CRIT=../../crit/bin/crit
TEST_IMG_DIR=test-imgs

function gen_img_list {
images_list=$(ls -1 ./*.img)
images_list=$(find "$TEST_IMG_DIR" -regex '^[^\.]*\.img$')
if [ -z "$images_list" ]; then
echo "Failed to generate images"
exit 1
fi
}

function run_test1 {
function recode_test {
for x in $images_list
do
echo "=== $x"
if [[ $x == *pages* ]]; then
echo "skip"
echo "=== SKIP"
continue
fi

Expand All @@ -33,62 +34,50 @@ function run_test1 {
}


function run_test2 {
PROTO_IN=./inventory.img
JSON_IN=$(mktemp -p ./ tmp.XXXXXXXXXX.json)
OUT=$(mktemp -p ./ tmp.XXXXXXXXXX.img)
function command_test {
PROTO_IN="$TEST_IMG_DIR"/inventory.img
JSON_IN=$(mktemp -p "$TEST_IMG_DIR" tmp.XXXXXXXXXX.json)
OUT=$(mktemp -p "$TEST_IMG_DIR" tmp.XXXXXXXXXX.img)

# prepare
${CRIT} decode -i "${PROTO_IN}" -o "${JSON_IN}"
$CRIT decode -i "$PROTO_IN" -o "$JSON_IN"

# proto in - json out decode
cat "${PROTO_IN}" | ${CRIT} decode || exit 1
cat "${PROTO_IN}" | ${CRIT} decode -o "${OUT}" || exit 1
cat "${PROTO_IN}" | ${CRIT} decode > "${OUT}" || exit 1
${CRIT} decode -i "${PROTO_IN}" || exit 1
${CRIT} decode -i "${PROTO_IN}" -o "${OUT}" || exit 1
${CRIT} decode -i "${PROTO_IN}" > "${OUT}" || exit 1
${CRIT} decode < "${PROTO_IN}" || exit 1
${CRIT} decode -o "${OUT}" < "${PROTO_IN}" || exit 1
${CRIT} decode < "${PROTO_IN}" > "${OUT}" || exit 1
$CRIT decode -i "$PROTO_IN" || exit 1
$CRIT decode -i "$PROTO_IN" -o "$OUT" || exit 1
$CRIT decode -i "$PROTO_IN" > "$OUT" || exit 1
$CRIT decode < "$PROTO_IN" || exit 1
$CRIT decode -o "$OUT" < "$PROTO_IN" || exit 1
$CRIT decode < "$PROTO_IN" > "$OUT" || exit 1

# proto in - json out encode -> should fail
cat "${PROTO_IN}" | ${CRIT} encode || true
cat "${PROTO_IN}" | ${CRIT} encode -o "${OUT}" || true
cat "${PROTO_IN}" | ${CRIT} encode > "${OUT}" || true
${CRIT} encode -i "${PROTO_IN}" || true
${CRIT} encode -i "${PROTO_IN}" -o "${OUT}" || true
${CRIT} encode -i "${PROTO_IN}" > "${OUT}" || true
# json in - proto out decode -> should fail
$CRIT decode -i "$JSON_IN" || true
$CRIT decode -i "$JSON_IN" -o "$OUT" || true
$CRIT decode -i "$JSON_IN" > "$OUT" || true

# json in - proto out encode
cat "${JSON_IN}" | ${CRIT} encode || exit 1
cat "${JSON_IN}" | ${CRIT} encode -o "${OUT}" || exit 1
cat "${JSON_IN}" | ${CRIT} encode > "${OUT}" || exit 1
${CRIT} encode -i "${JSON_IN}" || exit 1
${CRIT} encode -i "${JSON_IN}" -o "${OUT}" || exit 1
${CRIT} encode -i "${JSON_IN}" > "${OUT}" || exit 1
${CRIT} encode < "${JSON_IN}" || exit 1
${CRIT} encode -o "${OUT}" < "${JSON_IN}" || exit 1
${CRIT} encode < "${JSON_IN}" > "${OUT}" || exit 1
$CRIT encode -i "$JSON_IN" || exit 1
$CRIT encode -i "$JSON_IN" -o "$OUT" || exit 1
$CRIT encode -i "$JSON_IN" > "$OUT" || exit 1
$CRIT encode < "$JSON_IN" || exit 1
$CRIT encode -o "$OUT" < "$JSON_IN" || exit 1
$CRIT encode < "$JSON_IN" > "$OUT" || exit 1

# json in - proto out decode -> should fail
cat "${JSON_IN}" | ${CRIT} decode || true
cat "${JSON_IN}" | ${CRIT} decode -o "${OUT}" || true
cat "${JSON_IN}" | ${CRIT} decode > "${OUT}" || true
${CRIT} decode -i "${JSON_IN}" || true
${CRIT} decode -i "${JSON_IN}" -o "${OUT}" || true
${CRIT} decode -i "${JSON_IN}" > "${OUT}" || true
# proto in - json out encode -> should fail
$CRIT encode -i "$PROTO_IN" || true
$CRIT encode -i "$PROTO_IN" -o "$OUT" || true
$CRIT encode -i "$PROTO_IN" > "$OUT" || true

# explore image directory
${CRIT} x ./ ps || exit 1
${CRIT} x ./ fds || exit 1
${CRIT} x ./ mems || exit 1
${CRIT} x ./ rss || exit 1
$CRIT x "$TEST_IMG_DIR" ps || exit 1
$CRIT x "$TEST_IMG_DIR" fds || exit 1
$CRIT x "$TEST_IMG_DIR" mems || exit 1
$CRIT x "$TEST_IMG_DIR" rss || exit 1
}

echo "Generating image list..."
gen_img_list
echo "Testing recode..."
run_test1
recode_test
echo "Testing commands..."
run_test2
command_test
110 changes: 110 additions & 0 deletions test/crit/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/checkpoint-restore/go-criu/v5/crit"
)

const TEST_IMG_DIR = "test-imgs"

func main() {
// Get list of image files
imgs, err := getImgs()
if err != nil {
log.Fatal(err)
}
// Run recode test
if err = recodeImgs(imgs); err != nil {
log.Fatal(err)
}
log.Println("=== PASS")
}

func recodeImgs(imgs []string) error {
for _, img := range imgs {
log.Println("===", img)
testImg := fmt.Sprintf("%s.test.img", img)
c := crit.New(img, testImg, "", false, false)
// Decode the binary image file
decodedImg, err := c.Decode()
if err != nil {
return errors.New(fmt.Sprint("[DECODE]: ", err))
}
// Encode it into test binary image file
if err = c.Encode(decodedImg); err != nil {
return errors.New(fmt.Sprint("[ENCODE]: ", err))
}
// Open and compare original and test files
imgBytes, err := os.ReadFile(img)
if err != nil {
return err
}
testImgBytes, err := os.ReadFile(testImg)
if err != nil {
return err
}
if !bytes.Equal(imgBytes, testImgBytes) {
return errors.New(fmt.Sprint("[RECODE]: Files do not match"))
}
}

return nil
}

func getImgs() ([]string, error) {
// Certain image files generated by CRIU do not
// use the protobuf format and contain raw binary
// data. Some image files are also generated using
// external tools (ifaddr, route, tmpfs). As these
// images cannot be processed by CRIT, they are
// excluded from the tests.
var skipImgs = []string{
"pages-",
"pages-shmem-",
"iptables-",
"ip6tables-",
"nftables-",
"route-",
"route6-",
"ifaddr-",
"tmpfs-",
"tmpfs-dev-",
"autofs-",
"netns-ct-",
"netns-exp-",
"rule-",
}
// "*.test.img", "*.json.img" or "tmp.*.img" files
// must be skipped as they are generated by tests
criuImg := regexp.MustCompile(`^[^\.]*\.img$`)
dir, err := filepath.Glob(fmt.Sprintf("%s/*.img", TEST_IMG_DIR))
if err != nil {
return nil, err
}
var imgs []string

nextFile:
for _, file := range dir {
if filepath.Ext(file) == ".img" {
if !criuImg.MatchString(file) {
continue
}
for _, skip := range skipImgs {
if strings.HasPrefix(filepath.Base(file), skip) {
continue nextFile
}
}
imgs = append(imgs, file)
}
}

return imgs, nil
}
File renamed without changes.

0 comments on commit a47967c

Please sign in to comment.