Skip to content

Commit

Permalink
Simpler e2e-tests for generate
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Henrik Lunde <[email protected]>
  • Loading branch information
chlunde committed May 6, 2020
1 parent 73c68a0 commit 88fb60a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 61 deletions.
5 changes: 3 additions & 2 deletions pkg/cmd/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 6 additions & 59 deletions test/e2e/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
package e2e

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"testing"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 88fb60a

Please sign in to comment.