Skip to content

Commit 93fbeae

Browse files
Laeeqdevkartik-579
andauthored
feat:wire nil test in pre ci pipeline (#4858)
* wip * wip * refactored nil check * added runtime main script for creating binary file * added handling for githubClient panic * added some script for creating binary file * deleted binary file * updated go version * added env file support and also added other svc requirements * updated makefile * updated makefile * Update wireNil.env * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update wireNil.env * Update run-integration-test.sh * resolved merge conflicts * updated makefile * added * added * added * wip * wip * refactored nil check * added runtime main script for creating binary file * added handling for githubClient panic * added some script for creating binary file * deleted binary file * updated go version * added env file support and also added other svc requirements * updated makefile * updated makefile * Update wireNil.env * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update Makefile * Update wireNil.env * Update run-integration-test.sh * resolved merge conflicts * updated makefile * added * added * added * added readonly option for docker mount * reverted ro change * updated makefile * run go tidy and mod commands * update main file * deleted binary file * added fileds and value in skip function * map length added * map length added * create a file output.env * changes in makefile * changes in makefile * again updated makefile * updated makefile * updated makefile * added skipUnnecessaryFiledsForCheck function * have done some changes * have done some changes * have done some changes * have done some changes * removed some data * updated comment * PR code review comments resolved * final commit * updated nats-server.yaml file * updated nats image * refactored code * resolved error * resolved error * resolved error * resolved error * resolved error * resolved error * uncommented code * renamed filename * renamed filename * updated migartion yaml file * git hash comment resolved * git hash comment resolved * updated makefile * updated makefile * updated makefile * move docker exec cmd into other folder * move docker exec cmd into other folder * move docker exec cmd into other folder * move docker exec cmd into other folder * move docker exec cmd into other folder * move docker exec cmd into other folder * create a new file * updated code and renamed file name * increased migrator deadline * added sleep * added sleep * testing migrator * upadted env value * upadted env value * upadted img * upadted img * upadted img * upadted img * upadted img * upadted img * upadted img * add volume mounted functionality * remove sql dir * removed enforceUtil field * removed enforceUtil field * updated migrator image * added error in mod file * added PROXY_SERVICE_CONFIG env * removed rbac fields --------- Co-authored-by: kartik-579 <kartik@devtron.ai> Co-authored-by: kartik-579 <84493919+kartik-579@users.noreply.github.com>
1 parent 7119c08 commit 93fbeae

14 files changed

+515
-33
lines changed

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@ test-unit:
4040
go test ./pkg/pipeline
4141

4242
test-integration:
43-
export INTEGRATION_TEST_ENV_ID=$(docker run --env TEST_BRANCH=$TEST_BRANCH --env LATEST_HASH=$LATEST_HASH --privileged -d --name dind-test -v $PWD/tests/integrationTesting/:/tmp/ docker:dind)
44-
docker exec ${INTEGRATION_TEST_ENV_ID} sh /tmp/create-test-env.sh
45-
docker exec ${INTEGRATION_TEST_ENV_ID} sh /tests/integrationTesting/run-integration-test.sh
46-
43+
docker run --env-file=wireNilChecker.env --privileged -d --name dind-test -v $(PWD)/:/wirenil/:ro -v $(PWD)/temp/:/tempfile docker:dind
44+
docker exec dind-test sh -c "mkdir test && cp -r wirenil/* test/ && ./test/tests/integrationTesting/exportEnvsExecuteWireNilChecker.sh"
4745
run: build
4846
./devtron
49-
5047
.PHONY: build
5148
docker-build-image: build
5249
docker build -t devtron:$(TAG) .
53-
5450
.PHONY: build, all, wire, clean, run, set-docker-build-env, docker-build-push, devtron,
5551
docker-build-push: docker-build-image
5652
docker tag devtron:${TAG} ${REGISTRY}/devtron:${TAG}

WiringNilCheck.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"reflect"
8+
"strings"
9+
"unsafe"
10+
)
11+
12+
func CheckIfNilInWire() {
13+
app, err := InitializeApp()
14+
if err != nil {
15+
log.Panic(err)
16+
}
17+
nilFieldsMap := make(map[string]bool)
18+
checkNilFields(app, nilFieldsMap)
19+
fmt.Println("NIL Fields present in impls are: ", nilFieldsMap)
20+
//Writes the length of nilFieldsMap to a file (e.g., output.env) so that we can export this file's data in a pre-CI pipeline bash script and fail the pre-CI pipeline if the length of nilFieldsMap is greater than zero.
21+
err = writeResultToFile(len(nilFieldsMap))
22+
if err != nil {
23+
return
24+
}
25+
}
26+
27+
func checkNilFields(obj interface{}, nilObjMap map[string]bool) {
28+
val := reflect.ValueOf(obj)
29+
if val.Kind() == reflect.Ptr {
30+
val = val.Elem()
31+
}
32+
if val.Kind() != reflect.Struct {
33+
return
34+
}
35+
valName := val.Type().Name()
36+
for i := 0; i < val.NumField(); i++ {
37+
field := val.Field(i)
38+
fieldName := val.Type().Field(i).Name
39+
pkgPath := val.Type().PkgPath()
40+
if pkgPath != "main" && !strings.Contains(pkgPath, "devtron-labs/devtron") {
41+
//package not from this repo, ignoring
42+
continue
43+
}
44+
if skipUnnecessaryFieldsForCheck(fieldName, valName) { // skip unnecessary fileds and values
45+
continue
46+
}
47+
if !canFieldTypeBeNil(field) { // field can not be nil, skip
48+
continue
49+
} else if field.IsNil() { // check if the field is nil
50+
mapEntry := fmt.Sprintf("%s.%s", valName, fieldName)
51+
nilObjMap[mapEntry] = true
52+
continue
53+
}
54+
if canSkipFieldStructCheck(fieldName, valName) {
55+
continue
56+
}
57+
if !isExported(fieldName) && !field.CanInterface() {
58+
unexportedField := GetUnexportedField(field)
59+
checkNilFields(unexportedField, nilObjMap)
60+
} else {
61+
// Recurse
62+
checkNilFields(field.Interface(), nilObjMap)
63+
}
64+
}
65+
}
66+
67+
func canFieldTypeBeNil(field reflect.Value) bool {
68+
kind := field.Kind()
69+
switch kind {
70+
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer,
71+
reflect.Interface, reflect.Slice:
72+
return true
73+
default: //other types can not be nil
74+
return false
75+
}
76+
}
77+
78+
func canSkipFieldStructCheck(fieldName, valName string) bool {
79+
fieldName = strings.ToLower(fieldName)
80+
valName = strings.ToLower(valName)
81+
if valName == "githubclient" && (fieldName == "client" || fieldName == "gitopshelper") {
82+
return true
83+
}
84+
for _, str := range []string{"logger", "dbconnection", "syncedenforcer"} {
85+
if fieldName == str {
86+
return true
87+
}
88+
}
89+
return false
90+
}
91+
92+
func skipUnnecessaryFieldsForCheck(fieldName, valName string) bool {
93+
fieldName = strings.ToLower(fieldName)
94+
valName = strings.ToLower(valName)
95+
if valName == "cicdconfig" {
96+
return true
97+
}
98+
fieldAndValName := map[string][]string{
99+
"app": {"enforcerv2", "server"},
100+
"gitfactory": {"client"},
101+
"argocdconnectionmanagerimpl": {"argocdsettings"},
102+
"enforcerimpl": {"cache", "enforcerv2"},
103+
"helmappclientimpl": {"applicationserviceclient"},
104+
"modulecronserviceimpl": {"cron"},
105+
"oteltracingserviceimpl": {"traceprovider"},
106+
"terminalaccessrepositoryimpl": {"templatescache"},
107+
}
108+
if _, ok := fieldAndValName[valName]; ok {
109+
for _, ignoreFieldName := range fieldAndValName[valName] {
110+
if ignoreFieldName == fieldName {
111+
return true
112+
}
113+
}
114+
}
115+
return false
116+
}
117+
func GetUnexportedField(field reflect.Value) interface{} {
118+
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
119+
}
120+
121+
func isExported(fieldName string) bool {
122+
return strings.ToUpper(fieldName[0:1]) == fieldName[0:1]
123+
}
124+
125+
func writeResultToFile(data int) error {
126+
file, err := os.Create("/test/output.env")
127+
if err != nil {
128+
log.Println("Failed to create file:", err)
129+
return err
130+
}
131+
defer file.Close()
132+
_, err = file.WriteString(fmt.Sprintf("OUTPUT=%d", data))
133+
if err != nil {
134+
log.Println("Failed to write to file:", err)
135+
return err
136+
}
137+
return nil
138+
}

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ import (
2121
"fmt"
2222
_ "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2323
_ "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
24+
util2 "github.com/devtron-labs/devtron/util"
2425
"log"
2526
"os"
2627
"os/signal"
2728
"syscall"
2829
)
2930

