github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/pkg/test/live/config.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package live
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"sigs.k8s.io/kustomize/kyaml/yaml"
    23  )
    24  
    25  type TestCaseConfig struct {
    26  	// ExitCode is the expected exit code from the kpt commands. Default: 0
    27  	ExitCode int `yaml:"exitCode,omitempty"`
    28  
    29  	// StdErr is the expected standard error output. Default: ""
    30  	StdErr string `yaml:"stdErr,omitempty"`
    31  
    32  	// StdOut is the expected standard output from running the command.
    33  	// Default: ""
    34  	StdOut string `yaml:"stdOut,omitempty"`
    35  
    36  	// OptionalStdOut is a list of lines that are optional in the standard
    37  	// output from running the command.
    38  	// Default: nil
    39  	OptionalStdOut []string `yaml:"optionalStdOut,omitempty"`
    40  
    41  	// Inventory is the expected list of resource present in the inventory.
    42  	Inventory []InventoryEntry `yaml:"inventory,omitempty"`
    43  
    44  	// NoResourceGroup defines whether the RG CRD should be present in the cluster
    45  	// when the test starts.
    46  	NoResourceGroup bool `yaml:"noResourceGroup,omitempty"`
    47  
    48  	// Parallel defines whether the test can be run in parallel with other
    49  	// tests. The primary requirement here is that the test doesn't create,
    50  	// update, or delete any cluster-scoped resources.
    51  	Parallel bool `yaml:"parallel,omitempty"`
    52  
    53  	// KptArgs is a list of args that will be provided to the kpt command
    54  	// when running the test.
    55  	KptArgs []string `yaml:"kptArgs,omitempty"`
    56  }
    57  
    58  // InventoryEntry defines an entry in an inventory list.
    59  type InventoryEntry struct {
    60  	Group     string `yaml:"group,omitempty"`
    61  	Kind      string `yaml:"kind,omitempty"`
    62  	Name      string `yaml:"name,omitempty"`
    63  	Namespace string `yaml:"namespace,omitempty"`
    64  }
    65  
    66  func ReadTestCaseConfig(t *testing.T, path string) TestCaseConfig {
    67  	configPath := filepath.Join(path, "config.yaml")
    68  	b, err := os.ReadFile(configPath)
    69  	if err != nil {
    70  		t.Fatalf("unable to read test config at %s", configPath)
    71  	}
    72  
    73  	var config TestCaseConfig
    74  	err = yaml.Unmarshal(b, &config)
    75  	if err != nil {
    76  		t.Fatalf("unable to unmarshal test config file %s: %v", configPath, err)
    77  	}
    78  	return config
    79  }