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 (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseJoinCommand(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
joinCommand string
|
|
|
|
wantJoinArgs kubeadm.BootstrapTokenDiscovery
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"join command can be parsed": {
|
|
|
|
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIServerEndpoint: "192.0.2.0:8443",
|
|
|
|
Token: "dummy-token",
|
|
|
|
CACertHashes: []string{"sha512:dummy-hash"},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"incorrect join command returns error": {
|
|
|
|
joinCommand: "some string",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"missing api server endpoint is checked": {
|
|
|
|
joinCommand: "kubeadm join --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"missing token is checked": {
|
|
|
|
joinCommand: "kubeadm join 192.0.2.0:8443 --discovery-token-ca-cert-hash sha512:dummy-hash --control-plane",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"missing discovery-token-ca-cert-hash is checked": {
|
|
|
|
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --control-plane",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"missing control-plane": {
|
|
|
|
joinCommand: "kubeadm join 192.0.2.0:8443 --token dummy-token --discovery-token-ca-cert-hash sha512:dummy-hash",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantJoinArgs: kubeadm.BootstrapTokenDiscovery{
|
2022-03-22 11:03:15 -04:00
|
|
|
APIServerEndpoint: "192.0.2.0:8443",
|
|
|
|
Token: "dummy-token",
|
|
|
|
CACertHashes: []string{"sha512:dummy-hash"},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
joinArgs, err := ParseJoinCommand(tc.joinCommand)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(&tc.wantJoinArgs, joinArgs)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|