github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/extractors/docker_test.go (about) 1 // +build !windows 2 3 package extractors 4 5 import ( 6 "bufio" 7 "fmt" 8 "os" 9 "testing" 10 11 "github.com/docker/docker/api/types" 12 "github.com/docker/docker/api/types/container" 13 "go.aporeto.io/enforcerd/trireme-lib/monitor/constants" 14 ) 15 16 func TestDefaultMetadataExtractor(t *testing.T) { 17 info := &types.ContainerJSON{ 18 ContainerJSONBase: &types.ContainerJSONBase{ 19 Name: "name", 20 State: &types.ContainerState{}, 21 HostConfig: &container.HostConfig{ 22 NetworkMode: constants.DockerHostMode, 23 }, 24 }, 25 NetworkSettings: &types.NetworkSettings{ 26 DefaultNetworkSettings: types.DefaultNetworkSettings{ 27 IPAddress: "10.0.0.1", 28 }, 29 }, 30 Config: &container.Config{ 31 Image: "image", 32 Labels: map[string]string{ 33 " ": "remove me", 34 "empty-label": "", 35 "standard-label": "one", 36 }, 37 }, 38 } 39 40 pu, err := DefaultMetadataExtractor(info) 41 if err != nil { 42 t.Error(err) 43 } 44 var foundEmptyTag bool 45 for _, tag := range pu.Tags().GetSlice() { 46 if tag == "@usr:empty-label=<empty>" { 47 foundEmptyTag = true 48 break 49 } 50 } 51 if !foundEmptyTag { 52 t.Error("empty tag not found") 53 } 54 } 55 56 func TestCreate(t *testing.T) { 57 // Test for Empty file. 58 _, err := NewExternalExtractor("") 59 if err == nil { 60 t.Errorf("Expected Error, but got none") 61 } 62 63 // Test for NonExistent file. 64 _, err = NewExternalExtractor("/tmp/abcde.test") 65 if err == nil { 66 t.Errorf("Expected Error, but got none") 67 } 68 } 69 70 const testfile = `#!/bin/sh 71 echo '{"Pid":16823,"Name":"/stoic_snyder","IPAddresses":{"bridge":"172.17.0.2"},"Tags":{"image":"nginx","name":"/stoic_snyder"}}' 72 ` 73 74 func createFileTest(destination string) error { 75 76 fileHandle, err := os.Create(destination) 77 if err != nil { 78 return err 79 } 80 writer := bufio.NewWriter(fileHandle) 81 fmt.Fprintln(writer, testfile) 82 writer.Flush() // nolint: errcheck 83 return nil 84 } 85 86 func TestReturnedFunc(t *testing.T) { 87 88 if err := createFileTest("/tmp/test.sh"); err != nil { 89 t.Skipf("Skip test because no support for writing files to /tmp") 90 } 91 function, err := NewExternalExtractor("/tmp/test.sh") 92 if err != nil { 93 t.Skipf("Skip test because no support for writing files to /tmp") 94 } 95 _, err = function(nil) 96 if err != nil { 97 t.Errorf("Failed to create extractor") 98 } 99 }