File: netns_linux_test.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (77 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package rootlessnetns

import (
	"os"
	"path/filepath"
	"strconv"
	"testing"

	"github.com/stretchr/testify/assert"
)

func Test_refCount(t *testing.T) {
	tests := []struct {
		name    string
		content string
		inc     int
		want    int
	}{
		{
			name: "init counter",
			inc:  1,
			want: 1,
		},
		{
			name: "simple add",
			inc:  5,
			want: 5,
		},
		{
			name:    "add multiple with content",
			content: "0",
			inc:     5,
			want:    5,
		},
		{
			name:    "add multiple with high number content",
			content: "5500",
			inc:     2,
			want:    5502,
		},
		{
			name:    "simple dec",
			content: "5",
			inc:     -5,
			want:    0,
		},
		{
			name:    "dec negative should not go below 0",
			content: "0",
			inc:     -5,
			want:    0,
		},
		{
			name:    "dec multiple with high number content",
			content: "9800",
			inc:     -100,
			want:    9700,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			dir := t.TempDir()
			file := filepath.Join(dir, refCountFile)
			if tt.content != "" {
				err := os.WriteFile(file, []byte(tt.content), 0o700)
				assert.NoError(t, err, "write file error")
			}

			got, err := refCount(dir, tt.inc)
			assert.NoError(t, err, "refCount() error")
			assert.Equal(t, tt.want, got, "counter is equal")
			content, err := os.ReadFile(file)
			assert.NoError(t, err, "read file error")
			assert.Equal(t, strconv.Itoa(tt.want), string(content), "file content after refCount()")
		})
	}
}