Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): add e2e tests for dev mode #1147

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions e2e/dev_mode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// +build integration

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"

/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"io"
"testing"
"time"

"github.com/apache/camel-k/e2e/util"
. "github.com/onsi/gomega"
)

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

t.Run("run yaml dev mode", func(t *testing.T) {
ctx, cancel := context.WithCancel(testContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

file := util.MakeTempCopy(t, "files/yaml.yaml")

kamelRun := kamelWithContext(ctx, "run", "-n", ns, file, "--dev")
kamelRun.SetOut(pipew)

logScanner := util.NewLogScanner(ctx, piper, "Magicstring!", "Magicjordan!")

go kamelRun.Execute()

Eventually(logScanner.IsFound("Magicstring!"), 5*time.Minute).Should(BeTrue())
Expect(logScanner.IsFound("Magicjordan!")()).To(BeFalse())

util.ReplaceInFile(t, file, "string!", "jordan!")
Eventually(logScanner.IsFound("Magicjordan!"), 5*time.Minute).Should(BeTrue())
})

t.Run("run yaml remote dev mode", func(t *testing.T) {
ctx, cancel := context.WithCancel(testContext)
defer cancel()
piper, pipew := io.Pipe()
defer pipew.Close()
defer piper.Close()

remoteFile := "https://github.com/apache/camel-k/raw/e80eb5353cbccf47c89a9f0a1c68ffbe3d0f1521/e2e/files/yaml.yaml"
kamelRun := kamelWithContext(ctx, "run", "-n", ns, remoteFile, "--dev")
kamelRun.SetOut(pipew)

logScanner := util.NewLogScanner(ctx, piper, "Magicstring!")

go kamelRun.Execute()

Eventually(logScanner.IsFound("Magicstring!"), 5*time.Minute).Should(BeTrue())
})
})
}
6 changes: 5 additions & 1 deletion e2e/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func newTestClient() (client.Client, error) {
}

func kamel(args ...string) *cobra.Command {
return kamelWithContext(testContext, args...)
}

func kamelWithContext(ctx context.Context, args ...string) *cobra.Command {
var c *cobra.Command
var err error

Expand All @@ -121,7 +125,7 @@ func kamel(args ...string) *cobra.Command {
},
}
} else {
c, err = cmd.NewKamelCommand(testContext)
c, err = cmd.NewKamelCommand(ctx)
}
if err != nil {
panic(err)
Expand Down
71 changes: 71 additions & 0 deletions e2e/util/log_scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"bufio"
"context"
"fmt"
"io"
"strings"
)

// LogScanner can attach to a stream and check if a value is printed
type LogScanner struct {
in io.Reader
ctx context.Context
values map[string]bool
}

// NewLogScanner --
func NewLogScanner(ctx context.Context, in io.Reader, values ...string) *LogScanner {
scanner := LogScanner{
ctx: ctx,
in: in,
values: make(map[string]bool),
}
for _, v := range values {
scanner.values[v] = false
}
go scanner.startScan()
return &scanner
}

func (s *LogScanner) startScan() {
scanner := bufio.NewScanner(s.in)
for scanner.Scan() {
if s.ctx.Err() != nil {
return
}
text := scanner.Text()
fmt.Println(text)
for k := range s.values {
if strings.Contains(text, k) {
fmt.Printf("LogScanner - Found match for: %s\n", k)
s.values[k] = true
}
}
}
}

// IsFound returns if the string has been found in the logs
func (s *LogScanner) IsFound(value string) func() bool {
return func() bool {
return s.values[value]
}
}
62 changes: 62 additions & 0 deletions e2e/util/temp_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
)

// MakeTempCopy --
func MakeTempCopy(t *testing.T, fileName string) string {
_, simpleName := path.Split(fileName)
var err error
var tmpDir string
if tmpDir, err = ioutil.TempDir("", "camel-k-"); err != nil {
t.Error(err)
t.FailNow()
}
tmpFileName := path.Join(tmpDir, simpleName)
var content []byte
if content, err = ioutil.ReadFile(fileName); err != nil {
t.Error(err)
t.FailNow()
}
if err = ioutil.WriteFile(tmpFileName, content, os.FileMode(0777)); err != nil {
t.Error(err)
t.FailNow()
}
return tmpFileName
}

