|
| 1 | +// Copyright 2019-2022 The Liqo Authors |
| 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 install |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + . "github.com/onsi/ginkgo" |
| 22 | + . "github.com/onsi/ginkgo/extensions/table" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + installutils "github.com/liqotech/liqo/pkg/liqoctl/install/utils" |
| 27 | + argsutils "github.com/liqotech/liqo/pkg/utils/args" |
| 28 | +) |
| 29 | + |
| 30 | +// This recursive function takes a map and a list of keys and visits a tree of nested maps |
| 31 | +// using the keys in the order provided. At each iteration, if the number of non-visited keys |
| 32 | +// is 1, the function returns the value associated to the last key, else if it is greater |
| 33 | +// than 1, the function expects the value to be a map and a new recursive iteration happens. |
| 34 | +// In case the key is not found, an empty string is returned. |
| 35 | +// In case no keys are provided, an error is returned. |
| 36 | +// Example: |
| 37 | +// m := map[string]interface{}{ |
| 38 | +// "first": map[string]interface{}{ |
| 39 | +// "second": map[string]interface{}{ |
| 40 | +// "third": "value", |
| 41 | +// }, |
| 42 | +// }, |
| 43 | +// } |
| 44 | +// ValueFor(m, "first", "second", "third") // returns "value", nil |
| 45 | +// ValueFor(m, "first", "second") // returns map[string]interface{}{ "third": "value" }, nil |
| 46 | +// ValueFor(m, "first", "third") // returns "", nil |
| 47 | +// ValueFor(m) // returns nil, "At least one key is required" |
| 48 | +func ValueFor(m map[string]interface{}, keys ...string) (val interface{}, err error) { |
| 49 | + var ok bool |
| 50 | + if len(keys) == 0 { |
| 51 | + return nil, fmt.Errorf("At least one key is required") |
| 52 | + } else if val, ok = m[keys[0]]; !ok { |
| 53 | + return "", nil |
| 54 | + } else if len(keys) == 1 { |
| 55 | + return val, nil |
| 56 | + } else if m, ok = val.(map[string]interface{}); !ok { |
| 57 | + return nil, fmt.Errorf("The value for key %s is not map (expected to be a map)", keys[0]) |
| 58 | + } else { |
| 59 | + return ValueFor(m, keys[1:]...) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestInstallCommand(t *testing.T) { |
| 64 | + RegisterFailHandler(Fail) |
| 65 | + RunSpecs(t, "Test Install Command") |
| 66 | +} |
| 67 | + |
| 68 | +var _ = Describe("Test the install command works as expected", func() { |
| 69 | + type testCase struct { |
| 70 | + provider string |
| 71 | + parameters []string |
| 72 | + providerValue string |
| 73 | + regionValue string |
| 74 | + customLabelValue string |
| 75 | + clusterNameValue string |
| 76 | + enableAdvertisementValue bool |
| 77 | + enableDiscoveryValue bool |
| 78 | + reservedSubnetsValue []interface{} |
| 79 | + } |
| 80 | + |
| 81 | + DescribeTable("An install command is issued", |
| 82 | + func(tc testCase) { |
| 83 | + cmd := &cobra.Command{} |
| 84 | + cmd.PersistentFlags().Bool("enable-lan-discovery", false, "") |
| 85 | + cmd.PersistentFlags().String("cluster-labels", "", "") |
| 86 | + cmd.PersistentFlags().String("cluster-name", "default-cluster-name", "") |
| 87 | + cmd.PersistentFlags().String("reserved-subnets", "", "") |
| 88 | + cmd.PersistentFlags().Bool("generate-name", false, "") |
| 89 | + cmd.SetArgs(tc.parameters) |
| 90 | + Expect(cmd.Execute()).To(Succeed()) |
| 91 | + |
| 92 | + providerInstance := getProviderInstance(tc.provider) |
| 93 | + Expect(providerInstance).NotTo(BeNil()) |
| 94 | + err := providerInstance.PreValidateGenericCommandArguments(cmd.Flags()) |
| 95 | + Expect(err).ToNot(HaveOccurred()) |
| 96 | + err = providerInstance.ValidateCommandArguments(cmd.Flags()) |
| 97 | + Expect(err).ToNot(HaveOccurred()) |
| 98 | + err = providerInstance.PostValidateGenericCommandArguments("") |
| 99 | + Expect(err).ToNot(HaveOccurred()) |
| 100 | + |
| 101 | + // Chart values |
| 102 | + chartValues := map[string]interface{}{ |
| 103 | + "discovery": map[string]interface{}{ |
| 104 | + "config": map[string]interface{}{ |
| 105 | + "clusterLabels": map[string]interface{}{}, |
| 106 | + "clusterName": "", |
| 107 | + "enableAdvertisement": false, |
| 108 | + "enableDiscovery": false, |
| 109 | + }, |
| 110 | + }, |
| 111 | + "networkManager": map[string]interface{}{ |
| 112 | + "config": map[string]interface{}{ |
| 113 | + "reservedSubnets": []interface{}{}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + // Common values |
| 119 | + enableLanDiscovery, err := cmd.Flags().GetBool("enable-lan-discovery") |
| 120 | + Expect(err).ToNot(HaveOccurred()) |
| 121 | + clusterLabels, err := cmd.Flags().GetString("cluster-labels") |
| 122 | + Expect(err).ToNot(HaveOccurred()) |
| 123 | + clusterLabelsVar := argsutils.StringMap{} |
| 124 | + err = clusterLabelsVar.Set(clusterLabels) |
| 125 | + Expect(err).ToNot(HaveOccurred()) |
| 126 | + clusterLabelsMap := installutils.GetInterfaceMap(clusterLabelsVar.StringMap) |
| 127 | + commonValues := map[string]interface{}{ |
| 128 | + "discovery": map[string]interface{}{ |
| 129 | + "config": map[string]interface{}{ |
| 130 | + "clusterLabels": clusterLabelsMap, |
| 131 | + "enableAdvertisement": enableLanDiscovery, |
| 132 | + "enableDiscovery": enableLanDiscovery, |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + // Provider values |
| 138 | + providerValues := make(map[string]interface{}) |
| 139 | + providerInstance.UpdateChartValues(providerValues) |
| 140 | + |
| 141 | + // Merged values |
| 142 | + values, err := generateValues(chartValues, commonValues, providerValues) |
| 143 | + Expect(err).ToNot(HaveOccurred()) |
| 144 | + |
| 145 | + // Test values over expected ones |
| 146 | + Expect(ValueFor(values, "discovery", "config", "clusterLabels", "liqo.io/provider")).To(Equal(tc.providerValue)) |
| 147 | + Expect(ValueFor(values, "discovery", "config", "clusterName")).To(Equal(tc.clusterNameValue)) |
| 148 | + Expect(ValueFor(values, "discovery", "config", "clusterLabels", "topology.liqo.io/region")).To(Equal(tc.regionValue)) |
| 149 | + Expect(ValueFor(values, "discovery", "config", "clusterLabels", "liqo.io/my-label")).To(Equal(tc.customLabelValue)) |
| 150 | + Expect(ValueFor(values, "discovery", "config", "enableAdvertisement")).To(Equal(tc.enableAdvertisementValue)) |
| 151 | + Expect(ValueFor(values, "discovery", "config", "enableDiscovery")).To(Equal(tc.enableDiscoveryValue)) |
| 152 | + Expect(ValueFor(values, "networkManager", "config", "reservedSubnets")).To(Equal(tc.reservedSubnetsValue)) |
| 153 | + }, |
| 154 | + Entry("Install Kind cluster with default parameters", testCase{ |
| 155 | + "kind", |
| 156 | + []string{}, |
| 157 | + "kind", |
| 158 | + "", |
| 159 | + "", |
| 160 | + "default-cluster-name", |
| 161 | + true, |
| 162 | + true, |
| 163 | + []interface{}{}, |
| 164 | + }), |
| 165 | + Entry("Install Kind cluster with one cluster labels' key-value pair", testCase{ |
| 166 | + "kind", |
| 167 | + []string{"--cluster-labels=topology.liqo.io/region=eu-east"}, |
| 168 | + "kind", |
| 169 | + "eu-east", |
| 170 | + "", |
| 171 | + "default-cluster-name", |
| 172 | + true, |
| 173 | + true, |
| 174 | + []interface{}{}, |
| 175 | + }), |
| 176 | + Entry("Install Kind cluster with cluster labels, auto-discovery disabled, cluster name and reserved subnets", testCase{ |
| 177 | + "kind", |
| 178 | + []string{ |
| 179 | + "--cluster-labels=topology.liqo.io/region=eu-east,liqo.io/my-label=custom,liqo.io/provider=provider-1", |
| 180 | + "--enable-lan-discovery=false", |
| 181 | + "--cluster-name=cluster-1", |
| 182 | + "--reserved-subnets=10.20.30.0/24,10.20.31.0/24", |
| 183 | + }, |
| 184 | + "provider-1", |
| 185 | + "eu-east", |
| 186 | + "custom", |
| 187 | + "cluster-1", |
| 188 | + false, |
| 189 | + false, |
| 190 | + []interface{}{"10.20.30.0/24", "10.20.31.0/24"}, |
| 191 | + }), |
| 192 | + Entry("Install Kubeadm cluster with default parameters", testCase{ |
| 193 | + "kubeadm", |
| 194 | + []string{}, |
| 195 | + "kubeadm", |
| 196 | + "", |
| 197 | + "", |
| 198 | + "default-cluster-name", |
| 199 | + false, |
| 200 | + false, |
| 201 | + []interface{}{}, |
| 202 | + }), |
| 203 | + Entry("Install Kubeadm cluster with cluster labels, cluster name and reserved subnets", testCase{ |
| 204 | + "kubeadm", |
| 205 | + []string{ |
| 206 | + "--cluster-labels=topology.liqo.io/region=eu-east,liqo.io/my-label=custom,liqo.io/provider=provider-1", |
| 207 | + "--cluster-name=cluster-1", |
| 208 | + "--reserved-subnets=10.20.30.0/24,10.20.31.0/24", |
| 209 | + }, |
| 210 | + "provider-1", |
| 211 | + "eu-east", |
| 212 | + "custom", |
| 213 | + "cluster-1", |
| 214 | + false, |
| 215 | + false, |
| 216 | + []interface{}{"10.20.30.0/24", "10.20.31.0/24"}, |
| 217 | + }), |
| 218 | + ) |
| 219 | +}) |
0 commit comments