2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpdateMeasurements(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
updater *stubMeasurementsUpdater
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements measurements.M
|
2022-08-29 10:49:44 -04:00
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
updater: &stubMeasurementsUpdater{
|
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.MeasurementsFilename: `{"0":"AAAAAA=="}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements: measurements.M{
|
2022-08-29 10:49:44 -04:00
|
|
|
0: []byte("1"),
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
|
|
|
"measurements are the same": {
|
|
|
|
updater: &stubMeasurementsUpdater{
|
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.MeasurementsFilename: `{"0":"MQ=="}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements: measurements.M{
|
2022-08-29 10:49:44 -04:00
|
|
|
0: []byte("1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"getCurrent error": {
|
|
|
|
updater: &stubMeasurementsUpdater{getErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"update error": {
|
|
|
|
updater: &stubMeasurementsUpdater{
|
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.MeasurementsFilename: `{"0":"AAAAAA=="}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
updateErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
upgrader := &Upgrader{
|
|
|
|
measurementsUpdater: tc.updater,
|
2022-11-10 04:27:24 -05:00
|
|
|
outWriter: &bytes.Buffer{},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := upgrader.updateMeasurements(context.Background(), tc.newMeasurements)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.ErrorIs(err, someErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantUpdate {
|
|
|
|
newMeasurementsJSON, err := json.Marshal(tc.newMeasurements)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(string(newMeasurementsJSON), tc.updater.updatedMeasurements.Data[constants.MeasurementsFilename])
|
|
|
|
} else {
|
|
|
|
assert.Nil(tc.updater.updatedMeasurements)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubMeasurementsUpdater struct {
|
|
|
|
oldMeasurements *corev1.ConfigMap
|
|
|
|
updatedMeasurements *corev1.ConfigMap
|
|
|
|
getErr error
|
|
|
|
updateErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubMeasurementsUpdater) getCurrent(context.Context, string) (*corev1.ConfigMap, error) {
|
|
|
|
return u.oldMeasurements, u.getErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubMeasurementsUpdater) update(_ context.Context, updatedMeasurements *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
|
|
|
u.updatedMeasurements = updatedMeasurements
|
|
|
|
return nil, u.updateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateImage(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
updater *stubImageUpdater
|
|
|
|
newImage string
|
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-08-29 10:49:44 -04:00
|
|
|
"image": "old-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newImage: "new-image",
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
|
|
|
"image is the same": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-08-29 10:49:44 -04:00
|
|
|
"image": "old-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newImage: "old-image",
|
|
|
|
},
|
|
|
|
"getCurrent error": {
|
|
|
|
updater: &stubImageUpdater{getErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"update error": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-08-29 10:49:44 -04:00
|
|
|
"image": "old-image",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
updateErr: someErr,
|
|
|
|
},
|
|
|
|
newImage: "new-image",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"no spec": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{},
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
newImage: "new-image",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"not a map": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
2022-08-29 10:49:44 -04:00
|
|
|
"spec": "not a map",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newImage: "new-image",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"no spec.image": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{},
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newImage: "new-image",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
upgrader := &Upgrader{
|
|
|
|
imageUpdater: tc.updater,
|
2022-11-10 04:27:24 -05:00
|
|
|
outWriter: &bytes.Buffer{},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := upgrader.updateImage(context.Background(), tc.newImage)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantUpdate {
|
2022-10-25 09:51:23 -04:00
|
|
|
assert.Equal(tc.newImage, tc.updater.updatedImage.Object["spec"].(map[string]any)["image"])
|
2022-08-29 10:49:44 -04:00
|
|
|
} else {
|
|
|
|
assert.Nil(tc.updater.updatedImage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubImageUpdater struct {
|
|
|
|
setImage *unstructured.Unstructured
|
|
|
|
updatedImage *unstructured.Unstructured
|
|
|
|
getErr error
|
|
|
|
updateErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubImageUpdater) getCurrent(ctx context.Context, name string) (*unstructured.Unstructured, error) {
|
|
|
|
return u.setImage, u.getErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubImageUpdater) update(_ context.Context, updatedImage *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
|
|
|
u.updatedImage = updatedImage
|
|
|
|
return nil, u.updateErr
|
|
|
|
}
|