github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/concat_spec_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package preflight
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"k8s.io/apimachinery/pkg/util/yaml"
    27  
    28  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    29  )
    30  
    31  var _ = Describe("concat_spec_test", func() {
    32  
    33  	It("ConcatPreflightSpec Test", func() {
    34  		targetByte := `
    35  apiVersion: troubleshoot.sh/v1beta2
    36  kind: Preflight
    37  metadata:
    38   name: sample
    39  spec:
    40   analyzers:
    41     - nodeResources:
    42         checkName: Must have at least 3 nodes in the cluster
    43         outcomes:
    44           - fail:
    45               when: "< 3"
    46               message: This application requires at least 3 nodes
    47           - warn:
    48               when: "< 5"
    49               message: This application recommends at last 5 nodes.
    50           - pass:
    51               message: This cluster has enough nodes.`
    52  		sourceByte := `
    53  apiVersion: troubleshoot.sh/v1beta2
    54  kind: Preflight
    55  metadata:
    56   name: sample
    57  spec:
    58   collectors:
    59     - redis:
    60         collectorName: my-redis
    61         uri: rediss://default:replicated@server:6380
    62         tls:
    63           skipVerify: true
    64   analyzers:
    65     - redis:
    66         checkName: Must be redis 5.x or later
    67         collectorName: my-redis
    68         outcomes:
    69           - fail:
    70               when: "connected == false"
    71               message: Cannot connect to redis server
    72           - fail:
    73               when: "version < 5.0.0"
    74               message: The redis server must be at least version 5
    75           - pass:
    76               message: The redis connection checks out.`
    77  		targetSpec := new(preflightv1beta2.Preflight)
    78  		sourceSpec := new(preflightv1beta2.Preflight)
    79  		Expect(yaml.Unmarshal([]byte(targetByte), targetSpec)).Should(Succeed())
    80  		Expect(yaml.Unmarshal([]byte(sourceByte), sourceSpec)).Should(Succeed())
    81  		var newSpec = ConcatPreflightSpec(nil, sourceSpec)
    82  		Expect(newSpec).Should(Equal(sourceSpec))
    83  		newSpec = ConcatPreflightSpec(targetSpec, nil)
    84  		Expect(newSpec).Should(Equal(targetSpec))
    85  		newSpec = ConcatPreflightSpec(targetSpec, sourceSpec)
    86  		Expect(len(newSpec.Spec.Analyzers)).Should(Equal(2))
    87  	})
    88  	It("ConcatHostPreflightSpec Test", func() {
    89  		targetByte := `
    90  apiVersion: troubleshoot.sh/v1beta2
    91  kind: HostPreflight
    92  metadata:
    93   name: cpu
    94  spec:
    95   collectors:
    96     - cpu: {}
    97   analyzers:
    98     - cpu:
    99         outcomes:
   100           - fail:
   101               when: "physical < 4"
   102               message: At least 4 physical CPU cores are required
   103           - fail:
   104               when: "logical < 8"
   105               message: At least 8 CPU cores are required
   106           - warn:
   107               when: "count < 16"
   108               message: At least 16 CPU cores preferred
   109           - pass:
   110               message: This server has sufficient CPU cores.`
   111  		sourceByte := `
   112  apiVersion: troubleshoot.sh/v1beta2
   113  kind: HostPreflight
   114  metadata:
   115   name: http
   116  spec:
   117   collectors:
   118     - http:
   119         collectorName: registry
   120         get:
   121           url: https://registry.replicated.com
   122   analyzers:
   123     - http:
   124         collectorName: registry
   125         outcomes:
   126           - fail:
   127               when: "error"
   128               message: Error connecting to registry
   129           - pass:
   130               when: "statusCode == 404"
   131               message: Connected to registry
   132           - fail:
   133               message: "Unexpected response"`
   134  		targetSpec := new(preflightv1beta2.HostPreflight)
   135  		sourceSpec := new(preflightv1beta2.HostPreflight)
   136  		Expect(yaml.Unmarshal([]byte(targetByte), targetSpec)).Should(Succeed())
   137  		Expect(yaml.Unmarshal([]byte(sourceByte), sourceSpec)).Should(Succeed())
   138  		var newSpec = ConcatHostPreflightSpec(nil, sourceSpec)
   139  		Expect(newSpec).Should(Equal(sourceSpec))
   140  		newSpec = ConcatHostPreflightSpec(targetSpec, nil)
   141  		Expect(newSpec).Should(Equal(targetSpec))
   142  		newSpec = ConcatHostPreflightSpec(targetSpec, sourceSpec)
   143  		Expect(len(newSpec.Spec.Analyzers)).Should(Equal(2))
   144  	})
   145  
   146  	It("ExtractHostPreflightSpec Test", func() {
   147  		sourceByte := `
   148  apiVersion: troubleshoot.sh/v1beta2
   149  kind: HostPreflight
   150  metadata:
   151   name: http
   152  spec:
   153   collectors:
   154     - http:
   155         collectorName: registry
   156         get:
   157           url: https://registry.replicated.com
   158   analyzers:
   159     - http:
   160         collectorName: registry
   161         outcomes:
   162           - fail:
   163               when: "error"
   164               message: Error connecting to registry
   165           - pass:
   166               when: "statusCode == 404"
   167               message: Connected to registry
   168           - fail:
   169               message: "Unexpected response"`
   170  		sourceSpec := new(preflightv1beta2.HostPreflight)
   171  		Expect(yaml.Unmarshal([]byte(sourceByte), sourceSpec)).Should(Succeed())
   172  		Expect(ExtractHostPreflightSpec(nil)).Should(BeNil())
   173  		newSpec := ExtractHostPreflightSpec(sourceSpec)
   174  		Expect(len(newSpec.Spec.Collectors)).Should(Equal(1))
   175  	})
   176  })