|
| 1 | +// Copyright 2018 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmdtest |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "time" |
| 22 | + |
| 23 | + cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/client-go/tools/clientcmd" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + testNamespace string |
| 34 | + kubeconfigCluster string |
| 35 | + globalManifestPathCluster string |
| 36 | +) |
| 37 | + |
| 38 | +func NewTestClusterCmd() *cobra.Command { |
| 39 | + testCmd := &cobra.Command{ |
| 40 | + Use: "test --test-location <path to tests directory> [flags]", |
| 41 | + Short: "Run End-To-End tests", |
| 42 | + Run: testClusterFunc, |
| 43 | + } |
| 44 | + defaultKubeConfig := "" |
| 45 | + homedir, ok := os.LookupEnv("HOME") |
| 46 | + if ok { |
| 47 | + defaultKubeConfig = homedir + "/.kube/config" |
| 48 | + } |
| 49 | + testCmd.Flags().StringVarP(&testNamespace, "namespace", "n", "default", "Namespace to run tests in") |
| 50 | + testCmd.Flags().StringVarP(&kubeconfigCluster, "kubeconfig", "k", defaultKubeConfig, "Kubeconfig path") |
| 51 | + testCmd.Flags().StringVarP(&globalManifestPathCluster, "global-init", "g", "", "Path to manifest for Global resources (e.g. CRD manifest)") |
| 52 | + |
| 53 | + return testCmd |
| 54 | +} |
| 55 | + |
| 56 | +func testClusterFunc(cmd *cobra.Command, args []string) { |
| 57 | + if globalManifestPathCluster != "" { |
| 58 | + globalCmd := exec.Command("kubectl", "create", "-f", globalManifestPathCluster) |
| 59 | + cmdOut, err := globalCmd.CombinedOutput() |
| 60 | + if err != nil { |
| 61 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("Could not create global resources: %v\nKubectl Output: %v", err, cmdOut)) |
| 62 | + } |
| 63 | + } |
| 64 | + testPod := &v1.Pod{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: "operator-test", |
| 67 | + }, |
| 68 | + Spec: v1.PodSpec{ |
| 69 | + RestartPolicy: v1.RestartPolicyNever, |
| 70 | + Containers: []v1.Container{{ |
| 71 | + Name: "operator-test", |
| 72 | + Image: args[0], |
| 73 | + ImagePullPolicy: v1.PullAlways, |
| 74 | + Command: []string{"/go-test.sh"}, |
| 75 | + Env: []v1.EnvVar{{ |
| 76 | + Name: "TEST_NAMESPACE", |
| 77 | + ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}}, |
| 78 | + }}, |
| 79 | + }}, |
| 80 | + }, |
| 81 | + } |
| 82 | + kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigCluster) |
| 83 | + if err != nil { |
| 84 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get kubeconfig: %v", err)) |
| 85 | + } |
| 86 | + kubeclient, err := kubernetes.NewForConfig(kubeconfig) |
| 87 | + if err != nil { |
| 88 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create kubeclient: %v", err)) |
| 89 | + } |
| 90 | + testPod, err = kubeclient.CoreV1().Pods(testNamespace).Create(testPod) |
| 91 | + if err != nil { |
| 92 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create test pod: %v", err)) |
| 93 | + } |
| 94 | + for { |
| 95 | + testPod, err = kubeclient.CoreV1().Pods(testNamespace).Get("operator-test", metav1.GetOptions{}) |
| 96 | + if err != nil { |
| 97 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get test pod: %v", err)) |
| 98 | + } |
| 99 | + if testPod.Status.Phase != v1.PodSucceeded && testPod.Status.Phase != v1.PodFailed { |
| 100 | + time.Sleep(time.Second * 5) |
| 101 | + continue |
| 102 | + } else if testPod.Status.Phase == v1.PodSucceeded { |
| 103 | + fmt.Printf("Test Successfully Completed") |
| 104 | + return |
| 105 | + } else if testPod.Status.Phase == v1.PodFailed { |
| 106 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("Test Failed: %+v", kubeclient.CoreV1().Pods(testNamespace).GetLogs("operator-test", &v1.PodLogOptions{}))) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments