github.com/kubevela/workflow@v0.6.0/pkg/cue/model/instance_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 model
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"cuelang.org/go/cue/cuecontext"
    24  	"github.com/stretchr/testify/require"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  )
    28  
    29  func TestInstance(t *testing.T) {
    30  
    31  	testCases := []struct {
    32  		src string
    33  		gvk schema.GroupVersionKind
    34  	}{{
    35  		src: `apiVersion: "apps/v1"
    36  kind: "Deployment"
    37  metadata: name: "test"
    38  `,
    39  		gvk: schema.GroupVersionKind{
    40  			Group:   "apps",
    41  			Version: "v1",
    42  			Kind:    "Deployment",
    43  		}},
    44  	}
    45  
    46  	for _, v := range testCases {
    47  		inst := cuecontext.New().CompileString(v.src)
    48  		base, err := NewBase(inst.Value())
    49  		if err != nil {
    50  			t.Error(err)
    51  			return
    52  		}
    53  		baseObj, err := base.Unstructured()
    54  		if err != nil {
    55  			t.Error(err)
    56  			return
    57  		}
    58  		r := require.New(t)
    59  		r.Equal(v.gvk, baseObj.GetObjectKind().GroupVersionKind())
    60  		r.Equal(true, base.IsBase())
    61  
    62  		other, err := NewOther(inst.Value())
    63  		if err != nil {
    64  			t.Error(err)
    65  			return
    66  		}
    67  		otherObj, err := other.Unstructured()
    68  		if err != nil {
    69  			t.Error(err)
    70  			return
    71  		}
    72  
    73  		r.Equal(v.gvk, otherObj.GetObjectKind().GroupVersionKind())
    74  		r.Equal(false, other.IsBase())
    75  	}
    76  }
    77  
    78  func TestIncompleteError(t *testing.T) {
    79  	base := `parameter: {
    80  	name: string
    81  	// +usage=Which image would you like to use for your service
    82  	// +short=i
    83  	image: string
    84  	// +usage=Which port do you want customer traffic sent to
    85  	// +short=p
    86  	port: *8080 | int
    87  	env: [...{
    88  		name:  string
    89  		value: string
    90  	}]
    91  	cpu?: string
    92  }
    93  output: {
    94  	apiVersion: "apps/v1"
    95  	kind:       "Deployment"
    96  	metadata: name: parameter.name
    97  	spec: {
    98  		selector:
    99  			matchLabels:
   100  				app: parameter.name
   101  		template: {
   102  			metadata:
   103  				labels:
   104  					app: parameter.name
   105  			spec: containers: [{
   106  				image: parameter.image
   107  				name:  parameter.name
   108  				env:   parameter.env
   109  				ports: [{
   110  					containerPort: parameter.port
   111  					protocol:      "TCP"
   112  					name:          "default"
   113  				}]
   114  				if parameter["cpu"] != _|_ {
   115  					resources: {
   116  						limits:
   117  							cpu: parameter.cpu
   118  						requests:
   119  							cpu: parameter.cpu
   120  					}
   121  				}
   122  			}]
   123  	}
   124  	}
   125  }
   126  `
   127  
   128  	r := require.New(t)
   129  	inst := cuecontext.New().CompileString(base)
   130  	newbase, err := NewBase(inst.Value())
   131  	r.NoError(err)
   132  	data, err := newbase.Unstructured()
   133  	r.Error(err)
   134  	var expnil *unstructured.Unstructured
   135  	r.Equal(expnil, data)
   136  }
   137  
   138  func TestError(t *testing.T) {
   139  	ctx := cuecontext.New()
   140  	ins := &instance{
   141  		v: ctx.CompileString(``),
   142  	}
   143  	r := require.New(t)
   144  	_, err := ins.Unstructured()
   145  	r.Equal(err.Error(), "Object 'Kind' is missing in '{}'")
   146  	ins = &instance{
   147  		v: ctx.CompileString(`
   148  apiVersion: "apps/v1"
   149  kind:       "Deployment"
   150  metadata: name: parameter.name
   151  `),
   152  	}
   153  	_, err = ins.Unstructured()
   154  	r.Equal(err.Error(), fmt.Sprintf(`failed to have the workload/trait unstructured: metadata.name: reference "%s" not found`, ParameterFieldName))
   155  	ins = &instance{
   156  		v: ctx.CompileString(`
   157  apiVersion: "apps/v1"
   158  kind:       "Deployment"
   159  metadata: name: "abc"
   160  `),
   161  	}
   162  	obj, err := ins.Unstructured()
   163  	r.Equal(err, nil)
   164  	r.Equal(obj, &unstructured.Unstructured{
   165  		Object: map[string]interface{}{
   166  			"apiVersion": "apps/v1",
   167  			"kind":       "Deployment",
   168  			"metadata": map[string]interface{}{
   169  				"name": "abc",
   170  			},
   171  		},
   172  	})
   173  
   174  	ins = &instance{
   175  		v: ctx.CompileString(`
   176  apiVersion: "source.toolkit.fluxcd.io/v1beta1"
   177  metadata: {
   178  	name: "grafana"
   179  }
   180  kind: "HelmRepository"
   181  spec: {
   182  	url:      string
   183  	interval: *"5m" | string
   184  }`),
   185  	}
   186  	o, err := ins.Unstructured()
   187  	r.Nil(o)
   188  	r.NotNil(err)
   189  }