From fb37b92396dca1d05dc39976872c1144c411b5f4 Mon Sep 17 00:00:00 2001 From: David Simansky Date: Tue, 14 Sep 2021 08:52:53 +0200 Subject: [PATCH] [release-v0.24.0] Update spec file & kafka tests (#814) * [release-v0.24.0] Update spec file version * [release-v0.24.0] Add kafka plugin tests --- go.mod | 1 + openshift-serverless-clients.spec | 5 +- test/e2e/source_kafka_test.go | 123 +++++++++++++ .../kn-source-pkg/test/e2e/common.go | 56 ++++++ .../kn-source-pkg/test/e2e/kn_plugin.go | 162 ++++++++++++++++++ vendor/modules.txt | 2 + 6 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 test/e2e/source_kafka_test.go create mode 100644 vendor/github.com/maximilien/kn-source-pkg/test/e2e/common.go create mode 100644 vendor/github.com/maximilien/kn-source-pkg/test/e2e/kn_plugin.go diff --git a/go.mod b/go.mod index f61dc1a8b4..9aab51db1f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/google/go-cmp v0.5.6 + github.com/maximilien/kn-source-pkg v0.6.3 github.com/mitchellh/go-homedir v1.1.0 github.com/spf13/cobra v1.1.3 github.com/spf13/pflag v1.0.5 diff --git a/openshift-serverless-clients.spec b/openshift-serverless-clients.spec index d7dc7372c6..07d35c6780 100644 --- a/openshift-serverless-clients.spec +++ b/openshift-serverless-clients.spec @@ -3,7 +3,7 @@ %global package_name openshift-serverless-clients %global product_name OpenShift Serverless %global golang_version 1.15 -%global kn_version 0.23.2 +%global kn_version 0.24.0 %global kn_release 1 %global kn_cli_version v%{kn_version} %global source_dir knative-client @@ -68,6 +68,9 @@ Obsoletes: %{package_name} < %{kn_version} %{_datadir}/%{name}-redistributable/windows/kn-windows-amd64.exe %changelog +* Mon Sep 13 2021 David Simansky v0.24.0-1 +- Bump kn release v0.24.0 + * Mon Aug 2 2021 David Simansky v0.23.2-1 - Bump kn release v0.23.2 diff --git a/test/e2e/source_kafka_test.go b/test/e2e/source_kafka_test.go new file mode 100644 index 0000000000..901dbacbdc --- /dev/null +++ b/test/e2e/source_kafka_test.go @@ -0,0 +1,123 @@ +// Copyright 2020 The Knative Authors + +// Licensed 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. + +// +build e2e +// +build !serving + +package e2e + +import ( + "os" + "path/filepath" + "testing" + + testcommon "github.com/maximilien/kn-source-pkg/test/e2e" + "gotest.tools/v3/assert" + "knative.dev/client/lib/test" + "knative.dev/client/pkg/util" +) + +const ( + kafkaBootstrapUrl = "my-cluster-kafka-bootstrap.kafka.svc:9092" + kafkaClusterName = "my-cluster" + kafkaClusterNamespace = "kafka" + kafkaTopic = "test-topic" + ceo = "type=foo" +) + +type e2eTest struct { + it *testcommon.E2ETest +} + +func newE2ETest(t *testing.T) *e2eTest { + currentDir, err := os.Getwd() + if err != nil { + return nil + } + + it, err := testcommon.NewE2ETest("kn-source-kafka", filepath.Join(currentDir, "../.."), false) + if err != nil { + return nil + } + + e2eTest := &e2eTest{ + it: it, + } + return e2eTest +} + +func TestSourceKafka(t *testing.T) { + t.Parallel() + + e2eTest := newE2ETest(t) + assert.Assert(t, e2eTest != nil) + defer func() { + assert.NilError(t, e2eTest.it.KnTest().Teardown()) + }() + + r := test.NewKnRunResultCollector(t, e2eTest.it.KnTest()) + defer r.DumpIfFailed() + + //err := e2eTest.it.KnPlugin().Install() + //assert.NilError(t, err) + + serviceCreate(r, "sinksvc") + + t.Log("test kn-plugin-source-kafka create source-name") + e2eTest.knSourceKafkaCreate(t, r, "mykafka1", "sinksvc") + + t.Log("test kn-plugin-source-kafka describe source-name") + e2eTest.knSourceKafkaDescribe(t, r, "mykafka1", "sinksvc", "cloudevent") + + t.Log("test kn-plugin-source-kafka list") + e2eTest.knSourceKafkaList(t, r, "mykafka1") + + t.Log("test kn-plugin-source-kafka delete source-name") + e2eTest.knSourceKafkaDelete(t, r, "mykafka1") + + //err = e2eTest.it.KnPlugin().Uninstall() + //assert.NilError(t, err) +} + +// Private + +func (et *e2eTest) knSourceKafkaCreate(t *testing.T, r *test.KnRunResultCollector, sourceName, sinkName string) { + out := et.it.KnPlugin().Run("create", sourceName, "--servers", kafkaBootstrapUrl, "--topics", kafkaTopic, "--consumergroup", "test-consumer-group", "--sink", sinkName, "--ce-override", ceo) + r.AssertNoError(out) + assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "create", sourceName)) +} + +func (et *e2eTest) knSourceKafkaDelete(t *testing.T, r *test.KnRunResultCollector, sourceName string) { + out := et.it.KnPlugin().Run("delete", sourceName) + r.AssertNoError(out) + assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, "delete", sourceName)) +} + +func (et *e2eTest) knSourceKafkaDescribe(t *testing.T, r *test.KnRunResultCollector, sourceName, sinkName, cloudEvent string) { + out := et.it.KnPlugin().Run("describe", sourceName) + r.AssertNoError(out) + assert.Check(t, util.ContainsAllIgnoreCase(out.Stdout, sourceName, sinkName, cloudEvent)) +} + +func serviceCreate(r *test.KnRunResultCollector, serviceName string) { + out := r.KnTest().Kn().Run("service", "create", serviceName, "--image", "gcr.io/knative-samples/helloworld-go") + r.AssertNoError(out) + assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "service", serviceName, "creating", "namespace", r.KnTest().Kn().Namespace(), "ready")) +} + +func (et *e2eTest) knSourceKafkaList(t *testing.T, r *test.KnRunResultCollector, sourceName string) { + out := et.it.KnPlugin().Run("list") + r.AssertNoError(out) + assert.Check(t, util.ContainsAll(out.Stdout, "NAME", "AGE", "SINK", "BOOTSTRAPSERVERS", sourceName)) +} diff --git a/vendor/github.com/maximilien/kn-source-pkg/test/e2e/common.go b/vendor/github.com/maximilien/kn-source-pkg/test/e2e/common.go new file mode 100644 index 0000000000..e78f105c79 --- /dev/null +++ b/vendor/github.com/maximilien/kn-source-pkg/test/e2e/common.go @@ -0,0 +1,56 @@ +// Copyright 2020 The Knative Authors + +// Licensed 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 ( + "knative.dev/client/lib/test" +) + +type E2ETest struct { + knTest *test.KnTest + knPlugin *knPlugin +} + +// NewE2ETest for pluginName in pluginPath +func NewE2ETest(pluginName string, pluginPath string, install bool) (*E2ETest, error) { + knTest, err := test.NewKnTest() + if err != nil { + return nil, err + } + + knPlugin := &knPlugin{ + kn: knTest.Kn(), + pluginName: pluginName, + pluginPath: pluginPath, + install: install, + } + + e2eTest := &E2ETest{ + knTest: knTest, + knPlugin: knPlugin, + } + + return e2eTest, nil +} + +// KnTest object +func (e2eTest *E2ETest) KnTest() *test.KnTest { + return e2eTest.knTest +} + +// KnPlugin object +func (e2eTest *E2ETest) KnPlugin() *knPlugin { + return e2eTest.knPlugin +} diff --git a/vendor/github.com/maximilien/kn-source-pkg/test/e2e/kn_plugin.go b/vendor/github.com/maximilien/kn-source-pkg/test/e2e/kn_plugin.go new file mode 100644 index 0000000000..609705cafb --- /dev/null +++ b/vendor/github.com/maximilien/kn-source-pkg/test/e2e/kn_plugin.go @@ -0,0 +1,162 @@ +// Copyright 2020 The Knative Authors + +// Licensed 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 ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strings" + + homedir "github.com/mitchellh/go-homedir" + "knative.dev/client/lib/test" +) + +type knPlugin struct { + kn test.Kn + pluginName string + pluginPath string + install bool +} + +// Run the KnPlugin returning a KnRunResult +func (kp *knPlugin) Run(args ...string) test.KnRunResult { + if kp.install { + err := kp.Install() + if err != nil { + fmt.Printf("error installing kn plugin: %s\n", err.Error()) + return test.KnRunResult{} + } + defer kp.Uninstall() + } + return RunKnPlugin(kp.kn.Namespace(), kp.pluginName, args) +} + +// Kn object to run `kn` +func (kp *knPlugin) Kn() test.Kn { + return kp.kn +} + +// Install the KnPlugin +func (kp *knPlugin) Install() error { + configDir, err := defaultConfigDir() + if err != nil { + fmt.Printf("error determining config directory: %s\n", err.Error()) + return err + } + + pluginDir := filepath.Join(configDir, "plugins") + if !dirExists(pluginDir) { + err = os.MkdirAll(pluginDir, 0700) + if err != nil { + return err + } + } + + fmt.Printf("installing 'kn' plugin '%s' from path '%s' to config path: %s\n", kp.pluginName, kp.pluginPath, configDir) + + err = copyPluginFile(filepath.Join(kp.pluginPath, kp.pluginName), filepath.Join(pluginDir, kp.pluginName)) + if err != nil { + fmt.Printf("error copying plugin file to config directory: %s\n", err.Error()) + return err + } + + return nil +} + +// Uninstall the KnPlugin +func (kp *knPlugin) Uninstall() error { + configDir, err := defaultConfigDir() + if err != nil { + fmt.Printf("error determining config directory: %s\n", err.Error()) + return err + } + + fmt.Printf("uninstalling 'kn' plugin '%s' from config path '%s'\n", kp.pluginName, configDir) + + err = os.Remove(filepath.Join(configDir, "plugins", kp.pluginName)) + if err != nil { + fmt.Printf("error removing plugin from config directory: %s\n", err.Error()) + return err + } + + return nil +} + +// Utility functions + +func copyPluginFile(sourceFile string, destDir string) error { + input, err := ioutil.ReadFile(sourceFile) + if err != nil { + return err + } + + err = ioutil.WriteFile(destDir, input, 0700) + if err != nil { + return err + } + + return nil +} + +func defaultConfigDir() (string, error) { + home, err := homedir.Dir() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + // Check the deprecated path first and fallback to it, add warning to error message + if configHome := filepath.Join(home, ".kn"); dirExists(configHome) { + migrationPath := filepath.Join(home, ".config", "kn") + if runtime.GOOS == "windows" { + migrationPath = filepath.Join(os.Getenv("APPDATA"), "kn") + } + return configHome, fmt.Errorf("WARNING: deprecated kn config directory detected. "+ + "Please move your configuration to: %s", migrationPath) + } + // Respect %APPDATA% on MS Windows + // C:\Documents and Settings\username\Application JsonData + if runtime.GOOS == "windows" { + return filepath.Join(os.Getenv("APPDATA"), "kn"), nil + } + // Respect XDG_CONFIG_HOME if set + if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" { + return filepath.Join(xdgHome, "kn"), nil + } + // Fallback to XDG default for both Linux and macOS + // ~/.config/kn + return filepath.Join(home, ".config", "kn"), nil +} + +func dirExists(path string) bool { + if _, err := os.Stat(path); !os.IsNotExist(err) { + return true + } + return false +} + +func pluginArgs(pluginName string) []string { + pluginParts := strings.Split(pluginName, "-") + return pluginParts[1:] +} + +func RunKnPlugin(namespace string, pluginName string, args []string) test.KnRunResult { + pluginArgs := pluginArgs(pluginName) + args = append(args, []string{"--namespace", namespace}...) + argsWithPlugin := append(pluginArgs, args...) + return test.RunKn(namespace, argsWithPlugin) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 587989af29..01d6a9b393 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -256,12 +256,14 @@ github.com/mailru/easyjson/jwriter # github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 github.com/matttproud/golang_protobuf_extensions/pbutil # github.com/maximilien/kn-source-pkg v0.6.3 +## explicit github.com/maximilien/kn-source-pkg/pkg/client github.com/maximilien/kn-source-pkg/pkg/commands/source github.com/maximilien/kn-source-pkg/pkg/core github.com/maximilien/kn-source-pkg/pkg/factories github.com/maximilien/kn-source-pkg/pkg/types github.com/maximilien/kn-source-pkg/pkg/types/typesfakes +github.com/maximilien/kn-source-pkg/test/e2e # github.com/mitchellh/go-homedir v1.1.0 ## explicit github.com/mitchellh/go-homedir