mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-09 04:00:31 -07:00
adds tests
This commit is contained in:
parent
0434249881
commit
3edfac2d37
6 changed files with 117 additions and 33 deletions
|
@ -45,17 +45,17 @@ class Package:
|
|||
|
||||
|
||||
class AndroidSdkManager(object):
|
||||
_RE_INSTALLED_PACKAGES_HEADER = re.compile(r'^\s+Path\s+|\s+Version\s+|\s+Description\s+|\s+Location\s+$')
|
||||
_RE_INSTALLED_PACKAGES = (
|
||||
re.compile(r'^\s+(?P<name>\S+)\s+\|\s+(?P<version>\S+)\s+\|\s(?P<description>.+)\s\|\s+(\S+)$')
|
||||
)
|
||||
_RE_INSTALLED_PACKAGES_HEADER = re.compile(r'^Installed packages:$')
|
||||
_RE_UPDATABLE_PACKAGES_HEADER = re.compile(r'^Available Updates:$')
|
||||
|
||||
# Example: ' platform-tools | 27.0.0 | 35.0.2'
|
||||
_RE_UPDATABLE_PACKAGE = (
|
||||
re.compile(r'^\s+(?P<name>\S+)\s+\|\s+(?P<old_version>\S+)\s+\|\s+(?P<new_version>\S+)\s*$')
|
||||
# Example: ' platform-tools | 27.0.0 | Android SDK Platform-Tools 27 | platform-tools '
|
||||
_RE_INSTALLED_PACKAGE = (
|
||||
re.compile(r'^\s+(?P<name>\S+)\s+\|\s+(?P<version>\S+)\s+\|\s(?P<description>.+)\s\|\s+(\S+)\s*$')
|
||||
)
|
||||
|
||||
# Example: ' platform-tools | 27.0.0 | 35.0.2'
|
||||
_RE_UPDATABLE_PACKAGE = re.compile(r'^\s+(?P<name>\S+)\s+\|\s+(?P<old_version>\S+)\s+\|\s+(?P<new_version>\S+)\s*$')
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
|
||||
|
@ -63,24 +63,29 @@ class AndroidSdkManager(object):
|
|||
with self.runner('installed') as ctx:
|
||||
rc, stdout, stderr = ctx.run()
|
||||
|
||||
packages = set()
|
||||
data = stdout.split('\n')
|
||||
|
||||
lines_count = len(data)
|
||||
|
||||
installed_section_found = False
|
||||
i = 0
|
||||
lines_count = len(data)
|
||||
packages = set()
|
||||
|
||||
while i < lines_count:
|
||||
line = data[i]
|
||||
if self._RE_INSTALLED_PACKAGES_HEADER.match(line):
|
||||
i += 1
|
||||
|
||||
if not installed_section_found:
|
||||
installed_section_found = self._RE_INSTALLED_PACKAGES_HEADER.match(data[i])
|
||||
if installed_section_found:
|
||||
i += 3
|
||||
else:
|
||||
i += 1
|
||||
continue
|
||||
else:
|
||||
p = self._RE_INSTALLED_PACKAGES.search(line)
|
||||
p = self._RE_INSTALLED_PACKAGE.search(data[i])
|
||||
if p:
|
||||
package = Package(p.group('name'))
|
||||
packages.add(package)
|
||||
i += 1
|
||||
return packages
|
||||
return packages
|
||||
|
||||
def get_updatable_packages(self):
|
||||
with self.runner('list newer') as ctx:
|
||||
|
@ -92,7 +97,6 @@ class AndroidSdkManager(object):
|
|||
lines_count = len(data)
|
||||
packages = set()
|
||||
|
||||
dbg = []
|
||||
while i < lines_count:
|
||||
if not updatable_section_found:
|
||||
updatable_section_found = self._RE_UPDATABLE_PACKAGES_HEADER.match(data[i])
|
||||
|
@ -109,23 +113,20 @@ class AndroidSdkManager(object):
|
|||
continue
|
||||
else:
|
||||
p = self._RE_UPDATABLE_PACKAGE.match(data[i])
|
||||
dbg.append((data[i], p is not None))
|
||||
if p:
|
||||
packages.add(Package(p.group('name')))
|
||||
i += 1
|
||||
return packages
|
||||
|
||||
def install_packages(self, packages):
|
||||
self.apply_packages_changes(packages, 'present')
|
||||
return self.apply_packages_changes(packages, 'present')
|
||||
|
||||
def uninstall_packages(self, packages):
|
||||
self.apply_packages_changes(packages, 'absent')
|
||||
return self.apply_packages_changes(packages, 'absent')
|
||||
|
||||
def apply_packages_changes(self, packages, state):
|
||||
if len(packages) == 0:
|
||||
return
|
||||
return 0, '', ''
|
||||
command_arg = [x.name for x in packages]
|
||||
# ValueError: ['build-tools;34.0.0', 'build-tools;35.0.0']
|
||||
# raise ValueError(command_arg)
|
||||
with self.runner('state name') as ctx:
|
||||
ctx.run(name=command_arg, state=state)
|
||||
return ctx.run(name=command_arg, state=state)
|
||||
|
|
|
@ -40,7 +40,9 @@ class AndroidSdk(StateModuleHelper):
|
|||
|
||||
self.vars.installed = AndroidSdk._map_packages_to_names(pending_installation)
|
||||
if not self.check_mode:
|
||||
self.sdkmanager.install_packages(pending_installation)
|
||||
rc, stdout, stderr = self.sdkmanager.install_packages(packages)
|
||||
if rc != 0:
|
||||
self.do_raise("Could not install packages: %s" % stderr)
|
||||
|
||||
def state_absent(self):
|
||||
packages = self._parse_packages()
|
||||
|
@ -48,7 +50,9 @@ class AndroidSdk(StateModuleHelper):
|
|||
to_be_deleted = packages.intersection(installed)
|
||||
self.vars.removed = AndroidSdk._map_packages_to_names(to_be_deleted)
|
||||
if not self.check_mode:
|
||||
self.sdkmanager.uninstall_packages(to_be_deleted)
|
||||
rc, stdout, stderr = self.sdkmanager.uninstall_packages(packages)
|
||||
if rc != 0:
|
||||
self.do_raise("Could not uninstall packages: %s" % stderr)
|
||||
|
||||
def state_latest(self):
|
||||
packages = self._parse_packages()
|
||||
|
@ -59,7 +63,9 @@ class AndroidSdk(StateModuleHelper):
|
|||
self.vars.installed = AndroidSdk._map_packages_to_names(to_be_installed)
|
||||
|
||||
if not self.check_mode:
|
||||
self.sdkmanager.install_packages(packages)
|
||||
rc, stdout, stderr = self.sdkmanager.install_packages(packages)
|
||||
if rc != 0:
|
||||
self.do_raise("Could not install packages: %s" % stderr)
|
||||
|
||||
def update_packages(self):
|
||||
pass
|
||||
|
|
7
tests/integration/targets/android_sdk/meta/main.yml
Normal file
7
tests/integration/targets/android_sdk/meta/main.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
dependencies:
|
||||
- setup_pkg_mgr
|
69
tests/integration/targets/android_sdk/tasks/main.yml
Normal file
69
tests/integration/targets/android_sdk/tasks/main.yml
Normal file
|
@ -0,0 +1,69 @@
|
|||
- import_tasks: setup.yml
|
||||
|
||||
- name: Setup environment
|
||||
environment:
|
||||
PATH: '{{ ansible_env.PATH }}:{{ android_sdk_location }}/cmdline-tools/latest/bin'
|
||||
ANDROID_HOME: "{{ android_sdk_location }}"
|
||||
block:
|
||||
|
||||
- name: Accept all licenses
|
||||
shell: 'yes | sdkmanager --licenses'
|
||||
changed_when: false
|
||||
|
||||
- name: Install build-tools;34.0.0
|
||||
android_sdk:
|
||||
name: build-tools;34.0.0
|
||||
state: present
|
||||
register: build_tools_installed
|
||||
|
||||
- name: Install build-tools;34.0.0 second time
|
||||
android_sdk:
|
||||
name: build-tools;34.0.0
|
||||
state: present
|
||||
register: build_tools_installed2
|
||||
|
||||
- name: Stat build-tools
|
||||
stat:
|
||||
path: "{{ android_sdk_location }}/build-tools/34.0.0"
|
||||
register: build_tools_34_0_0
|
||||
|
||||
- name: Delete build-tools;34.0.0
|
||||
android_sdk:
|
||||
name: build-tools;34.0.0
|
||||
state: absent
|
||||
register: build_tools_deleted
|
||||
|
||||
- name: Delete build-tools;34.0.0 second time
|
||||
android_sdk:
|
||||
name: build-tools;34.0.0
|
||||
state: absent
|
||||
register: build_tools_deleted2
|
||||
|
||||
- name: Download old platform-tools
|
||||
unarchive:
|
||||
src: https://dl.google.com/android/repository/platform-tools_r27.0.0-linux.zip
|
||||
remote_src: true
|
||||
dest: "{{ android_sdk_location }}"
|
||||
|
||||
- name: Try installing platform-tools from sdkmanager
|
||||
android_sdk:
|
||||
name: platform-tools
|
||||
state: present
|
||||
register: platform_tools_present
|
||||
|
||||
- name: Install (update) platform-tools
|
||||
android_sdk:
|
||||
name: platform-tools
|
||||
state: latest
|
||||
register: platform_tools_updated
|
||||
|
||||
- name: Run tests
|
||||
assert:
|
||||
that:
|
||||
- build_tools_34_0_0.stat.exists
|
||||
- build_tools_installed.changed
|
||||
- not build_tools_installed2.changed
|
||||
- build_tools_deleted.changed
|
||||
- not build_tools_deleted2.changed
|
||||
- not platform_tools_present.changed
|
||||
- platform_tools_updated.changed
|
|
@ -7,23 +7,25 @@
|
|||
become: true
|
||||
package:
|
||||
name:
|
||||
- java-21-openjdk
|
||||
# - java-21-openjdk-headless
|
||||
- openjdk-17-jdk-headless
|
||||
- unzip
|
||||
state: present
|
||||
|
||||
- name: Print env
|
||||
debug:
|
||||
var: ansible_env
|
||||
|
||||
- name: Create Android SDK directory
|
||||
become: true
|
||||
file:
|
||||
path: "{{ android_sdk_location }}"
|
||||
state: directory
|
||||
owner: vagrant
|
||||
group: vagrant
|
||||
|
||||
- name: Check that sdkmanager is installed
|
||||
stat:
|
||||
path: "{{ android_sdk_location }}/cmdline-tools/latest/bin/sdkmanager"
|
||||
register: sdkmanager_installed
|
||||
|
||||
|
||||
- name: Install Android command line tools
|
||||
when: not sdkmanager_installed.stat.exists
|
||||
block:
|
||||
|
@ -49,4 +51,3 @@
|
|||
src: "{{ android_cmdline_temp_dir }}/cmdline-tools/"
|
||||
dest: "{{ android_sdk_location }}/cmdline-tools/latest"
|
||||
remote_src: yes
|
||||
|
|
@ -3,5 +3,5 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
android_cmdline_temp_dir: "/tmp/cmdlinetools"
|
||||
android_sdk_location: /usr/local/android/sdk
|
||||
android_sdk_location: "/tmp/androidsdk"
|
||||
commandline_tools_link: https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
|
||||
|
|
Loading…
Add table
Reference in a new issue