// ReplaceInFile replace strings in a file with new values
func ReplaceInFile(t *testing.T, fileName string, old, new string) {
content, err := ioutil.ReadFile(fileName)
if err != nil {
t.Error(err)
t.FailNow()
}
res := strings.ReplaceAll(string(content), old, new)
if err = ioutil.WriteFile(fileName, []byte(res), os.FileMode(0777)); err != nil {
t.Error(err)
t.FailNow()
}
}
3 changes: 1 addition & 2 deletions pkg/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package cmd
import (
"context"
"fmt"
"os"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand Down Expand Up @@ -184,7 +183,7 @@ func newCmdCompletionBash(root *cobra.Command) *cobra.Command {
Short: "Generates bash completion scripts",
Long: bashCompletionCmdLongDescription,
Run: func(_ *cobra.Command, _ []string) {
err := root.GenBashCompletion(os.Stdout)
err := root.GenBashCompletion(root.OutOrStdout())
if err != nil {
fmt.Print(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/completion_zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func newCmdCompletionZsh(root *cobra.Command) *cobra.Command {
Short: "Generates zsh completion scripts",
Long: zshCompletionCmdLongDescription,
Run: func(_ *cobra.Command, _ []string) {
err := root.GenZshCompletion(os.Stdout)
err := root.GenZshCompletion(root.OutOrStdout())
if err != nil {
fmt.Print(err.Error())
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"fmt"
"os"
"text/tabwriter"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -49,7 +48,7 @@ func newCmdGet(rootCmdOptions *RootCmdOptions) *cobra.Command {
return &cmd
}

func (o *getCmdOptions) run(_ *cobra.Command, args []string) error {
func (o *getCmdOptions) run(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
return err
Expand Down Expand Up @@ -78,7 +77,7 @@ func (o *getCmdOptions) run(_ *cobra.Command, args []string) error {
return err
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "NAME\tPHASE\tKIT")
for _, integration := range integrationList.Items {
fmt.Fprintf(w, "%s\t%s\t%s\n", integration.Name, string(integration.Status.Phase), integration.Status.Kit)
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/kit_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"fmt"
"os"
"text/tabwriter"

"github.com/spf13/cobra"
Expand All @@ -42,7 +41,7 @@ func newKitGetCmd(rootCmdOptions *RootCmdOptions) *cobra.Command {
if err := impl.validate(cmd, args); err != nil {
return err
}
if err := impl.run(); err != nil {
if err := impl.run(cmd); err != nil {
fmt.Println(err.Error())
}

Expand All @@ -68,7 +67,7 @@ func (command *kitGetCommand) validate(cmd *cobra.Command, args []string) error
return nil
}

func (command *kitGetCommand) run() error {
func (command *kitGetCommand) run(cmd *cobra.Command) error {
kitList := v1alpha1.NewIntegrationKitList()
c, err := command.GetCmdClient()
if err != nil {
Expand All @@ -78,7 +77,7 @@ func (command *kitGetCommand) run() error {
return err
}

w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "NAME\tPHASE\tTYPE\tIMAGE")
for _, ctx := range kitList.Items {
t := ctx.Labels["camel.apache.org/kit.type"]
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (o *logCmdOptions) validate(_ *cobra.Command, args []string) error {
return nil
}

func (o *logCmdOptions) run(_ *cobra.Command, args []string) error {
func (o *logCmdOptions) run(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
return err
Expand All @@ -81,7 +81,7 @@ func (o *logCmdOptions) run(_ *cobra.Command, args []string) error {
if err := c.Get(o.Context, key, &integration); err != nil {
return err
}
if err := k8slog.Print(o.Context, c, &integration); err != nil {
if err := k8slog.Print(o.Context, c, &integration, cmd.OutOrStdout()); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (o *runCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
return nil
}

func (o *runCmdOptions) run(_ *cobra.Command, args []string) error {
func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
return err
Expand Down Expand Up @@ -225,7 +225,7 @@ func (o *runCmdOptions) run(_ *cobra.Command, args []string) error {
}
}
if o.Logs || o.Dev {
err = k8slog.Print(o.Context, c, integration)
err = k8slog.Print(o.Context, c, integration, cmd.OutOrStdout())
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/kubernetes/log/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ import (
"fmt"
"io"
"io/ioutil"
"os"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"

"k8s.io/client-go/kubernetes"
)

// Print prints integrations logs to the stdout
func Print(ctx context.Context, client kubernetes.Interface, integration *v1alpha1.Integration) error {
func Print(ctx context.Context, client kubernetes.Interface, integration *v1alpha1.Integration, out io.Writer) error {
scraper := NewSelectorScraper(client, integration.Namespace, integration.Name, "camel.apache.org/integration="+integration.Name)
reader := scraper.Start(ctx)

if _, err := io.Copy(os.Stdout, ioutil.NopCloser(reader)); err != nil {
if _, err := io.Copy(out, ioutil.NopCloser(reader)); err != nil {
fmt.Println(err.Error())
}

Expand Down