summaryrefslogtreecommitdiff
path: root/test/system/255-auto-update.bats
blob: 59f53f77595ae39f9848e959f35cbcc1dcd89c49 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bats   -*- bats -*-
#
# Tests for automatically update images for containerized services
#

load helpers

UNIT_DIR="/usr/lib/systemd/system"
SNAME_FILE=$BATS_TMPDIR/services

function setup() {
    skip_if_remote "systemd tests are meaningless over remote"
    skip_if_rootless

    basic_setup
}

function teardown() {
    while read line; do
        if [[ "$line" =~ "podman-auto-update" ]]; then
            echo "Stop timer: $line.timer"
            systemctl stop $line.timer
            systemctl disable $line.timer
        else
            systemctl stop $line
        fi
        rm -f $UNIT_DIR/$line.{service,timer}
    done < $SNAME_FILE

    rm -f $SNAME_FILE
    run_podman ? rmi quay.io/libpod/alpine:latest
    run_podman ? rmi quay.io/libpod/alpine_nginx:latest
    run_podman ? rmi quay.io/libpod/localtest:latest
    basic_teardown
}

# This functions is used for handle the basic step in auto-update related
# tests. Including following steps:
#   1. Generate a random container name and echo it to output.
#   2. Tag the fake image before test
#   3. Start a container with io.containers.autoupdate
#   4. Generate the service file from the container
#   5. Remove the origin container
#   6. Start the container from service
function generate_service() {
    target_img_basename=$1
    autoupdate=$2

    # Please keep variable name for cname and ori_image. The
    # scripts will use them directly in following tests.
    cname=c_$(random_string)
    target_img="quay.io/libpod/$target_img_basename:latest"
    run_podman tag $IMAGE $target_img
    if [[ -n "$autoupdate" ]]; then
        label="--label io.containers.autoupdate=$autoupdate"
    else
        label=""
    fi
    run_podman run -d --name $cname $label $target_img top -d 120

    run_podman generate systemd --new $cname
    echo "$output" > "$UNIT_DIR/container-$cname.service"
    echo "container-$cname" >> $SNAME_FILE
    run_podman rm -f $cname

    systemctl daemon-reload
    systemctl start container-$cname
    systemctl status container-$cname

    run_podman inspect --format "{{.Image}}" $cname
    ori_image=$output
}

function _wait_service_ready() {
    local sname=$1

    local timeout=6
    while [[ $timeout -gt 1 ]]; do
        run systemctl is-active $sname
        if [[ $output == "active" ]]; then
            return
        fi
        sleep 1
        let timeout=$timeout-1
    done

    # Print serivce status as debug information before failed the case
    systemctl status $sname
    die "Timed out waiting for $sname to start"
}

function _confirm_update() {
    local sname=$1

    local timeout=6
    last_log=""
    while [[ $timeout -gt 1 ]]; do
        run journalctl -u $sname -n 10
        if [[ "$output" == "$last_log" ]]; then
            return
        fi
        last_log=$output
        sleep 1
        let timeout=$timeout-1
    done

    die "Timed out waiting for $sname to update"
}

# This test can fail in dev. environment because of SELinux.
# quick fix: chcon -t container_runtime_exec_t ./bin/podman
@test "podman auto-update - label io.containers.autoupdate=image" {
    run_podman images
    generate_service alpine image

    _wait_service_ready container-$cname.service
    run_podman ps -a
    run_podman auto-update
    is "$output" "Trying to pull.*" "Image is updated."
    run_podman ps -a
    _confirm_update container-$cname.service
    run_podman inspect --format "{{.Image}}" $cname
    [[ "$output" != "$ori_image" ]]
}

@test "podman auto-update - label io.containers.autoupdate=disabled" {
    generate_service alpine disabled

    _wait_service_ready container-$cname.service
    run_podman ps -a
    run_podman auto-update
    is "$output" "" "Image is not updated with disabled."
    run_podman ps -a
    _confirm_update container-$cname.service
    run_podman inspect --format "{{.Image}}" $cname
    is "$output" "$ori_image" "Image hash should not changed."
}

