From 88fb60afe3d7daa076c715b2b68b33ebf504e97c Mon Sep 17 00:00:00 2001 From: Carl Henrik Lunde Date: Wed, 6 May 2020 23:42:04 +0200 Subject: [PATCH] Simpler e2e-tests for generate Signed-off-by: Carl Henrik Lunde --- pkg/cmd/generate/main.go | 5 +-- test/e2e/generator_test.go | 65 ++++---------------------------------- 2 files changed, 9 insertions(+), 61 deletions(-) diff --git a/pkg/cmd/generate/main.go b/pkg/cmd/generate/main.go index 7fe274853a..2cd16af079 100644 --- a/pkg/cmd/generate/main.go +++ b/pkg/cmd/generate/main.go @@ -9,11 +9,12 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json" + "k8s.io/apimachinery/pkg/util/yaml" + v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1" "github.com/jaegertracing/jaeger-operator/pkg/strategy" "github.com/jaegertracing/jaeger-operator/pkg/version" - k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json" - "k8s.io/apimachinery/pkg/util/yaml" ) // NewGenerateCommand starts the Jaeger Operator diff --git a/test/e2e/generator_test.go b/test/e2e/generator_test.go index 309ce6b40c..c30d2ffb6d 100644 --- a/test/e2e/generator_test.go +++ b/test/e2e/generator_test.go @@ -3,12 +3,11 @@ package e2e import ( - "bytes" "fmt" - "io" "io/ioutil" "net/http" "os" + "os/exec" "strconv" "strings" "testing" @@ -18,17 +17,7 @@ import ( "github.com/operator-framework/operator-sdk/pkg/test/e2eutil" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime/schema" - k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json" - "k8s.io/apimachinery/pkg/runtime/serializer/streaming" - "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/restmapper" - - "github.com/jaegertracing/jaeger-operator/cmd" ) type GeneratorAllInOneTestSuite struct { @@ -98,19 +87,13 @@ func (suite *GeneratorAllInOneTestSuite) TestAllInOne() { }() // Execute the generate command - // https://github.com/operator-framework/operator-sdk/pull/2731 - command := cmd.RootCmd - command.SetArgs([]string{"generate", "--cr", cr.Name(), "--output", output.Name()}) - err = command.Execute() - require.NoError(t, err, "generate failed") - - out, err := ioutil.ReadAll(output) - require.NoError(t, err) + generateOutput, err := exec.Command("../../build/_output/bin/jaeger-operator", "generate", "--cr", cr.Name(), "--output", output.Name()).CombinedOutput() + require.NoError(t, err, "generate failed: %s", generateOutput) - err = createObjects(out) - require.NoError(t, err, "could not create objects from yaml") + kubectlOutput, err := exec.Command("kubectl", "create", "-n", namespace, "-f", output.Name()).CombinedOutput() + require.NoError(t, err, "could not create objects from yaml: %s", kubectlOutput) - err = e2eutil.WaitForDeployment(t, fw.KubeClient, namespace, name, 1, retryInterval, 3*timeout) + err = e2eutil.WaitForDeployment(t, fw.KubeClient, namespace, name, 1, retryInterval, 10*time.Second) // 3*timeout) require.NoError(t, err, "Error waiting for Jaeger deployment") // Check that deployment seems OK @@ -151,39 +134,3 @@ func (suite *GeneratorAllInOneTestSuite) TestAllInOne() { }) require.NoError(t, err, "Failed waiting for expected content") } - -// createObjects emulates `kubectl create -f manifests.yaml` -// Needed as we do not have a kubeconfig on disk -// Follow https://github.com/kubernetes/client-go/issues/711 -func createObjects(manifests []byte) error { - client, err := dynamic.NewForConfig(fw.KubeConfig) - require.NoError(t, err) - - namespace, _ = ctx.GetNamespace() - yamlDecoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) - - // Parse the objects from the yaml - reader := k8s_json.YAMLFramer.NewFrameReader(ioutil.NopCloser(bytes.NewReader(manifests))) - d := streaming.NewDecoder(reader, yamlDecoder) - for { - runtimeObj, gvk, err := d.Decode(nil, nil) - if err == io.EOF { - break - } - require.NoError(t, err, "Error during parse") - - gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind} - groupResources, err := restmapper.GetAPIGroupResources(fw.KubeClient.Discovery()) - require.NoError(t, err, "Error in restmapper.GetAPIGroupResources") - - rm := restmapper.NewDiscoveryRESTMapper(groupResources) - mapping, err := rm.RESTMapping(gk, gvk.Version) - require.NoError(t, err, "Error getting RESTMapping %+v", gk) - - obj := runtimeObj.(*unstructured.Unstructured) - t.Logf("Creating %+v %v (%v) %v", gvk.Kind, obj.GetNamespace(), namespace, obj.GetName()) - _, err = client.Resource(mapping.Resource).Namespace(namespace).Create(obj, metav1.CreateOptions{}) - require.NoError(t, err, "Error creating %+v", obj) - } - return nil -}