github.com/portworx/docker@v1.12.1/daemon/daemon_unix_test.go (about) 1 // +build !windows 2 3 package daemon 4 5 import ( 6 "io/ioutil" 7 "os" 8 "testing" 9 10 "github.com/docker/docker/container" 11 containertypes "github.com/docker/engine-api/types/container" 12 ) 13 14 // Unix test as uses settings which are not available on Windows 15 func TestAdjustCPUShares(t *testing.T) { 16 tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-") 17 if err != nil { 18 t.Fatal(err) 19 } 20 defer os.RemoveAll(tmp) 21 daemon := &Daemon{ 22 repository: tmp, 23 root: tmp, 24 } 25 26 hostConfig := &containertypes.HostConfig{ 27 Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1}, 28 } 29 daemon.adaptContainerSettings(hostConfig, true) 30 if hostConfig.CPUShares != linuxMinCPUShares { 31 t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares) 32 } 33 34 hostConfig.CPUShares = linuxMaxCPUShares + 1 35 daemon.adaptContainerSettings(hostConfig, true) 36 if hostConfig.CPUShares != linuxMaxCPUShares { 37 t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares) 38 } 39 40 hostConfig.CPUShares = 0 41 daemon.adaptContainerSettings(hostConfig, true) 42 if hostConfig.CPUShares != 0 { 43 t.Error("Expected CPUShares to be unchanged") 44 } 45 46 hostConfig.CPUShares = 1024 47 daemon.adaptContainerSettings(hostConfig, true) 48 if hostConfig.CPUShares != 1024 { 49 t.Error("Expected CPUShares to be unchanged") 50 } 51 } 52 53 // Unix test as uses settings which are not available on Windows 54 func TestAdjustCPUSharesNoAdjustment(t *testing.T) { 55 tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-") 56 if err != nil { 57 t.Fatal(err) 58 } 59 defer os.RemoveAll(tmp) 60 daemon := &Daemon{ 61 repository: tmp, 62 root: tmp, 63 } 64 65 hostConfig := &containertypes.HostConfig{ 66 Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1}, 67 } 68 daemon.adaptContainerSettings(hostConfig, false) 69 if hostConfig.CPUShares != linuxMinCPUShares-1 { 70 t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1) 71 } 72 73 hostConfig.CPUShares = linuxMaxCPUShares + 1 74 daemon.adaptContainerSettings(hostConfig, false) 75 if hostConfig.CPUShares != linuxMaxCPUShares+1 { 76 t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1) 77 } 78 79 hostConfig.CPUShares = 0 80 daemon.adaptContainerSettings(hostConfig, false) 81 if hostConfig.CPUShares != 0 { 82 t.Error("Expected CPUShares to be unchanged") 83 } 84 85 hostConfig.CPUShares = 1024 86 daemon.adaptContainerSettings(hostConfig, false) 87 if hostConfig.CPUShares != 1024 { 88 t.Error("Expected CPUShares to be unchanged") 89 } 90 } 91 92 // Unix test as uses settings which are not available on Windows 93 func TestParseSecurityOptWithDeprecatedColon(t *testing.T) { 94 container := &container.Container{} 95 config := &containertypes.HostConfig{} 96 97 // test apparmor 98 config.SecurityOpt = []string{"apparmor=test_profile"} 99 if err := parseSecurityOpt(container, config); err != nil { 100 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 101 } 102 if container.AppArmorProfile != "test_profile" { 103 t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile) 104 } 105 106 // test seccomp 107 sp := "/path/to/seccomp_test.json" 108 config.SecurityOpt = []string{"seccomp=" + sp} 109 if err := parseSecurityOpt(container, config); err != nil { 110 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 111 } 112 if container.SeccompProfile != sp { 113 t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, container.SeccompProfile) 114 } 115 116 // test valid label 117 config.SecurityOpt = []string{"label=user:USER"} 118 if err := parseSecurityOpt(container, config); err != nil { 119 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 120 } 121 122 // test invalid label 123 config.SecurityOpt = []string{"label"} 124 if err := parseSecurityOpt(container, config); err == nil { 125 t.Fatal("Expected parseSecurityOpt error, got nil") 126 } 127 128 // test invalid opt 129 config.SecurityOpt = []string{"test"} 130 if err := parseSecurityOpt(container, config); err == nil { 131 t.Fatal("Expected parseSecurityOpt error, got nil") 132 } 133 } 134 135 func TestParseSecurityOpt(t *testing.T) { 136 container := &container.Container{} 137 config := &containertypes.HostConfig{} 138 139 // test apparmor 140 config.SecurityOpt = []string{"apparmor=test_profile"} 141 if err := parseSecurityOpt(container, config); err != nil { 142 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 143 } 144 if container.AppArmorProfile != "test_profile" { 145 t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile) 146 } 147 148 // test seccomp 149 sp := "/path/to/seccomp_test.json" 150 config.SecurityOpt = []string{"seccomp=" + sp} 151 if err := parseSecurityOpt(container, config); err != nil { 152 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 153 } 154 if container.SeccompProfile != sp { 155 t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile) 156 } 157 158 // test valid label 159 config.SecurityOpt = []string{"label=user:USER"} 160 if err := parseSecurityOpt(container, config); err != nil { 161 t.Fatalf("Unexpected parseSecurityOpt error: %v", err) 162 } 163 164 // test invalid label 165 config.SecurityOpt = []string{"label"} 166 if err := parseSecurityOpt(container, config); err == nil { 167 t.Fatal("Expected parseSecurityOpt error, got nil") 168 } 169 170 // test invalid opt 171 config.SecurityOpt = []string{"test"} 172 if err := parseSecurityOpt(container, config); err == nil { 173 t.Fatal("Expected parseSecurityOpt error, got nil") 174 } 175 } 176 177 func TestNetworkOptions(t *testing.T) { 178 daemon := &Daemon{} 179 dconfigCorrect := &Config{ 180 CommonConfig: CommonConfig{ 181 ClusterStore: "consul://localhost:8500", 182 ClusterAdvertise: "192.168.0.1:8000", 183 }, 184 } 185 186 if _, err := daemon.networkOptions(dconfigCorrect, nil); err != nil { 187 t.Fatalf("Expect networkOptions success, got error: %v", err) 188 } 189 190 dconfigWrong := &Config{ 191 CommonConfig: CommonConfig{ 192 ClusterStore: "consul://localhost:8500://test://bbb", 193 }, 194 } 195 196 if _, err := daemon.networkOptions(dconfigWrong, nil); err == nil { 197 t.Fatalf("Expected networkOptions error, got nil") 198 } 199 }