mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-10-01 01:36:09 -04:00
13eea1ca31
* add wait and restartDS * cilium working (tested on azure + gcp) * clean helm code from bootstrapper * fixup! clean helm code from bootstrapper * fixup! clean helm code from bootstrapper * fixup! clean helm code from bootstrapper * add patchnode for gcp * fix gcp * patch node inside bootstrapper * apply renaming of client * fixup! apply renaming of client * otto feedback
197 lines
5.7 KiB
Go
197 lines
5.7 KiB
Go
/*
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package helm
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/afero"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
func TestBackupCRDs(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
upgradeID string
|
|
crd string
|
|
expectedFile string
|
|
getCRDsError error
|
|
wantError bool
|
|
}{
|
|
"success": {
|
|
upgradeID: "1234",
|
|
crd: "apiVersion: \nkind: \nmetadata:\n name: foobar\n creationTimestamp: null\nspec:\n group: \"\"\n names:\n kind: \"somename\"\n plural: \"somenames\"\n scope: \"\"\n versions: null\nstatus:\n acceptedNames:\n kind: \"\"\n plural: \"\"\n conditions: null\n storedVersions: null\n",
|
|
expectedFile: "apiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n name: foobar\n creationTimestamp: null\nspec:\n group: \"\"\n names:\n kind: \"somename\"\n plural: \"somenames\"\n scope: \"\"\n versions: null\nstatus:\n acceptedNames:\n kind: \"\"\n plural: \"\"\n conditions: null\n storedVersions: null\n",
|
|
},
|
|
"api request fails": {
|
|
upgradeID: "1234",
|
|
getCRDsError: errors.New("api error"),
|
|
wantError: true,
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
memFs := afero.NewMemMapFs()
|
|
|
|
crd := apiextensionsv1.CustomResourceDefinition{}
|
|
err := yaml.Unmarshal([]byte(tc.crd), &crd)
|
|
require.NoError(err)
|
|
client := UpgradeClient{
|
|
config: nil,
|
|
kubectl: stubCrdClient{crds: []apiextensionsv1.CustomResourceDefinition{crd}, getCRDsError: tc.getCRDsError},
|
|
fs: file.NewHandler(memFs),
|
|
log: stubLog{},
|
|
}
|
|
|
|
_, err = client.backupCRDs(context.Background(), tc.upgradeID)
|
|
if tc.wantError {
|
|
assert.Error(err)
|
|
return
|
|
}
|
|
assert.NoError(err)
|
|
|
|
data, err := afero.ReadFile(memFs, filepath.Join(client.crdBackupFolder(tc.upgradeID), crd.Name+".yaml"))
|
|
require.NoError(err)
|
|
assert.YAMLEq(tc.expectedFile, string(data))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBackupCRs(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
upgradeID string
|
|
crd apiextensionsv1.CustomResourceDefinition
|
|
resource unstructured.Unstructured
|
|
expectedFile string
|
|
getCRsError error
|
|
wantError bool
|
|
}{
|
|
"success": {
|
|
upgradeID: "1234",
|
|
crd: apiextensionsv1.CustomResourceDefinition{
|
|
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
|
|
Names: apiextensionsv1.CustomResourceDefinitionNames{
|
|
Plural: "foobars",
|
|
},
|
|
Group: "some.group",
|
|
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
|
|
{
|
|
Name: "versionZero",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
resource: unstructured.Unstructured{Object: map[string]any{"metadata": map[string]any{"name": "foobar"}}},
|
|
expectedFile: "metadata:\n name: foobar\n",
|
|
},
|
|
"api request fails": {
|
|
upgradeID: "1234",
|
|
crd: apiextensionsv1.CustomResourceDefinition{
|
|
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
|
|
Names: apiextensionsv1.CustomResourceDefinitionNames{
|
|
Plural: "foobars",
|
|
},
|
|
Group: "some.group",
|
|
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
|
|
{
|
|
Name: "versionZero",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
getCRsError: errors.New("api error"),
|
|
wantError: true,
|
|
},
|
|
"custom resource not found": {
|
|
upgradeID: "1234",
|
|
crd: apiextensionsv1.CustomResourceDefinition{
|
|
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
|
|
Names: apiextensionsv1.CustomResourceDefinitionNames{
|
|
Plural: "foobars",
|
|
},
|
|
Group: "some.group",
|
|
Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
|
|
{
|
|
Name: "versionZero",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
getCRsError: k8serrors.NewNotFound(schema.GroupResource{Group: "some.group"}, "foobars"),
|
|
},
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
memFs := afero.NewMemMapFs()
|
|
|
|
client := UpgradeClient{
|
|
config: nil,
|
|
kubectl: stubCrdClient{crs: []unstructured.Unstructured{tc.resource}, getCRsError: tc.getCRsError},
|
|
fs: file.NewHandler(memFs),
|
|
log: stubLog{},
|
|
}
|
|
|
|
err := client.backupCRs(context.Background(), []apiextensionsv1.CustomResourceDefinition{tc.crd}, tc.upgradeID)
|
|
if tc.wantError {
|
|
assert.Error(err)
|
|
return
|
|
}
|
|
assert.NoError(err)
|
|
|
|
data, err := afero.ReadFile(memFs, filepath.Join(client.backupFolder(tc.upgradeID), tc.crd.Spec.Group, tc.crd.Spec.Versions[0].Name, tc.resource.GetNamespace(), tc.resource.GetKind(), tc.resource.GetName()+".yaml"))
|
|
if tc.expectedFile == "" {
|
|
assert.Error(err)
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
assert.YAMLEq(tc.expectedFile, string(data))
|
|
})
|
|
}
|
|
}
|
|
|
|
type stubLog struct{}
|
|
|
|
func (s stubLog) Debugf(_ string, _ ...any) {}
|
|
func (s stubLog) Sync() {}
|
|
|
|
type stubCrdClient struct {
|
|
crds []apiextensionsv1.CustomResourceDefinition
|
|
getCRDsError error
|
|
crs []unstructured.Unstructured
|
|
getCRsError error
|
|
crdClient
|
|
}
|
|
|
|
func (c stubCrdClient) GetCRDs(_ context.Context) ([]apiextensionsv1.CustomResourceDefinition, error) {
|
|
if c.getCRDsError != nil {
|
|
return nil, c.getCRDsError
|
|
}
|
|
return c.crds, nil
|
|
}
|
|
|
|
func (c stubCrdClient) GetCRs(_ context.Context, _ schema.GroupVersionResource) ([]unstructured.Unstructured, error) {
|
|
if c.getCRsError != nil {
|
|
return nil, c.getCRsError
|
|
}
|
|
return c.crs, nil
|
|
}
|