...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main/testmain_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main

     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 testmain_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main"
    21  	"k8s.io/apimachinery/pkg/version"
    22  	"k8s.io/client-go/kubernetes"
    23  	"k8s.io/client-go/rest"
    24  	"sigs.k8s.io/controller-runtime/pkg/manager"
    25  )
    26  
    27  var (
    28  	mgr manager.Manager
    29  )
    30  
    31  // This test ensures that all test cases are running against the desired version
    32  // of kube-apiserver. The desired version is specified in scripts/shared-vars.sh.
    33  // The `expectedVersion` variable in the test will need to be updated when we upgrade the kube-apiserver binary.
    34  func TestKubeAPIServerVersion(t *testing.T) {
    35  	expectedVersion := "v1.21.0"
    36  	v, err := getKubernetesVersion(mgr.GetConfig())
    37  	if err != nil {
    38  		t.Fatalf("error retrieving the Kubernetes API Server version")
    39  	}
    40  	if v.String() != expectedVersion {
    41  		t.Fatalf("got the Kubernetes API Server version %v, expect to have %v", v, expectedVersion)
    42  	}
    43  }
    44  
    45  // getKubernetesVersion retrieves the Kubernetes API Server version,
    46  // and returns the major and minor version.
    47  func getKubernetesVersion(clientConfig *rest.Config) (*version.Info, error) {
    48  	clientGoClient := kubernetes.NewForConfigOrDie(mgr.GetConfig())
    49  	version, err := clientGoClient.ServerVersion()
    50  	return version, err
    51  }
    52  
    53  func TestMain(m *testing.M) {
    54  	testmain.TestMainForUnitTests(m, &mgr)
    55  }
    56  

View as plain text