๐Ÿ“ฆ da-x / deltaimage

๐Ÿ“„ run ยท 146 lines
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#!/bin/bash

set -eu
set -o pipefail
set +o posix
shopt -s inherit_errexit

build-small-static-exe() {
    local arch target output_exe

    arch=$(uname -m)
    case "$arch" in
        x86_64)      target="x86_64-unknown-linux-gnu" ;;
        aarch64|arm64) target="aarch64-unknown-linux-gnu" ;;
        *)
            echo "Error: unsupported architecture '$arch'" >&2
            return 1
            ;;
    esac

    RUSTFLAGS="-C target-feature=+crt-static" \
      cargo build --profile release-lto --target "$target"

    output_exe="target/${target}/release-lto/deltaimage"
    strip "$output_exe"
}

get-version() {
    cat Cargo.toml | grep "^version =" | awk -F\" '{print $2}'
}

build-docker-image() {
    local version

    version=$(get-version)

    echo Building: deltaimage/deltaimage:${version}

    docker build  --network=host . --tag deltaimage/deltaimage:${version}
}

# Tests

test-simple() {
    local version
    local IMAGE_B IMAGE_REPOS IMAGE_A

    IMAGE_REPOS="$1"
    IMAGE_A="$2"
    IMAGE_B="$3"

    version=$(get-version)

    echo
    echo "Target image history"
    echo

    # docker pull ${IMAGE_REPOS}:${IMAGE_B}
    docker history ${IMAGE_REPOS}:${IMAGE_B}

    echo
    echo "Checking delay image generation:"
    echo

    set -x
    docker run deltaimage/deltaimage:${version} docker-file diff \
	${IMAGE_REPOS}:${IMAGE_A} ${IMAGE_REPOS}:${IMAGE_B} |
        docker build --no-cache -t local/deltaimage:delta -
    set +x

    docker history local/deltaimage:delta

    echo
    echo "Checking restored image:"
    echo

    set -x
    docker run deltaimage/deltaimage:${version} docker-file apply \
	local/deltaimage:delta | \
        docker build --no-cache -t local/deltaimage:${IMAGE_B} -
    set +x

    docker history local/deltaimage:${IMAGE_B}

    local instance_id

    echo "Comparing ${IMAGE_REPOS}:${IMAGE_B} and local/deltaimage:${IMAGE_B}"

    local tmp_dir
    tmp_dir=$(mktemp -d -t prefix-XXXXXXXXXX)
    instance_id=$(docker create ${IMAGE_REPOS}:${IMAGE_B} x 2>/dev/null)
    sleep 0.1
    docker export ${instance_id} | tar -tvf - > ${tmp_dir}/before
    docker rm -f ${instance_id} >/dev/null

    instance_id=$(docker create local/deltaimage:${IMAGE_B} x 2>/dev/null)
    sleep 0.1
    docker export ${instance_id} | tar -tvf - > ${tmp_dir}/after
    docker rm -f ${instance_id} >/dev/null

    set +e
    diff -urN ${tmp_dir}/before ${tmp_dir}/after
    local e=$?
    set -e

    local nrfiles

    nrfiles=$(cat ${tmp_dir}/before | wc -l)

    if [[ "${e}" == "0" ]] ; then
	echo "No difference for ${nrfiles} files in resultant image"
    fi

    rm -rf ${tmp_dir}

    docker image rm local/deltaimage:${IMAGE_B} local/deltaimage:delta

    return $e
}

test-ubuntu-1() {
    test-simple ubuntu mantic-20230607 mantic-20230624
}

test-rocky-1() {
    # Wildly differing images
    test-simple rockylinux 8-minimal 9.2.20230513
}

test-alpine-1() {
    test-simple alpine 3.17.4 3.18.2
}

test-postgres-1() {
    test-simple postgres 16beta2-alpine3.17 16beta2-alpine3.18
}

tests() {
    test-ubuntu-1
    test-rocky-1
    test-alpine-1
    test-postgres-1
}

"$@"