2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-01-04 04:46:29 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-05-12 09:14:52 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigGenerateDefault(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown))
|
2022-05-12 09:14:52 -04:00
|
|
|
|
2022-05-16 12:54:25 -04:00
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
2022-05-12 09:14:52 -04:00
|
|
|
assert.NoError(err)
|
2022-05-16 12:54:25 -04:00
|
|
|
assert.Equal(*config.Default(), readConfig)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2022-05-18 05:39:14 -04:00
|
|
|
func TestConfigGenerateDefaultGCPSpecific(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-02-10 07:27:22 -05:00
|
|
|
wantConf := config.Default()
|
|
|
|
wantConf.RemoveProviderExcept(cloudprovider.GCP)
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, cloudprovider.GCP))
|
2022-05-18 05:39:14 -04:00
|
|
|
|
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(*wantConf, readConfig)
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
func TestConfigGenerateDefaultExists(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
require.NoError(fileHandler.Write(constants.ConfigFilename, []byte("foobar"), file.OptNone))
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown))
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGenerateFileFlagRemoved(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
cmd.ResetFlags()
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown))
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGenerateStdOut(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
|
|
|
|
var outBuffer bytes.Buffer
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
cmd.SetOut(&outBuffer)
|
|
|
|
require.NoError(cmd.Flags().Set("file", "-"))
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown))
|
2022-05-12 09:14:52 -04:00
|
|
|
|
2022-05-16 12:54:25 -04:00
|
|
|
var readConfig config.Config
|
|
|
|
require.NoError(yaml.NewDecoder(&outBuffer).Decode(&readConfig))
|
|
|
|
|
|
|
|
assert.Equal(*config.Default(), readConfig)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|