github.com/oam-dev/kubevela@v1.9.11/references/cli/debug_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    32  )
    33  
    34  func TestDebugApplicationWithWorkflow(t *testing.T) {
    35  	c := initArgs()
    36  	ioStream := cmdutil.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
    37  	ctx := context.TODO()
    38  
    39  	testCases := map[string]struct {
    40  		app         *v1beta1.Application
    41  		cm          *corev1.ConfigMap
    42  		step        string
    43  		focus       string
    44  		expectedErr string
    45  	}{
    46  		"no debug config map": {
    47  			app: &v1beta1.Application{
    48  				ObjectMeta: metav1.ObjectMeta{
    49  					Name:      "no-debug-config-map",
    50  					Namespace: "default",
    51  				},
    52  				Spec: workflowSpec,
    53  				Status: common.AppStatus{
    54  					Workflow: &common.WorkflowStatus{},
    55  				},
    56  			},
    57  			step:        "test-wf1",
    58  			focus:       "test",
    59  			expectedErr: "failed to get debug configmap",
    60  		},
    61  		"config map no data": {
    62  			app: &v1beta1.Application{
    63  				ObjectMeta: metav1.ObjectMeta{
    64  					Name:      "config-map-no-data",
    65  					Namespace: "default",
    66  					UID:       "12345",
    67  				},
    68  				Spec: workflowSpec,
    69  				Status: common.AppStatus{
    70  					Workflow: &common.WorkflowStatus{},
    71  				},
    72  			},
    73  			cm: &corev1.ConfigMap{
    74  				ObjectMeta: metav1.ObjectMeta{
    75  					Name:      "config-map-no-data-test-wf1-debug-12345",
    76  					Namespace: "default",
    77  				},
    78  			},
    79  			step:        "test-wf1",
    80  			focus:       "test",
    81  			expectedErr: "debug configmap is empty",
    82  		},
    83  		"config map error data": {
    84  			app: &v1beta1.Application{
    85  				ObjectMeta: metav1.ObjectMeta{
    86  					Name:      "config-map-error-data",
    87  					Namespace: "default",
    88  					UID:       "12345",
    89  				},
    90  				Spec: workflowSpec,
    91  				Status: common.AppStatus{
    92  					Workflow: &common.WorkflowStatus{},
    93  				},
    94  			},
    95  			cm: &corev1.ConfigMap{
    96  				ObjectMeta: metav1.ObjectMeta{
    97  					Name:      "config-map-error-data-test-wf1-debug-12345",
    98  					Namespace: "default",
    99  				},
   100  				Data: map[string]string{
   101  					"debug": "error",
   102  				},
   103  			},
   104  			step: "test-wf1",
   105  		},
   106  		"success": {
   107  			app: &v1beta1.Application{
   108  				ObjectMeta: metav1.ObjectMeta{
   109  					Name:      "success",
   110  					Namespace: "default",
   111  					UID:       "12345",
   112  				},
   113  				Spec: workflowSpec,
   114  				Status: common.AppStatus{
   115  					Workflow: &common.WorkflowStatus{},
   116  				},
   117  			},
   118  			cm: &corev1.ConfigMap{
   119  				ObjectMeta: metav1.ObjectMeta{
   120  					Name:      "success-test-wf1-debug-12345",
   121  					Namespace: "default",
   122  				},
   123  				Data: map[string]string{
   124  					"debug": `
   125  test: test
   126  `,
   127  				},
   128  			},
   129  			step:  "test-wf1",
   130  			focus: "test",
   131  		},
   132  		"success-component": {
   133  			app: &v1beta1.Application{
   134  				ObjectMeta: metav1.ObjectMeta{
   135  					Name:      "success",
   136  					Namespace: "default",
   137  				},
   138  				Spec: v1beta1.ApplicationSpec{
   139  					Components: []common.ApplicationComponent{{
   140  						Name:       "test-component",
   141  						Type:       "worker",
   142  						Properties: &runtime.RawExtension{Raw: []byte(`{"cmd":["sleep","1000"],"image":"busybox"}`)},
   143  					}},
   144  				},
   145  				Status: common.AppStatus{
   146  					Workflow: &common.WorkflowStatus{},
   147  				},
   148  			},
   149  			step: "test-component",
   150  		},
   151  	}
   152  
   153  	for name, tc := range testCases {
   154  		t.Run(name, func(t *testing.T) {
   155  			r := require.New(t)
   156  			d := &debugOpts{
   157  				step:  tc.step,
   158  				focus: tc.focus,
   159  			}
   160  			client, err := c.GetClient()
   161  			r.NoError(err)
   162  			if tc.cm != nil {
   163  				err := client.Create(ctx, tc.cm)
   164  				r.NoError(err)
   165  			}
   166  			wargs := &WorkflowArgs{
   167  				Args: c,
   168  				Type: instanceTypeApplication,
   169  				App:  tc.app,
   170  			}
   171  			err = wargs.generateWorkflowInstance(ctx, client)
   172  			r.NoError(err)
   173  			err = d.debugApplication(ctx, wargs, c, ioStream)
   174  			if tc.expectedErr != "" {
   175  				r.Contains(err.Error(), tc.expectedErr)
   176  				return
   177  			}
   178  			r.NoError(err)
   179  		})
   180  	}
   181  }