Skip to content

Commit d8a61a2

Browse files
committed
Check --fn-path
1 parent 150defb commit d8a61a2

File tree

2 files changed

+112
-8
lines changed

2 files changed

+112
-8
lines changed

internal/cmdexport/cmdexport.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package cmdexport
1818
import (
1919
"fmt"
2020
"os"
21+
"path"
22+
"strings"
2123

2224
"github.com/GoogleContainerTools/kpt/internal/cmdexport/orchestrators"
2325
"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
@@ -87,6 +89,10 @@ type ExportRunner struct {
8789

8890
// runE generates the pipeline and writes it into a file or stdout.
8991
func (r *ExportRunner) runE(c *cobra.Command, args []string) error {
92+
if err := r.checkFnPaths(); err != nil {
93+
return err
94+
}
95+
9096
pipeline, e := r.Pipeline.Init(r.PipelineConfig).Generate()
9197

9298
if e != nil {
@@ -107,3 +113,35 @@ func (r *ExportRunner) runE(c *cobra.Command, args []string) error {
107113

108114
return err
109115
}
116+
117+
// checkPaths checks if fnPaths exist within the current directory.
118+
func (r *ExportRunner) checkFnPaths() error {
119+
cwd, err := os.Getwd()
120+
if err != nil {
121+
return err
122+
}
123+
cwd = fmt.Sprintf("%s%s", cwd, string(os.PathSeparator))
124+
125+
for _, fnPath := range r.FnPaths {
126+
p := fnPath
127+
if !path.IsAbs(p) {
128+
p = path.Join(cwd, p)
129+
}
130+
131+
if !strings.HasPrefix(p, cwd) {
132+
return fmt.Errorf(
133+
"function path (%s) is not within the current working directory",
134+
fnPath,
135+
)
136+
}
137+
138+
if _, err := os.Stat(p); os.IsNotExist(err) {
139+
return fmt.Errorf(
140+
`function path (%s) does not exist`,
141+
fnPath,
142+
)
143+
}
144+
}
145+
146+
return nil
147+
}

internal/cmdexport/cmdexport_test.go

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import (
2525
"gotest.tools/assert"
2626
)
2727

28-
var tempDir, _ = ioutil.TempDir("", "kpt-fn-export-test")
28+
// Use file path as key, and content as value.
29+
type files map[string]string
2930

3031
type TestCase struct {
3132
description string
3233
params []string
3334
expected string
3435
err string
36+
files files
3537
}
3638

3739
var testCases = []TestCase{
@@ -70,7 +72,7 @@ jobs:
7072
"github-actions",
7173
".",
7274
"--output",
73-
filepath.Join(tempDir, "main.yaml"),
75+
"main.yaml",
7476
},
7577
expected: `
7678
name: kpt
@@ -90,7 +92,10 @@ jobs:
9092
},
9193
{
9294
description: "exports a GitHub Actions pipeline with --fn-path",
93-
params: []string{"github-actions", ".", "--fn-path", "functions/"},
95+
files: map[string]string{
96+
"function.yaml": "",
97+
},
98+
params: []string{"github-actions", ".", "--fn-path", "function.yaml"},
9499
expected: `
95100
name: kpt
96101
on:
@@ -104,18 +109,21 @@ jobs:
104109
- name: Run all kpt functions
105110
uses: docker://gongpu/kpt:latest
106111
with:
107-
args: fn run . --fn-path functions/
112+
args: fn run . --fn-path function.yaml
108113
`,
109114
},
110115
{
111-
description: "exports a Cloud Build pipeline",
116+
description: "exports a Cloud Build pipeline with --fn-path",
117+
files: map[string]string{
118+
"functions/function.yaml": "",
119+
},
112120
params: []string{
113121
"cloud-build",
114122
".",
115123
"--fn-path",
116124
"functions/",
117125
"--output",
118-
filepath.Join(tempDir, "cloudbuild.yaml"),
126+
"cloudbuild.yaml",
119127
},
120128
expected: `
121129
steps:
@@ -128,11 +136,69 @@ steps:
128136
- functions/
129137
`,
130138
},
139+
{
140+
description: "fails to export a Cloud Build pipeline with non-exist function path",
141+
params: []string{
142+
"cloud-build",
143+
".",
144+
"--fn-path",
145+
"functions.yaml",
146+
"--output",
147+
"cloudbuild.yaml",
148+
},
149+
err: "function path (functions.yaml) does not exist",
150+
},
151+
{
152+
description: "fails to export a Cloud Build pipeline with outside function path",
153+
params: []string{
154+
"cloud-build",
155+
".",
156+
"--fn-path",
157+
"../functions/functions.yaml",
158+
"--output",
159+
"cloudbuild.yaml",
160+
},
161+
err: "function path (../functions/functions.yaml) is not within the current working directory",
162+
},
163+
}
164+
165+
func setupTempDir(files files) (dir string, err error) {
166+
tempDir, err := ioutil.TempDir("", "kpt-fn-export-test")
167+
if err != nil {
168+
return "", err
169+
}
170+
171+
for p, content := range files {
172+
p = filepath.Join(tempDir, p)
173+
174+
err = os.MkdirAll(
175+
filepath.Dir(p),
176+
0755, // drwxr-xr-x
177+
)
178+
if err != nil {
179+
return "", nil
180+
}
181+
182+
err = ioutil.WriteFile(
183+
p,
184+
[]byte(content),
185+
0644, // -rw-r--r--
186+
)
187+
if err != nil {
188+
return "", err
189+
}
190+
}
191+
192+
return tempDir, nil
131193
}
132194

133195
func TestCmdExport(t *testing.T) {
134196
for i := range testCases {
135197
testCase := testCases[i]
198+
tempDir, err := setupTempDir(testCase.files)
199+
assert.NilError(t, err)
200+
err = os.Chdir(tempDir)
201+
assert.NilError(t, err)
136202

137203
t.Run(testCase.description, func(t *testing.T) {
138204
r := GetExportRunner()
@@ -162,7 +228,7 @@ func TestCmdExport(t *testing.T) {
162228
assert.Equal(t, expected, actual)
163229
}
164230
})
165-
}
166231

167-
_ = os.RemoveAll(tempDir)
232+
_ = os.RemoveAll(tempDir)
233+
}
168234
}

0 commit comments

Comments
 (0)