diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8e77867ad..504f3e64c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -105,7 +105,9 @@ jobs: - name: Run Go test run: make test - name: Upload coverage to Codecov - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 with: flags: unittests file: cover.out + fail_ci_if_error: true + verbose: true diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..75269b9bc --- /dev/null +++ b/codecov.yml @@ -0,0 +1,17 @@ +flag_management: + default_rules: + carryforward: true + statuses: + - name_prefix: project- + type: project + target: auto + threshold: 5% + if_no_uploads: error + if_not_found: success # no commit found? still set a success + - name_prefix: patch- + type: patch + target: 70% + if_no_uploads: success + if_not_found: success +github_checks: + annotations: true diff --git a/pkg/util/pod.go b/pkg/util/pod.go index d5ec24a01..3dd5543c8 100644 --- a/pkg/util/pod.go +++ b/pkg/util/pod.go @@ -95,7 +95,7 @@ func GetPodCgroupCPUStatPath(podParentDir string) string { func GetKubeQosClass(pod *corev1.Pod) corev1.PodQOSClass { qosClass := pod.Status.QOSClass - if qosClass == "" { + if len(qosClass) <= 0 { qosClass = qos.GetPodQOS(pod) } return qosClass diff --git a/pkg/util/stat_test.go b/pkg/util/stat_test.go index 9aa5413b2..68acf575d 100644 --- a/pkg/util/stat_test.go +++ b/pkg/util/stat_test.go @@ -18,6 +18,7 @@ package util import ( "io/ioutil" + "os" "path/filepath" "runtime" "testing" @@ -98,11 +99,14 @@ func Test_readPodCPUStatUsageTicks(t *testing.T) { tempDir := t.TempDir() tempInvalidPodCgroupDir := filepath.Join(tempDir, "no_cgroup") tempPodStatPath := filepath.Join(tempDir, system.CpuacctStatFileName) - statContentStr := getStatContents() - err := ioutil.WriteFile(tempPodStatPath, []byte(statContentStr), 0666) - if err != nil { - t.Error(err) - } + err := ioutil.WriteFile(tempPodStatPath, []byte(getStatContents()), 0666) + assert.NoError(t, err) + tempInvalidPodCgroupDir1 := filepath.Join(tempDir, "no_cgroup_1") + err = os.Mkdir(tempInvalidPodCgroupDir1, 0755) + assert.NoError(t, err) + tempPodInvalidStatPath := filepath.Join(tempInvalidPodCgroupDir1, system.CpuacctStatFileName) + err = ioutil.WriteFile(tempPodInvalidStatPath, []byte(getInvalidStatContents()), 0666) + assert.NoError(t, err) type args struct { podCgroupDir string } @@ -124,16 +128,18 @@ func Test_readPodCPUStatUsageTicks(t *testing.T) { want: 1356232, wantErr: false, }, + { + name: "read invalid cpu stat content", + args: args{podCgroupDir: tempPodInvalidStatPath}, + want: 0, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := readCPUAcctStatUsageTicks(tt.args.podCgroupDir) - if (err != nil) != tt.wantErr { - t.Errorf("readCPUAcctStatUsageTicks wantErr %v but got err %s", tt.wantErr, err) - } - if !tt.wantErr && got != tt.want { - t.Errorf("readCPUAcctStatUsageTicks want %v but got %v", tt.want, got) - } + assert.Equal(t, tt.wantErr, err != nil) + assert.Equal(t, tt.want, got) }) } } @@ -151,6 +157,12 @@ func getStatContents() string { "nr_running 0\nnr_uninterrupible 0\nnr_switches 234453363\n" } +func getInvalidStatContents() string { + return "user 407232\nnice a\nsystem a\nidle a\niowait a\nirq 0\nsoftirq 0\n" + + "steal 1115801\nguest 0\nload average(1min) 5\nload average(5min) 1\nload average(15min) 0\n" + + "nr_running 0\nnr_uninterrupible 0\nnr_switches 234453363\n" +} + func TestGetContainerCPUStatUsageTicks(t *testing.T) { helper := system.NewFileTestUtil(t) podCgroupDir := "pod-cgroup-dir"