Skip to content

Commit 0537f59

Browse files
author
Shawn Hurley
authored
pkg/ansible: fail early if we are unable to find role or playbook (#677)
**Description of the change:** Check that the absolute path to the playbook or role does exist. **Motivation for the change:** Not exposing errors to the user when we are unable to find the playbook or role makes it hard to figure out there is a problem when running.
1 parent ab09a3d commit 0537f59

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ tags
102102
# Build artifacts
103103
build/*
104104
test/ansible-operator/ansible-operator
105+
106+
# Test artifacts
107+
pkg/ansible/runner/testdata/valid.yaml

pkg/ansible/runner/runner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func NewForPlaybook(path string, gvk schema.GroupVersionKind, finalizer *Finaliz
123123
if !filepath.IsAbs(path) {
124124
return nil, fmt.Errorf("playbook path must be absolute for %v", gvk)
125125
}
126+
if _, err := os.Stat(path); err != nil {
127+
return nil, fmt.Errorf("playbook: %v was not found for %v", path, gvk)
128+
}
126129
r := &runner{
127130
Path: path,
128131
GVK: gvk,
@@ -143,6 +146,9 @@ func NewForRole(path string, gvk schema.GroupVersionKind, finalizer *Finalizer,
143146
if !filepath.IsAbs(path) {
144147
return nil, fmt.Errorf("role path must be absolute for %v", gvk)
145148
}
149+
if _, err := os.Stat(path); err != nil {
150+
return nil, fmt.Errorf("role path: %v was not found for %v", path, gvk)
151+
}
146152
path = strings.TrimRight(path, "/")
147153
r := &runner{
148154
Path: path,

pkg/ansible/runner/runner_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package runner
1616

1717
import (
18+
"html/template"
19+
"os"
20+
"path/filepath"
1821
"reflect"
1922
"testing"
2023
"time"
@@ -23,6 +26,33 @@ import (
2326
)
2427

2528
func TestNewFromWatches(t *testing.T) {
29+
cwd, err := os.Getwd()
30+
if err != nil {
31+
t.Fatalf("unable to get working director: %v", err)
32+
}
33+
34+
validTemplate := struct {
35+
ValidPlaybook string
36+
ValidRole string
37+
}{
38+
39+
ValidPlaybook: filepath.Join(cwd, "testdata", "playbook.yml"),
40+
ValidRole: filepath.Join(cwd, "testdata", "roles", "role"),
41+
}
42+
43+
tmpl, err := template.ParseFiles("testdata/valid.yaml.tmpl")
44+
if err != nil {
45+
}
46+
f, err := os.Create("testdata/valid.yaml")
47+
if err != nil {
48+
t.Fatalf("unable to create valid.yaml: %v", err)
49+
}
50+
err = tmpl.Execute(f, validTemplate)
51+
if err != nil {
52+
t.Fatalf("unable to create valid.yaml: %v", err)
53+
return
54+
}
55+
2656
testCases := []struct {
2757
name string
2858
path string
@@ -83,7 +113,7 @@ func TestNewFromWatches(t *testing.T) {
83113
Group: "app.example.com",
84114
Kind: "NoFinalizer",
85115
},
86-
Path: "/opt/ansible/playbook.yaml",
116+
Path: validTemplate.ValidPlaybook,
87117
reconcilePeriod: time.Second * 2,
88118
},
89119
schema.GroupVersionKind{
@@ -96,10 +126,10 @@ func TestNewFromWatches(t *testing.T) {
96126
Group: "app.example.com",
97127
Kind: "Playbook",
98128
},
99-
Path: "/opt/ansible/playbook.yaml",
129+
Path: validTemplate.ValidPlaybook,
100130
Finalizer: &Finalizer{
101131
Name: "finalizer.app.example.com",
102-
Role: "/opt/ansible/role",
132+
Role: validTemplate.ValidRole,
103133
Vars: map[string]interface{}{"sentinel": "finalizer_running"},
104134
},
105135
},
@@ -113,10 +143,10 @@ func TestNewFromWatches(t *testing.T) {
113143
Group: "app.example.com",
114144
Kind: "Role",
115145
},
116-
Path: "/opt/ansible/role",
146+
Path: validTemplate.ValidRole,
117147
Finalizer: &Finalizer{
118148
Name: "finalizer.app.example.com",
119-
Playbook: "/opt/ansible/playbook.yaml",
149+
Playbook: validTemplate.ValidPlaybook,
120150
Vars: map[string]interface{}{"sentinel": "finalizer_running"},
121151
},
122152
},

pkg/ansible/runner/testdata/playbook.yml

Whitespace-only changes.

pkg/ansible/runner/testdata/roles/role/tasks.yaml

Whitespace-only changes.

pkg/ansible/runner/testdata/valid.yaml renamed to pkg/ansible/runner/testdata/valid.yaml.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
- version: v1alpha1
33
group: app.example.com
44
kind: NoFinalizer
5-
playbook: /opt/ansible/playbook.yaml
5+
playbook: {{ .ValidPlaybook }}
66
reconcilePeriod: 2s
77
- version: v1alpha1
88
group: app.example.com
99
kind: Playbook
10-
playbook: /opt/ansible/playbook.yaml
10+
playbook: {{ .ValidPlaybook }}
1111
finalizer:
1212
name: finalizer.app.example.com
13-
role: /opt/ansible/role
13+
role: {{ .ValidRole }}
1414
vars:
1515
sentinel: finalizer_running
1616
- version: v1alpha1
1717
group: app.example.com
1818
kind: Role
19-
role: /opt/ansible/role
19+
role: {{ .ValidRole }}
2020
finalizer:
2121
name: finalizer.app.example.com
22-
playbook: /opt/ansible/playbook.yaml
22+
playbook: {{ .ValidPlaybook }}
2323
vars:
2424
sentinel: finalizer_running

0 commit comments

Comments
 (0)