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

     1  //go:build kind
     2  // +build kind
     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
    19  
    20  import (
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	livetest "github.com/GoogleContainerTools/kpt/pkg/test/live"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestLiveApply(t *testing.T) {
    30  	runTests(t, filepath.Join(".", "testdata", "live-apply"))
    31  }
    32  
    33  func TestLivePlan(t *testing.T) {
    34  	runTests(t, filepath.Join(".", "testdata", "live-plan"))
    35  }
    36  
    37  func runTests(t *testing.T, path string) {
    38  	testCases := scanTestCases(t, path)
    39  
    40  	livetest.RemoveKindCluster(t)
    41  	livetest.CreateKindCluster(t)
    42  
    43  	for p := range testCases {
    44  		p := p
    45  		c := testCases[p]
    46  
    47  		if !c.Parallel {
    48  			continue
    49  		}
    50  
    51  		t.Run(p, func(t *testing.T) {
    52  			if c.Parallel {
    53  				t.Parallel()
    54  			}
    55  
    56  			if c.NoResourceGroup {
    57  				require.False(t, c.Parallel, "Parallel tests can not modify the RG CRD")
    58  				if livetest.CheckIfResourceGroupInstalled(t) {
    59  					livetest.RemoveResourceGroup(t)
    60  				}
    61  			} else {
    62  				livetest.InstallResourceGroup(t)
    63  			}
    64  
    65  			ns := filepath.Base(p)
    66  			livetest.CreateNamespace(t, ns)
    67  			defer livetest.RemoveNamespace(t, ns)
    68  
    69  			(&livetest.Runner{
    70  				Config: c,
    71  				Path:   p,
    72  			}).Run(t)
    73  		})
    74  	}
    75  }
    76  
    77  func scanTestCases(t *testing.T, path string) map[string]livetest.TestCaseConfig {
    78  	testCases := make(map[string]livetest.TestCaseConfig)
    79  	err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		if !info.IsDir() {
    85  			return nil
    86  		}
    87  		if path == p {
    88  			return nil
    89  		}
    90  
    91  		testCases[p] = livetest.ReadTestCaseConfig(t, p)
    92  		return filepath.SkipDir
    93  	})
    94  	if err != nil {
    95  		t.Fatalf("failed to scan for test cases in %s", path)
    96  	}
    97  	return testCases
    98  }