2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-05-23 09:01:39 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateConfig(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
cnf *config.Config
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
wantOutput bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2022-08-08 05:04:17 -04:00
|
|
|
"default config is not valid": {
|
|
|
|
cnf: config.Default(),
|
|
|
|
wantOutput: true,
|
|
|
|
wantErr: true,
|
2022-05-23 09:01:39 -04:00
|
|
|
},
|
2022-08-08 05:04:17 -04:00
|
|
|
"default Azure config is not valid": {
|
2022-05-23 09:01:39 -04:00
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
2022-08-08 05:04:17 -04:00
|
|
|
az := cnf.Provider.Azure
|
|
|
|
cnf.Provider = config.ProviderConfig{}
|
|
|
|
cnf.Provider.Azure = az
|
2022-05-23 09:01:39 -04:00
|
|
|
return cnf
|
|
|
|
}(),
|
2022-08-08 05:04:17 -04:00
|
|
|
provider: cloudprovider.Azure,
|
2022-05-23 09:01:39 -04:00
|
|
|
wantOutput: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-08 05:04:17 -04:00
|
|
|
"default GCP config is not valid": {
|
2022-05-23 09:01:39 -04:00
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
2022-08-08 05:04:17 -04:00
|
|
|
gcp := cnf.Provider.GCP
|
2022-05-23 09:01:39 -04:00
|
|
|
cnf.Provider = config.ProviderConfig{}
|
2022-08-08 05:04:17 -04:00
|
|
|
cnf.Provider.GCP = gcp
|
2022-05-23 09:01:39 -04:00
|
|
|
return cnf
|
|
|
|
}(),
|
2022-08-08 05:04:17 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
wantOutput: true,
|
|
|
|
wantErr: true,
|
2022-05-23 09:01:39 -04:00
|
|
|
},
|
2022-08-08 05:04:17 -04:00
|
|
|
"default QEMU config is valid": {
|
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
|
|
|
qemu := cnf.Provider.QEMU
|
|
|
|
cnf.Provider = config.ProviderConfig{}
|
|
|
|
cnf.Provider.QEMU = qemu
|
|
|
|
return cnf
|
|
|
|
}(),
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
},
|
|
|
|
"config with an error": {
|
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
|
|
|
cnf.Version = "v0"
|
|
|
|
return cnf
|
|
|
|
}(),
|
|
|
|
wantOutput: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-09-07 09:38:29 -04:00
|
|
|
"config without provider is not ok": {
|
2022-05-23 09:01:39 -04:00
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
|
|
|
cnf.Provider = config.ProviderConfig{}
|
|
|
|
return cnf
|
|
|
|
}(),
|
2022-09-07 09:38:29 -04:00
|
|
|
wantErr: true,
|
2022-05-23 09:01:39 -04:00
|
|
|
},
|
2022-09-07 09:38:29 -04:00
|
|
|
|
2022-05-23 09:01:39 -04:00
|
|
|
"config without required provider": {
|
|
|
|
cnf: func() *config.Config {
|
|
|
|
cnf := config.Default()
|
|
|
|
cnf.Provider.Azure = nil
|
|
|
|
return cnf
|
|
|
|
}(),
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
|
2022-09-07 09:38:29 -04:00
|
|
|
err := validateConfig(out, tc.cnf)
|
2022-05-23 09:01:39 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
assert.Equal(tc.wantOutput, out.Len() > 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|