Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Ansible Playbook for Integration Test; Wait for Volume Active on Creation #145

Merged
merged 9 commits into from
Apr 3, 2024
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DOCKER_USERNAME ?= xxxxx
DOCKER_PASSWORD ?= xxxxx

# Test Arguments
TEST_TOKEN ?= $LINODE_TOKEN
TEST_TOKEN ?= $(LINODE_TOKEN)

# Quick Test Arguments
QUICKTEST_SSH_PUBKEY ?= ~/.ssh/id_rsa.pub
Expand Down Expand Up @@ -65,9 +65,12 @@ $(PLUGIN_DIR): *.go Dockerfile
docker rm -vf tmp

# Provision a test environment for docker-volume-linode using Ansible.
.PHONY: quick-test
quick-test:
ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook -v --extra-vars "ssh_pubkey_path=${QUICKTEST_SSH_PUBKEY} skip_tests=${QUICKTEST_SKIP_TESTS}" quick-test/deploy.yml
.PHONY: int-test
int-test:
ANSIBLE_HOST_KEY_CHECKING=False ANSIBLE_STDOUT_CALLBACK=yaml \
ansible-playbook -v --extra-vars \
"ssh_pubkey_path=${QUICKTEST_SSH_PUBKEY} skip_tests=${QUICKTEST_SKIP_TESTS}" \
integration-test/test.yaml

# Run Integration Tests
# Requires TEST_* Variables to be set
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ A great place to get started is the Docker Engine managed plugin system [documen

## Running Integration Tests

The integration tests for this project can be easily run using the `make quick-test` target.
The integration tests for this project can be easily run using the `make int-test` target.
This target provisions and connects to a Linode instance, uploads the plugin, builds it, enables it,
and runs the integration test suite. Subsequent runs of this target will re-use the existing Linode instance.

Expand All @@ -175,21 +175,21 @@ export LINODE_TOKEN=EXAMPLETOKEN
The integration test suite can now be run:

```bash
make quick-test
make int-test
```

NOTE: This target requires an existing SSH key be created. If an SSH key exists at a path other than
`~/.ssh/id_rsa`, the `QUICKTEST_SSH_PUBKEY` argument can be specified:

```bash
make QUICKTEST_SSH_PUBKEY="~/.ssh/mykey.pub" quick-test
make QUICKTEST_SSH_PUBKEY="~/.ssh/mykey.pub" int-test
```

If you would like to create a test environment for docker-volume-linode without running the integration test suite,
the `QUICKTEST_SKIP_TESTS` argument can be specified:

```bash
make QUICKTEST_SKIP_TESTS=1 quick-test
make QUICKTEST_SKIP_TESTS=1 int-test
```

## Discussion / Help
Expand Down
12 changes: 11 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,20 @@ func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error {
}
}

if _, err := api.CreateVolume(context.Background(), createOpts); err != nil {
volume, err := api.CreateVolume(context.Background(), createOpts)
if err != nil {
return fmt.Errorf("Create(%s) Failed: %s", req.Name, err)
}

_, err = driver.linodeAPIPtr.WaitForVolumeStatus(
context.Background(), volume.ID, linodego.VolumeActive, 600,
)
if err != nil {
return fmt.Errorf(
"Failed to wait for volume %d to be active: %w", volume.ID, err,
)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ disable_root: true
users:
- default
- name: linodedx
groups: docker
gecos: The primary account for development on this VM.
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
Expand Down
71 changes: 45 additions & 26 deletions quick-test/deploy.yml → integration-test/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
vars:
ssh_pubkey_path: ~/.ssh/id_rsa.pub
label: docker-volume-test
type: g6-standard-2
region: us-ord
type: g6-nanode-1
region: us-mia
temp_token_name: docker-volume-linode-dev
token_duration_seconds: 3600
tasks:
- name: Ensure the previous token has been removed
no_log: true
linode.cloud.token:
label: "{{ temp_token_name }}"
state: absent
Expand All @@ -18,6 +19,7 @@
ssh_pubkey: '{{ lookup("file", ssh_pubkey_path) }}'

- name: Create a temporary token for the plugin to consume
no_log: true
linode.cloud.token:
label: "{{ temp_token_name }}"
scopes: "events:read_write linodes:read_write volumes:read_write"
Expand All @@ -33,7 +35,7 @@
label: "{{ label }}"
type: "{{ type }}"
region: "{{ region }}"
image: linode/alpine3.18
image: linode/ubuntu23.10
booted: true
metadata:
user_data: '{{ lookup("template", playbook_dir ~ "/harden.yaml.j2") }}'
Expand All @@ -44,22 +46,38 @@
wait_for: host="{{ create_inst.instance.ipv4[0] }}" port=22 delay=1 timeout=300

- name: Append host to the in-memory inventory
no_log: true
add_host:
hostname: "test-runner"
ansible_host: "{{ create_inst.instance.ipv4[0] }}"
groups: test_runner
ansible_user: root
ansible_user: linodedx
ansible_ssh_retries: 50
temp_token: "{{ temp_token.token.token }}"

- name: Configure the test instance
hosts: test_runner
remote_user: root
remote_user: linodedx
gather_facts: no
vars:
skip_tests: 0
dest_dir: /home/linodedx/docker-volume-linode
tasks:
- name: Wait for cloud-init to finish initialization
command: cloud-init status --format json
retries: 30
delay: 5
register: cloud_init_status
until: cloud_init_status.rc == 0 and (cloud_init_status.stdout | from_json)["status"] == "done"

- name: Update repositories and install necessary packages
community.general.apk:
name: docker,py3-pip,rsync,make
become: yes
ansible.builtin.apt:
name:
- docker.io
- python3-pip
- rsync
- make
update_cache: true

- name: Start and enable the Docker service
Expand All @@ -68,40 +86,41 @@
state: started
enabled: yes

- name: Install the Docker Python package
pip:
name: docker
state: present

- name: Remove any existing project files
file:
path: /docker-volume-linode
path: "{{ dest_dir }}"
state: absent

- name: Copy the local project to the remote
synchronize:
src: ../../
dest: /docker-volume-linode
dest: "{{ dest_dir }}"
rsync_opts:
- "--exclude=.git"

- name: Build and install the plugin
shell: |
make PLUGIN_VERSION="dev" build
docker plugin set linode/docker-volume-linode:latest linode-token={{ temp_token }}
docker plugin enable linode/docker-volume-linode:latest
args:
chdir: /docker-volume-linode
when:
- skip_tests == "1"

- name: Run the test suite
command: "make TEST_TOKEN={{ temp_token }} test"
no_log: true
command: "make test"
args:
chdir: /docker-volume-linode
chdir: "{{ dest_dir }}"
when:
- skip_tests != "1"
environment:
LINODE_TOKEN: "{{ temp_token }}"
PLUGIN_VERSION: dev

- name: Output the test instance IP
debug:
msg: "{{ ansible_host }}"

- name: Clean up
hosts: localhost
gather_facts: no
vars:
temp_token_name: docker-volume-linode-dev

tasks:
- name: Remove the temp token
linode.cloud.token:
label: "{{ temp_token_name }}"
state: absent