3031
func main() {
31-
32+
globalEnvVariables, err := util2.GetEnvironmentVariables()
33+
if err != nil {
34+
log.Println("error while getting env variables reason:", err)
35+
return
36+
}
37+
if globalEnvVariables.GlobalEnvVariables.ExecuteWireNilChecker {
38+
CheckIfNilInWire()
39+
return
40+
}
3241
app, err := InitializeApp()
3342
if err != nil {
3443
log.Panic(err)

pkg/cluster/ClusterService.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ type ClusterServiceImpl struct {
197197
userAuthRepository repository3.UserAuthRepository
198198
userRepository repository3.UserRepository
199199
roleGroupRepository repository3.RoleGroupRepository
200-
*ClusterRbacServiceImpl
201200
}
202201

203202
func NewClusterServiceImpl(repository repository.ClusterRepository, logger *zap.SugaredLogger,
@@ -212,9 +211,6 @@ func NewClusterServiceImpl(repository repository.ClusterRepository, logger *zap.
212211
userAuthRepository: userAuthRepository,
213212
userRepository: userRepository,
214213
roleGroupRepository: roleGroupRepository,
215-
ClusterRbacServiceImpl: &ClusterRbacServiceImpl{
216-
logger: logger,
217-
},
218214
}
219215
go clusterService.buildInformer()
220216
return clusterService

tests/integrationTesting/create-test-env.sh

100644100755
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
1616
kubectl create ns devtroncd
1717
kubectl create ns devtron-cd
1818
kubectl create ns devtron-ci
19+
kubectl -n devtroncd create cm git-hash-cm --from-literal=GIT_HASH=$GIT_HASH
1920
kubectl -n devtroncd apply -f $PWD/tests/integrationTesting/postgresql-secret.yaml
2021
kubectl -ndevtroncd apply -f $PWD/tests/integrationTesting/postgresql.yaml
22+
kubectl -n devtroncd apply -f $PWD/tests/integrationTesting/devtron-secret.yaml
23+
kubectl -n devtroncd apply -f $PWD/tests/integrationTesting/nats-server.yaml
24+
# we are copying sql scripts into node container and this conainer's name is fixed
25+
docker cp $PWD/scripts/sql/ k3d-it-cluster-server-0:./tmp/scripts
2126
yq '(select(.metadata.name == "postgresql-migrate-devtron") | .spec.template.spec.containers[0].env[0].value) = env(TEST_BRANCH)' $PWD/tests/integrationTesting/migrator.yaml -i
2227
yq '(select(.metadata.name == "postgresql-migrate-devtron") | .spec.template.spec.containers[0].env[9].value) = env(LATEST_HASH)' $PWD/tests/integrationTesting/migrator.yaml -i
2328
kubectl -ndevtroncd apply -f $PWD/tests/integrationTesting/migrator.yaml
@@ -50,4 +55,4 @@ helm dependency up
5055
helm template devtron . --set installer.modules={cicd} -s templates/workflow.yaml >./argo_wf.yaml
5156
kubectl apply -f ./argo_wf.yaml
5257
while [ ! $(kubectl -n argo get deployment workflow-controller -o jsonpath="{.status.readyReplicas}") ]; do sleep 10; done
53-
cd $PWD
58+
cd $PWD
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
data:
3+
PG_PASSWORD: c2hhcmVkLWRldnRyb24tcGc=
4+
kind: Secret
5+
metadata:
6+
annotations:
7+
meta.helm.sh/release-name: orchestrator-oss-shared-cd-dcd
8+
meta.helm.sh/release-namespace: devtroncd
9+
creationTimestamp: "2024-03-19T13:58:54Z"
10+
labels:
11+
app.kubernetes.io/managed-by: Helm
12+
name: devtron-secret
13+
namespace: devtroncd
14+
type: Opaque
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cd test
2+
./tests/integrationTesting/create-test-env.sh
3+
./tests/integrationTesting/run-integration-test.sh
4+
touch output.env
5+
export NODE_IP_ADDRESS=$(kubectl get node --no-headers -o custom-columns=INTERNAL-IP:status.addresses[0].address)
6+
export PG_ADDR=$NODE_IP_ADDRESS
7+
export NATS_SERVER_HOST=nats://$NODE_IP_ADDRESS:30236
8+
go mod tidy
9+
go run .
10+
cp output.env ../tempfile

tests/integrationTesting/migrator.yaml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ spec:
77
spec:
88
containers:
99
- name: postgresql-migrate-devtron
10-
image: quay.io/devtron/migrator:71748de9-149-11112
10+
image: quay.io/devtron/migrator:e026843e-866-11925
11+
volumeMounts:
12+
- mountPath: /tmp/app/
13+
name: sql-scripts-volume
1114
env:
12-
- name: GIT_BRANCH
13-
value: main
14-
- name: SCRIPT_LOCATION
15-
value: scripts/sql/
16-
- name: GIT_REPO_URL
17-
value: https://github.com/devtron-labs/devtron.git
1815
- name: DB_TYPE
1916
value: postgres
2017
- name: DB_USER_NAME
@@ -27,12 +24,17 @@ spec:
2724
value: orchestrator
2825
- name: MIGRATE_TO_VERSION
2926
value: "0"
30-
- name: GIT_HASH
31-
value: be7da471e45a501eba19eaa5f8d08dfe5601598d
27+
- name: SCRIPT_MOUNTED
28+
value: "true"
3229
envFrom:
3330
- secretRef:
3431
name: postgresql-migrator
3532
restartPolicy: OnFailure
33+
volumes:
34+
- name: sql-scripts-volume
35+
hostPath:
36+
path: /tmp/scripts/
37+
type: DirectoryOrCreate
3638
backoffLimit: 20
3739
activeDeadlineSeconds: 1500
3840
---
@@ -45,7 +47,7 @@ spec:
4547
spec:
4648
containers:
4749
- name: postgresql-migrate-casbin
48-
image: quay.io/devtron/migrator:71748de9-149-11112
50+
image: quay.io/devtron/migrator:e026843e-866-11925
4951
env:
5052
- name: SCRIPT_LOCATION
5153
value: scripts/casbin/
@@ -83,7 +85,7 @@ spec:
8385
spec:
8486
containers:
8587
- name: postgresql-migrate-gitsensor
86-
image: quay.io/devtron/migrator:71748de9-149-11112
88+
image: quay.io/devtron/migrator:e026843e-866-11925
8789
env:
8890
- name: SCRIPT_LOCATION
8991
value: scripts/sql/
@@ -121,7 +123,7 @@ spec:
121123
spec:
122124
containers:
123125
- name: postgresql-migrate-lens
124-
image: quay.io/devtron/migrator:71748de9-149-11112
126+
image: quay.io/devtron/migrator:e026843e-866-11925
125127
env:
126128
- name: SCRIPT_LOCATION
127129
value: scripts/sql/

0 commit comments

Comments
 (0)