summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/apt_repository/tasks/apt.yml
blob: 28bb902913b378e3ea0bb7b0fa5d05cf95375b19 (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
---

- set_fact:
    test_ppa_name: 'ppa:git-core/ppa'
    test_ppa_filename: 'git-core'
    test_ppa_spec: 'deb http://ppa.launchpad.net/git-core/ppa/ubuntu {{ansible_distribution_release}} main'
    test_ppa_key: 'E1DF1F24' # http://keyserver.ubuntu.com:11371/pks/lookup?search=0xD06AAF4C11DAB86DF421421EFE6B20ECA7AD98A1&op=index

- name: show python version
  debug: var=ansible_python_version

- name: use python-apt
  set_fact:
    python_apt: python-apt
  when: ansible_python_version is version('3', '<')

- name: use python3-apt
  set_fact:
    python_apt: python3-apt
  when: ansible_python_version is version('3', '>=')

# UNINSTALL 'python-apt'
#  The `apt_repository` module has the smarts to auto-install `python-apt`.  To
# test, we will first uninstall `python-apt`.
- name: check {{ python_apt }} with dpkg
  shell: dpkg -s {{ python_apt }}
  register: dpkg_result
  ignore_errors: true

- name: uninstall {{ python_apt }} with apt
  apt: pkg={{ python_apt }} state=absent purge=yes
  register: apt_result
  when: dpkg_result is successful

#
# TEST: apt_repository: repo=<name>
#
- import_tasks: 'cleanup.yml'

- name: 'record apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_before

- name: 'name=<name> (expect: pass)'
  apt_repository: repo='{{test_ppa_name}}' state=present
  register: result

- name: 'assert the apt cache did *NOT* change'
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.repo == "{{test_ppa_name}}"'

- name: 'examine apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_after

- name: 'assert the apt cache did change'
  assert:
    that:
      - 'cache_before.stat.mtime != cache_after.stat.mtime'

- name: 'ensure ppa key is installed (expect: pass)'
  apt_key: id='{{test_ppa_key}}' state=present

#
# TEST: apt_repository: repo=<name> update_cache=no
#
- import_tasks: 'cleanup.yml'

- name: 'record apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_before

- name: 'name=<name> update_cache=no (expect: pass)'
  apt_repository: repo='{{test_ppa_name}}' state=present update_cache=no
  register: result

- assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.repo == "{{test_ppa_name}}"'

- name: 'examine apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_after

- name: 'assert the apt cache did *NOT* change'
  assert:
    that:
      - 'cache_before.stat.mtime == cache_after.stat.mtime'

- name: 'ensure ppa key is installed (expect: pass)'
  apt_key: id='{{test_ppa_key}}' state=present

#
# TEST: apt_repository: repo=<name> update_cache=yes
#
- import_tasks: 'cleanup.yml'

- name: 'record apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_before

- name: 'name=<name> update_cache=yes (expect: pass)'
  apt_repository: repo='{{test_ppa_name}}' state=present update_cache=yes
  register: result

- assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.repo == "{{test_ppa_name}}"'

- name: 'examine apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_after

- name: 'assert the apt cache did change'
  assert:
    that:
      - 'cache_before.stat.mtime != cache_after.stat.mtime'

- name: 'ensure ppa key is installed (expect: pass)'
  apt_key: id='{{test_ppa_key}}' state=present

#
# TEST: apt_repository: repo=<spec>
#
- import_tasks: 'cleanup.yml'

- name: 'record apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_before

- name: ensure ppa key is present before adding repo that requires authentication
  apt_key: keyserver=keyserver.ubuntu.com id='{{test_ppa_key}}' state=present

- name: 'name=<spec> (expect: pass)'
  apt_repository: repo='{{test_ppa_spec}}' state=present
  register: result

- name: update the cache
  apt:
    update_cache: true
  register: result_cache

- assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.repo == "{{test_ppa_spec}}"'
      - '"sources_added" in result'
      - 'result.sources_added | length == 1'
      - '"git" in result.sources_added[0]'
      - '"sources_removed" in result'
      - 'result.sources_removed | length == 0'
      - result_cache is not changed

- name: 'examine apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_after

- name: 'assert the apt cache did change'
  assert:
    that:
      - 'cache_before.stat.mtime != cache_after.stat.mtime'

- name: remove repo by spec
  apt_repository: repo='{{test_ppa_spec}}' state=absent
  register: result

- assert:
    that:
      - 'result.changed'
      - 'result.state == "absent"'
      - 'result.repo == "{{test_ppa_spec}}"'
      - '"sources_added" in result'
      - 'result.sources_added | length == 0'
      - '"sources_removed" in result'
      - 'result.sources_removed | length == 1'
      - '"git" in result.sources_removed[0]'

# When installing a repo with the spec, the key is *NOT* added
- name: 'ensure ppa key is absent (expect: pass)'
  apt_key: id='{{test_ppa_key}}' state=absent

#
# TEST: apt_repository: repo=<spec> filename=<filename>
#
- import_tasks: 'cleanup.yml'

- name: 'record apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_before

- name: ensure ppa key is present before adding repo that requires authentication
  apt_key: keyserver=keyserver.ubuntu.com id='{{test_ppa_key}}' state=present

- name: 'name=<spec> filename=<filename> (expect: pass)'
  apt_repository: repo='{{test_ppa_spec}}' filename='{{test_ppa_filename}}' state=present
  register: result

- assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.repo == "{{test_ppa_spec}}"'

- name: 'examine source file'
  stat: path='/etc/apt/sources.list.d/{{test_ppa_filename}}.list'
  register: source_file

- name: 'assert source file exists'
  assert:
    that:
      - 'source_file.stat.exists == True'

- name: 'examine apt cache mtime'
  stat: path='/var/cache/apt/pkgcache.bin'
  register: cache_after

- name: 'assert the apt cache did change'
  assert:
    that:
      - 'cache_before.stat.mtime != cache_after.stat.mtime'

# When installing a repo with the spec, the key is *NOT* added
- name: 'ensure ppa key is absent (expect: pass)'
  apt_key: id='{{test_ppa_key}}' state=absent

- name: Test apt_repository with a null value for repo
  apt_repository:
    repo:
  register: result
  ignore_errors: yes

- assert:
    that:
      - result is failed
      - result.msg.startswith("argument 'repo' is of type <class 'NoneType'> and we were unable to convert to str")

- name: Test apt_repository with an empty value for repo
  apt_repository:
    repo: ""
  register: result
  ignore_errors: yes

- assert:
    that:
      - result is failed
      - result.msg == 'Please set argument \'repo\' to a non-empty value'

#
# TEARDOWN
#
- import_tasks: 'cleanup.yml'