Skip to content

Commit

Permalink
Add support for role dependencies (#11)
Browse files Browse the repository at this point in the history
* Add support for role local and galaxy dependencies
  • Loading branch information
z0mbix authored Sep 6, 2024
1 parent 6cd2f4f commit de86b9b
Show file tree
Hide file tree
Showing 53 changed files with 1,063 additions and 143 deletions.
1 change: 0 additions & 1 deletion .envrc

This file was deleted.

42 changes: 18 additions & 24 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
name: Build and test
name: Test
run-name: Test ${{ github.ref_name }} triggered by @${{ github.actor }}

on:
push:
branches: [ "main" ]
branches:
- main
pull_request:
branches: [ "main" ]

jobs:

build-linux:
test-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
- name: Set up go
uses: actions/setup-go@v5
with:
go-version: 1.19

- name: Build
run: go build -v ./...
go-version-file: go.mod

- name: Test
run: go test -v ./...
run: go test -v ./... -coverprofile=cover.out

build-windows:
test-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./... -coverprofile=cover.out
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist/
.idea/
.vscode/
rolecule
rolecule.exe
6 changes: 6 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[env]
mise.path = ["./bin"]
GO_VERSION = "{{exec(command='grep \"^go 1\\.[0-9]\\+\\.[0-9]\\+$\" go.mod | cut -f2 -d\" \"')}}"

[tools]
golang = "{{exec(command='grep \"^go 1\\.[0-9]\\+\\.[0-9]\\+$\" go.mod | cut -f2 -d\" \"')}}"
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ instances:

```

Then, from the root of your role (e.g. [sshd](testing/ansible/roles/sshd/rolecule.yml)), run `rolecule test`, e.g.:
Then, from the root of your role (e.g. [sshd](testing/ansible/roles/sshd/tests/rolecule.yml)), run `rolecule test`, e.g.:

```text
» rolecule test
Expand Down Expand Up @@ -177,6 +177,14 @@ provisioner:
- --verbose
```

## Role dependencies

If you have role dependencies in your `meta/main.yml` file using local roles in the same location
as the current role, that directory will be mounted at `/etc/ansible/roles` in the container so
ansible can find them.

Support for using roles from a galaxy server is not yet implemented.

## Instances

These are instances of each test scenario, allowing you can test different ansible tags with specific test files.
Expand Down
11 changes: 7 additions & 4 deletions cmd/converge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2022 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down Expand Up @@ -33,14 +30,20 @@ var convergeCmd = &cobra.Command{
func converge(cfg *config.Config) error {
for _, instance := range cfg.Instances {
if !instance.Engine.Exists(instance.Name) {
log.Errorf("container does not exist, creating...")
err := create(cfg)
if err != nil {
log.Error(err.Error())
continue
}
}

if len(instance.Provisioner.GetDependencies().GalaxyRoles) > 0 {
log.Infof("preparing container %s", instance.Name)
if err := instance.Prepare(); err != nil {
log.Error(err.Error())
}
}

log.Infof("converging container %s with %s", instance.Name, instance.Provisioner)
if err := instance.Converge(); err != nil {
log.Error(err.Error())
Expand Down
3 changes: 0 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
7 changes: 2 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand All @@ -21,8 +18,8 @@ var rootCmd = &cobra.Command{
Use: "rolecule",
Short: "rolecule helps you test your ansible roles",
Long: `rolecule uses docker or podman to test your
configuration management roles/recipes/modules in a systemd enabled container,
then tests them with a verifier (goss/testinfra).`,
ansible roles in a systemd enabled container,
then tests them with a verifier (goss).`,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetHandler(cli.New(os.Stderr))
Expand Down
3 changes: 0 additions & 3 deletions cmd/shell.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions cmd/test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
3 changes: 0 additions & 3 deletions cmd/verify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package cmd

import (
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/z0mbix/rolecule

go 1.19
go 1.23.0

require (
github.com/apex/log v1.9.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
Expand Down Expand Up @@ -111,6 +112,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -149,9 +151,11 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
Expand Down Expand Up @@ -183,6 +187,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
Expand Down Expand Up @@ -211,6 +216,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
Expand Down
32 changes: 18 additions & 14 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,54 @@ set shell := ["bash", "-uc"]

# Show available targets/recipes
default:
@just --choose
@just --choose

# Clean up old files
clean:
rm -rf ./dist/*
rm ./rolecule
rm -rf ./dist/*
rm ./rolecule

# Build the binary for the current os/arch
build:
go build -o bin/rolecule
go build -o bin/rolecule

# Configure your host to use this repo
setup:
direnv allow
mise trust
mise install
mise ls -c

# Show git tags
tags:
@git tag | sort -V
@git tag | sort -V

# Run unit tests
test:
go test ./...
go test ./... -v -coverprofile=/dev/null

# Build docker images with ansible support
build-docker-ansible-images:
docker build -t rockylinux-systemd:9.1 -f testing/ansible/rockylinux-9.1-systemd.Dockerfile .
docker build -t ubuntu-systemd:22.04 -f testing/ansible/ubuntu-22.04-systemd.Dockerfile .
docker build -t rockylinux-systemd:9.1 -f testing/ansible/rockylinux-9.1-systemd.Dockerfile .
docker build -t ubuntu-systemd:22.04 -f testing/ansible/ubuntu-22.04-systemd.Dockerfile .
docker build -t ubuntu-systemd:24.04 -f testing/ansible/ubuntu-24.04-systemd.Dockerfile .

# Build podman images with ansible support
build-podman-ansible-images:
podman build -t rockylinux-systemd:9.1 -f testing/ansible/rockylinux-9.1-systemd.Dockerfile .
podman build -t ubuntu-systemd:22.04 -f testing/ansible/ubuntu-22.04-systemd.Dockerfile .
podman build -t rockylinux-systemd:9.1 -f testing/ansible/rockylinux-9.1-systemd.Dockerfile .
podman build -t ubuntu-systemd:22.04 -f testing/ansible/ubuntu-22.04-systemd.Dockerfile .
podman build -t ubuntu-systemd:24.04 -f testing/ansible/ubuntu-24.04-systemd.Dockerfile .

# Build all images with ansible support
build-ansible-images: build-docker-ansible-images build-podman-ansible-images

# Build a local only, snapshot release
snapshot:
goreleaser --snapshot --skip-publish --rm-dist --debug
goreleaser --snapshot --skip-publish --rm-dist --debug

# Create and publish a new release
release:
goreleaser --rm-dist
goreleaser --rm-dist

# Show help menu
help:
@just --list --list-prefix ' ❯ '
@just --list --list-prefix ' ❯ '
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
Copyright © 2023 David Wooldridge <[email protected]>
*/
package main

import "github.com/z0mbix/rolecule/cmd"
Expand Down
Loading

0 comments on commit de86b9b

Please sign in to comment.