~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/test/suites/snapshots.sh

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
test_snapshots() {
 
4
  ensure_import_testimage
 
5
  ensure_has_localhost_remote "${LXD_ADDR}"
 
6
 
 
7
  lxc init testimage foo
 
8
 
 
9
  lxc snapshot foo
 
10
  # FIXME: make this backend agnostic
 
11
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
12
    [ -d "${LXD_DIR}/snapshots/foo/snap0" ]
 
13
  fi
 
14
 
 
15
  lxc snapshot foo
 
16
  # FIXME: make this backend agnostic
 
17
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
18
    [ -d "${LXD_DIR}/snapshots/foo/snap1" ]
 
19
  fi
 
20
 
 
21
  lxc snapshot foo tester
 
22
  # FIXME: make this backend agnostic
 
23
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
24
    [ -d "${LXD_DIR}/snapshots/foo/tester" ]
 
25
  fi
 
26
 
 
27
  lxc copy foo/tester foosnap1
 
28
  # FIXME: make this backend agnostic
 
29
  if [ "${LXD_BACKEND}" != "lvm" ]; then
 
30
    [ -d "${LXD_DIR}/containers/foosnap1/rootfs" ]
 
31
  fi
 
32
 
 
33
  lxc delete foo/snap0
 
34
  # FIXME: make this backend agnostic
 
35
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
36
    [ ! -d "${LXD_DIR}/snapshots/foo/snap0" ]
 
37
  fi
 
38
 
 
39
  # no CLI for this, so we use the API directly (rename a snapshot)
 
40
  wait_for "${LXD_ADDR}" my_curl -X POST "https://${LXD_ADDR}/1.0/containers/foo/snapshots/tester" -d "{\"name\":\"tester2\"}"
 
41
  # FIXME: make this backend agnostic
 
42
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
43
    [ ! -d "${LXD_DIR}/snapshots/foo/tester" ]
 
44
  fi
 
45
 
 
46
  lxc move foo/tester2 foo/tester-two
 
47
  lxc delete foo/tester-two
 
48
  # FIXME: make this backend agnostic
 
49
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
50
    [ ! -d "${LXD_DIR}/snapshots/foo/tester-two" ]
 
51
  fi
 
52
 
 
53
  lxc snapshot foo namechange
 
54
  # FIXME: make this backend agnostic
 
55
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
56
    [ -d "${LXD_DIR}/snapshots/foo/namechange" ]
 
57
  fi
 
58
  lxc move foo foople
 
59
  [ ! -d "${LXD_DIR}/containers/foo" ]
 
60
  [ -d "${LXD_DIR}/containers/foople" ]
 
61
  # FIXME: make this backend agnostic
 
62
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
63
    [ -d "${LXD_DIR}/snapshots/foople/namechange" ]
 
64
    [ -d "${LXD_DIR}/snapshots/foople/namechange" ]
 
65
  fi
 
66
 
 
67
  lxc delete foople
 
68
  lxc delete foosnap1
 
69
  [ ! -d "${LXD_DIR}/containers/foople" ]
 
70
  [ ! -d "${LXD_DIR}/containers/foosnap1" ]
 
71
}
 
72
 
 
73
test_snap_restore() {
 
74
  ensure_import_testimage
 
75
  ensure_has_localhost_remote "${LXD_ADDR}"
 
76
 
 
77
  ##########################################################
 
78
  # PREPARATION
 
79
  ##########################################################
 
80
 
 
81
  ## create some state we will check for when snapshot is restored
 
82
 
 
83
  ## prepare snap0
 
84
  lxc launch testimage bar
 
85
  echo snap0 > state
 
86
  lxc file push state bar/root/state
 
87
  lxc file push state bar/root/file_only_in_snap0
 
88
 
 
89
  mkdir "${LXD_DIR}/containers/bar/rootfs/root/dir_only_in_snap0"
 
90
  cd "${LXD_DIR}/containers/bar/rootfs/root/"
 
91
  ln -s ./file_only_in_snap0 statelink
 
92
  cd -
 
93
  lxc stop bar --force
 
94
 
 
95
  lxc snapshot bar snap0
 
96
 
 
97
  ## prepare snap1
 
98
  lxc start bar
 
99
  echo snap1 > state
 
100
  lxc file push state bar/root/state
 
101
  lxc file push state bar/root/file_only_in_snap1
 
102
 
 
103
  cd "${LXD_DIR}/containers/bar/rootfs/root/"
 
104
  rmdir dir_only_in_snap0
 
105
  rm    file_only_in_snap0
 
106
  rm    statelink
 
107
  ln -s ./file_only_in_snap1 statelink
 
108
  mkdir dir_only_in_snap1
 
109
  cd -
 
110
  lxc stop bar --force
 
111
 
 
112
  # Delete the state file we created to prevent leaking.
 
113
  rm state
 
114
 
 
115
  lxc config set bar limits.cpu 1
 
116
 
 
117
  lxc snapshot bar snap1
 
118
 
 
119
  ##########################################################
 
120
 
 
121
  # FIXME: make this backend agnostic
 
122
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
123
    # The problem here is that you can't `zfs rollback` to a snapshot with a
 
124
    # parent, which snap0 has (snap1).
 
125
    restore_and_compare_fs snap0
 
126
 
 
127
    # Check container config has been restored (limits.cpu is unset)
 
128
    cpus=$(lxc config get bar limits.cpu)
 
129
    if [ -n "${cpus}" ]; then
 
130
     echo "==> config didn't match expected value after restore (${cpus})"
 
131
     false
 
132
    fi
 
133
  fi
 
134
 
 
135
  ##########################################################
 
136
 
 
137
  # test restore using full snapshot name
 
138
  restore_and_compare_fs snap1
 
139
 
 
140
  # Check config value in snapshot has been restored
 
141
  cpus=$(lxc config get bar limits.cpu)
 
142
  if [ "${cpus}" != "1" ]; then
 
143
   echo "==> config didn't match expected value after restore (${cpus})"
 
144
   false
 
145
  fi
 
146
 
 
147
  ##########################################################
 
148
 
 
149
  # Start container and then restore snapshot to verify the running state after restore.
 
150
  lxc start bar
 
151
 
 
152
  # FIXME: make this backend agnostic
 
153
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
154
    # see comment above about snap0
 
155
    restore_and_compare_fs snap0
 
156
 
 
157
    # check container is running after restore
 
158
    lxc list | grep bar | grep RUNNING
 
159
  fi
 
160
 
 
161
  lxc stop --force bar
 
162
 
 
163
  lxc delete bar
 
164
}
 
165
 
 
166
restore_and_compare_fs() {
 
167
  snap=${1}
 
168
  echo "==> Restoring ${snap}"
 
169
 
 
170
  lxc restore bar "${snap}"
 
171
 
 
172
  # FIXME: make this backend agnostic
 
173
  if [ "${LXD_BACKEND}" = "dir" ]; then
 
174
    # Recursive diff of container FS
 
175
    diff -r "${LXD_DIR}/containers/bar/rootfs" "${LXD_DIR}/snapshots/bar/${snap}/rootfs"
 
176
  fi
 
177
}