Skip to content

Commit

Permalink
Fix #1201: add label flag and fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Feb 21, 2020
1 parent c94a49d commit 2914381
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
29 changes: 25 additions & 4 deletions e2e/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,42 @@ import (

. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"

_ "github.com/apache/camel-k/addons"
)

func TestAddons(t *testing.T) {
withNewTestNamespace(t, func(ns string) {
Expect(kamel("install", "-n", ns).Execute()).Should(BeNil())

t.Run("master addon", func(t *testing.T) {
t.Run("master works", func(t *testing.T) {
RegisterTestingT(t)
Expect(kamel("run", "-n", ns, "files/Master.java", "-t", `master.label-key=""`).Execute()).Should(BeNil())
Expect(kamel("run", "-n", ns, "files/Master.java").Execute()).Should(BeNil())
Eventually(integrationPodPhase(ns, "master"), 5*time.Minute).Should(Equal(v1.PodRunning))
Eventually(integrationLogs(ns, "master"), 1*time.Minute).Should(ContainSubstring("Magicstring!"))
Eventually(configMap(ns, "master-lock"), 30*time.Second).ShouldNot(BeNil())
Expect(kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil())
})

t.Run("only one integration with master runs", func(t *testing.T) {
RegisterTestingT(t)
Expect(kamel("run", "-n", ns, "files/Master.java",
"--name", "first",
"--label", "leader-group=same",
"-t", "master.label-key=leader-group",
"-t", "master.label-value=same",
"-t", "owner.target-labels=leader-group").Execute()).Should(BeNil())
Eventually(integrationPodPhase(ns, "first"), 5*time.Minute).Should(Equal(v1.PodRunning))
Eventually(integrationLogs(ns, "first"), 1*time.Minute).Should(ContainSubstring("Magicstring!"))
Eventually(configMap(ns, "first-lock"), 30*time.Second).ShouldNot(BeNil())
// Start a second integration with the same lock (it should not start the route)
Expect(kamel("run", "-n", ns, "files/Master.java", "--name", "second",
"-t", "master.configmap=master-lock", "-t", `master.label-key=""`).Execute()).Should(BeNil())
Expect(kamel("run", "-n", ns, "files/Master.java",
"--name", "second",
"--label", "leader-group=same",
"-t", "master.label-key=leader-group",
"-t", "master.label-value=same",
"-t", "master.configmap=first-lock",
"-t", "owner.target-labels=leader-group").Execute()).Should(BeNil())
Eventually(integrationPodPhase(ns, "second"), 5*time.Minute).Should(Equal(v1.PodRunning))
Eventually(integrationLogs(ns, "second"), 1*time.Minute).Should(ContainSubstring("started in"))
Eventually(integrationLogs(ns, "second"), 30*time.Second).ShouldNot(ContainSubstring("Magicstring!"))
Expand Down
2 changes: 1 addition & 1 deletion e2e/files/Master.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.apache.camel.builder.RouteBuilder;

public class Java extends RouteBuilder {
public class Master extends RouteBuilder {
@Override
public void configure() throws Exception {
from("master:lock:timer:tick")
Expand Down
19 changes: 19 additions & 0 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
cmd.Flags().StringArrayP("volume", "v", nil, "Mount a volume into the integration container. E.g \"-v pvcname:/container/path\"")
cmd.Flags().StringArrayP("env", "e", nil, "Set an environment variable in the integration container. E.g \"-e MY_VAR=my-value\"")
cmd.Flags().StringArrayP("property-file", "", nil, "Bind a property file to the integration. E.g. \"--property-file integration.properties\"")
cmd.Flags().StringArrayP("label", "", nil, "Add a label to the integration. E.g. \"--label my.company=hello\"")

cmd.Flags().Bool("save", false, "Save the run parameters into the default kamel configuration file (kamel-config.yaml)")

Expand Down Expand Up @@ -120,6 +121,7 @@ type runCmdOptions struct {
Volumes []string `mapstructure:"volumes"`
EnvVars []string `mapstructure:"envs"`
PropertyFiles []string `mapstructure:"property-files"`
Labels []string `mapstructure:"labels"`
}

func (o *runCmdOptions) decode(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -188,6 +190,13 @@ func (o *runCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
}
}

for _, label := range o.Labels {
parts := strings.Split(label, "=")
if len(parts) != 2 {
return fmt.Errorf(`invalid label specification %s. Expected "<labelkey>=<labelvalue>"`, label)
}
}

return nil
}

Expand Down Expand Up @@ -378,6 +387,16 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string)
},
}

for _, label := range o.Labels {
parts := strings.Split(label, "=")
if len(parts) == 2 {
if integration.Labels == nil {
integration.Labels = make(map[string]string)
}
integration.Labels[parts[0]] = parts[1]
}
}

for _, source := range sources {
data, err := o.loadData(source, o.Compression)
if err != nil {
Expand Down

0 comments on commit 2914381

Please sign in to comment.