github.com/kubevela/workflow@v0.6.0/pkg/utils/utils_test.go (about)

     1  /*
     2  Copyright 2022 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 utils
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    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  	clientfake "k8s.io/client-go/kubernetes/fake"
    28  
    29  	"github.com/kubevela/workflow/pkg/cue/model/sets"
    30  	"github.com/kubevela/workflow/pkg/types"
    31  )
    32  
    33  func TestGetWorkflowContextData(t *testing.T) {
    34  	ctx := context.Background()
    35  	cm := &corev1.ConfigMap{
    36  		ObjectMeta: metav1.ObjectMeta{
    37  			Name:      "workflow-test-context",
    38  			Namespace: "default",
    39  		},
    40  		Data: map[string]string{
    41  			"vars": `{"test-test": "test"}`,
    42  		},
    43  	}
    44  	err := cli.Create(ctx, cm)
    45  	r := require.New(t)
    46  	r.NoError(err)
    47  	defer func() {
    48  		err = cli.Delete(ctx, cm)
    49  		r.NoError(err)
    50  	}()
    51  
    52  	testCases := map[string]struct {
    53  		name        string
    54  		paths       string
    55  		expected    string
    56  		expectedErr string
    57  	}{
    58  		"not found": {
    59  			name:        "not-found",
    60  			expectedErr: "not found",
    61  		},
    62  		"found": {
    63  			name:     "workflow-test-context",
    64  			expected: "\"test-test\": \"test\"\n",
    65  		},
    66  		"found with path": {
    67  			name:     "workflow-test-context",
    68  			paths:    "test-test",
    69  			expected: "\"test\"\n",
    70  		},
    71  		"path not found": {
    72  			name:        "workflow-test-context",
    73  			paths:       "not-found",
    74  			expectedErr: "not exist",
    75  		},
    76  	}
    77  	for name, tc := range testCases {
    78  		t.Run(name, func(t *testing.T) {
    79  			r := require.New(t)
    80  			v, err := GetDataFromContext(ctx, cli, tc.name, tc.name, "default", tc.paths)
    81  			if tc.expectedErr != "" {
    82  				r.Contains(err.Error(), tc.expectedErr)
    83  				return
    84  			}
    85  			r.NoError(err)
    86  			s, err := sets.ToString(v.CueValue())
    87  			r.NoError(err)
    88  			r.Equal(tc.expected, s)
    89  		})
    90  	}
    91  }
    92  
    93  func TestGetStepLogConfig(t *testing.T) {
    94  	ctx := context.Background()
    95  
    96  	testCases := map[string]struct {
    97  		name        string
    98  		step        string
    99  		config      string
   100  		expected    string
   101  		expectedErr string
   102  	}{
   103  		"not found": {
   104  			name:        "not-found",
   105  			config:      "not-found",
   106  			expectedErr: "not found",
   107  		},
   108  		"no data": {
   109  			name:        "workflow-test-context",
   110  			step:        "step-test",
   111  			config:      "",
   112  			expectedErr: "no log config found",
   113  		},
   114  		"failed to marshal": {
   115  			name:        "workflow-test-context",
   116  			step:        "step-test",
   117  			config:      "test",
   118  			expectedErr: "invalid character",
   119  		},
   120  		"invalid config": {
   121  			name:        "workflow-test-context",
   122  			step:        "step-test",
   123  			config:      `{"test": "test"}`,
   124  			expectedErr: "cannot unmarshal string into Go value of type types.LogConfig",
   125  		},
   126  		"no config for step": {
   127  			name:        "workflow-test-context",
   128  			step:        "step-test",
   129  			config:      `{"no-step": {}}`,
   130  			expectedErr: "no log config found for step step-test",
   131  		},
   132  		"success": {
   133  			name:     "workflow-test-context",
   134  			step:     "step-test",
   135  			config:   `{"step-test": {"data":true}}`,
   136  			expected: `{"data":true}`,
   137  		},
   138  	}
   139  	for name, tc := range testCases {
   140  		t.Run(name, func(t *testing.T) {
   141  			r := require.New(t)
   142  			cm := &corev1.ConfigMap{}
   143  			if tc.config != "not-found" {
   144  				cm = &corev1.ConfigMap{
   145  					ObjectMeta: metav1.ObjectMeta{
   146  						Name:      tc.name,
   147  						Namespace: "default",
   148  					},
   149  					Data: map[string]string{
   150  						"logConfig": tc.config,
   151  					},
   152  				}
   153  				err := cli.Create(ctx, cm)
   154  				r.NoError(err)
   155  				defer func() {
   156  					err = cli.Delete(ctx, cm)
   157  					r.NoError(err)
   158  				}()
   159  			}
   160  			v, err := GetLogConfigFromStep(ctx, cli, tc.name, tc.name, "default", tc.step)
   161  			if tc.expectedErr != "" {
   162  				r.Contains(err.Error(), tc.expectedErr)
   163  				return
   164  			}
   165  			r.NoError(err)
   166  			b, err := json.Marshal(v)
   167  			r.NoError(err)
   168  			r.Equal(tc.expected, string(b))
   169  		})
   170  	}
   171  }
   172  
   173  func TestGetPodListFromResources(t *testing.T) {
   174  	ctx := context.Background()
   175  	pod := &corev1.Pod{
   176  		ObjectMeta: metav1.ObjectMeta{
   177  			Name:      "pod-test",
   178  			Namespace: "default",
   179  			Labels: map[string]string{
   180  				"test-label": "test",
   181  			},
   182  		},
   183  	}
   184  	err := cli.Create(ctx, pod)
   185  	r := require.New(t)
   186  	r.NoError(err)
   187  	defer func() {
   188  		err = cli.Delete(ctx, pod)
   189  		r.NoError(err)
   190  	}()
   191  
   192  	testCases := map[string]struct {
   193  		name        string
   194  		step        string
   195  		resources   []types.Resource
   196  		expected    string
   197  		expectedErr string
   198  	}{
   199  		"not found": {
   200  			name: "not-found",
   201  			resources: []types.Resource{
   202  				{
   203  					Name: "not-found",
   204  				},
   205  			},
   206  			expectedErr: "not found",
   207  		},
   208  		"not found with label": {
   209  			name: "not-found",
   210  			resources: []types.Resource{
   211  				{
   212  					LabelSelector: map[string]string{
   213  						"test-label": "not-found",
   214  					},
   215  				},
   216  			},
   217  			expectedErr: "no pod found",
   218  		},
   219  		"found with name": {
   220  			name: "not-found",
   221  			resources: []types.Resource{
   222  				{
   223  					Name: "pod-test",
   224  				},
   225  			},
   226  			expected: "pod-test",
   227  		},
   228  		"found with label": {
   229  			name: "not-found",
   230  			resources: []types.Resource{
   231  				{
   232  					LabelSelector: map[string]string{
   233  						"test-label": "test",
   234  					},
   235  				},
   236  			},
   237  			expected: "pod-test",
   238  		},
   239  	}
   240  	for name, tc := range testCases {
   241  		t.Run(name, func(t *testing.T) {
   242  			r := require.New(t)
   243  			pods, err := GetPodListFromResources(ctx, cli, tc.resources)
   244  			if tc.expectedErr != "" {
   245  				r.Contains(err.Error(), tc.expectedErr)
   246  				return
   247  			}
   248  			r.NoError(err)
   249  			r.Equal(tc.expected, pods[0].Name)
   250  		})
   251  	}
   252  }
   253  
   254  func TestGetLogsFromURL(t *testing.T) {
   255  	r := require.New(t)
   256  	_, err := GetLogsFromURL(context.Background(), "https://kubevela.io")
   257  	r.NoError(err)
   258  }
   259  
   260  func TestGetLogsFromPod(t *testing.T) {
   261  	r := require.New(t)
   262  	clientSet := clientfake.NewSimpleClientset()
   263  	ctx := context.Background()
   264  	err := cli.Create(ctx, &corev1.Pod{
   265  		ObjectMeta: metav1.ObjectMeta{
   266  			Name:      "pod-test",
   267  			Namespace: "default",
   268  			Labels: map[string]string{
   269  				"test-label": "test",
   270  			},
   271  		},
   272  	})
   273  	r.NoError(err)
   274  	_, err = GetLogsFromPod(ctx, clientSet, cli, "pod-test", "default", "", nil)
   275  	r.NoError(err)
   276  }