Skip to content

Commit

Permalink
Fix the unit test error and add a juadgement for fetching object to a…
Browse files Browse the repository at this point in the history
…void the error

Signed-off-by: Ye Cao <[email protected]>
  • Loading branch information
dashanji committed Dec 29, 2023
1 parent 7575eee commit bf686bf
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
6 changes: 3 additions & 3 deletions k8s/cmd/commands/deploy/deploy_vineyard_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func GetVineyardDeploymentObjectsFromTemplate() ([]*unstructured.Unstructured, e
return etcdConfig
},
"toYaml": func(v interface{}) string {
bs, error := yaml.Marshal(v)
if error != nil {
log.Error(error, "failed to marshal object %v to yaml", v)
bs, err := yaml.Marshal(v)
if err != nil {
log.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
Expand Down
1 change: 1 addition & 0 deletions k8s/cmd/commands/deploy/deploy_vineyard_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func TestGetVineyardDeploymentObjectsFromTemplate_third(t *testing.T) {
"limits": nil,
"requests": nil,
},
"securityContext": map[string]interface{}{},
"command": []interface{}{
"/bin/bash",
"-c",
Expand Down
7 changes: 4 additions & 3 deletions k8s/cmd/commands/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ var (
resources:
limits: null
requests: null
securityContext: {}
volumeMounts:
- mountPath: /var/run
name: vineyard-socket
Expand Down Expand Up @@ -487,9 +488,9 @@ func GetManifestFromTemplate(workload string) (OutputManifests, error) {
return etcdConfig
},
"toYaml": func(v interface{}) string {
bs, error := yaml.Marshal(v)
if error != nil {
log.Error(error, "failed to marshal object %v to yaml", v)
bs, err := yaml.Marshal(v)
if err != nil {
log.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
Expand Down
2 changes: 2 additions & 0 deletions k8s/cmd/commands/inject/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ spec:
resources:
limits: null
requests: null
securityContext: {}
volumeMounts:
- mountPath: /var/run
name: vineyard-socket
Expand Down Expand Up @@ -303,6 +304,7 @@ spec:
resources:
limits: null
requests: null
securityContext: {}
volumeMounts:
- mountPath: /var/run
name: vineyard-socket
Expand Down
50 changes: 50 additions & 0 deletions k8s/cmd/commands/util/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package util
import (
_ "embed"
"reflect"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"

"github.com/v6d-io/v6d/k8s/apis/k8s/v1alpha1"
"github.com/v6d-io/v6d/k8s/cmd/commands/flags"
Expand Down Expand Up @@ -57,6 +59,18 @@ func Test_RenderManifestAsObj(t *testing.T) {
"getEtcdConfig": func() k8s.EtcdConfig {
return etcdConfig
},
"toYaml": func(v interface{}) string {
bs, err := yaml.Marshal(v)
if err != nil {
t.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
},
"indent": func(spaces int, s string) string {
prefix := strings.Repeat(" ", spaces)
return prefix + strings.Replace(s, "\n", "\n"+prefix, -1)
},
},
},
want: &unstructured.Unstructured{
Expand Down Expand Up @@ -152,6 +166,18 @@ func TestBuildObjsFromEtcdManifests(t *testing.T) {
"getEtcdConfig": func() k8s.EtcdConfig {
return etcdConfig
},
"toYaml": func(v interface{}) string {
bs, err := yaml.Marshal(v)
if err != nil {
t.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
},
"indent": func(spaces int, s string) string {
prefix := strings.Repeat(" ", spaces)
return prefix + strings.Replace(s, "\n", "\n"+prefix, -1)
},
}
vineyardd := value
name := vineyardd.Name
Expand Down Expand Up @@ -215,6 +241,18 @@ func TestBuildObjsFromVineyarddManifests(t *testing.T) {
"getEtcdConfig": func() k8s.EtcdConfig {
return etcdConfig
},
"toYaml": func(v interface{}) string {
bs, err := yaml.Marshal(v)
if err != nil {
t.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
},
"indent": func(spaces int, s string) string {
prefix := strings.Repeat(" ", spaces)
return prefix + strings.Replace(s, "\n", "\n"+prefix, -1)
},
}

objs, err := BuildObjsFromVineyarddManifests(files, value, tmplFunc)
Expand Down Expand Up @@ -271,6 +309,18 @@ func TestBuildObjsFromManifests(t *testing.T) {
"getBackupConfig": func() k8s.BackupConfig {
return backupCfg
},
"toYaml": func(v interface{}) string {
bs, err := yaml.Marshal(v)
if err != nil {
t.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
},
"indent": func(spaces int, s string) string {
prefix := strings.Repeat(" ", spaces)
return prefix + strings.Replace(s, "\n", "\n"+prefix, -1)
},
}

objs, err := BuildObjsFromManifests(templateName, backup, tmplFunc)
Expand Down
6 changes: 3 additions & 3 deletions k8s/controllers/k8s/sidecar_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func (r *SidecarReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
TmplFunc: map[string]interface{}{
"getEtcdConfig": nil,
"toYaml": func(v interface{}) string {
bs, error := yaml.Marshal(v)
if error != nil {
logger.Error(error, "failed to marshal object %v to yaml", v)
bs, err := yaml.Marshal(v)
if err != nil {
logger.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
Expand Down
6 changes: 3 additions & 3 deletions k8s/controllers/k8s/vineyardd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (r *VineyarddReconciler) Reconcile(
return q.String()
},
"toYaml": func(v interface{}) string {
bs, error := yaml.Marshal(v)
if error != nil {
logger.Error(error, "failed to marshal object %v to yaml", v)
bs, err := yaml.Marshal(v)
if err != nil {
logger.Error(err, "failed to marshal object %v to yaml", v)
return ""
}
return string(bs)
Expand Down
2 changes: 1 addition & 1 deletion python/vineyard/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def _fetch_object(self, object_id: ObjectID) -> Object:
meta = self.get_meta(object_id)

if self.has_ipc_client():
if meta.instance_id == self._ipc_client.instance_id:
if meta.instance_id == self._ipc_client.instance_id or meta.isglobal:
return self._ipc_client.get_object(object_id, fetch=False)
else:
warnings.warn(
Expand Down

0 comments on commit bf686bf

Please sign in to comment.