From 814ac648ec5459ac19ce07e966e1e214ee412332 Mon Sep 17 00:00:00 2001 From: Hardy Date: Mon, 6 Jan 2025 22:59:41 +0800 Subject: [PATCH] test: unit test ci task (#70) * test: unit test ci task --- .github/workflows/unit_test.yml | 105 ++++++++++++++++++++++++++++++ plugin/api/insight/metric_util.go | 14 ++-- 2 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/unit_test.yml diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml new file mode 100644 index 00000000..bd213dbe --- /dev/null +++ b/.github/workflows/unit_test.yml @@ -0,0 +1,105 @@ +name: Unit Test + +on: + pull_request: + branches: [ "main" ] + +defaults: + run: + shell: bash + +jobs: + build: + runs-on: ubuntu-latest + env: + GO_VERSION: 1.23.4 + NODEJS_VERSION: 16.20.2 + steps: + - name: Checkout current repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + path: console + + - name: Checkout framework repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + repository: infinilabs/framework + path: framework + + - name: Checkout framework-vendor + uses: actions/checkout@v4 + with: + ref: main + fetch-depth: 0 + repository: infinilabs/framework-vendor + path: vendor + + - name: Set up nodejs toolchain + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODEJS_VERSION }} + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + node_modules + key: ${{ runner.os }}-cnpm-${{ hashFiles('**/package.json') }} + restore-keys: | + ${{ runner.os }}-cnpm- + + - name: Check nodejs toolchain + run: | + if ! command -v cnpm >/dev/null 2>&1; then + npm install -g rimraf --quiet --no-progress + npm install -g cnpm@9.2.0 --quiet --no-progress + fi + node -v && npm -v && cnpm -v + + - name: Set up go toolchain + uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + check-latest: false + cache: true + + - name: Check go toolchain + run: go version + + - name: Cache Build Output + uses: actions/cache@v4 + with: + path: | + .public + key: ${{ runner.os }}-build-${{ hashFiles('**/package.json') }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-build-${{ hashFiles('**/package.json') }}- + ${{ runner.os }}-build- + + - name: Unit test + env: + GOFLAGS: -tags=ci + run: | + echo Home path is $HOME + export WORKBASE=$HOME/go/src/infini.sh + export WORK=$WORKBASE/console + + # for test workspace + mkdir -p $HOME/go/src/ + ln -s $GITHUB_WORKSPACE $WORKBASE + + # for web build + cd $WORK/web + cnpm install --quiet --no-progress + cnpm run build --quiet + + # check work folder + ls -lrt $WORKBASE/ + ls -alrt $WORK + + # for unit test + cd $WORK + echo Testing code at $PWD ... + make test \ No newline at end of file diff --git a/plugin/api/insight/metric_util.go b/plugin/api/insight/metric_util.go index 283648c1..d972a81a 100644 --- a/plugin/api/insight/metric_util.go +++ b/plugin/api/insight/metric_util.go @@ -32,8 +32,8 @@ import ( "strconv" "strings" - "infini.sh/framework/core/elastic" "infini.sh/console/model/insight" + "infini.sh/framework/core/elastic" "infini.sh/framework/core/util" ) @@ -107,7 +107,7 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) { i := 0 for _, metricItem := range metric.Items { if metricItem.Name == "" { - metricItem.Name = string('a' + i) + metricItem.Name = string(rune('a' + i)) } metricAggs := generateAgg(&metricItem, metric.TimeField) if err := util.MergeFields(basicAggs, metricAggs, true); err != nil { @@ -156,7 +156,7 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) { "field": groups[i].Field, "size": limit, } - if i == grpLength - 1 && len(metric.Sort) > 0 { + if i == grpLength-1 && len(metric.Sort) > 0 { var termsOrder []interface{} for _, sortItem := range metric.Sort { termsOrder = append(termsOrder, util.MapStr{sortItem.Key: sortItem.Direction}) @@ -244,7 +244,7 @@ func collectMetricData(agg interface{}, groupValues []string, metricData *[]insi if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok { if bks, ok := timeBks["buckets"].([]interface{}); ok { md := insight.MetricData{ - Data: map[string][]insight.MetricDataItem{}, + Data: map[string][]insight.MetricDataItem{}, Groups: groupValues, } for _, bk := range bks { @@ -268,7 +268,7 @@ func collectMetricData(agg interface{}, groupValues []string, metricData *[]insi } else { md := insight.MetricData{ - Data: map[string][]insight.MetricDataItem{}, + Data: map[string][]insight.MetricDataItem{}, Groups: groupValues, } for k, v := range aggM { @@ -307,7 +307,7 @@ func collectMetricDataOther(agg interface{}, groupValues []string, metricData *[ if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok { if bks, ok := timeBks["buckets"].([]interface{}); ok { md := insight.MetricData{ - Data: map[string][]insight.MetricDataItem{}, + Data: map[string][]insight.MetricDataItem{}, Groups: groupValues, } for _, bk := range bks { @@ -332,7 +332,7 @@ func collectMetricDataOther(agg interface{}, groupValues []string, metricData *[ } } else { md := insight.MetricData{ - Data: map[string][]insight.MetricDataItem{}, + Data: map[string][]insight.MetricDataItem{}, Groups: groupValues, }