...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/manifest/manifest_loader_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/manifest

     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 manifest_test
    16  
    17  import (
    18  	"context"
    19  	"reflect"
    20  	"strings"
    21  	"testing"
    22  
    23  	corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/manifest"
    26  
    27  	"github.com/google/go-cmp/cmp"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  )
    30  
    31  func TestManifestLoader_ResolveManifest(t *testing.T) {
    32  	t.Parallel()
    33  	basedir, repo := newTestNewLocalRepository(t)
    34  	ml := manifest.NewManifestLoader(repo)
    35  	tests := []struct {
    36  		name   string
    37  		cc     *corev1beta1.ConfigConnector
    38  		result map[string]string
    39  	}{
    40  		{
    41  			name: "cluster mode, workload identity",
    42  			cc: &corev1beta1.ConfigConnector{
    43  				ObjectMeta: metav1.ObjectMeta{
    44  					Name: k8s.ConfigConnectorAllowedName,
    45  				},
    46  				Spec: corev1beta1.ConfigConnectorSpec{
    47  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    48  					Mode:                 "cluster",
    49  				},
    50  			},
    51  			result: map[string]string{strings.Join([]string{basedir, "packages", "configconnector", "0.0.0-test", "cluster", "workload-identity"}, "/"): clusterModeWIManifest},
    52  		},
    53  		{
    54  			name: "cluster mode, gcp identity",
    55  			cc: &corev1beta1.ConfigConnector{
    56  				ObjectMeta: metav1.ObjectMeta{
    57  					Name: k8s.ConfigConnectorAllowedName,
    58  				},
    59  				Spec: corev1beta1.ConfigConnectorSpec{
    60  					CredentialSecretName: "my-key",
    61  					Mode:                 "cluster",
    62  				},
    63  			},
    64  			result: map[string]string{strings.Join([]string{basedir, "packages", "configconnector", "0.0.0-test", "cluster", "gcp-identity"}, "/"): clusterModeGcpManifest},
    65  		},
    66  		{
    67  			name: "namespaced mode",
    68  			cc: &corev1beta1.ConfigConnector{
    69  				ObjectMeta: metav1.ObjectMeta{
    70  					Name: k8s.ConfigConnectorAllowedName,
    71  				},
    72  				Spec: corev1beta1.ConfigConnectorSpec{
    73  					GoogleServiceAccount: "foo@bar.iam.gserviceaccount.com",
    74  					Mode:                 "namespaced",
    75  				},
    76  			},
    77  			result: map[string]string{strings.Join([]string{basedir, "packages", "configconnector", "0.0.0-test", "namespaced"}, "/"): namespacedManifest},
    78  		},
    79  	}
    80  	for _, test := range tests {
    81  		tc := test
    82  		t.Run(tc.name, func(t *testing.T) {
    83  			t.Parallel()
    84  			manifestStr, err := ml.ResolveManifest(context.TODO(), tc.cc)
    85  			if err != nil {
    86  				t.Fatalf("unexpected error while loadding the manifest for namespaced components: %v", err)
    87  			}
    88  			if !reflect.DeepEqual(manifestStr, tc.result) {
    89  				t.Fatalf("unexpected diff: %v", cmp.Diff(manifestStr, tc.result))
    90  			}
    91  		})
    92  	}
    93  }
    94  

View as plain text