Skip to content

Commit 189edb8

Browse files
authored
Perform a catalog initialization if needed (#738)
If the last executed version is < 1.3.0, we refresh some of the catalog data to make sure all the command remapping data is properly stored. The commit also stores the last executed CLI version in the datastore to make it available for future use. Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
1 parent f379199 commit 189edb8

13 files changed

Lines changed: 462 additions & 45 deletions

File tree

pkg/command/plugin_search_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/assert"
14-
15-
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
16-
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
1714
)
1815

1916
func TestPluginSearch(t *testing.T) {
@@ -61,10 +58,6 @@ func TestPluginSearch(t *testing.T) {
6158
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
6259
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
6360

64-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
65-
err = config.SetFeature(featureArray[1], featureArray[2], "true")
66-
assert.Nil(err)
67-
6861
defer func() {
6962
os.Unsetenv("TANZU_CONFIG")
7063
os.Unsetenv("TANZU_CONFIG_NEXT_GEN")

pkg/command/plugin_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"github.com/vmware-tanzu/tanzu-cli/pkg/catalog"
1818
"github.com/vmware-tanzu/tanzu-cli/pkg/cli"
1919
"github.com/vmware-tanzu/tanzu-cli/pkg/common"
20-
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
21-
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
2220
configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
2321
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
2422
)
@@ -109,11 +107,6 @@ func TestPluginList(t *testing.T) {
109107
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
110108
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
111109

112-
// Always turn on the context feature
113-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
114-
err = config.SetFeature(featureArray[1], featureArray[2], "true")
115-
assert.Nil(t, err)
116-
117110
var completionType uint8
118111
t.Run(spec.test, func(t *testing.T) {
119112
assert := assert.New(t)
@@ -389,10 +382,6 @@ func TestInstallPlugin(t *testing.T) {
389382
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
390383
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
391384

392-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
393-
err = config.SetFeature(featureArray[1], featureArray[2], "true")
394-
assert.Nil(err)
395-
396385
defer func() {
397386
os.Unsetenv("TANZU_CONFIG")
398387
os.Unsetenv("TANZU_CONFIG_NEXT_GEN")
@@ -444,10 +433,6 @@ func TestUpgradePlugin(t *testing.T) {
444433
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
445434
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
446435

447-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
448-
err = config.SetFeature(featureArray[1], featureArray[2], "true")
449-
assert.Nil(err)
450-
451436
defer func() {
452437
os.Unsetenv("TANZU_CONFIG")
453438
os.Unsetenv("TANZU_CONFIG_NEXT_GEN")

pkg/command/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
2626
"github.com/vmware-tanzu/tanzu-cli/pkg/discovery"
2727
"github.com/vmware-tanzu/tanzu-cli/pkg/globalinit"
28+
"github.com/vmware-tanzu/tanzu-cli/pkg/lastversion"
2829
"github.com/vmware-tanzu/tanzu-cli/pkg/pluginmanager"
2930
"github.com/vmware-tanzu/tanzu-cli/pkg/pluginsupplier"
3031
"github.com/vmware-tanzu/tanzu-cli/pkg/recommendedversion"
@@ -297,6 +298,16 @@ func newRootCmd() *cobra.Command {
297298
// for any other logic below.
298299
if !shouldSkipGlobalInit(cmd) {
299300
checkGlobalInit(cmd)
301+
302+
// Store the last executed CLI version in the datastore.
303+
// This can be useful for future features.
304+
// This must be done after the global initialization so initializers can
305+
// use the previously stored version if necessary.
306+
//
307+
// Note that we cannot run this in the PersistentPostRunE because if the command fails,
308+
// the PersistentPostRunE will not be called. This could lead to the global initialization
309+
// running again on the next command execution.
310+
lastversion.SetLastExecutedCLIVersion()
300311
}
301312

302313
// Ensure mutual exclusion in current contexts just in case if any plugins with old

pkg/command/root_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
2020
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
2121

22+
"github.com/vmware-tanzu/tanzu-cli/pkg/buildinfo"
2223
"github.com/vmware-tanzu/tanzu-cli/pkg/catalog"
2324
"github.com/vmware-tanzu/tanzu-cli/pkg/cli"
2425
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
@@ -98,6 +99,7 @@ type testCLIEnvironment struct {
9899
cacheDir string
99100
configFile string
100101
configFileNG string
102+
dataStore string
101103
envVars []string
102104
}
103105

@@ -120,6 +122,9 @@ func setupTestCLIEnvironment(t *testing.T) testCLIEnvironment {
120122
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
121123
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
122124

125+
tmpDataStoreFile, _ := os.CreateTemp("", "data-store.yaml")
126+
os.Setenv("TEST_CUSTOM_DATA_STORE_FILE", tmpDataStoreFile.Name())
127+
123128
// Start each test with the defaults of the target commands
124129
// and reset the help flag in case it was set
125130
tmcCmd.ResetCommands()
@@ -145,12 +150,15 @@ func setupTestCLIEnvironment(t *testing.T) testCLIEnvironment {
145150
cacheDir: dir,
146151
configFile: configFile.Name(),
147152
configFileNG: configFileNG.Name(),
153+
dataStore: tmpDataStoreFile.Name(),
154+
148155
envVars: []string{
149156
"TEST_CUSTOM_CATALOG_CACHE_DIR",
150157
"TANZU_CONFIG",
151158
"TANZU_CONFIG_NEXT_GEN",
152159
"TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER",
153160
"TANZU_CLI_EULA_PROMPT_ANSWER",
161+
"TEST_CUSTOM_DATA_STORE_FILE",
154162
},
155163
}
156164
}
@@ -160,6 +168,7 @@ func tearDownTestCLIEnvironment(env testCLIEnvironment) {
160168
os.RemoveAll(env.cacheDir)
161169
os.RemoveAll(env.configFile)
162170
os.RemoveAll(env.configFileNG)
171+
os.RemoveAll(env.dataStore)
163172

164173
for _, envVar := range env.envVars {
165174
os.Unsetenv(envVar)
@@ -895,6 +904,48 @@ func TestGlobalInit(t *testing.T) {
895904
}
896905
}
897906

907+
func TestSetLastVersion(t *testing.T) {
908+
tests := []struct {
909+
test string
910+
version string
911+
expectedVersion string
912+
}{
913+
{
914+
test: "set version to v1.2.3",
915+
version: "v1.2.3",
916+
expectedVersion: "v1.2.3",
917+
},
918+
}
919+
920+
originalVersion := buildinfo.Version
921+
defer func() {
922+
buildinfo.Version = originalVersion
923+
}()
924+
925+
for _, spec := range tests {
926+
env := setupTestCLIEnvironment(t)
927+
defer tearDownTestCLIEnvironment(env)
928+
929+
t.Run(spec.test, func(t *testing.T) {
930+
assert := assert.New(t)
931+
932+
buildinfo.Version = spec.version
933+
934+
rootCmd, err := NewRootCmd()
935+
assert.Nil(err)
936+
// Execute any command to trigger the version update
937+
rootCmd.SetArgs([]string{"plugin", "list"})
938+
err = rootCmd.Execute()
939+
assert.Nil(err)
940+
941+
// Read the data store file and make sure it contains the last executed version
942+
b, err := os.ReadFile(env.dataStore)
943+
assert.Nil(err)
944+
assert.Contains(string(b), spec.expectedVersion)
945+
})
946+
}
947+
}
948+
898949
func TestEnvVarsSet(t *testing.T) {
899950
env := setupTestCLIEnvironment(t)
900951
defer tearDownTestCLIEnvironment(env)

pkg/config/defaults_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package config
55

66
import (
77
"os"
8-
"strings"
98

109
. "github.com/onsi/ginkgo/v2"
1110
. "github.com/onsi/gomega"
@@ -47,10 +46,6 @@ var _ = Describe("defaults test cases", func() {
4746
configFileNG, err = os.CreateTemp("", "config_ng")
4847
Expect(err).To(BeNil())
4948
os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
50-
51-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
52-
err = configlib.SetFeature(featureArray[1], featureArray[2], "true")
53-
Expect(err).To(BeNil())
5449
})
5550
AfterEach(func() {
5651
os.Unsetenv("TANZU_CONFIG")

pkg/config/discovery_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package config
55

66
import (
77
"os"
8-
"strings"
98

109
. "github.com/onsi/ginkgo/v2"
1110
. "github.com/onsi/gomega"
@@ -31,10 +30,6 @@ var _ = Describe("Populate default central discovery", func() {
3130
os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
3231
os.Setenv("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER", "No")
3332
os.Setenv("TANZU_CLI_EULA_PROMPT_ANSWER", "Yes")
34-
35-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
36-
err = configlib.SetFeature(featureArray[1], featureArray[2], "true")
37-
Expect(err).To(BeNil())
3833
})
3934
AfterEach(func() {
4035
os.Unsetenv("TANZU_CONFIG")

pkg/config/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
)
1111

1212
func init() {
13+
runInit()
14+
}
15+
16+
func runInit() {
1317
// Configure default feature flags
1418
_ = config.ConfigureFeatureFlags(constants.DefaultCliFeatureFlags, config.SkipIfExists())
1519

pkg/config/init_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2021 VMware, Inc. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package config contains useful functionality for config updates
5+
package config
6+
7+
import (
8+
"os"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
13+
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
14+
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
15+
)
16+
17+
func TestFeatureContextCommandDoesNotGetSet(t *testing.T) {
18+
assert := assert.New(t)
19+
20+
// Setup a temporary configuration
21+
configFile, err := os.CreateTemp("", "config")
22+
assert.Nil(err)
23+
err = os.Setenv("TANZU_CONFIG", configFile.Name())
24+
assert.Nil(err)
25+
defer os.RemoveAll(configFile.Name())
26+
defer os.Unsetenv("TANZU_CONFIG")
27+
28+
configFileNG, err := os.CreateTemp("", "config_ng")
29+
assert.Nil(err)
30+
err = os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
31+
assert.Nil(err)
32+
defer os.RemoveAll(configFileNG.Name())
33+
defer os.Unsetenv("TANZU_CONFIG_NEXT_GEN")
34+
35+
runInit()
36+
37+
// Starting with CLI 1.3.0 we no longer set the feature flag features.global.context-target-v2.
38+
// This is important because it allows us to determine if the last version executed was < 1.3.0.
39+
assert.False(config.IsFeatureActivated(constants.FeatureContextCommand))
40+
}
41+
42+
func TestCentralRepoGetsSet(t *testing.T) {
43+
assert := assert.New(t)
44+
45+
// Setup a temporary configuration
46+
configFile, err := os.CreateTemp("", "config")
47+
assert.Nil(err)
48+
err = os.Setenv("TANZU_CONFIG", configFile.Name())
49+
assert.Nil(err)
50+
defer os.RemoveAll(configFile.Name())
51+
defer os.Unsetenv("TANZU_CONFIG")
52+
53+
configFileNG, err := os.CreateTemp("", "config_ng")
54+
assert.Nil(err)
55+
err = os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
56+
assert.Nil(err)
57+
defer os.RemoveAll(configFileNG.Name())
58+
defer os.Unsetenv("TANZU_CONFIG_NEXT_GEN")
59+
60+
// Check that the central repo gets set
61+
runInit()
62+
63+
sources, err := config.GetCLIDiscoverySources()
64+
assert.Nil(err)
65+
assert.Equal(1, len(sources))
66+
assert.Equal(DefaultStandaloneDiscoveryName, sources[0].OCI.Name)
67+
assert.Equal(constants.TanzuCLIDefaultCentralPluginDiscoveryImage, sources[0].OCI.Image)
68+
69+
// Check that the central repo does not get set if the user has deleted it
70+
err = config.DeleteCLIDiscoverySource(DefaultStandaloneDiscoveryName)
71+
assert.Nil(err)
72+
73+
sources, err = config.GetCLIDiscoverySources()
74+
assert.Nil(err)
75+
assert.Equal(0, len(sources))
76+
77+
runInit()
78+
79+
sources, err = config.GetCLIDiscoverySources()
80+
assert.Nil(err)
81+
assert.Equal(0, len(sources))
82+
}

pkg/constants/featureflags.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package constants
66
// This block is for global feature constants, to allow them to be used more broadly
77
const (
88
// FeatureContextCommand determines whether to surface the context command. This is disabled by default.
9+
// This feature flag is no longer used. However, we keep it defined so that it can be used to know
10+
// if an older CLI (< 1.3.0) was last executed.
911
FeatureContextCommand = "features.global.context-target-v2"
1012

1113
// FeaturePluginDiscoveryForTanzuContext determines whether to enable context-scoped plugin discovery for Tanzu context.
@@ -25,10 +27,5 @@ const (
2527
// mainstreaming the feature (with a default true value) under the flag name "features.global.foo-bar", as there will be
2628
// no conflict with previous installs (that have a false value for the entry "features.global.foo-bar-beta").
2729
var (
28-
DefaultCliFeatureFlags = map[string]bool{
29-
FeatureContextCommand: true,
30-
// Do NOT include the test feature flag to disable the central repo.
31-
// We don't want to publicize this feature flag.
32-
// It defaults to false when not specified, which is what is needed.
33-
}
30+
DefaultCliFeatureFlags = map[string]bool{}
3431
)

pkg/discovery/oci_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ package discovery
55

66
import (
77
"os"
8-
"strings"
98
"testing"
109

1110
"github.com/stretchr/testify/assert"
1211

1312
"github.com/vmware-tanzu/tanzu-cli/pkg/common"
14-
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
15-
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
1613
)
1714

1815
func Test_NewOCIDiscovery(t *testing.T) {
@@ -26,10 +23,6 @@ func Test_NewOCIDiscovery(t *testing.T) {
2623
assert.Nil(err)
2724
os.Setenv("TANZU_CONFIG_NEXT_GEN", configFileNG.Name())
2825

29-
featureArray := strings.Split(constants.FeatureContextCommand, ".")
30-
err = config.SetFeature(featureArray[1], featureArray[2], "true")
31-
assert.Nil(err)
32-
3326
defer func() {
3427
os.Unsetenv("TANZU_CONFIG")
3528
os.Unsetenv("TANZU_CONFIG_NEXT_GEN")

0 commit comments

Comments
 (0)