mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-28 00:07:06 -05:00
005e865a13
* [wip] use state file in CLI Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> tidy Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * use state file in CLI Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> take clusterConfig from IDFile for compat Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> various fixes Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> wip Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add GCP-specific values in Helm loader test Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove unnecessary pointer Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * write ClusterValues in one step Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * move stub to test file Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove mention of id-file Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * move output to `migrateTerraform` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * unconditional assignments converting from idFile Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * move require block in go modules file Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fall back to id file on upgrade Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * tidy Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix linter check Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add notice to remove Terraform state check on manual migration Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add `name` field Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> fix name tests Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * return early if no Terraform diff Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * tidy Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * return infrastructure state even if no diff exists Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add TODO to remove comment Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * use state-file in miniconstellation Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * cli: remove id-file (#2402) * remove id-file from `constellation create` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add file renaming to handler * rename id-file after upgrade * use idFile on `constellation init` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove id-file from `constellation verify` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * linter fixes Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove id-file from `constellation mini` * remove id-file from `constellation recover` * linter fixes * remove id-file from `constellation terminate` * fix initSecret type * fix recover argument precedence * fix terminate test * generate * add TODO to remove id-file removal * Update cli/internal/cmd/init.go Co-authored-by: Adrian Stobbe <stobbe.adrian@gmail.com> * fix verify arg parse logic Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add version test Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove id-file from docs * add file not found log * use state-file in miniconstellation Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove id-file from `constellation iam destroy` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * remove id-file from `cdbg deploy` Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> --------- Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> Co-authored-by: Adrian Stobbe <stobbe.adrian@gmail.com> * use state-file in CI Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * update orchestration docs --------- Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> Co-authored-by: Adrian Stobbe <stobbe.adrian@gmail.com>
393 lines
8.2 KiB
Go
393 lines
8.2 KiB
Go
/*
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package state
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
"github.com/spf13/afero"
|
|
"github.com/stretchr/testify/assert"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
var defaultState = &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "123",
|
|
ClusterEndpoint: "test-cluster-endpoint",
|
|
InitSecret: []byte{0x41},
|
|
APIServerCertSANs: []string{
|
|
"api-server-cert-san-test",
|
|
"api-server-cert-san-test-2",
|
|
},
|
|
Azure: &Azure{
|
|
ResourceGroup: "test-rg",
|
|
SubscriptionID: "test-sub",
|
|
NetworkSecurityGroupName: "test-nsg",
|
|
LoadBalancerName: "test-lb",
|
|
UserAssignedIdentity: "test-uami",
|
|
AttestationURL: "test-maaUrl",
|
|
},
|
|
GCP: &GCP{
|
|
ProjectID: "test-project",
|
|
IPCidrNode: "test-cidr-node",
|
|
IPCidrPod: "test-cidr-pod",
|
|
},
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
OwnerID: "test-owner-id",
|
|
MeasurementSalt: []byte{0x41},
|
|
},
|
|
}
|
|
|
|
func TestWriteToFile(t *testing.T) {
|
|
prepareFs := func(existingFiles ...string) file.Handler {
|
|
fs := afero.NewMemMapFs()
|
|
fh := file.NewHandler(fs)
|
|
for _, name := range existingFiles {
|
|
if err := fh.Write(name, []byte{0x41}); err != nil {
|
|
t.Fatalf("failed to create file %s: %v", name, err)
|
|
}
|
|
}
|
|
return fh
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
state *State
|
|
fh file.Handler
|
|
wantErr bool
|
|
}{
|
|
"success": {
|
|
state: defaultState,
|
|
fh: prepareFs(),
|
|
},
|
|
"overwrite": {
|
|
state: defaultState,
|
|
fh: prepareFs(constants.StateFilename),
|
|
},
|
|
"empty state": {
|
|
state: &State{},
|
|
fh: prepareFs(),
|
|
},
|
|
"rofs": {
|
|
state: defaultState,
|
|
fh: file.NewHandler(afero.NewReadOnlyFs(afero.NewMemMapFs())),
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
err := tc.state.WriteToFile(tc.fh, constants.StateFilename)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(err)
|
|
} else {
|
|
assert.NoError(err)
|
|
assert.Equal(mustMarshalYaml(t, tc.state), mustReadFromFile(t, tc.fh))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReadFromFile(t *testing.T) {
|
|
prepareFs := func(existingFiles map[string][]byte) file.Handler {
|
|
fs := afero.NewMemMapFs()
|
|
fh := file.NewHandler(fs)
|
|
for name, content := range existingFiles {
|
|
if err := fh.Write(name, content); err != nil {
|
|
t.Fatalf("failed to create file %s: %v", name, err)
|
|
}
|
|
}
|
|
return fh
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
existingFiles map[string][]byte
|
|
wantErr bool
|
|
}{
|
|
"success": {
|
|
existingFiles: map[string][]byte{
|
|
constants.StateFilename: mustMarshalYaml(t, defaultState),
|
|
},
|
|
},
|
|
"no state file present": {
|
|
existingFiles: map[string][]byte{},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
fh := prepareFs(tc.existingFiles)
|
|
|
|
state, err := ReadFromFile(fh, constants.StateFilename)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(err)
|
|
} else {
|
|
assert.NoError(err)
|
|
assert.Equal(tc.existingFiles[constants.StateFilename], mustMarshalYaml(t, state))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func mustMarshalYaml(t *testing.T, v any) []byte {
|
|
t.Helper()
|
|
b, err := yaml.Marshal(v)
|
|
if err != nil {
|
|
t.Fatalf("failed to marshal yaml: %v", err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func mustReadFromFile(t *testing.T, fh file.Handler) []byte {
|
|
t.Helper()
|
|
b, err := fh.Read(constants.StateFilename)
|
|
if err != nil {
|
|
t.Fatalf("failed to read file: %v", err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestMerge(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
state *State
|
|
other *State
|
|
expected *State
|
|
wantErr bool
|
|
}{
|
|
"success": {
|
|
state: &State{
|
|
Infrastructure: Infrastructure{
|
|
ClusterEndpoint: "test-cluster-endpoint",
|
|
UID: "123",
|
|
},
|
|
},
|
|
other: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
expected: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
ClusterEndpoint: "test-cluster-endpoint",
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
"empty state": {
|
|
state: &State{},
|
|
other: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
expected: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
"empty other": {
|
|
state: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
other: &State{},
|
|
expected: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
"empty state and other": {
|
|
state: &State{},
|
|
other: &State{},
|
|
expected: &State{},
|
|
},
|
|
"identical": {
|
|
state: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
other: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
expected: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
"nested pointer": {
|
|
state: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "123",
|
|
Azure: &Azure{
|
|
AttestationURL: "test-maaUrl",
|
|
},
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
other: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
Azure: &Azure{
|
|
AttestationURL: "test-maaUrl-2",
|
|
},
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
expected: &State{
|
|
Version: "v1",
|
|
Infrastructure: Infrastructure{
|
|
UID: "456",
|
|
Azure: &Azure{
|
|
AttestationURL: "test-maaUrl-2",
|
|
},
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
_, err := tc.state.Merge(tc.other)
|
|
|
|
if tc.wantErr {
|
|
assert.Error(err)
|
|
} else {
|
|
assert.NoError(err)
|
|
assert.Equal(tc.expected, tc.state)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewFromIDFile(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
idFile clusterid.File
|
|
cfg *config.Config
|
|
expected *State
|
|
}{
|
|
"success": {
|
|
idFile: clusterid.File{
|
|
ClusterID: "test-cluster-id",
|
|
UID: "test-uid",
|
|
},
|
|
cfg: config.Default(),
|
|
expected: &State{
|
|
Version: Version1,
|
|
Infrastructure: Infrastructure{
|
|
UID: "test-uid",
|
|
Name: "constell-test-uid",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
"empty id file": {
|
|
idFile: clusterid.File{},
|
|
cfg: config.Default(),
|
|
expected: &State{Version: Version1, Infrastructure: Infrastructure{Name: "constell-"}},
|
|
},
|
|
"nested pointer": {
|
|
idFile: clusterid.File{
|
|
ClusterID: "test-cluster-id",
|
|
UID: "test-uid",
|
|
AttestationURL: "test-maaUrl",
|
|
},
|
|
cfg: config.Default(),
|
|
expected: &State{
|
|
Version: Version1,
|
|
Infrastructure: Infrastructure{
|
|
UID: "test-uid",
|
|
Azure: &Azure{
|
|
AttestationURL: "test-maaUrl",
|
|
},
|
|
Name: "constell-test-uid",
|
|
},
|
|
ClusterValues: ClusterValues{
|
|
ClusterID: "test-cluster-id",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
state := NewFromIDFile(tc.idFile, tc.cfg)
|
|
|
|
assert.Equal(tc.expected, state)
|
|
})
|
|
}
|
|
}
|