mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-26 07:59:37 -05:00
a68ee817ff
AB#2074: Add configurable k8s version Configurable version flow: * cli config holds/validates k8sVersion * InitCluster receive a k8sVersion arg * InitCluster creates CM "k8s-version" * kubeadm's InitConfiguration receives k8sVersion * joinservice spec mounts/reads k8s-version CM * joinservice supplies k8sVersion via JoinTicketResponse Other changes: * Remove unused test code (FakeK8SClient) * move VersionConfig map to /internal/versions * installk8sComponents is now a function instead of a method
123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
package kubectl
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/internal/kubernetes/k8sapi/resources"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/goleak"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/cli-runtime/pkg/resource"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
goleak.VerifyTestMain(m)
|
|
}
|
|
|
|
type stubClient struct {
|
|
applyOneObjectErr error
|
|
getObjectsInfos []*resource.Info
|
|
getObjectsErr error
|
|
createConfigMapErr 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
|
|
}
|
|
|
|
func (s *stubClient) CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error {
|
|
return s.createConfigMapErr
|
|
}
|
|
|
|
type stubClientGenerator struct {
|
|
applyOneObjectErr error
|
|
getObjectsInfos []*resource.Info
|
|
getObjectsErr error
|
|
newClientErr error
|
|
createConfigMapErr error
|
|
}
|
|
|
|
func (s *stubClientGenerator) NewClient(kubeconfig []byte) (Client, error) {
|
|
return &stubClient{
|
|
s.applyOneObjectErr,
|
|
s.getObjectsInfos,
|
|
s.getObjectsErr,
|
|
s.createConfigMapErr,
|
|
}, 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)
|
|
}
|