github.com/oam-dev/kubevela@v1.9.11/references/docgen/markdown_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 docgen
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/crossplane/crossplane-runtime/pkg/test"
    27  	"github.com/google/go-cmp/cmp"
    28  	"github.com/stretchr/testify/assert"
    29  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    30  
    31  	"github.com/oam-dev/kubevela/apis/types"
    32  	"github.com/oam-dev/kubevela/pkg/utils/common"
    33  )
    34  
    35  func TestCreateMarkdownForCUE(t *testing.T) {
    36  	go func() {
    37  		svr := http.NewServeMux()
    38  		svr.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    39  			fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
    40  		})
    41  		http.ListenAndServe(":65501", svr)
    42  	}()
    43  
    44  	time.Sleep(time.Millisecond)
    45  
    46  	mr := MarkdownReference{ParseReference: ParseReference{Client: fake.NewClientBuilder().Build()}}
    47  	mr.Local = &FromLocal{Paths: []string{"./testdata/testdef.cue"}}
    48  	capp, err := ParseLocalFile(mr.Local.Paths[0], common.Args{})
    49  	assert.NoError(t, err)
    50  	got, err := mr.GenerateMarkdownForCap(context.Background(), *capp, nil, false)
    51  	assert.NoError(t, err)
    52  	fmt.Println(got)
    53  	assert.Contains(t, got, "A test key")
    54  	assert.Contains(t, got, "title:  Testdef")
    55  	assert.Contains(t, got, "K8s-objects allow users to specify raw K8s objects in properties")
    56  	assert.Contains(t, got, "Examples")
    57  	assert.Contains(t, got, "Hello, examples/applications/create-namespace.yaml!")
    58  
    59  	mr.Local = &FromLocal{Paths: []string{"./testdata/testdeftrait.cue"}}
    60  	capp, err = ParseLocalFile(mr.Local.Paths[0], common.Args{})
    61  	assert.NoError(t, err)
    62  	got, err = mr.GenerateMarkdownForCap(context.Background(), *capp, nil, false)
    63  	assert.NoError(t, err)
    64  	assert.Contains(t, got, "Specify the hostAliases to add")
    65  	assert.Contains(t, got, "title:  Testdeftrait")
    66  	assert.Contains(t, got, "Add host aliases on K8s pod for your workload which follows the pod spec in path 'spec.template'.")
    67  }
    68  
    69  func TestCreateMarkdown(t *testing.T) {
    70  	ctx := context.Background()
    71  	ref := &MarkdownReference{}
    72  	ref.I18N = &En
    73  
    74  	refZh := &MarkdownReference{}
    75  	refZh.I18N = &Zh
    76  
    77  	workloadName := "workload1"
    78  	traitName := "trait1"
    79  	workloadName2 := "workload2"
    80  
    81  	workloadCueTemplate := `
    82  parameter: {
    83  	// +usage=Which image would you like to use for your service
    84  	// +short=i
    85  	image: string
    86  }
    87  `
    88  	traitCueTemplate := `
    89  parameter: {
    90  	replicas: int
    91  }
    92  `
    93  
    94  	configuration := `
    95  resource "alicloud_oss_bucket" "bucket-acl" {
    96    bucket = var.bucket
    97    acl = var.acl
    98  }
    99  
   100  output "BUCKET_NAME" {
   101    value = "${alicloud_oss_bucket.bucket-acl.bucket}.${alicloud_oss_bucket.bucket-acl.extranet_endpoint}"
   102  }
   103  
   104  variable "bucket" {
   105    description = "OSS bucket name"
   106    default = "vela-website"
   107    type = string
   108  }
   109  
   110  variable "acl" {
   111    description = "OSS bucket ACL, supported 'private', 'public-read', 'public-read-write'"
   112    default = "private"
   113    type = string
   114  }
   115  `
   116  
   117  	cases := map[string]struct {
   118  		reason       string
   119  		ref          *MarkdownReference
   120  		capabilities []types.Capability
   121  		want         error
   122  	}{
   123  		"WorkloadTypeAndTraitCapability": {
   124  			reason: "valid capabilities",
   125  			ref:    ref,
   126  			capabilities: []types.Capability{
   127  				{
   128  					Name:        workloadName,
   129  					Type:        types.TypeWorkload,
   130  					CueTemplate: workloadCueTemplate,
   131  					Category:    types.CUECategory,
   132  				},
   133  				{
   134  					Name:        traitName,
   135  					Type:        types.TypeTrait,
   136  					CueTemplate: traitCueTemplate,
   137  					Category:    types.CUECategory,
   138  				},
   139  				{
   140  					Name:                   workloadName2,
   141  					TerraformConfiguration: configuration,
   142  					Type:                   types.TypeWorkload,
   143  					Category:               types.TerraformCategory,
   144  				},
   145  			},
   146  			want: nil,
   147  		},
   148  		"TerraformCapabilityInChinese": {
   149  			reason: "terraform capability",
   150  			ref:    refZh,
   151  			capabilities: []types.Capability{
   152  				{
   153  					Name:                   workloadName2,
   154  					TerraformConfiguration: configuration,
   155  					Type:                   types.TypeWorkload,
   156  					Category:               types.TerraformCategory,
   157  				},
   158  			},
   159  			want: nil,
   160  		},
   161  	}
   162  
   163  	for name, tc := range cases {
   164  		t.Run(name, func(t *testing.T) {
   165  			got := tc.ref.CreateMarkdown(ctx, tc.capabilities, RefTestDir, false, nil)
   166  			if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" {
   167  				t.Errorf("\n%s\nCreateMakrdown(...): -want error, +got error:\n%s", tc.reason, diff)
   168  			}
   169  		})
   170  	}
   171  
   172  }