github.com/adelq/deis@v0.13.2-0.20141021145840-f05550607c59/tests/tags_test.go (about)

     1  // +build integration
     2  
     3  package tests
     4  
     5  import (
     6  	"fmt"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/deis/deis/tests/utils"
    13  )
    14  
    15  var (
    16  	tagsListCmd  = "tags:list --app={{.AppName}}"
    17  	tagsSetCmd   = "tags:set --app={{.AppName}} environ=test"
    18  	tagsUnsetCmd = "tags:unset --app={{.AppName}} environ"
    19  )
    20  
    21  func tagsTest(t *testing.T, cfg *utils.DeisTestConfig, ver int) {
    22  	configFleetMetadata(t, cfg)
    23  	utils.Execute(t, tagsListCmd, cfg, false, "No tags defined")
    24  	utils.Execute(t, tagsSetCmd, cfg, false, "test")
    25  	utils.Execute(t, tagsListCmd, cfg, false, "test")
    26  	utils.Execute(t, tagsUnsetCmd, cfg, false, "No tags defined")
    27  }
    28  
    29  // configFleetMetdata applies Fleet metadata configuration over SSH
    30  // and restarts the Fleet systemd unit
    31  func configFleetMetadata(t *testing.T, cfg *utils.DeisTestConfig) {
    32  	// check for existing metadata configuration
    33  	cmd := "sudo systemctl cat fleet.service"
    34  	sshCmd := exec.Command("ssh",
    35  		"-o", "StrictHostKeyChecking=no",
    36  		"-o", "UserKnownHostsFile=/dev/null",
    37  		"-o", "PasswordAuthentication=no",
    38  		"core@deis."+cfg.Domain, cmd)
    39  	out, err := sshCmd.Output()
    40  	if err != nil {
    41  		t.Fatal(out, err)
    42  	}
    43  	if strings.Contains(string(out), "FLEET_METADATA") {
    44  		return
    45  	}
    46  	// append metadata to fleet unit
    47  	metadata := "environ=test"
    48  	cmd = fmt.Sprintf(`sudo /bin/sh -c 'echo Environment=\"FLEET_METADATA=%s\" >> /run/systemd/system/fleet.service.d/20-cloudinit.conf'`, metadata)
    49  	sshCmd = exec.Command("ssh",
    50  		"-o", "StrictHostKeyChecking=no",
    51  		"-o", "UserKnownHostsFile=/dev/null",
    52  		"-o", "PasswordAuthentication=no",
    53  		"core@deis."+cfg.Domain, cmd)
    54  	out, err = sshCmd.Output()
    55  	if err != nil {
    56  		t.Fatal(out, err)
    57  	}
    58  	// reload all units and restart fleet
    59  	cmd = "sudo systemctl daemon-reload && sudo systemctl restart fleet"
    60  	sshCmd = exec.Command("ssh",
    61  		"-o", "StrictHostKeyChecking=no",
    62  		"-o", "UserKnownHostsFile=/dev/null",
    63  		"-o", "PasswordAuthentication=no",
    64  		"core@deis."+cfg.Domain, cmd)
    65  	out, err = sshCmd.Output()
    66  	if err != nil {
    67  		t.Fatal(out, err)
    68  	}
    69  	// take a nap while fleet restarts
    70  	time.Sleep(5000 * time.Millisecond)
    71  }