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 k8sapi
|
|
|
|
|
|
|
|
import (
|
2022-07-22 09:05:04 -04:00
|
|
|
"fmt"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
2022-07-22 09:05:04 -04:00
|
|
|
kubeadmUtil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2022-05-04 06:10:23 -04:00
|
|
|
goleak.VerifyTestMain(m)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitConfiguration(t *testing.T) {
|
2022-10-21 04:16:44 -04:00
|
|
|
kubeadmConfig := KubdeadmConfiguration{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
config KubeadmInitYAML
|
|
|
|
}{
|
2022-10-21 04:16:44 -04:00
|
|
|
"kubeadm init config can be created": {
|
|
|
|
config: kubeadmConfig.InitConfiguration(true, versions.Default),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-10-21 04:16:44 -04:00
|
|
|
"kubeadm init config with all fields can be created": {
|
2022-03-22 11:03:15 -04:00
|
|
|
config: func() KubeadmInitYAML {
|
2022-10-21 04:16:44 -04:00
|
|
|
c := kubeadmConfig.InitConfiguration(true, versions.Default)
|
2022-07-08 04:59:59 -04:00
|
|
|
c.SetAPIServerAdvertiseAddress("192.0.2.0")
|
2022-03-22 11:03:15 -04:00
|
|
|
c.SetNodeIP("192.0.2.0")
|
|
|
|
c.SetNodeName("node")
|
|
|
|
c.SetPodNetworkCIDR("10.244.0.0/16")
|
|
|
|
c.SetServiceCIDR("10.245.0.0/24")
|
|
|
|
c.SetProviderID("somecloudprovider://instance-id")
|
|
|
|
return c
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
config, err := tc.config.Marshal()
|
|
|
|
require.NoError(err)
|
|
|
|
tmp, err := tc.config.Unmarshal(config)
|
|
|
|
require.NoError(err)
|
|
|
|
// test on correct mashalling and unmarshalling
|
|
|
|
assert.Equal(tc.config.ClusterConfiguration, tmp.ClusterConfiguration)
|
|
|
|
assert.Equal(tc.config.InitConfiguration, tmp.InitConfiguration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 09:05:04 -04:00
|
|
|
func TestInitConfigurationKubeadmCompatibility(t *testing.T) {
|
2022-10-21 04:16:44 -04:00
|
|
|
kubeadmConfig := KubdeadmConfiguration{}
|
2022-07-22 09:05:04 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
config KubeadmInitYAML
|
|
|
|
expectedVersion string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"Kubeadm accepts version 'Latest'": {
|
2022-10-21 04:16:44 -04:00
|
|
|
config: kubeadmConfig.InitConfiguration(true, versions.Default),
|
2022-09-02 07:57:57 -04:00
|
|
|
expectedVersion: fmt.Sprintf("v%s", versions.VersionConfigs[versions.Default].PatchVersion),
|
2022-07-22 09:05:04 -04:00
|
|
|
},
|
|
|
|
"Kubeadm receives incompatible version": {
|
2022-10-21 04:16:44 -04:00
|
|
|
config: kubeadmConfig.InitConfiguration(true, "1.19"),
|
2022-07-22 09:05:04 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
version, err := kubeadmUtil.KubernetesReleaseVersion(tc.config.ClusterConfiguration.KubernetesVersion)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(tc.expectedVersion, version)
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestJoinConfiguration(t *testing.T) {
|
2022-10-21 04:16:44 -04:00
|
|
|
kubdeadmConfig := KubdeadmConfiguration{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
config KubeadmJoinYAML
|
|
|
|
}{
|
2022-10-21 04:16:44 -04:00
|
|
|
"kubeadm join config can be created": {
|
|
|
|
config: kubdeadmConfig.JoinConfiguration(true),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-10-21 04:16:44 -04:00
|
|
|
"kubeadm join config with all fields can be created": {
|
2022-03-22 11:03:15 -04:00
|
|
|
config: func() KubeadmJoinYAML {
|
2022-10-21 04:16:44 -04:00
|
|
|
c := kubdeadmConfig.JoinConfiguration(true)
|
2022-07-08 04:59:59 -04:00
|
|
|
c.SetAPIServerEndpoint("192.0.2.0:6443")
|
2022-03-22 11:03:15 -04:00
|
|
|
c.SetNodeIP("192.0.2.0")
|
|
|
|
c.SetNodeName("node")
|
|
|
|
c.SetToken("token")
|
|
|
|
c.AppendDiscoveryTokenCaCertHash("discovery-token-ca-cert-hash")
|
|
|
|
c.SetProviderID("somecloudprovider://instance-id")
|
2022-07-11 07:29:22 -04:00
|
|
|
c.SetControlPlane("192.0.2.0")
|
2022-03-22 11:03:15 -04:00
|
|
|
return c
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
config, err := tc.config.Marshal()
|
|
|
|
require.NoError(err)
|
|
|
|
tmp, err := tc.config.Unmarshal(config)
|
|
|
|
require.NoError(err)
|
|
|
|
// test on correct mashalling and unmarshalling
|
|
|
|
assert.Equal(tc.config.JoinConfiguration, tmp.JoinConfiguration)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|