...

Source file src/sigs.k8s.io/kustomize/kyaml/fn/framework/frameworktestutil/frameworktestutil_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/fn/framework/frameworktestutil

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package frameworktestutil
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/spf13/cobra"
    11  	"github.com/stretchr/testify/require"
    12  	"sigs.k8s.io/kustomize/kyaml/fn/framework"
    13  	"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
    14  	"sigs.k8s.io/kustomize/kyaml/kio"
    15  	"sigs.k8s.io/kustomize/kyaml/yaml"
    16  )
    17  
    18  func TestProcessorResultsChecker_UpdateExpectedFromActual(t *testing.T) {
    19  	dir := filepath.FromSlash("testdata/update_expectations/processor")
    20  	checker := ProcessorResultsChecker{
    21  		TestDataDirectory:        dir,
    22  		UpdateExpectedFromActual: true,
    23  		Processor:                testProcessor,
    24  	}
    25  	// This should result in the test being skipped. If no tests are found, it will instead fail.
    26  	checker.Assert(t)
    27  	require.Contains(t, checker.TestCasesRun(), filepath.Join(dir, "important_subdir"))
    28  
    29  	checker.UpdateExpectedFromActual = false
    30  	// This time should inherently pass
    31  	checker.Assert(t)
    32  	require.Contains(t, checker.TestCasesRun(), filepath.Join(dir, "important_subdir"))
    33  }
    34  
    35  func TestCommandResultsChecker_UpdateExpectedFromActual(t *testing.T) {
    36  	dir := filepath.FromSlash("testdata/update_expectations/command")
    37  	checker := CommandResultsChecker{
    38  		TestDataDirectory:        dir,
    39  		UpdateExpectedFromActual: true,
    40  		Command:                  testCommand,
    41  	}
    42  	// This should result in the test being skipped. If no tests are found, it will instead fail.
    43  	checker.Assert(t)
    44  	require.Contains(t, checker.TestCasesRun(), filepath.Join(dir, "important_subdir"))
    45  
    46  	checker.UpdateExpectedFromActual = false
    47  	// This time should inherently pass
    48  	checker.Assert(t)
    49  	require.Contains(t, checker.TestCasesRun(), filepath.Join(dir, "important_subdir"))
    50  }
    51  
    52  func testCommand() *cobra.Command {
    53  	return command.Build(testProcessor(), command.StandaloneEnabled, false)
    54  }
    55  
    56  func testProcessor() framework.ResourceListProcessor {
    57  	return framework.SimpleProcessor{
    58  		Filter: kio.FilterFunc(func(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
    59  			for _, node := range nodes {
    60  				err := node.SetAnnotations(map[string]string{"updated": "true"})
    61  				if err != nil {
    62  					return nil, err
    63  				}
    64  			}
    65  			return nodes, nil
    66  		}),
    67  	}
    68  }
    69  

View as plain text