sigs.k8s.io/kubebuilder/v3@v3.14.0/test/e2e/utils/kubectl_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("Kubectl", func() {
    25  	var ver KubernetesVersion
    26  	AfterEach(func() {
    27  		ver = KubernetesVersion{}
    28  	})
    29  	Context("successful 'kubectl version' output", func() {
    30  		It("decodes both client and server versions", func() {
    31  			Expect(ver.decode(clientServerOutput)).To(Succeed())
    32  			Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
    33  			Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
    34  			Expect(ver.ServerVersion.major).To(BeNumerically("==", 1))
    35  			Expect(ver.ServerVersion.minor).To(BeNumerically("==", 21))
    36  		})
    37  		It("decodes only client version", func() {
    38  			Expect(ver.decode(clientOnlyOutput)).To(Succeed())
    39  			Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
    40  			Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
    41  			Expect(ver.ServerVersion.major).To(BeNumerically("==", 0))
    42  			Expect(ver.ServerVersion.minor).To(BeNumerically("==", 0))
    43  		})
    44  	})
    45  	Context("'kubectl version' output with non-JSON text", func() {
    46  		It("handles warning logs", func() {
    47  			Expect(ver.decode(clientServerWithWarningOutput)).To(Succeed())
    48  			Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
    49  			Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
    50  			Expect(ver.ServerVersion.major).To(BeNumerically("==", 1))
    51  			Expect(ver.ServerVersion.minor).To(BeNumerically("==", 21))
    52  		})
    53  	})
    54  	Context("with error text", func() {
    55  		It("returns an error", func() {
    56  			Expect(ver.decode(errorOutput)).NotTo(Succeed())
    57  		})
    58  	})
    59  })
    60  
    61  const clientServerOutput = `
    62  {
    63    "clientVersion": {
    64      "major": "1",
    65      "minor": "21",
    66      "gitVersion": "v0.21.0-beta.1",
    67      "gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
    68      "gitTreeState": "clean",
    69      "buildDate": "2021-08-31T22:03:33Z",
    70      "goVersion": "go1.16.6",
    71      "compiler": "gc",
    72      "platform": "linux/amd64"
    73    },
    74    "serverVersion": {
    75      "major": "1",
    76      "minor": "21",
    77      "gitVersion": "v1.21.1",
    78      "gitCommit": "5e58841cce77d4bc13713ad2b91fa0d961e69192",
    79      "gitTreeState": "clean",
    80      "buildDate": "2021-05-18T01:10:20Z",
    81      "goVersion": "go1.16.4",
    82      "compiler": "gc",
    83      "platform": "linux/amd64"
    84    }
    85  }
    86  `
    87  
    88  const clientOnlyOutput = `
    89  {
    90    "clientVersion": {
    91      "major": "1",
    92      "minor": "21",
    93      "gitVersion": "v0.21.0-beta.1",
    94      "gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
    95      "gitTreeState": "clean",
    96      "buildDate": "2021-08-31T22:03:33Z",
    97      "goVersion": "go1.16.6",
    98      "compiler": "gc",
    99      "platform": "linux/amd64"
   100    }
   101  }
   102  `
   103  
   104  const clientServerWithWarningOutput = `
   105  {
   106    "clientVersion": {
   107      "major": "1",
   108      "minor": "21",
   109      "gitVersion": "v0.21.0-beta.1",
   110      "gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
   111      "gitTreeState": "clean",
   112      "buildDate": "2021-08-31T22:03:33Z",
   113      "goVersion": "go1.16.6",
   114      "compiler": "gc",
   115      "platform": "linux/amd64"
   116    },
   117    "serverVersion": {
   118      "major": "1",
   119      "minor": "21",
   120      "gitVersion": "v1.21.1",
   121      "gitCommit": "5e58841cce77d4bc13713ad2b91fa0d961e69192",
   122      "gitTreeState": "clean",
   123      "buildDate": "2021-05-18T01:10:20Z",
   124      "goVersion": "go1.16.4",
   125      "compiler": "gc",
   126      "platform": "linux/amd64"
   127    }
   128  }
   129  WARNING: version difference between client (0.21) and server (1.21) exceeds the supported minor version skew of +/-1
   130  `
   131  
   132  const errorOutput = `
   133  ERROR: reason blah blah
   134  `