2022-12-25 12:49:45 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
package server
|
2022-12-25 12:49:45 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-06 06:04:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
2022-12-25 12:49:45 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/upgrade-agent/upgradeproto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVersionVerifier(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
versionString string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"valid version": {
|
|
|
|
versionString: "v1.1.1",
|
|
|
|
},
|
|
|
|
"v prefix missing": {
|
|
|
|
versionString: "1.1.1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid space": {
|
|
|
|
versionString: "v 1.1.1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid version": {
|
|
|
|
versionString: "v1.1.1a",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := verifyVersion(tc.versionString)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepareUpdate(t *testing.T) {
|
2023-12-08 12:38:00 -05:00
|
|
|
invalidUpgradeRequest := &upgradeproto.ExecuteUpdateRequest{
|
|
|
|
WantedKubernetesVersion: "1337",
|
|
|
|
}
|
|
|
|
slimUpdateRequest := &upgradeproto.ExecuteUpdateRequest{
|
|
|
|
WantedKubernetesVersion: "v1.1.1",
|
|
|
|
}
|
2024-01-29 15:32:37 -05:00
|
|
|
updateRequest := &upgradeproto.ExecuteUpdateRequest{
|
2022-12-25 12:49:45 -05:00
|
|
|
WantedKubernetesVersion: "v1.1.1",
|
2023-12-08 12:38:00 -05:00
|
|
|
KubernetesComponents: []*components.Component{
|
|
|
|
{
|
|
|
|
Url: "http://example.com/kubeadm",
|
|
|
|
Hash: "sha256:foo",
|
|
|
|
InstallPath: "/tmp/kubeadm",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
combinedStyleUpdateRequest := &upgradeproto.ExecuteUpdateRequest{
|
|
|
|
WantedKubernetesVersion: "v1.1.1",
|
|
|
|
KubernetesComponents: []*components.Component{
|
|
|
|
{
|
|
|
|
Url: "data:application/octet-stream,foo",
|
|
|
|
InstallPath: "/tmp/foo",
|
|
|
|
},
|
|
|
|
},
|
2022-12-25 12:49:45 -05:00
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
installer osInstaller
|
|
|
|
updateRequest *upgradeproto.ExecuteUpdateRequest
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"works": {
|
|
|
|
installer: stubOsInstaller{},
|
2023-12-08 12:38:00 -05:00
|
|
|
updateRequest: slimUpdateRequest,
|
2022-12-25 12:49:45 -05:00
|
|
|
},
|
|
|
|
"invalid version string": {
|
|
|
|
installer: stubOsInstaller{},
|
2023-12-08 12:38:00 -05:00
|
|
|
updateRequest: invalidUpgradeRequest,
|
2022-12-25 12:49:45 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"install error": {
|
|
|
|
installer: stubOsInstaller{InstallErr: fmt.Errorf("install error")},
|
2024-01-29 15:32:37 -05:00
|
|
|
updateRequest: updateRequest,
|
2023-12-08 12:38:00 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"new style works": {
|
|
|
|
installer: stubOsInstaller{},
|
2024-01-29 15:32:37 -05:00
|
|
|
updateRequest: updateRequest,
|
2023-12-08 12:38:00 -05:00
|
|
|
},
|
|
|
|
"new style install error": {
|
|
|
|
installer: stubOsInstaller{InstallErr: fmt.Errorf("install error")},
|
2024-01-29 15:32:37 -05:00
|
|
|
updateRequest: updateRequest,
|
2023-12-08 12:38:00 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"combined style works": {
|
|
|
|
installer: stubOsInstaller{},
|
|
|
|
updateRequest: combinedStyleUpdateRequest,
|
|
|
|
},
|
|
|
|
"combined style install error": {
|
|
|
|
installer: stubOsInstaller{InstallErr: fmt.Errorf("install error")},
|
|
|
|
updateRequest: combinedStyleUpdateRequest,
|
2022-12-25 12:49:45 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
err := prepareUpdate(context.Background(), tc.installer, tc.updateRequest)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubOsInstaller struct {
|
|
|
|
InstallErr error
|
|
|
|
}
|
|
|
|
|
2023-12-11 02:08:55 -05:00
|
|
|
func (s stubOsInstaller) Install(_ context.Context, _ *components.Component) error {
|
2022-12-25 12:49:45 -05:00
|
|
|
return s.InstallErr
|
|
|
|
}
|