Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
JameKeal committed Jun 12, 2023
1 parent 76145b3 commit b7ad495
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 145 deletions.
107 changes: 0 additions & 107 deletions charts/openyurt/templates/yurthub-staticpod.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions charts/yurthub/templates/yurthub-cloud-yurtstaticset.yaml
Expand Up @@ -42,6 +42,9 @@ spec:
{{- if .Values.organizations }}
- --hub-cert-organizations={{ .Values.organizations }}
{{- end }}
{{- if .nodePoolName }}
- --nodepool-name={{ .Values.nodePoolName }}
{{- end }}
livenessProbe:
httpGet:
host: {{ .Values.yurthubBindingAddr }}
Expand Down
3 changes: 3 additions & 0 deletions charts/yurthub/templates/yurthub-yurtstaticset.yaml
Expand Up @@ -42,6 +42,9 @@ spec:
{{- if .Values.organizations }}
- --hub-cert-organizations={{ .Values.organizations }}
{{- end }}
{{- if .nodePoolName }}
- --nodepool-name={{ .Values.nodePoolName }}
{{- end }}
livenessProbe:
httpGet:
host: {{ .Values.yurthubBindingAddr }}
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/kubeconfig/kubeconfig_test.go
Expand Up @@ -183,6 +183,8 @@ func TestWriteKubeconfigToDisk(t *testing.T) {
newFile,
)
}
client, err := ToYurtClientSet(c)
t.Log(client, err)
})
}
}
25 changes: 0 additions & 25 deletions pkg/util/kubernetes/kubeadm/app/util/apiclient/idempotency.go
Expand Up @@ -154,28 +154,3 @@ func GetNodePoolInfoWithRetry(client yurtclientset.Interface, name string) (*nod
}
return nil, lastError
}

func JoinNodeInSpecifiedNodePool(client clientset.Interface, nodeName, nodePoolName string) error {
var node *v1.Node
var lastError error
err := wait.ExponentialBackoff(clientsetretry.DefaultBackoff, func() (bool, error) {
var err error
node, err = client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
lastError = err
return false, nil
}

node.Labels[nodepoolv1alpha1.LabelDesiredNodePool] = nodePoolName
_, err = client.CoreV1().Nodes().Update(context.TODO(), node, metav1.UpdateOptions{})
if err != nil {
lastError = err
return false, nil
}
return true, nil
})
if err == nil {
return nil
}
return lastError
}
3 changes: 3 additions & 0 deletions pkg/yurtadm/cmd/join/join.go
Expand Up @@ -38,6 +38,7 @@ import (
yurtconstants "github.com/openyurtio/openyurt/pkg/yurtadm/constants"
"github.com/openyurtio/openyurt/pkg/yurtadm/util/edgenode"
yurtadmutil "github.com/openyurtio/openyurt/pkg/yurtadm/util/kubernetes"
nodepoolv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1"
)

type joinOptions struct {
Expand Down Expand Up @@ -341,6 +342,8 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
// the specified nodePool not exist, return
return nil, errors.Errorf("when --nodepool-name is specified, the specified nodePool should be exist.")
}

Check warning on line 344 in pkg/yurtadm/cmd/join/join.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtadm/cmd/join/join.go#L340-L344

Added lines #L340 - L344 were not covered by tests
// add nodePool label for node by kubelet
data.nodeLabels[nodepoolv1alpha1.LabelDesiredNodePool] = opt.nodePoolName

Check warning on line 346 in pkg/yurtadm/cmd/join/join.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtadm/cmd/join/join.go#L346

Added line #L346 was not covered by tests
}
klog.Infof("node join data info: %#+v", *data)

