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

     1  // Copyright 2022 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 porch
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"gopkg.in/yaml.v3"
    24  )
    25  
    26  type Command struct {
    27  	// Args is a list of args for the kpt CLI.
    28  	Args []string `yaml:"args,omitempty"`
    29  	// StdIn contents will be passed as the command's standard input, if not empty.
    30  	Stdin string `yaml:"stdin,omitempty"`
    31  	// StdOut is the standard output expected from the command.
    32  	Stdout string `yaml:"stdout,omitempty"`
    33  	// StdErr is the standard error output expected from the command.
    34  	Stderr string `yaml:"stderr,omitempty"`
    35  	// ExitCode is the expected exit code frm the command.
    36  	ExitCode int `yaml:"exitCode,omitempty"`
    37  	// Yaml indicates that stdout is yaml and the test will reformat it for stable ordering
    38  	Yaml bool `yaml:"yaml,omitempty"`
    39  }
    40  
    41  type TestCaseConfig struct {
    42  	// TestCase is the name of the test case.
    43  	TestCase string `yaml:"-"`
    44  	// ConfigFile stores the name of the config file from which the config was loaded.
    45  	// Used when generating or updating golden files.
    46  	ConfigFile string `yaml:"-"`
    47  	// Repository is the name of the k8s Repository resource to register the default Git repo.
    48  	Repository string `yaml:"repository,omitempty"`
    49  	// Commands is a list of kpt commands to be executed by the test.
    50  	Commands []Command `yaml:"commands,omitempty"`
    51  	// Skip the test? If the value is not empty, it will be used as a message with which to skip the test.
    52  	Skip string `yaml:"skip,omitempty"`
    53  }
    54  
    55  func ReadTestCaseConfig(t *testing.T, name, path string) TestCaseConfig {
    56  	configPath := filepath.Join(path, "config.yaml")
    57  	b, err := os.ReadFile(configPath)
    58  	if err != nil {
    59  		t.Fatalf("Failed to read test config file %q: %v", configPath, err)
    60  	}
    61  
    62  	var tc TestCaseConfig
    63  	if err := yaml.Unmarshal(b, &tc); err != nil {
    64  		t.Fatalf("Failed to unmarshal test config %q: %v", configPath, err)
    65  	}
    66  
    67  	tc.TestCase = name
    68  	tc.ConfigFile = configPath
    69  	return tc
    70  }
    71  
    72  func WriteTestCaseConfig(t *testing.T, tc *TestCaseConfig) {
    73  	var out bytes.Buffer
    74  	e := yaml.NewEncoder(&out)
    75  	e.SetIndent(2)
    76  	if err := e.Encode(tc); err != nil {
    77  		t.Fatalf("Failed to marshal test case config for %s: %v", tc.TestCase, err)
    78  	}
    79  	if err := os.WriteFile(tc.ConfigFile, out.Bytes(), 0644); err != nil {
    80  		t.Errorf("Failed to save test case config for %s into %q: %v", tc.TestCase, tc.ConfigFile, err)
    81  	}
    82  }