cli: generate state file during constellation config generate (#2455)

* create state file during config generate

* use written file in `constellation create`

* document creation of state file

* remove accidentally added test

* check error when writing state file
This commit is contained in:
Moritz Sanft 2023-10-16 20:18:59 +02:00 committed by GitHub
parent e5513f14e6
commit 25b23689ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 22 deletions

View file

@ -24,7 +24,21 @@ import (
)
func TestCreate(t *testing.T) {
fsWithDefaultConfig := func(require *require.Assertions, provider cloudprovider.Provider) afero.Fs {
fsWithDefaultConfigAndState := func(require *require.Assertions, provider cloudprovider.Provider) afero.Fs {
fs := afero.NewMemMapFs()
file := file.NewHandler(fs)
require.NoError(file.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), provider)))
stateFile := state.New()
switch provider {
case cloudprovider.GCP:
stateFile.SetInfrastructure(state.Infrastructure{GCP: &state.GCP{}})
case cloudprovider.Azure:
stateFile.SetInfrastructure(state.Infrastructure{Azure: &state.Azure{}})
}
require.NoError(stateFile.WriteToFile(file, constants.StateFilename))
return fs
}
fsWithoutState := func(require *require.Assertions, provider cloudprovider.Provider) afero.Fs {
fs := afero.NewMemMapFs()
file := file.NewHandler(fs)
require.NoError(file.WriteYAML(constants.ConfigFilename, defaultConfigWithExpectedMeasurements(t, config.Default(), provider)))
@ -45,26 +59,26 @@ func TestCreate(t *testing.T) {
wantAbort bool
}{
"create": {
setupFs: fsWithDefaultConfig,
setupFs: fsWithDefaultConfigAndState,
creator: &stubCloudCreator{state: infraState},
provider: cloudprovider.GCP,
yesFlag: true,
},
"interactive": {
setupFs: fsWithDefaultConfig,
setupFs: fsWithDefaultConfigAndState,
creator: &stubCloudCreator{state: infraState},
provider: cloudprovider.Azure,
stdin: "yes\n",
},
"interactive abort": {
setupFs: fsWithDefaultConfig,
setupFs: fsWithDefaultConfigAndState,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
stdin: "no\n",
wantAbort: true,
},
"interactive error": {
setupFs: fsWithDefaultConfig,
setupFs: fsWithDefaultConfigAndState,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
stdin: "foo\nfoo\nfoo\n",
@ -103,8 +117,15 @@ func TestCreate(t *testing.T) {
yesFlag: true,
wantErr: true,
},
"state file does not exist": {
setupFs: fsWithoutState,
creator: &stubCloudCreator{},
provider: cloudprovider.GCP,
yesFlag: true,
wantErr: true,
},
"create error": {
setupFs: fsWithDefaultConfig,
setupFs: fsWithDefaultConfigAndState,
creator: &stubCloudCreator{createErr: someErr},
provider: cloudprovider.GCP,
yesFlag: true,