github.com/oam-dev/kubevela@v1.9.11/references/cli/portforward_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  package cli
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"os"
    22  	"strings"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  
    27  	"sigs.k8s.io/yaml"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    30  	util2 "github.com/oam-dev/kubevela/pkg/oam/util"
    31  	common2 "github.com/oam-dev/kubevela/pkg/utils/common"
    32  	"github.com/oam-dev/kubevela/pkg/utils/util"
    33  )
    34  
    35  var _ = Describe("Test port-forward cli", func() {
    36  
    37  	When("test port-forward cli", func() {
    38  
    39  		It("should not have err", func() {
    40  			app := v1beta1.Application{}
    41  			Expect(yaml.Unmarshal([]byte(appWithNginx), &app)).Should(BeNil())
    42  			Expect(k8sClient.Create(context.Background(), &app)).Should(SatisfyAny(BeNil(), util2.AlreadyExistMatcher{}))
    43  			arg := common2.Args{}
    44  			arg.SetClient(k8sClient)
    45  			buffer := bytes.NewBuffer(nil)
    46  			ioStreams := util.IOStreams{In: os.Stdin, Out: buffer, ErrOut: buffer}
    47  			cmd := NewPortForwardCommand(arg, "nginx", ioStreams)
    48  			Expect(cmd.Execute()).Should(BeNil())
    49  			buf, ok := ioStreams.Out.(*bytes.Buffer)
    50  			Expect(ok).Should(BeTrue())
    51  			Expect(strings.Contains(buf.String(), "error")).Should(BeFalse())
    52  		})
    53  	})
    54  })
    55  
    56  const (
    57  	appWithNginx = `
    58  apiVersion: core.oam.dev/v1beta1
    59  kind: Application
    60  metadata:
    61    name: nginx
    62    namespace: default
    63  spec:
    64    components:
    65    - name: nginx
    66      type: webservice
    67      properties:
    68        image: nginx
    69        ports:
    70        - expose: true
    71          port: 80
    72          protocol: TCP
    73      traits:
    74      - properties:
    75          replicas: 1
    76        type: scaler
    77  `
    78  )