Skip to content

Commit

Permalink
Add golang unit test demo (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Aug 6, 2023
1 parent cf495a6 commit 16ae659
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
Expand All @@ -13,6 +12,9 @@ on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

env:
GO_VERSION: '1.17.3'

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
misspell-check:
Expand All @@ -36,12 +38,12 @@ jobs:
- name: Set up Golang
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: ${{ env.GO_VERSION }}

- name: Run go fmt test
run: hack/verify-gofmt.sh
env:
GO111MODULE: auto

# - name: Run go-learning unit test
# run: go test -v ./...
- name: Run golang unit test
run: cd practise/test-practise && go test -v
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go-learning 适用于有一定 `kubernetes` 经验,且想更进一步的同学
- [gRPC Usage](./practise/grpc-practise/README.md)
- [gin&informer](./practise/k8s-practise/gin-informer.go) 提供 `gin` 调用 `informer` 的用法
- [webShell](https://github.com/caoyingjunz/kube-webshell) 提供 `webshell` 调用 `kubernetes` `pod` 的用法展示
- [unit test](./practise/test-practise/README.md)

## Rust
- TODO
Expand Down
5 changes: 3 additions & 2 deletions examples/services/incluster/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"context"
"fmt"
apiv1 "k8s.io/api/core/v1"
"time"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -22,7 +23,7 @@ func main() {
fmt.Println(err)
}
for {
services, err := clientset.CoreV1().Services(apiv1.NamespaceDefault).List(metav1.ListOptions{})
services, err := clientset.CoreV1().Services(apiv1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println(err)
} else {
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ require (
github.com/hpcloud/tail v1.0.0
github.com/jinzhu/gorm v1.9.12
github.com/kubernetes-csi/csi-lib-utils v0.11.0
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/onsi/gomega v1.23.0 // indirect
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.4
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
Expand All @@ -48,5 +44,12 @@ require (
k8s.io/klog/v2 v2.90.1
k8s.io/kubectl v0.24.8
k8s.io/metrics v0.24.8
)

require (
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/onsi/gomega v1.23.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
)
6 changes: 6 additions & 0 deletions practise/test-practise/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 单元测试

### 基本规则
- 测试文件以 `_test.go` 结尾
- 测试函数名必须以 `Test` 开始
- 测试以 `go test` 命令开始
5 changes: 5 additions & 0 deletions practise/test-practise/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test

func Add(x, y int) int {
return x + y
}
11 changes: 11 additions & 0 deletions practise/test-practise/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import "testing"

func TestAdd(t *testing.T) {
x, y := 1, 2
z := Add(x, y)
if z != 3 {
t.Errorf("Add(%d,%d)=%d,real=%d", x, y, z, 3)
}
}

0 comments on commit 16ae659

Please sign in to comment.