...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/tests/e2e/unified_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/tests/e2e

     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 e2e
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/config/tests/samples/create"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    26  	testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
    27  	testgcp "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/gcp"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture"
    29  	testvariable "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture/variable"
    30  	testyaml "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/yaml"
    31  
    32  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    33  	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
    34  )
    35  
    36  func TestAllInSeries(t *testing.T) {
    37  	if os.Getenv("RUN_E2E") == "" {
    38  		t.Skip("RUN_E2E not set; skipping")
    39  	}
    40  
    41  	var project testgcp.GCPProject
    42  	if os.Getenv("E2E_GCP_TARGET") == "mock" {
    43  		projectNumber := time.Now().Unix()
    44  		project = testgcp.GCPProject{
    45  			ProjectID:     "mock-project-" + strconv.FormatInt(projectNumber, 10),
    46  			ProjectNumber: projectNumber,
    47  		}
    48  	} else {
    49  		project = testgcp.GetDefaultProject(t)
    50  	}
    51  
    52  	ctx := signals.SetupSignalHandler()
    53  	ctx, cancel := context.WithCancel(ctx)
    54  	t.Cleanup(func() {
    55  		cancel()
    56  	})
    57  
    58  	t.Run("samples", func(t *testing.T) {
    59  		samples := create.LoadSamples(t, project)
    60  
    61  		for _, s := range samples {
    62  			s := s
    63  			// TODO(b/259496928): Randomize the resource names for parallel execution when/if needed.
    64  
    65  			t.Run(s.Name, func(t *testing.T) {
    66  				create.MaybeSkip(t, s.Name, s.Resources)
    67  
    68  				h := create.NewHarness(t, ctx)
    69  
    70  				cleanupResources := true
    71  
    72  				create.SetupNamespacesAndApplyDefaults(h, []create.Sample{s}, project)
    73  
    74  				// Hack: set project-id because mockkubeapiserver does not support webhooks
    75  				for _, u := range s.Resources {
    76  					annotations := u.GetAnnotations()
    77  					if annotations == nil {
    78  						annotations = make(map[string]string)
    79  					}
    80  					annotations["cnrm.cloud.google.com/project-id"] = project.ProjectID
    81  					u.SetAnnotations(annotations)
    82  				}
    83  
    84  				create.RunCreateDeleteTest(h, s.Resources, cleanupResources)
    85  			})
    86  		}
    87  	})
    88  
    89  	t.Run("fixtures", func(t *testing.T) {
    90  		fixtures := resourcefixture.Load(t)
    91  		for _, fixture := range fixtures {
    92  			fixture := fixture
    93  			// TODO(b/259496928): Randomize the resource names for parallel execution when/if needed.
    94  
    95  			testID := testvariable.NewUniqueId()
    96  
    97  			s := create.Sample{
    98  				Name: fixture.Name,
    99  			}
   100  
   101  			initialUnstruct := bytesToUnstructured(t, fixture.Create, testID, project)
   102  			s.Resources = append(s.Resources, initialUnstruct)
   103  
   104  			if fixture.Dependencies != nil {
   105  				dependencyYamls := testyaml.SplitYAML(t, fixture.Dependencies)
   106  				for _, dependBytes := range dependencyYamls {
   107  					depUnstruct := bytesToUnstructured(t, dependBytes, testID, project)
   108  					s.Resources = append(s.Resources, depUnstruct)
   109  				}
   110  			}
   111  
   112  			t.Run(s.Name, func(t *testing.T) {
   113  				create.MaybeSkip(t, s.Name, s.Resources)
   114  
   115  				h := create.NewHarness(t, ctx)
   116  
   117  				cleanupResources := true
   118  
   119  				create.SetupNamespacesAndApplyDefaults(h, []create.Sample{s}, project)
   120  				create.RunCreateDeleteTest(h, s.Resources, cleanupResources)
   121  			})
   122  		}
   123  	})
   124  
   125  	// Do a cleanup while we can still handle the error.
   126  	t.Logf("shutting down manager")
   127  	cancel()
   128  }
   129  
   130  func bytesToUnstructured(t *testing.T, bytes []byte, testID string, project testgcp.GCPProject) *unstructured.Unstructured {
   131  	t.Helper()
   132  	updatedBytes := testcontroller.ReplaceTestVars(t, bytes, testID, project)
   133  	return test.ToUnstructWithNamespace(t, updatedBytes, testID)
   134  }
   135  

View as plain text