...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/mocktests/secretmanager_secret_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/mocktests

     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 mocktests
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp"
    26  	"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
    27  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/clientconfig"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    29  	testreconciler "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller/reconciler"
    30  	tfprovider "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/tf/provider"
    31  	transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
    32  	"k8s.io/apimachinery/pkg/types"
    33  	ctrl "sigs.k8s.io/controller-runtime"
    34  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    35  )
    36  
    37  type httpRoundTripperKeyType int
    38  
    39  // httpRoundTripperKey is the key value for http.RoundTripper in a context.Context
    40  var httpRoundTripperKey httpRoundTripperKeyType
    41  
    42  func TestSecretManagerSecretVersion(t *testing.T) {
    43  	h := NewHarness(t)
    44  
    45  	dir := "testdata/secretmanager/secret_and_secretversion"
    46  
    47  	y := h.MustReadFile(filepath.Join(dir, "input.yaml"))
    48  
    49  	t.Logf("parsing objects")
    50  	objects := h.ParseObjects(y)
    51  	h.WithObjects(objects...)
    52  
    53  	t.Logf("creating mock cloud")
    54  
    55  	mockCloud := mockgcp.NewMockRoundTripper(t, h.Client, storage.NewInMemoryStorage())
    56  
    57  	roundTripper := http.RoundTripper(mockCloud)
    58  
    59  	artifacts := os.Getenv("ARTIFACTS")
    60  	if artifacts == "" {
    61  		t.Logf("env var ARTIFACTS is not set; will not record http log")
    62  	} else {
    63  		outputDir := filepath.Join(artifacts, "http-logs")
    64  
    65  		roundTripper = test.NewHTTPRecorder(mockCloud, outputDir)
    66  	}
    67  
    68  	h.Ctx = context.WithValue(h.Ctx, httpRoundTripperKey, roundTripper)
    69  
    70  	transport_tpg.DefaultHTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
    71  		t := ctx.Value(httpRoundTripperKey)
    72  		if t != nil {
    73  			return &http.Client{Transport: t.(http.RoundTripper)}
    74  		}
    75  		return inner
    76  	}
    77  	transport_tpg.OAuth2HTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
    78  		t := ctx.Value(httpRoundTripperKey)
    79  		if t != nil {
    80  			return &http.Client{Transport: t.(http.RoundTripper)}
    81  		}
    82  		return inner
    83  	}
    84  
    85  	t.Logf("creating controller")
    86  	mgr, err := ctrl.NewManager(h.RESTConfig(), ctrl.Options{
    87  		MetricsBindAddress: "0",
    88  		NewClient:          h.NewClient,
    89  	})
    90  	if err != nil {
    91  		t.Fatalf("NewManager failed: %v", err)
    92  	}
    93  
    94  	t.Logf("creating tfprovider config")
    95  	tfConfig := tfprovider.NewConfig()
    96  	tfConfig.AccessToken = "dummytoken"
    97  
    98  	t.Logf("creating tfprovider")
    99  	tfProvider, err := tfprovider.New(h.Ctx, tfConfig)
   100  	if err != nil {
   101  		t.Fatalf("error from tfprovider.New: %v", err)
   102  	}
   103  	t.Logf("creating dclconfig")
   104  	dclConfig := clientconfig.NewForIntegrationTest()
   105  	t.Logf("creating testreconciler")
   106  	testhelper := testreconciler.NewForDCLAndTFTestReconciler(t, mgr, tfProvider, dclConfig)
   107  
   108  	for _, object := range objects {
   109  		gvk := object.GetObjectKind().GroupVersionKind()
   110  		if !strings.Contains(gvk.Group, "cnrm.cloud.google.com") {
   111  			continue
   112  		}
   113  		t.Logf("creating reconciler")
   114  		reconciler := testhelper.NewReconcilerForKind(gvk.Kind)
   115  		t.Logf("reconciler for %v is %T", gvk, reconciler)
   116  
   117  		request := reconcile.Request{
   118  			NamespacedName: types.NamespacedName{
   119  				Namespace: object.GetNamespace(),
   120  				Name:      object.GetName(),
   121  			},
   122  		}
   123  
   124  		result, err := reconciler.Reconcile(h.Ctx, request)
   125  		t.Logf("reconcile result is %#v, %#v", result, err)
   126  		if err != nil {
   127  			t.Errorf("reconcile failed: %v", err)
   128  		}
   129  	}
   130  }
   131  

View as plain text