Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
Require an invoker be specified for init and create commands
Browse files Browse the repository at this point in the history
riff will no longer attempt to resolve an invoker based on the files in
the current directory. The previous resolution could change behavior as`
new invokers are installed in the cluster or files change on disk.

Fixes #502
  • Loading branch information
scothis committed Apr 6, 2018
1 parent 93cd330 commit cfc0926
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 186 deletions.
24 changes: 8 additions & 16 deletions riff-cli/cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -82,6 +83,7 @@ var _ = Describe("The create command", func() {

err = rootCommand.Execute()
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(fmt.Errorf("Invokers must be installed, run `riff invokers apply --help` for help")))

Expect(".").NotTo(HaveUnstagedChanges())
})
Expand Down Expand Up @@ -112,19 +114,9 @@ var _ = Describe("The create command", func() {

rootCommand.SetArgs(append([]string{"create"}, commonRiffArgs...))

normalDocker.On("Exec", "build", "-t", "rifftest/matching-invoker:0.0.1", ".").
Return(nil).
Once()
functionYamlPath, _ := filepath.Abs("matching-invoker-function.yaml")
topicsYamlPath, _ := filepath.Abs("matching-invoker-topics.yaml")
normalKubeCtl.On("Exec", []string{"apply", "-f", functionYamlPath, "-f", topicsYamlPath}).
Return("function \"matching-invoker\" created\ntopic \"matching-invoker\" created", nil).
Once()

err = rootCommand.Execute()
Expect(err).NotTo(HaveOccurred())

Expect(".").NotTo(HaveUnstagedChanges())
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(fmt.Errorf("The invoker must be specified. Pick one of: node")))
})

It("creates a function with an explicit invoker", func() {
Expand Down Expand Up @@ -160,7 +152,7 @@ var _ = Describe("The create command", func() {
rootCommand, initOptions, _, _, err := setupCreateTest(invokers, normalDocker, dryRunDocker, normalKubeCtl, dryRunKubeCtl)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs(append([]string{"create", "--dry-run", "../test_data/command/echo", "-a", "echo.sh", "-v", "0.0.1-snapshot"}, commonRiffArgs...))
rootCommand.SetArgs(append([]string{"create", "command", "--dry-run", "../test_data/command/echo", "-a", "echo.sh", "-v", "0.0.1-snapshot"}, commonRiffArgs...))

dryRunDocker.On("Exec", "build", "-t", "rifftest/echo:0.0.1-snapshot", "../test_data/command/echo").
Return(nil).
Expand All @@ -186,7 +178,7 @@ var _ = Describe("The create command", func() {
rootCommand, _, _, _, err := setupCreateTest(invokers, normalDocker, dryRunDocker, normalKubeCtl, dryRunKubeCtl)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs(append([]string{"create", "--dry-run", "-a", "echo.sh"}, commonRiffArgs...))
rootCommand.SetArgs(append([]string{"create", "command", "--dry-run", "-a", "echo.sh"}, commonRiffArgs...))

dryRunDocker.On("Exec", "build", "-t", "rifftest/echo:0.0.1", ".").
Return(nil).
Expand All @@ -208,7 +200,7 @@ var _ = Describe("The create command", func() {

path, _ := filepath.Abs("../test_data/command/echo")

rootCommand.SetArgs(append([]string{"create", "--dry-run", "-f", path, "-v", "0.0.1-snapshot", "-a", "echo.sh"}, commonRiffArgs...))
rootCommand.SetArgs(append([]string{"create", "command", "--dry-run", "-f", path, "-v", "0.0.1-snapshot", "-a", "echo.sh"}, commonRiffArgs...))

dryRunDocker.On("Exec", "build", "-t", "rifftest/echo:0.0.1-snapshot", path).
Return(nil).
Expand All @@ -232,7 +224,7 @@ var _ = Describe("The create command", func() {
rootCommand, initOptions, _, _, err := setupCreateTest(invokers, normalDocker, dryRunDocker, normalKubeCtl, dryRunKubeCtl)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs([]string{"create", "--dry-run", "../test_data/command/echo", "-u", "me", "-a", "echo.sh"})
rootCommand.SetArgs([]string{"create", "command", "--dry-run", "../test_data/command/echo", "-u", "me", "-a", "echo.sh"})

dryRunDocker.On("Exec", "build", "-t", "me/echo:0.0.1", "../test_data/command/echo").
Return(nil).
Expand Down
34 changes: 27 additions & 7 deletions riff-cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"
"strings"

"path/filepath"

Expand All @@ -36,14 +37,14 @@ func Init(invokers []projectriff_v1.Invoker) (*cobra.Command, *options.InitOptio
Use: "init",
Short: "Initialize a function",
Long: utils.InitCmdLong(),
Args: utils.AliasFlagToSoleArg("filepath"),

RunE: func(cmd *cobra.Command, args []string) error {
err := initializer.Initialize(invokers, &initOptions)
if err != nil {
cmd.SilenceUsage = true
cmd.SilenceUsage = true
if len(invokers) == 0 {
return fmt.Errorf("Invokers must be installed, run `riff invokers apply --help` for help")
}
return err
names := invokerNames(invokers)
return fmt.Errorf("The invoker must be specified. Pick one of: %s", strings.Join(names, ", "))
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
initOptions.UserAccount = utils.GetUseraccountWithOverride("useraccount", *cmd.Flags())
Expand Down Expand Up @@ -82,8 +83,11 @@ func InitInvokers(invokers []projectriff_v1.Invoker, initOptions *options.InitOp
Short: fmt.Sprintf("Initialize a %s function", invokerName),
Long: utils.InitInvokerCmdLong(invoker),
RunE: func(cmd *cobra.Command, args []string) error {
initOptions.InvokerName = invokerName
return initializer.Initialize(invokers, initOptions)
invoker, err := invokerForName(invokerName, invokers)
if err != nil {
return err
}
return initializer.Initialize(invoker, initOptions)
},
}

Expand Down Expand Up @@ -116,3 +120,19 @@ func validateInitOptions(options *options.InitOptions) error {
return nil
}

func invokerForName(name string, invokers []projectriff_v1.Invoker) (projectriff_v1.Invoker, error) {
for _, invoker := range invokers {
if invoker.ObjectMeta.Name == name {
return invoker, nil
}
}
return projectriff_v1.Invoker{}, fmt.Errorf("No invoker found for %s", name)
}

func invokerNames(invokers []projectriff_v1.Invoker) []string {
names := []string{}
for _, invoker := range invokers {
names = append(names, invoker.ObjectMeta.Name)
}
return names
}
53 changes: 4 additions & 49 deletions riff-cli/cmd/init_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,11 +50,12 @@ var _ = Describe("The init command", func() {

err = rootCommand.Execute()
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(fmt.Errorf("Invokers must be installed, run `riff invokers apply --help` for help")))

Expect(".").NotTo(HaveUnstagedChanges())
})

It("should fail if no matching invoker is defined", func() {
It("should fail if no invoker is specified", func() {
os.Chdir("../test_data/riff-init/no-matching-invoker")

invokers, err := stubInvokers("invokers/*.yaml")
Expand All @@ -65,54 +67,7 @@ var _ = Describe("The init command", func() {

err = rootCommand.Execute()
Expect(err).To(HaveOccurred())

Expect(".").NotTo(HaveUnstagedChanges())
})

It("should find an invoker based on an artifact", func() {
os.Chdir("../test_data/riff-init/matching-invoker")

invokers, err := stubInvokers("invokers/*.yaml")
Expect(err).NotTo(HaveOccurred())
rootCommand, _, _, _, err := setupInitTest(invokers)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs(append([]string{"init", "--artifact", "echo.js"}, commonRiffArgs...))

err = rootCommand.Execute()
Expect(err).NotTo(HaveOccurred())

Expect(".").NotTo(HaveUnstagedChanges())
})

It("should find an artifact based on an invoker", func() {
os.Chdir("../test_data/riff-init/multiple-matching-invokers-with-one-selected-no-artifact")

invokers, err := stubInvokers("invokers/*.yaml")
Expect(err).NotTo(HaveOccurred())
rootCommand, _, _, _, err := setupInitTest(invokers)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs(append([]string{"init", "python3"}, commonRiffArgs...))

err = rootCommand.Execute()
Expect(err).NotTo(HaveOccurred())

Expect(".").NotTo(HaveUnstagedChanges())
})

It("should fail when multiple invokers match", func() {
os.Chdir("../test_data/riff-init/multiple-matching-invokers")

invokers, err := stubInvokers("invokers/*.yaml")
Expect(err).NotTo(HaveOccurred())
rootCommand, _, _, _, err := setupInitTest(invokers)
Expect(err).NotTo(HaveOccurred())

rootCommand.SetArgs(append([]string{"init", "--artifact", "echo.py"}, commonRiffArgs...))

err = rootCommand.Execute()
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(fmt.Errorf("The invoker must be specified. Pick one of: node")))

Expect(".").NotTo(HaveUnstagedChanges())
})
Expand Down
Loading

0 comments on commit cfc0926

Please sign in to comment.