Monday, February 21, 2022

Run Istio Integration test in debug mode in VSCode

Assume that you've setup your istio project in VSCode, and also run go mod vendor,

you can do the following to debug step-by-step in VSCode.


1. Create a kind k8s cluster

2. Create an Istio integration k8s cluster topology file named single.json, like this.

[
  {
    "kind": "Kubernetes",
    "clusterName": "istio-testing",
    "network": "istio-testing",
    "meta": {
      "kubeconfig": "/home/ubuntu/.kube/config"
    }
  }
]

Notice that the kubeconfig field, the value should be the kube config file

3. Now in VSCode, make sure that you have the following in your settings.

    "go.buildTags": "integ",
    "go.testFlags": ["-args", "--istio.test.kube.topology=/home/ubuntu/test/single.json", "--istio.test.skipVM"],

Now if you nagivate to an integration test go file in VSCode, you should be able to click on the codelens `debug test` to start debugging your code.

=================================================

For multiple cluster integration tests, the following few items will need to be taken care of:

1. Create multiple clusters using this script with a topology json file 

2. To use the code that you as a developer just built (such as istioctl or docker images such as pilot and proxyv2), you will need to make sure that these images get preloaded into the clusters created in the above step #1. If you use the scripts described in that step, then the newly built images should be loaded onto the cluster automatically.

3. In each test setup (most likely in TestMain method), you will need to setup how the tag and hub should be so that the process will use these images correctly. Otherwise, the process will use the public images not the ones that you just built.

   To do this, you most likely will need to do the followings:

   a. Create a new method like this

func enableMCSServiceDiscovery(t resource.Context, cfg *istio.Config) {
cfg.Values["global.tag"] = "1.15-dev"
cfg.Values["global.imagePullPolicy"] = "IfNotPresent"
cfg.Values["global.hub"] = "istio"
cfg.ControlPlaneValues = fmt.Sprintf(`
values:
pilot:
env:
PILOT_USE_ENDPOINT_SLICE: "true"
ENABLE_MCS_SERVICE_DISCOVERY: "true"
ENABLE_MCS_HOST: "true"
ENABLE_MCS_CLUSTER_LOCAL: "true"
MCS_API_GROUP: %s
MCS_API_VERSION: %s`,
common.KubeSettings(t).MCSAPIGroup,
common.KubeSettings(t).MCSAPIVersion)
}


   b. Then call that method in the TestMain method's Setup call like this. Notice one of the Setup method call uses the method enableMCSServiceDiscovery which got defined above.

func TestMain(m *testing.M) {
framework.
NewSuite(m).
Label(label.CustomSetup).
RequireMinVersion(17).
RequireMinClusters(2).
Setup(common.InstallMCSCRDs).
Setup(istio.Setup(&i, enableMCSServiceDiscovery)).
Setup(common.DeployEchosFunc("mcs", &echos)).
Run()
}


4. Make sure that VSCode settings file, contains the go.buildTags, go.testFlags settings like the followings

"go.buildTags": "integ",
"go.testFlags": ["-args", "--istio.test.kube.topology=/tmp/work/topology.json", "--istio.test.skipVM"],

 5. Once these above steps done, you can simply click on the debug test button (the codelens) above the MainTest method.

====================================================

If want to run the test locally (not via vscode codelens), you can do the following:

1. Found a PR from istio project on github which run successfully, there should be many integration tests, such as the following:

 
Notice that any test start with integ is an integration tests, you can pick any of them, then go to the raw build-log.txt file, in this file, you should be able to find the cluster topology file content. Then create a json file with the content like below:


[
{
"kind": "Kubernetes",
"clusterName": "config",
"podSubnet": "10.20.0.0/16",
"svcSubnet": "10.255.20.0/24",
"network": "network-1",
"primaryClusterName": "external",
"configClusterName": "config",
"meta": {
"kubeconfig": "/tmp/work/config"
}
},
{
"kind": "Kubernetes",
"clusterName": "remote",
"podSubnet": "10.30.0.0/16",
"svcSubnet": "10.255.30.0/24",
"network": "network-2",
"primaryClusterName": "external",
"configClusterName": "config",
"meta": {
"fakeVM": false,
"kubeconfig": "/tmp/work/remote"
}
},
{
"kind": "Kubernetes",
"clusterName": "external",
"podSubnet": "10.10.0.0/16",
"svcSubnet": "10.255.10.0/24",
"network": "network-1",
"primaryClusterName": "external",
"configClusterName": "config",
"meta": {
"fakeVM": false,
"kubeconfig": "/tmp/work/external"
}
}
]
 

Save the above content into a file such as topology.json

2. Then you should be able to find a command like the following:

go test -p 1 -v -count=1 -tags=integ -vet=off ./tests/integration/pilot/... \
-timeout 30m --istio.test.skipVM --istio.test.ci --istio.test.pullpolicy=IfNotPresent \
--istio.test.work_dir=/tmp/work --istio.test.hub=istio --istio.test.tag=1.15-dev \
--istio.test.kube.topology=/tmp/work/topology.json "--istio.test.select=,-postsubmit"

3. Change the parameters of the above command to fit your own env. Pay special attention to parameters like istio.test.tag, istio.test.hub, making changes based on your own build. In the above command, I built istio images locally and tagged them like public istio images, and preloaded into the k8s clusters, so that everything is ready to go.

4. The parameter ./tests/integration/pilot/... indicates what tests will be run, that must be a directory from the source tree. It will normally contain multiple TestMain methods, each TestMain method is considered as test suite. When it starts, you should see something like the following:

2022-04-29T14:47:53.132024Z info tf === DONE: Building clusters ===
2022-04-29T14:47:53.132029Z info tf === BEGIN: Setup: 'pilot_analysis' ===
2022-04-29T14:47:53.132083Z info tf === BEGIN: Deploy Istio [Suite=pilot_analysis] ===

that should give you some clear indication what test suite it is running. If there is any error, you can find that test suite in the source code and start debugging using VSCode. Notice that normally one integration test may contain many test suites, that is, as stated above, many TestMain methods in that directory or sub directory.

Friday, February 18, 2022

Check disk space usages

Run the following command to show which folder uses spaces exceeded G bits

 du -h ~ 2>/dev/null | grep '[0-9\.]\+G'