github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/e2e/fn_test.go (about)

     1  //go:build docker
     2  // +build docker
     3  
     4  // Copyright 2021 Google LLC
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package e2e_test
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/GoogleContainerTools/kpt/internal/fnruntime"
    27  	"github.com/GoogleContainerTools/kpt/pkg/test/runner"
    28  )
    29  
    30  func TestFnRender(t *testing.T) {
    31  	runAllTests(t, filepath.Join(".", "testdata", "fn-render"))
    32  }
    33  
    34  func TestFnEval(t *testing.T) {
    35  	runAllTests(t, filepath.Join(".", "testdata", "fn-eval"))
    36  }
    37  
    38  func TestFnSink(t *testing.T) {
    39  	runAllTests(t, filepath.Join(".", "testdata", "fn-sink"))
    40  }
    41  
    42  // runTests will scan test cases in 'path', run the command
    43  // on all of the packages in path, and test that
    44  // the diff between the results and the original package is as
    45  // expected
    46  func runAllTests(t *testing.T, path string) {
    47  	cases, err := runner.ScanTestCases(path)
    48  	if err != nil {
    49  		t.Fatalf("failed to scan test cases: %s", err)
    50  	}
    51  	// Run all the sequential tests first then run the parallel tests.
    52  	runTests(t, cases, true)
    53  	runTests(t, cases, false)
    54  }
    55  
    56  func runTests(t *testing.T, cases *runner.TestCases, sequential bool) {
    57  	for _, c := range *cases {
    58  		c := c // capture range variable
    59  		if c.Config.Sequential != sequential {
    60  			continue
    61  		}
    62  		// If the current function runtime doesn't match, we skip this test case.
    63  		currRuntime := strings.ToLower(os.Getenv(fnruntime.ContainerRuntimeEnv))
    64  		if len(c.Config.Runtimes) > 0 {
    65  			skip := true
    66  			for _, rt := range c.Config.Runtimes {
    67  				if currRuntime == strings.ToLower(rt) {
    68  					skip = false
    69  					break
    70  				}
    71  			}
    72  			if skip {
    73  				continue
    74  			}
    75  		}
    76  		t.Run(c.Path, func(t *testing.T) {
    77  			if !c.Config.Sequential {
    78  				t.Parallel()
    79  			}
    80  			r, err := runner.NewRunner(t, c, c.Config.TestType)
    81  			if err != nil {
    82  				t.Fatalf("failed to create test runner: %s", err)
    83  			}
    84  			if r.Skip() {
    85  				t.Skip()
    86  			}
    87  			err = r.Run()
    88  			if err != nil {
    89  				t.Fatalf("failed when running test: %s", err)
    90  			}
    91  		})
    92  	}
    93  }