Skip to content

Commit

Permalink
capability: test nits
Browse files Browse the repository at this point in the history
First, make capability_test.go a separate package so we are limited to
testing the external API (as opposed to e.g. state_linux_test.go in
which we test internal stuff as well).

Second, streamline the linux/non-linux code so it's easier to read and
copy/paste (in general, we only want to check that non-linux functions
can be called and return an error).

Third, change some non-fatal errors to use t.Errorf.

Fourth, use want/got everywhere in error messages, and add more context
to some errors.

Nothing here is critical or makes any noticeable impact on tests.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Sep 27, 2024
1 parent 46235e8 commit 7208f83
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package capability
package capability_test

import (
"runtime"
"testing"

. "github.com/moby/sys/capability"
)

// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
Expand All @@ -24,47 +26,43 @@ const (
func TestLastCap(t *testing.T) {
last, err := LastCap()
switch runtime.GOOS {
case "linux":
if err != nil {
t.Fatal(err)
}
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
return
case "linux":
}

// Sanity checks (Linux only).
if err != nil {
t.Fatalf("LastCap: want nil, got error: %v", err)
}
// Sanity checks.
if last < minLastCap {
t.Fatalf("LastCap returned %d (%s), expected >= %d (%s)",
t.Errorf("LastCap: want >= %d (%s), got %d (%s)",
last, last, minLastCap, minLastCap)
}
if last > maxLastCap {
t.Fatalf("LastCap returned %d, expected <= %d (%s). Package needs to be updated.",
last, maxLastCap, maxLastCap)
t.Errorf("LastCap: want <= %d (%s), got %d (%s). Package needs to be updated.",
last, last, maxLastCap, maxLastCap)
}
}

func TestListSupported(t *testing.T) {
list, err := ListSupported()
switch runtime.GOOS {
case "linux":
if err != nil {
t.Fatal(err)
}
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
}
if runtime.GOOS != "linux" {
return
case "linux":
}
if err != nil {
t.Fatalf("ListSupported: want nil, got error: %v", err)
}
// Sanity check (Linux only).
t.Logf("got +%v (len %d)", list, len(list))
minLen := int(minLastCap) + 1
if len(list) < minLen {
t.Fatalf("result is too short (got %d, want %d): +%v", len(list), minLen, list)
t.Errorf("ListSupported: too short (want %d, got %d): +%v", minLen, len(list), list)
}
}

0 comments on commit 7208f83

Please sign in to comment.