2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
2022-07-18 06:28:02 -04:00
|
|
|
"context"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-07-18 06:28:02 -04:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-03-22 11:03:15 -04:00
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubClient struct {
|
2022-07-14 15:15:31 -04:00
|
|
|
applyOneObjectErr error
|
|
|
|
getObjectsInfos []*resource.Info
|
|
|
|
getObjectsErr error
|
|
|
|
createConfigMapErr error
|
|
|
|
addTolerationsToDeploymentErr error
|
2022-07-26 04:10:34 -04:00
|
|
|
addNodeSelectorToDeploymentErr error
|
2022-08-04 10:15:52 -04:00
|
|
|
waitForCRDErr error
|
2022-11-04 07:36:26 -04:00
|
|
|
listAllNamespacesResp *corev1.NamespaceList
|
|
|
|
listAllNamespacesErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClient) ApplyOneObject(info *resource.Info, forceConflicts bool) error {
|
|
|
|
return s.applyOneObjectErr
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
func (s *stubClient) GetObjects(resources kubernetes.Marshaler) ([]*resource.Info, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return s.getObjectsInfos, s.getObjectsErr
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:28:02 -04:00
|
|
|
func (s *stubClient) CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error {
|
|
|
|
return s.createConfigMapErr
|
|
|
|
}
|
|
|
|
|
2022-07-28 10:07:29 -04:00
|
|
|
func (s *stubClient) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
|
2022-07-14 15:15:31 -04:00
|
|
|
return s.addTolerationsToDeploymentErr
|
|
|
|
}
|
|
|
|
|
2022-07-28 10:07:29 -04:00
|
|
|
func (s *stubClient) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
|
2022-07-26 04:10:34 -04:00
|
|
|
return s.addNodeSelectorToDeploymentErr
|
|
|
|
}
|
|
|
|
|
2022-11-04 07:36:26 -04:00
|
|
|
func (s *stubClient) ListAllNamespaces(ctx context.Context) (*corev1.NamespaceList, error) {
|
|
|
|
return s.listAllNamespacesResp, s.listAllNamespacesErr
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubClientGenerator struct {
|
2022-07-14 15:15:31 -04:00
|
|
|
applyOneObjectErr error
|
|
|
|
getObjectsInfos []*resource.Info
|
|
|
|
getObjectsErr error
|
|
|
|
newClientErr error
|
|
|
|
createConfigMapErr error
|
|
|
|
addTolerationsToDeploymentErr error
|
2022-07-26 04:10:34 -04:00
|
|
|
addNodeSelectorToDeploymentErr error
|
2022-08-04 10:15:52 -04:00
|
|
|
waitForCRDErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClient) WaitForCRD(ctx context.Context, crd string) error {
|
|
|
|
return s.waitForCRDErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClientGenerator) NewClient(kubeconfig []byte) (Client, error) {
|
|
|
|
return &stubClient{
|
2022-08-04 10:15:52 -04:00
|
|
|
applyOneObjectErr: s.applyOneObjectErr,
|
|
|
|
getObjectsInfos: s.getObjectsInfos,
|
|
|
|
getObjectsErr: s.getObjectsErr,
|
|
|
|
createConfigMapErr: s.createConfigMapErr,
|
|
|
|
addTolerationsToDeploymentErr: s.addTolerationsToDeploymentErr,
|
|
|
|
addNodeSelectorToDeploymentErr: s.addNodeSelectorToDeploymentErr,
|
|
|
|
waitForCRDErr: s.waitForCRDErr,
|
2022-03-22 11:03:15 -04:00
|
|
|
}, s.newClientErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyResource struct{}
|
|
|
|
|
|
|
|
func (*dummyResource) Marshal() ([]byte, error) {
|
|
|
|
panic("dummy")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyWorks(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
kube := Kubectl{
|
|
|
|
clientGenerator: &stubClientGenerator{
|
|
|
|
getObjectsInfos: []*resource.Info{
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
kube.SetKubeconfig([]byte("someConfig"))
|
|
|
|
|
|
|
|
assert.NoError(kube.Apply(&dummyResource{}, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKubeconfigUnset(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
kube := Kubectl{}
|
|
|
|
|
|
|
|
assert.ErrorIs(kube.Apply(&dummyResource{}, true), ErrKubeconfigNotSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientGeneratorFails(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := errors.New("generator failed")
|
|
|
|
kube := Kubectl{
|
|
|
|
clientGenerator: &stubClientGenerator{
|
|
|
|
newClientErr: err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
kube.SetKubeconfig([]byte("someConfig"))
|
|
|
|
|
|
|
|
assert.ErrorIs(kube.Apply(&dummyResource{}, true), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetObjectsFails(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := errors.New("getObjects failed")
|
|
|
|
kube := Kubectl{
|
|
|
|
clientGenerator: &stubClientGenerator{
|
|
|
|
getObjectsErr: err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
kube.SetKubeconfig([]byte("someConfig"))
|
|
|
|
|
|
|
|
assert.ErrorIs(kube.Apply(&dummyResource{}, true), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyOneObjectFails(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
err := errors.New("applyOneObject failed")
|
|
|
|
kube := Kubectl{
|
|
|
|
clientGenerator: &stubClientGenerator{
|
|
|
|
getObjectsInfos: []*resource.Info{
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
applyOneObjectErr: err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
kube.SetKubeconfig([]byte("someConfig"))
|
|
|
|
|
|
|
|
assert.ErrorIs(kube.Apply(&dummyResource{}, true), err)
|
|
|
|
}
|