mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-14 21:36:54 +00:00
* test(cli): enhance unit test coverage for theme and color config This commit introduces a comprehensive suite of unit tests for the theme and color configuration functions in `references/cli/top/config`. Key changes include: - Refactored existing tests in `color_test.go` to use table-driven sub-tests for improved clarity and maintainability. - Added new test functions to validate color parsing, hex color detection, and default theme creation. - Implemented tests for theme file lifecycle management, including creation and loading logic. These additions significantly increase the test coverage and ensure the robustness and correctness of the CLI's theme and color functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(cli): refactor and enhance tests for top view models and utils This commit improves the unit test suite for the CLI's top view functionality by refactoring existing tests and adding new ones to increase coverage. Key changes include: - In `application_test.go`, `TestApplicationList_ToTableBody` is refactored to be a table-driven test, and new tests are added for `serviceNum`, `workflowMode`, and `workflowStepNum` helpers. - In `time_test.go`, `TestTimeFormat` is refactored into a table-driven test for better structure and readability. These changes align the tests with best practices and improve the overall robustness of the CLI top view's data presentation logic. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(cuegen): enhance unit test coverage for CUE generation packages This commit introduces a comprehensive suite of unit tests and refactors existing tests for the CUE generation packages located in `references/cuegen`. Key changes include: - Refactored existing tests in `generator_test.go` and `provider_test.go` to use table-driven sub-tests, improving clarity, maintainability, and coverage of error conditions. - Added new test functions to `convert_test.go` to validate helper functions for comment generation, type support, and enum field handling. - Added new tests in `provider_test.go` to cover provider extraction, declaration modification, and panic recovery logic. These changes significantly increase the test coverage for the `cuegen` libraries, ensuring the correctness and robustness of the CUE code generation functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(docgen): add comprehensive unit tests for doc generation This commit introduces a comprehensive suite of unit tests for the documentation generation package located in `references/docgen`. Key changes include: - Added new test files (`console_test.go`, `convert_test.go`, `openapi_test.go`) to cover the core functions for parsing and generating documentation for CUE, Terraform, and OpenAPI schemas. - Refactored and enhanced `i18n_test.go` to use sub-tests, resolve race conditions, and improve coverage for fallback logic and error handling. - Ensured all new and existing tests follow best practices, using table-driven tests for clarity and maintainability. This effort significantly increases the test coverage for the `docgen` package, improving the reliability and robustness of the documentation generation features. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test: improve test reliability and conventions This commit introduces several improvements to the test suite to enhance reliability and adhere to best practices. - **Fix flaky test in `docgen/openapi_test.go`**: The test for `GenerateConsoleDocument` was flaky because it performed an exact string match on table output generated from a map. Since map iteration order is not guaranteed, this could cause spurious failures. The test is now order-insensitive, comparing sorted sets of lines instead. - **Improve assertions in `docgen/console_test.go`**: - Removes an unnecessary `test.EquateErrors()` option, which is not needed for simple string comparisons. - Corrects the `cmp.Diff` argument order to the standard `(want, got)` convention for clearer failure messages. - Fixes a typo in an error message. - **Standardize assertions in `cli/top/config/color_test.go`**: Swaps `assert.Equal` arguments to the standard `(expected, actual)` convention. - **Clean up `cuegen/generators/provider/provider_test.go`**: Removes a redundant error check. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> --------- Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
737 lines
20 KiB
Go
737 lines
20 KiB
Go
/*
|
|
Copyright 2021 The KubeVela Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package docgen
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/oam-dev/kubevela/apis/types"
|
|
"github.com/oam-dev/kubevela/pkg/utils/common"
|
|
)
|
|
|
|
var RefTestDir = filepath.Join(TestDir, "ref")
|
|
|
|
func TestCreateRefTestDir(t *testing.T) {
|
|
if _, err := os.Stat(RefTestDir); err != nil && os.IsNotExist(err) {
|
|
err := os.MkdirAll(RefTestDir, 0750)
|
|
assert.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareParameterTable(t *testing.T) {
|
|
ref := MarkdownReference{}
|
|
ref.I18N = &En
|
|
tableName := "hello"
|
|
parameterList := []ReferenceParameter{
|
|
{
|
|
PrintableType: "string",
|
|
},
|
|
}
|
|
parameterName := "cpu"
|
|
parameterList[0].Name = parameterName
|
|
parameterList[0].Required = true
|
|
refContent := ref.getParameterString(tableName, parameterList, types.CUECategory)
|
|
assert.Contains(t, refContent, parameterName)
|
|
assert.Contains(t, refContent, "cpu")
|
|
}
|
|
|
|
func TestDeleteRefTestDir(t *testing.T) {
|
|
if _, err := os.Stat(RefTestDir); err == nil {
|
|
err := os.RemoveAll(RefTestDir)
|
|
assert.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
func TestWalkParameterSchema(t *testing.T) {
|
|
testcases := []struct {
|
|
data string
|
|
ExpectRefs map[string]map[string]ReferenceParameter
|
|
}{
|
|
{
|
|
data: `{
|
|
"properties": {
|
|
"cmd": {
|
|
"description": "Commands to run in the container",
|
|
"items": {
|
|
"type": "string"
|
|
},
|
|
"title": "cmd",
|
|
"type": "array"
|
|
},
|
|
"image": {
|
|
"description": "Which image would you like to use for your service",
|
|
"title": "image",
|
|
"type": "string"
|
|
}
|
|
},
|
|
"required": [
|
|
"image"
|
|
],
|
|
"type": "object"
|
|
}`,
|
|
ExpectRefs: map[string]map[string]ReferenceParameter{
|
|
"# Specification": {
|
|
"cmd": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "cmd",
|
|
Usage: "Commands to run in the container",
|
|
JSONType: "array",
|
|
},
|
|
PrintableType: "array",
|
|
},
|
|
"image": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "image",
|
|
Required: true,
|
|
Usage: "Which image would you like to use for your service",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
data: `{
|
|
"properties": {
|
|
"obj": {
|
|
"properties": {
|
|
"f0": {
|
|
"default": "v0",
|
|
"type": "string"
|
|
},
|
|
"f1": {
|
|
"default": "v1",
|
|
"type": "string"
|
|
},
|
|
"f2": {
|
|
"default": "v2",
|
|
"type": "string"
|
|
}
|
|
},
|
|
"type": "object"
|
|
},
|
|
},
|
|
"type": "object"
|
|
}`,
|
|
ExpectRefs: map[string]map[string]ReferenceParameter{
|
|
"# Specification": {
|
|
"obj": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "obj",
|
|
JSONType: "object",
|
|
},
|
|
PrintableType: "[obj](#obj)",
|
|
},
|
|
},
|
|
"## obj": {
|
|
"f0": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "f0",
|
|
Default: "v0",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
"f1": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "f1",
|
|
Default: "v1",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
"f2": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "f2",
|
|
Default: "v2",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
data: `{
|
|
"properties": {
|
|
"obj": {
|
|
"properties": {
|
|
"f0": {
|
|
"default": "v0",
|
|
"type": "string"
|
|
},
|
|
"f1": {
|
|
"default": "v1",
|
|
"type": "object",
|
|
"properties": {
|
|
"g0": {
|
|
"default": "v2",
|
|
"type": "string"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"type": "object"
|
|
}
|
|
},
|
|
"type": "object"
|
|
}`,
|
|
ExpectRefs: map[string]map[string]ReferenceParameter{
|
|
"# Specification": {
|
|
"obj": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "obj",
|
|
JSONType: "object",
|
|
},
|
|
PrintableType: "[obj](#obj)",
|
|
},
|
|
},
|
|
"## obj": {
|
|
"f0": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "f0",
|
|
Default: "v0",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
"f1": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "f1",
|
|
Default: "v1",
|
|
JSONType: "object",
|
|
},
|
|
PrintableType: "[f1](#f1)",
|
|
},
|
|
},
|
|
"### f1": {
|
|
"g0": ReferenceParameter{
|
|
Parameter: types.Parameter{
|
|
Name: "g0",
|
|
Default: "v2",
|
|
JSONType: "string",
|
|
},
|
|
PrintableType: "string",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, cases := range testcases {
|
|
commonRefs = make([]CommonReference, 0)
|
|
parameterJSON := fmt.Sprintf(BaseOpenAPIV3Template, cases.data)
|
|
swagger, err := openapi3.NewLoader().LoadFromData(json.RawMessage(parameterJSON))
|
|
assert.Equal(t, nil, err)
|
|
parameters := swagger.Components.Schemas["parameter"].Value
|
|
err = WalkParameterSchema(parameters, Specification, 0)
|
|
assert.Nil(t, err)
|
|
|
|
refs := make(map[string]map[string]ReferenceParameter)
|
|
for _, items := range commonRefs {
|
|
refs[items.Name] = make(map[string]ReferenceParameter)
|
|
for _, item := range items.Parameters {
|
|
refs[items.Name][item.Name] = item
|
|
}
|
|
}
|
|
assert.Equal(t, true, reflect.DeepEqual(cases.ExpectRefs, refs))
|
|
}
|
|
}
|
|
|
|
func TestPrepareTerraformOutputs(t *testing.T) {
|
|
type args struct {
|
|
tableName string
|
|
parameterList []ReferenceParameter
|
|
}
|
|
|
|
param := ReferenceParameter{}
|
|
param.Name = "ID"
|
|
param.Usage = "Identity of the cloud resource"
|
|
|
|
testcases := []struct {
|
|
args args
|
|
expect string
|
|
}{
|
|
{
|
|
args: args{
|
|
tableName: "",
|
|
parameterList: nil,
|
|
},
|
|
expect: "",
|
|
},
|
|
{
|
|
args: args{
|
|
tableName: "abc",
|
|
parameterList: []ReferenceParameter{param},
|
|
},
|
|
expect: "\n\nabc\n\n Name | Description \n ------------ | ------------- \n ID | Identity of the cloud resource\n",
|
|
},
|
|
}
|
|
ref := &MarkdownReference{}
|
|
for _, tc := range testcases {
|
|
t.Run("", func(t *testing.T) {
|
|
content := ref.prepareTerraformOutputs(tc.args.tableName, tc.args.parameterList)
|
|
if content != tc.expect {
|
|
t.Errorf("prepareTerraformOutputs(...): -want, +got:\n%s\n", cmp.Diff(tc.expect, content))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMakeReadableTitle(t *testing.T) {
|
|
type args struct {
|
|
ref *MarkdownReference
|
|
title string
|
|
}
|
|
|
|
ref := &MarkdownReference{}
|
|
|
|
refZh := &MarkdownReference{}
|
|
refZh.I18N = &Zh
|
|
|
|
testcases := []struct {
|
|
args args
|
|
want string
|
|
}{
|
|
{
|
|
args: args{
|
|
title: "abc",
|
|
ref: ref,
|
|
},
|
|
want: "Abc",
|
|
},
|
|
{
|
|
args: args{
|
|
title: "abc-def",
|
|
ref: ref,
|
|
},
|
|
want: "Abc-Def",
|
|
},
|
|
{
|
|
args: args{
|
|
title: "alibaba-def-ghi",
|
|
ref: ref,
|
|
},
|
|
want: "Alibaba Cloud DEF-GHI",
|
|
},
|
|
{
|
|
args: args{
|
|
title: "alibaba-def-ghi",
|
|
ref: refZh,
|
|
},
|
|
want: "阿里云 DEF-GHI",
|
|
},
|
|
{
|
|
args: args{
|
|
title: "aws-jk",
|
|
ref: refZh,
|
|
},
|
|
want: "AWS JK",
|
|
},
|
|
{
|
|
args: args{
|
|
title: "azure-jk",
|
|
ref: refZh,
|
|
},
|
|
want: "Azure JK",
|
|
},
|
|
}
|
|
for _, tc := range testcases {
|
|
t.Run("", func(t *testing.T) {
|
|
title := tc.args.ref.makeReadableTitle(tc.args.title)
|
|
if title != tc.want {
|
|
t.Errorf("makeReadableTitle(...): -want, +got:\n%s\n", cmp.Diff(tc.want, title))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseLocalFile(t *testing.T) {
|
|
testcases := []struct {
|
|
localFilePath string
|
|
want types.Capability
|
|
}{
|
|
{
|
|
localFilePath: "testdata/terraform-alibaba-ask.yaml",
|
|
want: types.Capability{
|
|
Name: "alibaba-ask",
|
|
Description: "Terraform configuration for Alibaba Cloud Serverless Kubernetes (ASK)",
|
|
TerraformConfiguration: "https://github.com/kubevela-contrib/terraform-modules.git",
|
|
ConfigurationType: "remote",
|
|
Path: "alibaba/cs/serverless-kubernetes",
|
|
},
|
|
},
|
|
{
|
|
localFilePath: "testdata/terraform-aws-elb.yaml",
|
|
want: types.Capability{
|
|
Name: "aws-elb",
|
|
Description: "Terraform module which creates ELB resources on AWS",
|
|
TerraformConfiguration: "https://github.com/terraform-aws-modules/terraform-aws-elb.git",
|
|
ConfigurationType: "remote",
|
|
Path: "",
|
|
},
|
|
},
|
|
{
|
|
localFilePath: "testdata/terraform-azure-database-mariadb.yaml",
|
|
want: types.Capability{
|
|
Name: "azure-database-mariadb",
|
|
Description: "Terraform configuration for Azure Database Mariadb",
|
|
TerraformConfiguration: "",
|
|
ConfigurationType: "",
|
|
Path: "",
|
|
},
|
|
},
|
|
{
|
|
localFilePath: "testdata/terraform-baidu-vpc.yaml",
|
|
want: types.Capability{
|
|
Name: "baidu-vpc",
|
|
Description: "Baidu Cloud VPC",
|
|
TerraformConfiguration: "",
|
|
ConfigurationType: "",
|
|
Path: "",
|
|
},
|
|
},
|
|
{
|
|
localFilePath: "testdata/terraform-gcp-gcs.yaml",
|
|
want: types.Capability{
|
|
Name: "gcp-gcs",
|
|
Description: "GCP Gcs",
|
|
TerraformConfiguration: "https://github.com/woernfl/terraform-gcp-gcs.git",
|
|
ConfigurationType: "remote",
|
|
Path: "",
|
|
},
|
|
},
|
|
{
|
|
localFilePath: "testdata/terraform-tencent-subnet.yaml",
|
|
want: types.Capability{
|
|
Name: "tencent-subnet",
|
|
Description: "Tencent Cloud Subnet",
|
|
TerraformConfiguration: "",
|
|
ConfigurationType: "",
|
|
Path: "",
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range testcases {
|
|
t.Run("", func(t *testing.T) {
|
|
lc, err := ParseLocalFile(tc.localFilePath, common.Args{})
|
|
if err != nil {
|
|
t.Errorf("ParseLocalFile(...): -want: %v, got error: %s\n", tc.want, err)
|
|
}
|
|
if !reflect.DeepEqual(*lc, tc.want) {
|
|
if !reflect.DeepEqual(lc.Name, tc.want.Name) {
|
|
t.Errorf("Name not equal, - got: %v - want: %v", lc.Name, tc.want.Name)
|
|
}
|
|
if !reflect.DeepEqual(lc.Description, tc.want.Description) {
|
|
t.Errorf("Description not equal, - got: %v - want: %v", lc.Description, tc.want.Description)
|
|
}
|
|
if !reflect.DeepEqual(lc.TerraformConfiguration, tc.want.TerraformConfiguration) {
|
|
if len(lc.TerraformConfiguration) == 0 {
|
|
t.Errorf("Parse TerraformConfiguration failed")
|
|
}
|
|
}
|
|
if !reflect.DeepEqual(lc.ConfigurationType, tc.want.ConfigurationType) {
|
|
t.Errorf("ConfigurationType not equal, - got: %v - want: %v", lc.ConfigurationType, tc.want.ConfigurationType)
|
|
}
|
|
if !reflect.DeepEqual(lc.Path, tc.want.Path) {
|
|
t.Errorf("Path not equal, - got: %v - want: %v", lc.Path, tc.want.Path)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractParameter(t *testing.T) {
|
|
testcases := map[string]struct {
|
|
cueTemplate string
|
|
contains string
|
|
}{
|
|
"normal-case": {
|
|
cueTemplate: `parameter: {
|
|
str: string
|
|
itr: int
|
|
btr: bool
|
|
ct: cts: string
|
|
}`,
|
|
contains: `### normal-case
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
str | | string | true |
|
|
itr | | int | true |
|
|
btr | | bool | true |
|
|
ct | | [ct](#ct) | true |
|
|
|
|
|
|
#### ct
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
cts | | string | true |`,
|
|
},
|
|
"normal-map-string-string": {
|
|
cueTemplate: `parameter: {
|
|
envMappings: [string]: string
|
|
}`,
|
|
contains: `### normal-map-string-string
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
envMappings | | map[string]string | true | `,
|
|
},
|
|
"normal-map-case": {
|
|
cueTemplate: `parameter: {
|
|
// +usage=The mapping of environment variables to secret
|
|
envMappings: [string]: #KeySecret
|
|
}
|
|
#KeySecret: {
|
|
key?: string
|
|
secret: string
|
|
}`,
|
|
contains: `### normal-map-case
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
envMappings | The mapping of environment variables to secret. | map[string]KeySecret(#keysecret) | true |
|
|
|
|
|
|
#### KeySecret
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
key | | string | false |
|
|
secret | | string | true |`,
|
|
},
|
|
"or-case-with-type": {
|
|
cueTemplate: ` parameter: {
|
|
orValue: #KeyConfig | #KeySecret
|
|
}
|
|
|
|
#KeySecret: {
|
|
key: "abc"
|
|
secret: string
|
|
}
|
|
|
|
#KeyConfig: {
|
|
key: "abc"
|
|
config: string
|
|
}`,
|
|
contains: `### or-case-with-type
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
orValue | | [KeyConfig](#keyconfig) or [KeySecret](#keysecret) | true |
|
|
|
|
|
|
#### KeyConfig
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
key | | string | true |
|
|
config | | string | true |
|
|
|
|
|
|
#### KeySecret
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
key | | string | true |
|
|
secret | | string | true | `,
|
|
},
|
|
"or-type-with-const-str": {
|
|
cueTemplate: `parameter: {
|
|
type: *"configMap" | "secret" | "emptyDir" | "ephemeral"
|
|
}`,
|
|
contains: `### or-type-with-const-str
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
type | | "configMap" or "secret" or "emptyDir" or "ephemeral" | false | configMap`,
|
|
},
|
|
"or-type-with-const-and-string": {
|
|
cueTemplate: `parameter: {
|
|
type: *"configMap" | "secret" | "emptyDir" | string
|
|
}`,
|
|
contains: `### or-type-with-const-and-string
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
type | | string | false | configMap`,
|
|
},
|
|
"var-or-with-struct-var": {
|
|
cueTemplate: `
|
|
parameter: {
|
|
orValue: KeyConfig | KeySecret
|
|
}
|
|
|
|
KeySecret: {
|
|
key: "abc"
|
|
secret: string
|
|
}
|
|
|
|
KeyConfig: {
|
|
key: "abc"
|
|
config: string
|
|
}`,
|
|
contains: `### var-or-with-struct-var
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
orValue | | [KeyConfig](#keyconfig) or [KeySecret](#keysecret) | true |
|
|
|
|
|
|
#### KeyConfig
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
key | | string | true |
|
|
config | | string | true |
|
|
|
|
|
|
#### KeySecret
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
key | | string | true |
|
|
secret | | string | true | `,
|
|
},
|
|
}
|
|
|
|
ref := &MarkdownReference{}
|
|
for key, ca := range testcases {
|
|
cueValue, _ := common.GetCUEParameterValue(ca.cueTemplate)
|
|
out, _, err := ref.parseParameters("", cueValue, key, 0, false)
|
|
assert.NoError(t, err, key)
|
|
assert.Contains(t, out, ca.contains, key)
|
|
}
|
|
}
|
|
|
|
func TestExtractParameterFromFiles(t *testing.T) {
|
|
ref := &MarkdownReference{}
|
|
testcases := map[string]struct {
|
|
path string
|
|
contains string
|
|
}{
|
|
"env": {
|
|
path: "testdata/parameter/env.cue",
|
|
contains: `### env
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
| | [PatchParams](#patchparams) or [type-option-2](#type-option-2) | false |
|
|
|
|
|
|
#### PatchParams
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containerName | Specify the name of the target container, if not set, use the component name. | string | false | empty
|
|
replace | Specify if replacing the whole environment settings for the container. | bool | false | false
|
|
env | Specify the environment variables to merge, if key already existing, override its value. | map[string]string | true |
|
|
unset | Specify which existing environment variables to unset. | list | true |
|
|
|
|
|
|
#### type-option-2
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containers | Specify the environment variables for multiple containers. | [[]containers](#containers) | true |
|
|
|
|
|
|
##### containers
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containerName | Specify the name of the target container, if not set, use the component name. | string | false | empty
|
|
replace | Specify if replacing the whole environment settings for the container. | bool | false | false
|
|
env | Specify the environment variables to merge, if key already existing, override its value. | map[string]string | true |
|
|
unset | Specify which existing environment variables to unset. | list | true |`,
|
|
},
|
|
"command": {
|
|
path: "testdata/parameter/command.cue",
|
|
contains: `### command
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
| | [PatchParams](#patchparams) or [type-option-2](#type-option-2) | false |
|
|
|
|
|
|
#### PatchParams
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containerName | Specify the name of the target container, if not set, use the component name. | string | false | empty
|
|
command | Specify the command to use in the target container, if not set, it will not be changed. | null | true |
|
|
args | Specify the args to use in the target container, if set, it will override existing args. | null | true |
|
|
addArgs | Specify the args to add in the target container, existing args will be kept, cannot be used with args. | null | true |
|
|
delArgs | Specify the existing args to delete in the target container, cannot be used with args. | null | true |
|
|
|
|
|
|
#### type-option-2
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containers | Specify the commands for multiple containers. | [[]containers](#containers) | true |
|
|
|
|
|
|
##### containers
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
containerName | Specify the name of the target container, if not set, use the component name. | string | false | empty
|
|
command | Specify the command to use in the target container, if not set, it will not be changed. | null | true |
|
|
args | Specify the args to use in the target container, if set, it will override existing args. | null | true |
|
|
addArgs | Specify the args to add in the target container, existing args will be kept, cannot be used with args. | null | true |
|
|
delArgs | Specify the existing args to delete in the target container, cannot be used with args. | null | true |`,
|
|
},
|
|
"condition": {
|
|
path: "testdata/parameter/condition.cue",
|
|
contains: `### condition
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
volumes | | [[]volumes](#volumes) | true |
|
|
|
|
|
|
#### volumes
|
|
|
|
Name | Description | Type | Required | Default
|
|
---- | ----------- | ---- | -------- | -------
|
|
name | | string | true |
|
|
type | | "configMap" or "secret" or "emptyDir" or "ephemeral" | false | configMap
|
|
defaultMode | only works when type equals configmap. | int | false | 420 `,
|
|
},
|
|
}
|
|
for key, ca := range testcases {
|
|
content, err := os.ReadFile(ca.path)
|
|
assert.NoError(t, err, ca.path)
|
|
cueValue, _ := common.GetCUEParameterValue(string(content))
|
|
out, _, err := ref.parseParameters("", cueValue, key, 0, false)
|
|
assert.NoError(t, err, key)
|
|
assert.Contains(t, out, ca.contains, key)
|
|
}
|
|
}
|