|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. Devtron Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package commonHelmService |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "github.com/devtron-labs/common-lib/utils/k8s" |
| 23 | + "github.com/devtron-labs/kubelink/config" |
| 24 | + "github.com/devtron-labs/kubelink/internals/logger" |
| 25 | + "github.com/devtron-labs/kubelink/pkg/service/commonHelmService/testdata" |
| 26 | + "k8s.io/client-go/rest" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 31 | +) |
| 32 | + |
| 33 | +func getTestCases() []testdata.TestCase { |
| 34 | + return []testdata.TestCase{ |
| 35 | + testdata.GetFluxCDDeploymentTestCase(), |
| 36 | + testdata.FluentBitDaemonsetTestCase(), |
| 37 | + testdata.GetRolloutDeploymentTestCase(), |
| 38 | + testdata.GetDeploymentTestCase(), |
| 39 | + testdata.GetCronJobTestCase(), |
| 40 | + testdata.GetMongodbCommunityTestCase(), |
| 41 | + testdata.GetKubePrometheusDaemonsetTestcase(), |
| 42 | + testdata.GetKubePrometheusDeploymentTestcase(), |
| 43 | + testdata.GetKubePrometheusAlertManagerTestcase(), |
| 44 | + testdata.GetKubePrometheusTestcase(), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetChildObjectsIntegration(t *testing.T) { |
| 49 | + // Mock K8sService implementation |
| 50 | + sugaredLogger := logger.NewSugaredLogger() |
| 51 | + helmReleaseConfig, err := config.GetHelmReleaseConfig() |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to get helm release config: %v", err) |
| 54 | + return |
| 55 | + } |
| 56 | + helmReleaseConfig.ParentChildGvkMapping = testdata.GetParentChildGvkMapForIntegrationTest() |
| 57 | + k8sService, err := NewK8sServiceImpl(sugaredLogger, helmReleaseConfig) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to create K8sService: %v", err) |
| 60 | + return |
| 61 | + } |
| 62 | + runtimeCfg, err := k8s.GetRuntimeConfig() |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to get runtime config: %v", err) |
| 65 | + return |
| 66 | + } |
| 67 | + runtimeCfg.LocalDevMode = true |
| 68 | + k8sUtil, err := k8s.NewK8sUtil(sugaredLogger, runtimeCfg) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("Failed to create K8sUtil: %v", err) |
| 71 | + return |
| 72 | + } |
| 73 | + restConfig, err := k8sUtil.GetK8sInClusterRestConfig() |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("Failed to get rest config: %v", err) |
| 76 | + return |
| 77 | + } |
| 78 | + for _, tt := range getTestCases() { |
| 79 | + t.Run(tt.Name, func(t *testing.T) { |
| 80 | + // Create context |
| 81 | + ctx, cancel := context.WithCancel(context.Background()) |
| 82 | + t.Cleanup(func() { |
| 83 | + // cleanup logic |
| 84 | + cancel() |
| 85 | + _ = tt.Cleanup() |
| 86 | + }) |
| 87 | + // setup logic |
| 88 | + if err = tt.Setup(ctx); err != nil { |
| 89 | + t.Fatalf("Failed to setup test: %v", err) |
| 90 | + return |
| 91 | + } |
| 92 | + testGetChildObjects(t, tt, k8sService, restConfig) |
| 93 | + return |
| 94 | + }) |
| 95 | + } |
| 96 | + return |
| 97 | +} |
| 98 | + |
| 99 | +func testGetChildObjects(t *testing.T, tt testdata.TestCase, k8sService K8sService, restConfig *rest.Config) { |
| 100 | + t.Run(fmt.Sprintf("GVK-%q", tt.ParentGvk.String()), func(t *testing.T) { |
| 101 | + parentApiVersion, _ := tt.ParentGvk.ToAPIVersionAndKind() |
| 102 | + resultV1, errV1 := k8sService.GetChildObjectsV1(restConfig, tt.Namespace, tt.ParentGvk, tt.ParentName, parentApiVersion) |
| 103 | + resultV2, errV2 := k8sService.GetChildObjectsV2(restConfig, tt.Namespace, tt.ParentGvk, tt.ParentName) |
| 104 | + assert.Truef(t, compareValidResourceTree(resultV1, resultV2), "Resource trees do not match for %s", tt.Name) |
| 105 | + if errV1 != nil { |
| 106 | + assert.EqualError(t, errV1, errV2.Error(), "Errors do not match for %s", tt.Name) |
| 107 | + } else { |
| 108 | + assert.NoError(t, errV2, "Errors do not match for %s", tt.Name) |
| 109 | + } |
| 110 | + fmt.Println("=== Child Object Count:", len(resultV1)) |
| 111 | + for _, manifest := range resultV1 { |
| 112 | + gvk := manifest.GroupVersionKind() |
| 113 | + if k8sService.CanHaveChild(gvk) { |
| 114 | + tt.ParentGvk = gvk |
| 115 | + tt.ParentName = manifest.GetName() |
| 116 | + tt.Namespace = manifest.GetNamespace() |
| 117 | + // Recursively call the test function for child objects |
| 118 | + testGetChildObjects(t, tt, k8sService, restConfig) |
| 119 | + } |
| 120 | + } |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +// compareValidResourceTree compares two slices of unstructured.Unstructured objects |
| 125 | +func compareValidResourceTree(v1Result []*unstructured.Unstructured, v2Result []*unstructured.Unstructured) bool { |
| 126 | + if len(v1Result) != len(v2Result) { |
| 127 | + return false |
| 128 | + } |
| 129 | + // group by gvk and compare for each gvk |
| 130 | + v1Map := make(map[string][]*unstructured.Unstructured) |
| 131 | + v2Map := make(map[string][]*unstructured.Unstructured) |
| 132 | + for _, v1Obj := range v1Result { |
| 133 | + gvk := v1Obj.GroupVersionKind() |
| 134 | + key := gvk.String() |
| 135 | + v1Map[key] = append(v1Map[key], v1Obj) |
| 136 | + } |
| 137 | + // group by gvk and compare for each gvk |
| 138 | + for _, v2Obj := range v2Result { |
| 139 | + gvk := v2Obj.GroupVersionKind() |
| 140 | + key := gvk.String() |
| 141 | + v2Map[key] = append(v2Map[key], v2Obj) |
| 142 | + } |
| 143 | + // compare length of each gvk |
| 144 | + if len(v1Map) != len(v2Map) { |
| 145 | + return false |
| 146 | + } |
| 147 | + // compare each gvk |
| 148 | + for key, v1Objs := range v1Map { |
| 149 | + v2Objs, ok := v2Map[key] |
| 150 | + if !ok { |
| 151 | + return false |
| 152 | + } |
| 153 | + if len(v1Objs) != len(v2Objs) { |
| 154 | + return false |
| 155 | + } |
| 156 | + for i, v1Obj := range v1Objs { |
| 157 | + v2Obj := v2Objs[i] |
| 158 | + if v1Obj.GetKind() != v2Obj.GetKind() { |
| 159 | + return false |
| 160 | + } |
| 161 | + if v1Obj.GetAPIVersion() != v2Obj.GetAPIVersion() { |
| 162 | + return false |
| 163 | + } |
| 164 | + if v1Obj.GetNamespace() != v2Obj.GetNamespace() { |
| 165 | + return false |
| 166 | + } |
| 167 | + if v1Obj.GetName() != v2Obj.GetName() { |
| 168 | + return false |
| 169 | + } |
| 170 | + if v1Obj.GetResourceVersion() != v2Obj.GetResourceVersion() { |
| 171 | + return false |
| 172 | + } |
| 173 | + if v1Obj.GetCreationTimestamp() != v2Obj.GetCreationTimestamp() { |
| 174 | + return false |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + return true |
| 179 | +} |
0 commit comments