diff --git a/.github/workflows/ut.yml b/.github/workflows/ut.yml new file mode 100644 index 000000000..ac9557689 --- /dev/null +++ b/.github/workflows/ut.yml @@ -0,0 +1,32 @@ +name: Kmesh Unit Test Workflow + +on: pull_request + +jobs: + + build: + runs-on: ubuntu-22.04 + + env: + PKG_CONFIG_PATH: $GITHUB_WORKSPACE/mk + + strategy: + matrix: + go-version: [ '1.21' ] + + steps: + - uses: actions/checkout@v3 + + - name: Setup Go + uses: actions/setup-go@v4.0.0 + with: + go-version: ${{ matrix.go-version }} + + - name: Set enviroments + run: | + find $GITHUB_WORKSPACE/mk -name "*.pc" | xargs sed -i "s#^prefix=.*#prefix=$GITHUB_WORKSPACE#g" + echo "PKG_CONFIG_PATH=$GITHUB_WORKSPACE/mk" >> $GITHUB_ENV + + - name: Go Test + run: | + go test -v -vet=off ./... \ No newline at end of file diff --git a/Makefile b/Makefile index 7ffca8ce9..d42a3e03a 100644 --- a/Makefile +++ b/Makefile @@ -166,3 +166,8 @@ clean: $(QUIET) make clean -C bpf/deserialization_to_bpf_map $(call printlog, CLEAN, "kernel") $(QUIET) make clean -C kernel/ko_src + +test-ut: + $(QUIET) find $(ROOT_DIR)/mk -name "*.pc" | xargs sed -i "s#^prefix=.*#prefix=${ROOT_DIR}#g" + $(QUIET) (export PKG_CONFIG_PATH=$(PKG_CONFIG_PATH):$(ROOT_DIR)mk; \ + $(GO) test -v -vet=off -tags testut ./pkg/...) \ No newline at end of file diff --git a/mk/api-v2-c.pc b/mk/api-v2-c.pc index 9de9a58f0..bff783a19 100644 --- a/mk/api-v2-c.pc +++ b/mk/api-v2-c.pc @@ -1,4 +1,4 @@ -prefix= +prefix=/home/vander0x00/workplace/kmesh/ Name: api-v2-c Description: api-v2-c diff --git a/mk/bpf.pc b/mk/bpf.pc index a2aebee25..78f81ba20 100644 --- a/mk/bpf.pc +++ b/mk/bpf.pc @@ -1,4 +1,4 @@ -prefix= +prefix=/home/vander0x00/workplace/kmesh/ Name: bpf Description: bpf Version: diff --git a/pkg/bpf/bpf.go b/pkg/bpf/bpf.go index d7a769100..61c5f33c3 100644 --- a/pkg/bpf/bpf.go +++ b/pkg/bpf/bpf.go @@ -1,3 +1,6 @@ +//go:build !testut +// +build !testut + /* * Copyright 2023 The Kmesh Authors. * diff --git a/pkg/bpf/bpf_kmesh.go b/pkg/bpf/bpf_kmesh.go index f16c50df4..9d051b934 100644 --- a/pkg/bpf/bpf_kmesh.go +++ b/pkg/bpf/bpf_kmesh.go @@ -1,4 +1,5 @@ -// +build enhanced +//go:build enhanced || ignore || !testut +// +build enhanced ignore !testut /* * Copyright 2023 The Kmesh Authors. @@ -46,8 +47,8 @@ type BpfSockOps struct { type BpfKmesh struct { TracePoint BpfTracePoint - SockConn BpfSockConn - SockOps BpfSockOps + SockConn BpfSockConn + SockOps BpfSockOps } func (sc *BpfTracePoint) NewBpf(cfg *Config) { @@ -93,7 +94,7 @@ func NewBpfKmesh(cfg *Config) (BpfKmesh, error) { func (sc *BpfTracePoint) loadKmeshTracePointObjects() (*ebpf.CollectionSpec, error) { var ( - err error + err error spec *ebpf.CollectionSpec opts ebpf.CollectionOptions ) @@ -314,7 +315,7 @@ func (sc *BpfKmesh) ApiEnvCfg() error { } func (sc *BpfTracePoint) Attach() error { - tpopt := link.RawTracepointOptions { + tpopt := link.RawTracepointOptions{ Name: "connect_ret", Program: sc.KmeshTracePointObjects.ConnectRet, } diff --git a/pkg/bpf/bpf_kmesh_common.go b/pkg/bpf/bpf_kmesh_common.go index 3441fccb5..0995f6575 100644 --- a/pkg/bpf/bpf_kmesh_common.go +++ b/pkg/bpf/bpf_kmesh_common.go @@ -1,3 +1,6 @@ +//go:build !testut +// +build !testut + /* * Copyright 2023 The Kmesh Authors. * diff --git a/pkg/bpf/bpf_kmesh_l4.go b/pkg/bpf/bpf_kmesh_l4.go index e20a0360c..6701be0b3 100644 --- a/pkg/bpf/bpf_kmesh_l4.go +++ b/pkg/bpf/bpf_kmesh_l4.go @@ -1,5 +1,5 @@ -//go:build !enhanced -// +build !enhanced +//go:build !enhanced && !testut +// +build !enhanced,!testut /* * Copyright 2023 The Kmesh Authors. diff --git a/pkg/bpf/bpf_kmesh_l4_workload.go b/pkg/bpf/bpf_kmesh_l4_workload.go index 78d1e02b8..633018fdf 100644 --- a/pkg/bpf/bpf_kmesh_l4_workload.go +++ b/pkg/bpf/bpf_kmesh_l4_workload.go @@ -1,3 +1,6 @@ +//go:build !testut +// +build !testut + /* * Copyright 2024 The Kmesh Authors. * diff --git a/pkg/bpf/bpf_kmesh_workload.go b/pkg/bpf/bpf_kmesh_workload.go index fa4ec7e29..4cb6f88e6 100644 --- a/pkg/bpf/bpf_kmesh_workload.go +++ b/pkg/bpf/bpf_kmesh_workload.go @@ -1,3 +1,6 @@ +//go:build !testut +// +build !testut + /* * Copyright 2024 The Kmesh Authors. * diff --git a/pkg/bpf/config_test.go b/pkg/bpf/config_test.go new file mode 100644 index 000000000..bffa7dd94 --- /dev/null +++ b/pkg/bpf/config_test.go @@ -0,0 +1,83 @@ +/* + * Copyright 2023 The Kmesh Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + + * Author: Min Chen + * Create: 2024-01-23 + */ + +package bpf + +import "testing" + +func TestConfig_ParseConfig(t *testing.T) { + type fields struct { + BpfFsPath string + Cgroup2Path string + EnableKmesh bool + EnableMda bool + BpfVerifyLogSize int + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + // TODO: Add test cases. + { + name: "valid Mda config", + fields: fields{ + EnableMda: true, + }, + wantErr: false, + }, + // { + // name: "valid Kmesh config", + // fields: fields{ + // EnableKmesh: true, + + // }, + // }, + { + name: "none of EnableKmesh or EnableMda", + fields: fields{ + EnableKmesh: false, + EnableMda: false, + }, + wantErr: true, + }, + { + name: "both EnableKmesh and EnableMda", + fields: fields{ + EnableKmesh: true, + EnableMda: true, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + BpfFsPath: tt.fields.BpfFsPath, + Cgroup2Path: tt.fields.Cgroup2Path, + EnableKmesh: tt.fields.EnableKmesh, + EnableMda: tt.fields.EnableMda, + BpfVerifyLogSize: tt.fields.BpfVerifyLogSize, + } + if err := c.ParseConfig(); (err != nil) != tt.wantErr { + t.Errorf("ParseConfig() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/bpf/pin.go b/pkg/bpf/pin.go index e95e8f196..b1bc93ab8 100644 --- a/pkg/bpf/pin.go +++ b/pkg/bpf/pin.go @@ -1,3 +1,6 @@ +//go:build !testut +// +build !testut + /* * Copyright 2023 The Kmesh Authors. *