github.com/blystad/deis@v0.11.0/tests/limits_test.go (about)

     1  // +build integration
     2  
     3  package tests
     4  
     5  import (
     6  	"fmt"
     7  	"os/exec"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/deis/deis/tests/utils"
    13  )
    14  
    15  var (
    16  	limitsListCmd     = "limits:list --app={{.AppName}}"
    17  	limitsSetMemCmd   = "limits:set --app={{.AppName}} web=256M"
    18  	limitsSetCPUCmd   = "limits:set --app={{.AppName}} -c web=512"
    19  	limitsUnsetMemCmd = "limits:unset --app={{.AppName}} --memory web"
    20  	limitsUnsetCPUCmd = "limits:unset --app={{.AppName}} -c web"
    21  	output1           = `(?s)"CpuShares": 512,.*"Memory": 0,`
    22  	output2           = `(?s)"CpuShares": 512,.*"Memory": 268435456,`
    23  	output3           = `(?s)"CpuShares": 0,.*"Memory": 268435456,`
    24  	output4           = `(?s)"CpuShares": 0,.*"Memory": 0,`
    25  )
    26  
    27  func limitsSetTest(t *testing.T, cfg *utils.DeisTestConfig, ver int) {
    28  	cpuCmd, memCmd := limitsSetCPUCmd, limitsSetMemCmd
    29  	// regression test for https://github.com/deis/deis/issues/1563
    30  	// previously the client would throw a stack trace with empty limits
    31  	utils.Execute(t, limitsListCmd, cfg, false, "Unlimited")
    32  	if strings.Contains(cfg.ExampleApp, "dockerfile") {
    33  		cpuCmd = strings.Replace(cpuCmd, "web", "cmd", 1)
    34  		memCmd = strings.Replace(memCmd, "web", "cmd", 1)
    35  	}
    36  	utils.Execute(t, cpuCmd, cfg, false, "512")
    37  	out := dockerInspect(t, cfg, ver)
    38  	if _, err := regexp.MatchString(output1, out); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	utils.Execute(t, limitsListCmd, cfg, false, "512")
    42  	utils.Execute(t, memCmd, cfg, false, "256M")
    43  	out = dockerInspect(t, cfg, ver+1)
    44  	if _, err := regexp.MatchString(output2, out); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	utils.Execute(t, limitsListCmd, cfg, false, "256M")
    48  }
    49  
    50  func limitsUnsetTest(t *testing.T, cfg *utils.DeisTestConfig, ver int) {
    51  	cpuCmd, memCmd := limitsUnsetCPUCmd, limitsUnsetMemCmd
    52  	if strings.Contains(cfg.ExampleApp, "dockerfile") {
    53  		cpuCmd = strings.Replace(cpuCmd, "web", "cmd", 1)
    54  		memCmd = strings.Replace(memCmd, "web", "cmd", 1)
    55  	}
    56  	utils.Execute(t, cpuCmd, cfg, false, "Unlimited")
    57  	out := dockerInspect(t, cfg, ver)
    58  	if _, err := regexp.MatchString(output3, out); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	utils.Execute(t, limitsListCmd, cfg, false, "Unlimited")
    62  	utils.Execute(t, memCmd, cfg, false, "Unlimited")
    63  	out = dockerInspect(t, cfg, ver+1)
    64  	if _, err := regexp.MatchString(output4, out); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	utils.Execute(t, limitsListCmd, cfg, false, "Unlimited")
    68  }
    69  
    70  // dockerInspect creates an SSH session to the Deis controller
    71  // and runs "docker inspect" on the first app container.
    72  func dockerInspect(
    73  	t *testing.T, cfg *utils.DeisTestConfig, ver int) string {
    74  	cmd := fmt.Sprintf("docker inspect %s_v%d.web.1", cfg.AppName, ver)
    75  	sshCmd := exec.Command("ssh",
    76  		"-o", "StrictHostKeyChecking=no",
    77  		"-o", "UserKnownHostsFile=/dev/null",
    78  		"-o", "PasswordAuthentication=no",
    79  		"core@deis."+cfg.Domain, cmd)
    80  	out, err := sshCmd.Output()
    81  	if err != nil {
    82  		t.Fatal(out, err)
    83  	}
    84  	return string(out)
    85  }