Skip to content

Commit

Permalink
Check --fn-path
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuGongpu committed Jul 1, 2020
1 parent 150defb commit d8a61a2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 8 deletions.
38 changes: 38 additions & 0 deletions internal/cmdexport/cmdexport.go
Expand Up @@ -18,6 +18,8 @@ package cmdexport
import (
"fmt"
"os"
"path"
"strings"

"github.com/GoogleContainerTools/kpt/internal/cmdexport/orchestrators"
"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
Expand Down Expand Up @@ -87,6 +89,10 @@ type ExportRunner struct {

// runE generates the pipeline and writes it into a file or stdout.
func (r *ExportRunner) runE(c *cobra.Command, args []string) error {
if err := r.checkFnPaths(); err != nil {
return err
}

pipeline, e := r.Pipeline.Init(r.PipelineConfig).Generate()

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

return err
}

// checkPaths checks if fnPaths exist within the current directory.
func (r *ExportRunner) checkFnPaths() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
cwd = fmt.Sprintf("%s%s", cwd, string(os.PathSeparator))

for _, fnPath := range r.FnPaths {
p := fnPath
if !path.IsAbs(p) {
p = path.Join(cwd, p)
}

if !strings.HasPrefix(p, cwd) {
return fmt.Errorf(
"function path (%s) is not within the current working directory",
fnPath,
)
}

if _, err := os.Stat(p); os.IsNotExist(err) {
return fmt.Errorf(
`function path (%s) does not exist`,
fnPath,
)
}
}

return nil
}
82 changes: 74 additions & 8 deletions internal/cmdexport/cmdexport_test.go
Expand Up @@ -25,13 +25,15 @@ import (
"gotest.tools/assert"
)

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

type TestCase struct {
description string
params []string
expected string
err string
files files
}

var testCases = []TestCase{
Expand Down Expand Up @@ -70,7 +72,7 @@ jobs:
"github-actions",
".",
"--output",
filepath.Join(tempDir, "main.yaml"),
"main.yaml",
},
expected: `
name: kpt
Expand All @@ -90,7 +92,10 @@ jobs:
},
{
description: "exports a GitHub Actions pipeline with --fn-path",
params: []string{"github-actions", ".", "--fn-path", "functions/"},
files: map[string]string{
"function.yaml": "",
},
params: []string{"github-actions", ".", "--fn-path", "function.yaml"},
expected: `
name: kpt
on:
Expand All @@ -104,18 +109,21 @@ jobs:
- name: Run all kpt functions
uses: docker://gongpu/kpt:latest
with:
args: fn run . --fn-path functions/
args: fn run . --fn-path function.yaml
`,
},
{
description: "exports a Cloud Build pipeline",
description: "exports a Cloud Build pipeline with --fn-path",
files: map[string]string{
"functions/function.yaml": "",
},
params: []string{
"cloud-build",
".",
"--fn-path",
"functions/",
"--output",
filepath.Join(tempDir, "cloudbuild.yaml"),
"cloudbuild.yaml",
},
expected: `
steps:
Expand All @@ -128,11 +136,69 @@ steps:
- functions/
`,
},
{
description: "fails to export a Cloud Build pipeline with non-exist function path",
params: []string{
"cloud-build",
".",
"--fn-path",
"functions.yaml",
"--output",
"cloudbuild.yaml",
},
err: "function path (functions.yaml) does not exist",
},
{
description: "fails to export a Cloud Build pipeline with outside function path",
params: []string{
"cloud-build",
".",
"--fn-path",
"../functions/functions.yaml",
"--output",
"cloudbuild.yaml",
},
err: "function path (../functions/functions.yaml) is not within the current working directory",
},
}

func setupTempDir(files files) (dir string, err error) {
tempDir, err := ioutil.TempDir("", "kpt-fn-export-test")
if err != nil {
return "", err
}

for p, content := range files {
p = filepath.Join(tempDir, p)

err = os.MkdirAll(
filepath.Dir(p),
0755, // drwxr-xr-x
)
if err != nil {
return "", nil
}

err = ioutil.WriteFile(
p,
[]byte(content),
0644, // -rw-r--r--
)
if err != nil {
return "", err
}
}

return tempDir, nil
}

func TestCmdExport(t *testing.T) {
for i := range testCases {
testCase := testCases[i]
tempDir, err := setupTempDir(testCase.files)
assert.NilError(t, err)
err = os.Chdir(tempDir)
assert.NilError(t, err)

t.Run(testCase.description, func(t *testing.T) {
r := GetExportRunner()
Expand Down Expand Up @@ -162,7 +228,7 @@ func TestCmdExport(t *testing.T) {
assert.Equal(t, expected, actual)
}
})
}

_ = os.RemoveAll(tempDir)
_ = os.RemoveAll(tempDir)
}
}

0 comments on commit d8a61a2

Please sign in to comment.