|
| 1 | +// Copyright 2021 Tetrate |
| 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 cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + |
| 23 | + "github.com/tetratelabs/getistio/api" |
| 24 | +) |
| 25 | + |
| 26 | +func Test_fetchParams(t *testing.T) { |
| 27 | + type tc struct { |
| 28 | + flag *fetchFlags |
| 29 | + mf *api.Manifest |
| 30 | + exp *api.IstioDistribution |
| 31 | + } |
| 32 | + |
| 33 | + for i, c := range []tc{ |
| 34 | + { |
| 35 | + // no args -> fall back to the latest tetrate flavor |
| 36 | + flag: &fetchFlags{flavorVersion: -1}, |
| 37 | + mf: &api.Manifest{ |
| 38 | + IstioDistributions: []*api.IstioDistribution{ |
| 39 | + {Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 43 | + }, |
| 44 | + { |
| 45 | + // all given |
| 46 | + flag: &fetchFlags{version: "1.7.3", flavorVersion: 100, flavor: api.IstioDistributionFlavorTetrate}, |
| 47 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrate}, |
| 48 | + }, |
| 49 | + { |
| 50 | + // given name |
| 51 | + flag: &fetchFlags{name: "1.7.3-tetrate-v100"}, |
| 52 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrate}, |
| 53 | + }, |
| 54 | + { |
| 55 | + // flavor not given |
| 56 | + flag: &fetchFlags{version: "1.7.3", flavorVersion: 100}, |
| 57 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrate}, |
| 58 | + }, |
| 59 | + { |
| 60 | + // flavorVersion not given -> fall back to the latest flavor version |
| 61 | + flag: &fetchFlags{version: "1.7.3", flavor: api.IstioDistributionFlavorTetrateFIPS, flavorVersion: -1}, |
| 62 | + mf: &api.Manifest{ |
| 63 | + IstioDistributions: []*api.IstioDistribution{ |
| 64 | + {Version: "1.7.3", FlavorVersion: 50, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 65 | + {Version: "1.7.3", FlavorVersion: 10000000, Flavor: api.IstioDistributionFlavorTetrate}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 50, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 69 | + }, |
| 70 | + { |
| 71 | + // version not given -> choose the latest version given flavor in manifest |
| 72 | + flag: &fetchFlags{flavor: api.IstioDistributionFlavorIstio, flavorVersion: 0}, |
| 73 | + mf: &api.Manifest{ |
| 74 | + IstioDistributions: []*api.IstioDistribution{ |
| 75 | + {Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 76 | + {Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorIstio}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + exp: &api.IstioDistribution{Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorIstio}, |
| 80 | + }, |
| 81 | + { |
| 82 | + // version and flavor version not given -> choose the latest version given flavor in manifest |
| 83 | + flag: &fetchFlags{flavor: api.IstioDistributionFlavorIstio, flavorVersion: -1}, |
| 84 | + mf: &api.Manifest{ |
| 85 | + IstioDistributions: []*api.IstioDistribution{ |
| 86 | + {Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 87 | + {Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorIstio}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + exp: &api.IstioDistribution{Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorIstio}, |
| 91 | + }, |
| 92 | + { |
| 93 | + // flavorVersion not given -> not found error |
| 94 | + flag: &fetchFlags{version: "1.7.3", flavor: api.IstioDistributionFlavorTetrateFIPS, flavorVersion: -1}, |
| 95 | + mf: &api.Manifest{ |
| 96 | + IstioDistributions: []*api.IstioDistribution{ |
| 97 | + {Version: "1.7.3", FlavorVersion: 50, Flavor: api.IstioDistributionFlavorTetrate}, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + // flavor, flavorVersion not given -> fall back to the latest tetrate flavor |
| 103 | + flag: &fetchFlags{version: "1.7.3", flavorVersion: -1}, |
| 104 | + mf: &api.Manifest{ |
| 105 | + IstioDistributions: []*api.IstioDistribution{ |
| 106 | + {Version: "1.7.3", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 107 | + {Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + exp: &api.IstioDistribution{Version: "1.7.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 111 | + }, |
| 112 | + { |
| 113 | + // patch version is not given in 'version', so should fallback to the latest patch version in the minor version |
| 114 | + flag: &fetchFlags{version: "1.7", flavor: api.IstioDistributionFlavorTetrateFIPS, flavorVersion: -1}, |
| 115 | + mf: &api.Manifest{ |
| 116 | + IstioDistributions: []*api.IstioDistribution{ |
| 117 | + {Version: "1.7.3", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrate}, |
| 118 | + {Version: "1.7.1", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 119 | + {Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 120 | + }, |
| 121 | + }, |
| 122 | + exp: &api.IstioDistribution{Version: "1.7.1", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 123 | + }, |
| 124 | + { |
| 125 | + // patch version is not given in 'version', so should fallback to the latest patch version in the minor version |
| 126 | + flag: &fetchFlags{version: "1.7", flavorVersion: 0}, |
| 127 | + mf: &api.Manifest{ |
| 128 | + IstioDistributions: []*api.IstioDistribution{ |
| 129 | + {Version: "1.7.100", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrate}, |
| 130 | + {Version: "1.7.20", FlavorVersion: 20, Flavor: api.IstioDistributionFlavorTetrate}, |
| 131 | + {Version: "1.7.1", FlavorVersion: 1, Flavor: api.IstioDistributionFlavorTetrate}, |
| 132 | + {Version: "1.7.1", FlavorVersion: 100, Flavor: api.IstioDistributionFlavorTetrateFIPS}, |
| 133 | + {Version: "1.8.3", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 134 | + }, |
| 135 | + }, |
| 136 | + exp: &api.IstioDistribution{Version: "1.7.100", FlavorVersion: 0, Flavor: api.IstioDistributionFlavorTetrate}, |
| 137 | + }, |
| 138 | + } { |
| 139 | + t.Run(fmt.Sprintf("%d-th case", i), func(t *testing.T) { |
| 140 | + actual, err := fetchParams(c.flag, c.mf) |
| 141 | + if c.exp == nil { |
| 142 | + assert.Error(t, err) |
| 143 | + } else { |
| 144 | + assert.NoError(t, err) |
| 145 | + assert.Equal(t, c.exp, actual) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + } |
| 150 | +} |
0 commit comments