...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/writer/secret_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/writer

     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 writer_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
    21  	testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/writer"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/manager"
    27  )
    28  
    29  var mgr manager.Manager
    30  
    31  func TestSecretCertWriter(t *testing.T) {
    32  	namespaceName := "my-namespace"
    33  	testcontroller.EnsureNamespaceExistsT(t, mgr.GetClient(), namespaceName)
    34  	opts := writer.SecretCertWriterOptions{
    35  		Client: mgr.GetClient(),
    36  		Secret: &types.NamespacedName{
    37  			Name:      "my-name",
    38  			Namespace: namespaceName,
    39  		},
    40  	}
    41  	secretWriter, err := writer.NewSecretCertWriter(opts)
    42  	if err != nil {
    43  		t.Fatalf("error creating secret writer: %v", err)
    44  	}
    45  	artifacts1, changed, err := secretWriter.EnsureCert("localhost")
    46  	if err != nil {
    47  		t.Fatalf("error ensuring certificate: %v", err)
    48  	}
    49  	if !changed {
    50  		t.Fatalf("unexpected value for changed: got '%v', want '%v'", changed, true)
    51  	}
    52  	if artifacts1 == nil {
    53  		t.Fatalf("unexpected nil value for artifacts")
    54  	}
    55  	artifacts2, changed, err := secretWriter.EnsureCert("localhost")
    56  	if changed {
    57  		t.Fatalf("unexpected value for changed: got '%v', want '%v'", changed, false)
    58  	}
    59  	if !cmp.Equal(artifacts1, artifacts2) {
    60  		t.Fatalf("expected certs to be equal")
    61  	}
    62  }
    63  
    64  func TestMain(m *testing.M) {
    65  	testmain.TestMainForUnitTests(m, &mgr)
    66  }
    67  

View as plain text