mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-29 01:16:12 -05:00
2d8fcd9bf4
Co-authored-by: Malte Poll <mp@edgeless.systems> Co-authored-by: katexochen <katexochen@users.noreply.github.com> Co-authored-by: Daniel Weiße <dw@edgeless.systems> Co-authored-by: Thomas Tendyck <tt@edgeless.systems> Co-authored-by: Benedict Schlueter <bs@edgeless.systems> Co-authored-by: leongross <leon.gross@rub.de> Co-authored-by: Moritz Eckert <m1gh7ym0@gmail.com>
109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
package kubectl
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/kubernetes/k8sapi/resources"
|
|
"github.com/stretchr/testify/assert"
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
)
|
|
|
|
type stubClient struct {
|
|
applyOneObjectErr error
|
|
getObjectsInfos []*resource.Info
|
|
getObjectsErr error
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type stubClientGenerator struct {
|
|
applyOneObjectErr error
|
|
getObjectsInfos []*resource.Info
|
|
getObjectsErr error
|
|
newClientErr error
|
|
}
|
|
|
|
func (s *stubClientGenerator) NewClient(kubeconfig []byte) (Client, error) {
|
|
return &stubClient{
|
|
s.applyOneObjectErr,
|
|
s.getObjectsInfos,
|
|
s.getObjectsErr,
|
|
}, 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)
|
|
}
|