-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
normalize the Service when importing in k8s runtime (#244)
- Loading branch information
Showing
3 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
pkg/engine/runtime/kubernetes/kubernetes_runtime_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
// ignore for test coverage | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"bou.ke/monkey" | ||
yamlv3 "gopkg.in/yaml.v3" | ||
|
||
"kusionstack.io/kusion/pkg/engine/models" | ||
"kusionstack.io/kusion/pkg/engine/runtime" | ||
jsonutil "kusionstack.io/kusion/pkg/util/json" | ||
) | ||
|
||
func TestKubernetesRuntime_Import(t *testing.T) { | ||
planServiceYaml, _ := os.ReadFile("testdata/plan_service.yaml") | ||
planSvc := &models.Resource{} | ||
yamlv3.Unmarshal(planServiceYaml, planSvc) | ||
|
||
lastAppliedYaml, _ := os.ReadFile("testdata/live_service_with_last_applied_annotation.yaml") | ||
lastAppliedObj := make(map[string]interface{}) | ||
yamlv3.Unmarshal(lastAppliedYaml, lastAppliedObj) | ||
|
||
liveSvcYaml, _ := os.ReadFile("testdata/live_service.yaml") | ||
liveSvcObj := make(map[string]interface{}) | ||
yamlv3.Unmarshal(liveSvcYaml, liveSvcObj) | ||
|
||
liveSvcImpYaml, _ := os.ReadFile("testdata/live_service_import_result.yaml") | ||
liveSvcImpObj := make(map[string]interface{}) | ||
yamlv3.Unmarshal(liveSvcImpYaml, liveSvcImpObj) | ||
|
||
svcYaml, _ := os.ReadFile("testdata/service.yaml") | ||
svcObj := make(map[string]interface{}) | ||
yamlv3.Unmarshal(svcYaml, svcObj) | ||
|
||
type args struct { | ||
ctx context.Context | ||
request *runtime.ImportRequest | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *runtime.ImportResponse | ||
}{ | ||
{name: "import-svc-with-last-applied", args: struct { | ||
ctx context.Context | ||
request *runtime.ImportRequest | ||
}{ctx: context.Background(), request: &runtime.ImportRequest{ | ||
PlanResource: planSvc, | ||
}}, want: &runtime.ImportResponse{ | ||
Resource: &models.Resource{ | ||
ID: planSvc.ResourceKey(), | ||
Type: planSvc.Type, | ||
Attributes: svcObj, | ||
DependsOn: planSvc.DependsOn, | ||
Extensions: planSvc.Extensions, | ||
}, | ||
Status: nil, | ||
}}, | ||
{name: "import-svc", args: args{ | ||
ctx: context.Background(), | ||
request: &runtime.ImportRequest{ | ||
PlanResource: planSvc, | ||
}, | ||
}, want: &runtime.ImportResponse{ | ||
Resource: &models.Resource{ | ||
ID: planSvc.ResourceKey(), | ||
Type: planSvc.Type, | ||
Attributes: liveSvcImpObj, | ||
DependsOn: planSvc.DependsOn, | ||
Extensions: planSvc.Extensions, | ||
}, | ||
Status: nil, | ||
}}, | ||
} | ||
|
||
t.Run(tests[0].name, func(t *testing.T) { | ||
k := &KubernetesRuntime{} | ||
defer monkey.UnpatchAll() | ||
|
||
monkey.PatchInstanceMethod(reflect.TypeOf(k), "Read", func(k *KubernetesRuntime, ctx context.Context, request *runtime.ReadRequest) *runtime.ReadResponse { | ||
return &runtime.ReadResponse{Resource: &models.Resource{ | ||
ID: planSvc.ResourceKey(), | ||
Type: planSvc.Type, | ||
Attributes: lastAppliedObj, | ||
DependsOn: planSvc.DependsOn, | ||
Extensions: planSvc.Extensions, | ||
}} | ||
}) | ||
|
||
got := k.Import(tests[0].args.ctx, tests[0].args.request) | ||
if !reflect.DeepEqual(jsonutil.Marshal2PrettyString(got.Resource), jsonutil.Marshal2PrettyString(tests[0].want.Resource)) { | ||
t.Errorf("Import() = %v, want %v", jsonutil.Marshal2PrettyString(got.Resource), jsonutil.Marshal2PrettyString(tests[0].want.Resource)) | ||
} | ||
}) | ||
|
||
t.Run(tests[1].name, func(t *testing.T) { | ||
k := &KubernetesRuntime{} | ||
monkey.PatchInstanceMethod(reflect.TypeOf(k), "Read", func(k *KubernetesRuntime, ctx context.Context, request *runtime.ReadRequest) *runtime.ReadResponse { | ||
return &runtime.ReadResponse{Resource: &models.Resource{ | ||
ID: planSvc.ResourceKey(), | ||
Type: planSvc.Type, | ||
Attributes: liveSvcObj, | ||
DependsOn: planSvc.DependsOn, | ||
Extensions: planSvc.Extensions, | ||
}} | ||
}) | ||
defer monkey.UnpatchAll() | ||
|
||
if got := k.Import(tests[1].args.ctx, tests[1].args.request); !reflect.DeepEqual(jsonutil.Marshal2PrettyString(got), jsonutil.Marshal2PrettyString(tests[1].want)) { | ||
t.Errorf("Import() = %v, want %v", jsonutil.Marshal2PrettyString(got), jsonutil.Marshal2PrettyString(tests[1].want)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters