2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-11 04:35:17 -04:00
|
|
|
package nodestate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
2022-04-11 04:35:17 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-04-11 04:35:17 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
2024-01-16 11:45:35 -05:00
|
|
|
goleak.VerifyTestMain(m, goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"))
|
2022-06-30 09:24:36 -04:00
|
|
|
}
|
|
|
|
|
2022-04-11 04:35:17 -04:00
|
|
|
func TestFromFile(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
fileContents string
|
|
|
|
wantState *NodeState
|
|
|
|
wantErr bool
|
2022-04-11 04:35:17 -04:00
|
|
|
}{
|
|
|
|
"nodestate exists": {
|
2022-07-26 04:58:39 -04:00
|
|
|
fileContents: `{ "Role": "ControlPlane", "MeasurementSalt": "U2FsdA==" }`,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantState: &NodeState{
|
2022-07-26 04:58:39 -04:00
|
|
|
Role: role.ControlPlane,
|
|
|
|
MeasurementSalt: []byte("Salt"),
|
2022-04-11 04:35:17 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"nodestate file does not exist": {
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 04:35:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
if tc.fileContents != "" {
|
|
|
|
require.NoError(fs.MkdirAll(filepath.Dir(nodeStatePath), 0o755))
|
|
|
|
require.NoError(afero.WriteFile(fs, nodeStatePath, []byte(tc.fileContents), 0o644))
|
|
|
|
}
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
state, err := FromFile(fileHandler)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:35:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantState, state)
|
2022-04-11 04:35:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToFile(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
precreateFile bool
|
|
|
|
state *NodeState
|
2022-04-26 10:54:05 -04:00
|
|
|
wantFile string
|
|
|
|
wantErr bool
|
2022-04-11 04:35:17 -04:00
|
|
|
}{
|
|
|
|
"writing works": {
|
|
|
|
state: &NodeState{
|
2022-07-26 04:58:39 -04:00
|
|
|
Role: role.ControlPlane,
|
|
|
|
MeasurementSalt: []byte("Salt"),
|
2022-04-11 04:35:17 -04:00
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantFile: `{
|
2022-06-29 09:26:29 -04:00
|
|
|
"Role": "ControlPlane",
|
2022-07-26 04:58:39 -04:00
|
|
|
"MeasurementSalt": "U2FsdA=="
|
2022-04-11 04:35:17 -04:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
"file exists already": {
|
|
|
|
precreateFile: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 04:35:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
if tc.precreateFile {
|
2022-04-13 03:18:32 -04:00
|
|
|
require.NoError(fs.MkdirAll(filepath.Dir(nodeStatePath), 0o755))
|
2022-04-11 04:35:17 -04:00
|
|
|
require.NoError(afero.WriteFile(fs, nodeStatePath, []byte("pre-existing"), 0o644))
|
|
|
|
}
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
err := tc.state.ToFile(fileHandler)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:35:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
fileContents, err := afero.ReadFile(fs, nodeStatePath)
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantFile, string(fileContents))
|
2022-04-11 04:35:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|