Expand Down
12 changes: 11 additions & 1 deletion pkg/yurtadm/cmd/join/join_test.go
Expand Up @@ -235,6 +235,10 @@ func TestRun(t *testing.T) {

func TestNewJoinData(t *testing.T) {
jo := newJoinOptions()
jo2 := newJoinOptions()
jo2.token = "v22u0b.17490yh3xp8azpr0"
jo2.unsafeSkipCAVerification = true
jo2.nodePoolName = "nodePool2"

tests := []struct {
name string
Expand All @@ -244,10 +248,16 @@ func TestNewJoinData(t *testing.T) {
}{
{
"normal",
[]string{},
[]string{"localhost:8080"},
jo,
nil,
},
{
"norma2",
[]string{"localhost:8080"},
jo2,
nil,
},
}

for _, tt := range tests {
Expand Down
13 changes: 1 addition & 12 deletions pkg/yurtadm/cmd/join/phases/postcheck.go
Expand Up @@ -19,14 +19,12 @@ package phases
import (
"k8s.io/klog/v2"

"github.com/openyurtio/openyurt/pkg/util/kubernetes/kubeadm/app/util/apiclient"
"github.com/openyurtio/openyurt/pkg/yurtadm/cmd/join/joindata"
"github.com/openyurtio/openyurt/pkg/yurtadm/util/kubernetes"
"github.com/openyurtio/openyurt/pkg/yurtadm/util/yurthub"
)

// RunPostCheck executes the node health check and clean process,
// if specified nodePool, it will join node in specified nodePool.
// RunPostCheck executes the node health check and clean process.
func RunPostCheck(data joindata.YurtJoinData) error {
klog.V(1).Infof("check kubelet status.")
if err := kubernetes.CheckKubeletStatus(); err != nil {
Expand All @@ -40,15 +38,6 @@ func RunPostCheck(data joindata.YurtJoinData) error {
}
klog.V(1).Infof("hub agent is ready")

if len(data.NodeRegistration().NodePoolName) != 0 {
klog.V(1).Infof("starting join node in specified nodePool.")
if err := apiclient.JoinNodeInSpecifiedNodePool(data.BootstrapClient(),
data.NodeRegistration().Name, data.NodeRegistration().NodePoolName); err != nil {
return err
}
klog.V(1).Infof("join node in specified nodePool successful.")
}

if err := yurthub.CleanHubBootstrapConfig(); err != nil {
return err
}
Expand Down
109 changes: 109 additions & 0 deletions pkg/yurtadm/util/yurthub/yurthub_test.go
Expand Up @@ -20,6 +20,11 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"github.com/openyurtio/openyurt/pkg/yurtadm/cmd/join/joindata"
)

var (
Expand Down Expand Up @@ -225,3 +230,107 @@ func Test_useRealServerAddr(t *testing.T) {
})
}
}

type testData struct {
joinNodeData *joindata.NodeRegistration
}

func (j *testData) ServerAddr() string {
return ""
}

func (j *testData) JoinToken() string {
return ""
}

func (j *testData) PauseImage() string {
return ""
}

func (j *testData) YurtHubImage() string {
return ""
}

func (j *testData) YurtHubServer() string {
return ""
}

func (j *testData) YurtHubTemplate() string {
return ""
}

func (j *testData) YurtHubManifest() string {
return ""
}

func (j *testData) KubernetesVersion() string {
return ""
}

func (j *testData) TLSBootstrapCfg() *clientcmdapi.Config {
return nil
}

func (j *testData) BootstrapClient() *clientset.Clientset {
return nil
}

func (j *testData) NodeRegistration() *joindata.NodeRegistration {
return j.joinNodeData
}

func (j *testData) IgnorePreflightErrors() sets.String {
return nil
}

func (j *testData) CaCertHashes() []string {
return nil
}

func (j *testData) NodeLabels() map[string]string {
return nil
}

func (j *testData) KubernetesResourceServer() string {
return ""
}

func (j *testData) ReuseCNIBin() bool {
return false
}

func (j *testData) Namespace() string {
return ""
}

func TestAddYurthubStaticYaml(t *testing.T) {
xdata := testData{
joinNodeData: &joindata.NodeRegistration{
Name: "name1",
NodePoolName: "nodePool1",
CRISocket: "",
WorkingMode: "edge",
Organizations: "",
}}

tests := []struct {
name string
data testData
podManifestPath string
wantErr bool
}{
{
name: "test",
data: xdata,
podManifestPath: "/tmp",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := AddYurthubStaticYaml(&tt.data, tt.podManifestPath); (err != nil) != tt.wantErr {
t.Errorf("AddYurthubStaticYaml() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit b7ad495

Please sign in to comment.