opea-project / GenAIInfra

Containerization and cloud native suite for OPEA
Apache License 2.0
16 stars 22 forks source link

[WIP] GMC: fix the problem that the unit-tests cannot be run on non-k8s env #96

Closed KfreeZ closed 2 weeks ago

KfreeZ commented 2 weeks ago

Description

fix #92

the root cause:

  1. the CI/CD env has not ~/.kube/config, but in the code it is required to create a dynamicClient
  2. the UT due to cause 1, but was not captured
  3. on local env, the kubeconfig exists, so the UT successed
  4. the config file templates are not planted in CI/CD env neither

solution: When create the fake env of k8s in UT suite, create a fake KUBECONFIG based on this env, and cp the templates to /tmp/microservices/yaml, which is required by code. in the code, if ~/.kube/config cannot be found, use the KUBECONFIG instead.

Issues

92

Type of change

List the type of change like below. Please delete options that are not relevant.

Dependencies

n/a

Tests

Describe the tests that you ran to verify your changes. on the local testbed, remove all the templates and ~/.kube/config, run the UT, all passed

root@cis-gms-worker-3:~/zkf/repo/GenAIInfra/microservices-connector# make test
/root/zkf/repo/GenAIInfra/microservices-connector/bin/controller-gen-v0.14.0 rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/root/zkf/repo/GenAIInfra/microservices-connector/bin/controller-gen-v0.14.0 object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
KUBEBUILDER_ASSETS="/root/zkf/repo/GenAIInfra/microservices-connector/bin/k8s/1.29.0-linux-amd64" go test $(go list ./... | grep -v /e2e) -coverprofile cover.out
?       github.com/opea-project/GenAIInfra/microservices-connector/api/v1alpha3 [no test files]
?       github.com/opea-project/GenAIInfra/microservices-connector/cmd  [no test files]
ok      github.com/opea-project/GenAIInfra/microservices-connector/cmd/router   0.033s  coverage: 58.8% of statements
?       github.com/opea-project/GenAIInfra/microservices-connector/test/utils   [no test files]
ok      github.com/opea-project/GenAIInfra/microservices-connector/internal/controller  7.191s  coverage: 70.8% of statements

if templates are not copied intentionally, UT failed, and error is captured:

root@cis-gms-worker-3:~/zkf/repo/GenAIInfra/microservices-connector# make test
/root/zkf/repo/GenAIInfra/microservices-connector/bin/controller-gen-v0.14.0 rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/root/zkf/repo/GenAIInfra/microservices-connector/bin/controller-gen-v0.14.0 object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
KUBEBUILDER_ASSETS="/root/zkf/repo/GenAIInfra/microservices-connector/bin/k8s/1.29.0-linux-amd64" go test $(go list ./... | grep -v /e2e) -coverprofile cover.out
?       github.com/opea-project/GenAIInfra/microservices-connector/api/v1alpha3 [no test files]
?       github.com/opea-project/GenAIInfra/microservices-connector/cmd  [no test files]
ok      github.com/opea-project/GenAIInfra/microservices-connector/cmd/router   0.063s  coverage: 58.8% of statements
?       github.com/opea-project/GenAIInfra/microservices-connector/test/utils   [no test files]
user config {localhost http://proxy.example.com https://proxy.example.com}
user config { localhost http://proxy.example.com https://proxy.example.com}
user config <nil>
user config {localhost http://proxy.example.com https://proxy.example.com}
user config {localhost http://proxy.example.com https://proxy.example.com}
user config { localhost http://proxy.example.com https://proxy.example.com}
user config {localhost http://proxy.example.com https://proxy.example.com}
user config {localhost http://proxy.example.com https://proxy.example.com}
user config {localhost http://proxy.example.com https://proxy.example.com }
Running Suite: Controller Suite - /root/zkf/repo/GenAIInfra/microservices-connector/internal/controller
=======================================================================================================
Random Seed: 1718280112

Will run 1 of 1 specs
Reconciling connector graph apiVersion  graph test-resource
/tmp/fake-kubeconfig-28464316.yaml
------------------------------
• [FAILED] [2.061 seconds]
GMConnector Controller When reconciling a resource [It] should successfully reconcile the resource
/root/zkf/repo/GenAIInfra/microservices-connector/internal/controller/gmconnector_controller_test.go:94

  Timeline >>
  STEP: creating the custom resource for the Kind GMConnector @ 06/13/24 20:01:57.287
  STEP: Reconciling the created resource @ 06/13/24 20:01:59.329
  [FAILED] in [It] - /root/zkf/repo/GenAIInfra/microservices-connector/internal/controller/gmconnector_controller_test.go:104 @ 06/13/24 20:01:59.336
  STEP: Cleanup the specific resource instance GMConnector @ 06/13/24 20:01:59.34
  << Timeline

  [FAILED] Unexpected error:
      <*errors.withStack | 0xc000513d70>: 
      Failed to pre-process the Configmap file for xeon: failed to read /tmp/microservices/yamls/qna_configmap_xeon.yaml : open /tmp/microservices/yamls/qna_configmap_xeon.yaml: no such file or directory
      {
          error: <*errors.withMessage | 0xc000372600>{
              cause: <*errors.errorString | 0xc0008bfbb0>{
                  s: "failed to read /tmp/microservices/yamls/qna_configmap_xeon.yaml : open /tmp/microservices/yamls/qna_configmap_xeon.yaml: no such file or directory",
              },
              msg: "Failed to pre-process the Configmap file for xeon",
          },
          stack: [0x158e511, 0x1593c8d, 0x14c0df3, 0x14d4d0d, 0x473e21],
      }
  occurred
  In [It] at: /root/zkf/repo/GenAIInfra/microservices-connector/internal/controller/gmconnector_controller_test.go:104 @ 06/13/24 20:01:59.336
------------------------------

Summarizing 1 Failure:
  [FAIL] GMConnector Controller When reconciling a resource [It] should successfully reconcile the resource
  /root/zkf/repo/GenAIInfra/microservices-connector/internal/controller/gmconnector_controller_test.go:104

Ran 1 of 1 Specs in 7.882 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped
--- FAIL: TestControllers (7.88s)
FAIL
coverage: 21.4% of statements
FAIL    github.com/opea-project/GenAIInfra/microservices-connector/internal/controller  7.931s
FAIL
make: *** [Makefile:70: test] Error 1
KfreeZ commented 2 weeks ago

As @mkbhanda @irisdingbj point out, there is still some issues in this PR, move to WIP due to lower priority

KfreeZ commented 2 weeks ago

the #98 contains the solution to the UT coverage failed to run on CI/CD, close this one.