github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/runtime/env_linux_test.go (about) 1 //go:build linux 2 // +build linux 3 4 package runtime 5 6 import ( 7 "github.com/stretchr/testify/assert" 8 "testing" 9 ) 10 11 func TestIsRunningAtDocker(t *testing.T) { 12 t.Logf("running in docker: %v\n", IsRunningAtDocker()) 13 } 14 15 func TestIsRunningAtKubernetes(t *testing.T) { 16 t.Logf("running in kubernetes: %v\n", IsRunningAtKubernetes()) 17 } 18 19 func TestContainerIDParse(t *testing.T) { 20 example := `0::/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pode6ac4a8d_1076_453e_9ddb_3976520e3178.slice/cri-containerd-19cd7a809d879d9c855bb93e4d399efe795a769ac856faaa5256cdd8387fe4b1.scope` 21 path := procSelfCgroupLineRegex.FindStringSubmatch(example) 22 if len(path) != 2 { 23 } 24 assert.Equal(t, 2, len(path)) 25 parts := containerIDRegex.FindStringSubmatch(path[1]) 26 assert.Equal(t, 2, len(parts)) 27 expected := `19cd7a809d879d9c855bb93e4d399efe795a769ac856faaa5256cdd8387fe4b1` 28 assert.Equal(t, expected, parts[1]) 29 }