@test "podman auto-update - label io.containers.autoupdate=fakevalue" {
    fakevalue=$(random_string)
    generate_service alpine $fakevalue

    _wait_service_ready container-$cname.service
    run_podman ps -a
    run_podman ? auto-update
    is "$output" ".*invalid auto-update policy.*" "invalid policy setup"
    run_podman ps -a
    _confirm_update container-$cname.service
    run_podman inspect --format "{{.Image}}" $cname
    is "$output" "$ori_image" "Image hash should not changed."
}

@test "podman auto-update - label io.containers.autoupdate=local" {
    generate_service localtest local
    podman commit --change CMD=/bin/bash $cname quay.io/libpod/localtest:latest

    _wait_service_ready container-$cname.service
    run_podman ps -a
    run_podman auto-update
    run_podman ps -a
    _confirm_update container-$cname.service
    run_podman inspect --format "{{.Image}}" $cname
    [[ "$output" != "$ori_image" ]]
}

@test "podman auto-update with multiple services" {
    fakevalue=$(random_string)
    run_podman inspect --format "{{.Id}}" $IMAGE
    img_id="$output"
    cnames=()
    local -A expect_update
    local -A will_update=([image]=1 [registry]=1 [local]=1)

    for auto_update in image registry "" disabled "''" $fakevalue local
    do
        img_base="alpine"
        if [[ $auto_update == "registry" ]]; then
            img_base="alpine_nginx"
        elif [[ $auto_update == "local" ]]; then
            img_base="localtest"
        fi
        generate_service $img_base $auto_update
        cnames+=($cname)
        if [[ $auto_update == "local" ]]; then
            local_cname=$cname
        fi
        if [[ -n "$auto_update" && -n "${will_update[$auto_update]}" ]]; then
            expect_update[$cname]=1
        fi
    done

    # Only check the last service is started. Previous services should already actived.
    _wait_service_ready container-$cname.service
    run_podman commit --change CMD=/bin/bash $local_cname quay.io/libpod/localtest:latest
    run_podman ? auto-update
    update_log=$output
    for cname in "${cnames[@]}"; do
        _confirm_update container-$cname.service
    done
    count=0
    while read line; do
       if [[ "$line" =~ "Trying to pull" ]]; then
           ((count+=1))
       fi
    done <<< "$update_log"
    is "$update_log" ".*invalid auto-update policy.*" "invalid policy setup"
    is "$update_log" ".*1 error occurred.*" "invalid policy setup"
    is "$count" "2" "There are two images being updated from registry."

    for cname in "${!expect_update[@]}"; do

        is "$update_log" ".*$cname.*" "container with auto-update policy image updated"
    done

    for cname in "${cnames[@]}"; do
        run_podman inspect --format "{{.Image}}" $cname
        if [[ -n "${expect_update[$cname]}" ]]; then
            [[ "$output" != "$img_id" ]]
        else
            is "$output" "$img_id" "Image should not be changed."
        fi
    done
}

@test "podman auto-update using systemd" {
    generate_service alpine image

    cat >$UNIT_DIR/podman-auto-update-$cname.timer <<EOF
[Unit]
Description=Podman auto-update testing timer

[Timer]
OnCalendar=*-*-* *:*:0/2
Persistent=true

[Install]
WantedBy=timers.target
EOF
    cat >$UNIT_DIR/podman-auto-update-$cname.service <<EOF
[Unit]
Description=Podman auto-update testing service
Documentation=man:podman-auto-update(1)
Wants=network.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/podman auto-update

[Install]
WantedBy=multi-user.target default.target
EOF

    echo "podman-auto-update-$cname" >> $SNAME_FILE
    systemctl enable --now podman-auto-update-$cname.timer
    systemctl list-timers --all

    count=0
    failed_start=1
    while [ $count -lt 120 ]; do
        run journalctl -n 15 -u podman-auto-update-$cname.service
        if [[ "$output" =~ "Finished Podman auto-update testing service" ]]; then
            failed_start=0
            break
        fi
        ((count+=1))
        sleep 1
    done
    echo $output

    _confirm_update container-$cname.service
    run_podman inspect --format "{{.Image}}" $cname
    if [[ $failed_start == 1 ]]; then
       die "Failed to get podman auto-update service finished"
    fi
    [[ "$output" != "$ori_image" ]]
}

# vim: filetype=sh