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-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
|
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-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClient) ApplyOneObject(info *resource.Info, forceConflicts bool) error {
|
|
|
|
return s.applyOneObjectErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClient) GetObjects(resources resources.Marshaler) ([]*resource.Info, error) {
|
|
|
|
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-14 15:15:31 -04:00
|
|
|
func (s *stubClient) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error {
|
|
|
|
return s.addTolerationsToDeploymentErr
|
|
|
|
}
|
|
|
|
|
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-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubClientGenerator) NewClient(kubeconfig []byte) (Client, error) {
|
|
|
|
return &stubClient{
|
|
|
|
s.applyOneObjectErr,
|
|
|
|
s.getObjectsInfos,
|
|
|
|
s.getObjectsErr,
|
2022-07-18 06:28:02 -04:00
|
|
|
s.createConfigMapErr,
|
2022-07-14 15:15:31 -04:00
|
|
|
s.addTolerationsToDeploymentErr,
|
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)
|
|
|
|
}
|